Files
2026-11-13 12:08:12 +03:00

40 lines
1.2 KiB
TypeScript

import { promises as fs } from "node:fs";
import path from "node:path";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
const contentTypes: Record<string, string> = {
".pdf": "application/pdf",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".webp": "image/webp",
".avif": "image/avif",
};
// Next.js indexes public files at startup; admin uploads arrive afterward.
export async function GET(_request: Request, { params }: { params: { filename: string } }) {
const { filename } = params;
const contentType = contentTypes[path.extname(filename).toLowerCase()];
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(filename) || !contentType) {
return new Response(null, { status: 404 });
}
try {
const file = await fs.readFile(path.join(process.cwd(), "public", "uploads", filename));
return new Response(new Uint8Array(file), {
headers: {
"Content-Type": contentType,
"Cache-Control": "public, max-age=31536000, immutable",
"X-Content-Type-Options": "nosniff",
},
});
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
return new Response(null, { status: 404 });
}
throw error;
}
}