| 23 | import { chunkPrefix } from "./utils.js"; |
| 24 | |
| 25 | export class StreamableFS implements IStreamableFS { |
| 26 | /** |
| 27 | * @param db name of the indexeddb database |
| 28 | */ |
| 29 | constructor(private readonly storage: IFileStorage) {} |
| 30 | |
| 31 | async createFile( |
| 32 | filename: string, |
| 33 | size: number, |
| 34 | type: string, |
| 35 | options?: { overwrite?: boolean } |
| 36 | ): Promise<FileHandle> { |
| 37 | const exists = await this.exists(filename); |
| 38 | if (!options?.overwrite && exists) throw new Error("File already exists."); |
| 39 | else if (options?.overwrite && exists) await this.deleteFile(filename); |
| 40 | |
| 41 | const file: File = { |
| 42 | filename, |
| 43 | size, |
| 44 | type |
| 45 | }; |
| 46 | await this.storage.setMetadata(filename, file); |
| 47 | return new FileHandle(this.storage, file, []); |
| 48 | } |
| 49 | |
| 50 | async readFile(filename: string): Promise<FileHandle | undefined> { |
| 51 | const file = await this.storage.getMetadata(filename); |
| 52 | if (!file) return undefined; |
| 53 | const chunks = (await this.storage.listChunks(chunkPrefix(filename))).sort( |
| 54 | (a, b) => a.localeCompare(b, undefined, { numeric: true }) |
| 55 | ); |
| 56 | return new FileHandle(this.storage, file, chunks); |
| 57 | } |
| 58 | |
| 59 | async exists(filename: string): Promise<boolean> { |
| 60 | const file = await this.storage.getMetadata(filename); |
| 61 | return !!file; |
| 62 | } |
| 63 | |
| 64 | async list(): Promise<string[]> { |
| 65 | return this.storage.list(); |
| 66 | } |
| 67 | |
| 68 | async deleteFile(filename: string): Promise<boolean> { |
| 69 | const handle = await this.readFile(filename); |
| 70 | if (!handle) return true; |
| 71 | await handle.delete(); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | async bulkDeleteFiles(filenames: string[]): Promise<boolean> { |
| 76 | for (const filename of filenames) { |
| 77 | await this.deleteFile(filename); |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | async moveFile(source: FileHandle, dest: FileHandle) { |
nothing calls this directly
no outgoing calls
no test coverage detected