my_photo_album/src/pages/IMG/[file].ts
2026-06-05 01:03:03 +02:00

25 lines
576 B
TypeScript

import fs from "node:fs";
import path from "node:path";
import type { APIRoute } from "astro";
export const prerender = false;
export const GET: APIRoute = ({ params }) => {
const file = params.file;
const filePath = path.join(process.env.PHOTO_DIR_ABSOLUTE_PATH!, file!);
if (!fs.existsSync(filePath)) {
return new Response("Image introuvable", { status: 404 });
}
const image = fs.readFileSync(filePath);
return new Response(image, {
headers: {
"Content-Type": "image/jpeg",
"Cache-Control": "public, max-age=31536000",
},
});
};