16 lines
615 B
TypeScript

import { randomUUID } from "node:crypto";
export const MEDIA_ROOT = "/data/media";
export function newMediaPath(originalFilename: string, now: Date = new Date()): string {
const ext = (originalFilename.match(/\.[a-zA-Z0-9]{1,8}$/)?.[0] ?? "").toLowerCase();
const yyyy = now.getUTCFullYear();
const mm = String(now.getUTCMonth() + 1).padStart(2, "0");
return `${yyyy}/${mm}/${randomUUID()}${ext}`;
}
export function absoluteMediaPath(storagePath: string, root: string = MEDIA_ROOT): string {
if (storagePath.includes("..")) throw new Error("Invalid storage path");
return `${root}/${storagePath}`;
}