mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat(storage): add file storage helpers module
Adds ensureStorageDir, saveFile, deleteStoredFile, and getStoredFilePath utilities that manage the lifecycle of files on the local filesystem under FILES_STORAGE_PATH.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { mkdir, writeFile, unlink } from "node:fs/promises";
|
||||
import { join, extname } from "node:path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { env } from "../config.js";
|
||||
|
||||
let storageReady = false;
|
||||
|
||||
export async function ensureStorageDir(): Promise<void> {
|
||||
if (storageReady) return;
|
||||
await mkdir(env.FILES_STORAGE_PATH, { recursive: true });
|
||||
storageReady = true;
|
||||
}
|
||||
|
||||
export async function saveFile(buffer: Buffer, originalName: string): Promise<string> {
|
||||
await ensureStorageDir();
|
||||
const ext = extname(originalName).toLowerCase() || ".bin";
|
||||
const storedName = `${randomUUID()}${ext}`;
|
||||
await writeFile(join(env.FILES_STORAGE_PATH, storedName), buffer);
|
||||
return storedName;
|
||||
}
|
||||
|
||||
export async function deleteStoredFile(storedName: string): Promise<void> {
|
||||
try {
|
||||
await unlink(join(env.FILES_STORAGE_PATH, storedName));
|
||||
} catch {
|
||||
// File already gone
|
||||
}
|
||||
}
|
||||
|
||||
export function getStoredFilePath(storedName: string): string {
|
||||
return join(env.FILES_STORAGE_PATH, storedName);
|
||||
}
|
||||
Reference in New Issue
Block a user