feat: support local storage

This commit is contained in:
Hk-Gosuto
2024-01-05 18:41:16 +08:00
parent 8e10354109
commit 296df592e0
18 changed files with 181 additions and 56 deletions

View File

@@ -0,0 +1,29 @@
import fs from "fs";
import path from "path";
export default class LocalFileStorage {
static async get(fileName: string) {
const filePath = path.resolve(`./uploads`, fileName);
const file = fs.readFileSync(filePath);
if (!file) {
throw new Error("not found.");
}
return file;
}
static async put(fileName: string, data: Buffer) {
try {
const filePath = path.resolve(`./uploads`, fileName);
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
await fs.promises.writeFile(filePath, data);
console.log("[LocalFileStorage]", filePath);
return `/api/file/${fileName}`;
} catch (e) {
console.error("[LocalFileStorage]", e);
throw e;
}
}
}