This package provides a set of services for managing files on various backend services. All implementations adhere to the FileManagerInterface, ensuring consistent functionality across different providers.
- Create, read, update, and delete files and directories
- List directory contents and retrieve file tree structure
- Consistent interface across different backend services
- Easy to extend with new service implementations
The package includes three implementations:
GithubFileManager: Manages files and directories on GitHub repositories.GoogleDriveFileManager: Manages files and directories on Google Drive.InMemoryFileManager: A memory-based implementation useful for testing and debugging.
All implementations adhere to the FileManagerInterface, which defines the following methods:
getFileContent(path: string): Promise<string | Buffer>updateTextFile(path: string, content: string): Promise<void>updateBinaryFile(path: string, content: Buffer): Promise<void>deleteFile(path: string): Promise<void>listDirectoryContent(path: string, recursive?: boolean): Promise<ResourceInfo[]>createDirectory(path: string): Promise<void>deleteDirectory(path: string): Promise<void>
The ResourceInfo interface represents information about a file or directory. It includes the following properties:
path: The full path to the resource (file or directory). Directory paths end with a trailing/.name: The name of the file or directory.parent: The path to the parent directory.ext: The file extension (only for files).isText: A boolean indicating if the resource is a known text format.isDirectory: A boolean indicating if the resource is a directory.isFile: A boolean indicating if the resource is a file.
To use a file manager service:
- Import the desired implementation
- Initialize it with the necessary options
- Use the interface methods to perform file operations
Usage Example with GithubFileManager:
import { GithubFileManager } from "./services/GithubFileManager";
const githubManager = new GithubFileManager({
githubRepoUrl: "https://github.com/user/repo",
githubApplicationToken: "your-github-token",
rootDir: "optional/root/directory",
});
// List the first level of a directory content
const files = await githubManager.listDirectoryContent("/path/to/directory");
// Update a text file
await githubManager.updateTextFile("/path/to/file.txt", "New content");
// Delete a file
await githubManager.deleteFile("/path/to/file.txt");To run the tests for the FileManager, you need to set up the following environment variables:
- Copy the
.env.samplefile to a new file named.env.test: - Edit the
.env.testfile and replace the placeholder values with your actual GitHub test repository URL and application token: - The test suite will automatically use these environment variables when running the tests.
Note: The .env file is ignored by git to keep your credentials secure. Never commit this file to the repository.