mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-23 06:00:17 +09:00
30 lines
795 B
TypeScript
30 lines
795 B
TypeScript
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;
|
|
}
|
|
}
|
|
}
|