Bilder
This commit is contained in:
39
src/pages/bilder/[uid].jpg.ts
Normal file
39
src/pages/bilder/[uid].jpg.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
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",
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user