Filesystem
Core ships with a filesystem abstraction that makes it easy to switch from a local filesystem to a remote one. The default driver that is shipped provides access to the local filesystem but using your own filesystem driver is just as easy.
Prerequisites
Before we start, we need to establish what a few recurring variables and imports in this document refer to when they are used.
1import { app, Container, Services } from "@arkecosystem/core-kernel";
- The
app
import refers to the application instance which grants access to the container, configurations, system information and more. - The
Container
import refers to a namespace that contains all of the container specific entities like binding symbols and interfaces. - The
Services
import refers to a namespace that contains all of the core services. This generally will only be needed for type hints as Core is responsible for service creation and maintenance.
Filesystem Usage
Determine if a file exists
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .exists("/home/ark/stats.txt");
Get the contents of a file
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .get("/home/ark/stats.txt");
Write the contents of a file
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .put("/home/ark/stats.txt", "Hello World");
Delete the file at a given path
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .delete("/home/ark/stats.txt");
Copy a file to a new location
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .copy("/home/ark/old.txt", "/home/ark/new.txt");
Move a file to a new location
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .move("/home/ark/old.txt", "/home/ark/new.txt");
Get the file size of a given file
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .size("/home/ark/stats.txt");
Get the file’s last modification time
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .lastModified("/home/ark/stats.txt");
Get an array of all files in a directory
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .files("/home/ark");
Get all of the directories within a given directory
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .directories("/home/ark");
Create a directory
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .makeDirectory("/home/ark");
Recursively delete a directory
1app2 .get<Services.Filesystem.FilesystemService>(Container.Identifiers.FilesystemService)3 .deleteDirectory("/home/ark");
Extending
@TODO
Last updated 3 years ago
Edit Page