fs: add support for moving files

This commit is contained in:
Abdullah Atta
2024-08-03 11:18:55 +05:00
committed by Abdullah Atta
parent 69cb24149d
commit 7bb53a5e0c
2 changed files with 18 additions and 3 deletions

View File

@@ -31,9 +31,12 @@ export class StreamableFS implements IStreamableFS {
async createFile(
filename: string,
size: number,
type: string
type: string,
options?: { overwrite?: boolean }
): Promise<FileHandle> {
if (await this.exists(filename)) throw new Error("File already exists.");
const exists = await this.exists(filename);
if (!options?.overwrite && exists) throw new Error("File already exists.");
else if (options?.overwrite && exists) await this.deleteFile(filename);
const file: File = {
filename,
@@ -69,6 +72,11 @@ export class StreamableFS implements IStreamableFS {
return true;
}
async moveFile(source: FileHandle, dest: FileHandle) {
await source.readable.pipeTo(dest.writeable);
await source.delete();
}
async clear(): Promise<void> {
await this.storage.clear();
}

View File

@@ -21,10 +21,17 @@ import FileHandle from "./filehandle";
import { File } from "./types";
export interface IStreamableFS {
createFile(filename: string, size: number, type: string): Promise<FileHandle>;
createFile(
filename: string,
size: number,
type: string,
options?: { overwrite?: boolean }
): Promise<FileHandle>;
readFile(filename: string): Promise<FileHandle | undefined>;
exists(filename: string): Promise<boolean>;
deleteFile(filename: string): Promise<boolean>;
list(): Promise<string[]>;
moveFile(source: FileHandle, dest: FileHandle): Promise<void>;
clear(): Promise<void>;
}