Files
online-energieausweis/src/pages/bilder/[uid].jpg.ts
Moritz Utcke fb706c5999 Bilder
2025-02-22 12:03:36 +11:00

40 lines
711 B
TypeScript

import { prisma } from "#lib/server/prisma.js";
import { APIRoute } from "astro";
import * as fs from "fs";
import { fileURLToPath } from "url";
export const GET: APIRoute = async (Astro) => {
const { uid } = Astro.params;
const image = await prisma.bild.findUnique({
where: {
uid,
},
});
if (!image) {
return new Response(null, {
status: 404,
});
}
const path = fileURLToPath(
new URL(`../../../persistent/images/${image.uid}.jpg`, import.meta.url)
);
if (!fs.existsSync(path)) {
return new Response(null, {
status: 404,
});
}
const buffer = fs.readFileSync(path);
return new Response(buffer, {
status: 200,
headers: {
"Content-Type": "image/jpeg",
},
});
};