28 lines
845 B
TypeScript
28 lines
845 B
TypeScript
|
|
import { randomUUID } from "node:crypto";
|
||
|
|
import fs from "node:fs/promises";
|
||
|
|
import path from "node:path";
|
||
|
|
|
||
|
|
import { paths } from "../config/paths.js";
|
||
|
|
|
||
|
|
export async function ensureDataDirectories() {
|
||
|
|
await fs.mkdir(paths.uploadsDir, { recursive: true });
|
||
|
|
await fs.mkdir(paths.prismaDir, { recursive: true });
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function writeUpload(buffer: Buffer, originalName: string) {
|
||
|
|
const extension = path.extname(originalName);
|
||
|
|
const storedName = `${Date.now()}-${randomUUID()}${extension}`;
|
||
|
|
const relativePath = path.join("uploads", storedName);
|
||
|
|
const absolutePath = path.join(paths.dataDir, relativePath);
|
||
|
|
|
||
|
|
await fs.mkdir(path.dirname(absolutePath), { recursive: true });
|
||
|
|
await fs.writeFile(absolutePath, buffer);
|
||
|
|
|
||
|
|
return {
|
||
|
|
storedName,
|
||
|
|
relativePath: relativePath.replaceAll("\\", "/"),
|
||
|
|
absolutePath,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|