import * as fs from 'fs/promises'; import { createReadStream } from 'fs'; import path from 'path'; import { StorageProvider } from './StorageProvider'; /** * Default StorageProvider backed by the local filesystem via `fs/promises`. * Mirrors the existing logic in `files.ts`. */ export class FsStorageProvider implements StorageProvider { // --- Text file operations --- async checkIfExists(filePath: string): Promise { try { await fs.access(filePath); return true; } catch { return false; } } async loadFile(filePath: string): Promise { return await fs.readFile(filePath, 'utf8'); } async saveFile(filePath: string, content: string): Promise { await fs.writeFile(filePath, content, 'utf8'); } async deleteFile(filePath: string): Promise { await fs.unlink(filePath); } // --- Binary file operations --- async saveBuffer(filePath: string, data: Buffer): Promise { await fs.writeFile(filePath, data); } async loadBuffer(filePath: string): Promise { return await fs.readFile(filePath); } async stat(filePath: string): Promise<{ size: number; isFile: boolean }> { const s = await fs.stat(filePath); return { size: s.size, isFile: s.isFile() }; } createReadStream(filePath: string): NodeJS.ReadableStream { return createReadStream(filePath); } // --- Directory operations --- async listFiles(dirPath: string): Promise { return (await fs.readdir(dirPath)).filter(file => !file.startsWith('.') && file.includes('.')); } async listFolders(dirPath: string): Promise { const entries = await fs.readdir(dirPath, { withFileTypes: true }); return entries .filter(entry => entry.isDirectory() && !entry.name.startsWith('.')) .map(entry => entry.name); } async ensureFolderExists(dirPath: string): Promise { await fs.mkdir(dirPath, { recursive: true }); } async deleteFolder(dirPath: string): Promise { await fs.rm(dirPath, { recursive: true }); } async copyFolderRecursive(srcPath: string, destPath: string): Promise { await this.ensureFolderExists(destPath); const entries = await fs.readdir(srcPath, { withFileTypes: true }); for (const entry of entries) { const src = path.join(srcPath, entry.name); const dest = path.join(destPath, entry.name); if (entry.isDirectory()) { await this.copyFolderRecursive(src, dest); } else { await fs.copyFile(src, dest); } } } }