Bestellen auf Rechnung

This commit is contained in:
Moritz Utcke
2025-02-17 20:46:27 +11:00
parent 7f6430f20f
commit 9951179b2a
16 changed files with 198 additions and 80 deletions

View File

@@ -1,31 +1,54 @@
import { createCaller } from "#lib/caller";
import { APIRoute } from "astro";
import { validate } from "uuid";
export const get: APIRoute = async ({params, cookies}) => {
const { uid } = params;
import { API_ACCESS_TOKEN_COOKIE_NAME } from "#lib/constants.js";
import { validateAccessTokenServer } from "#server/lib/validateAccessToken.js";
import { prisma } from "@ibcornelsen/database/server";
import { APIError, defineApiRoute } from "astro-typesafe-api/server";
import { fileURLToPath } from "bun";
import * as fs from "fs";
if (!uid) {
return new Response("No uid provided", { status: 400 });
}
export const GET = defineApiRoute({
async fetch(input, context, transfer) {
const { uid } = context.params
const token = context.cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)?.value;
if (!validate(uid)) {
return new Response("Invalid uid", { status: 400 });
}
if (!token) {
throw new APIError({
code: "BAD_REQUEST",
message: "Invalid access token"
})
}
const caller = createCaller({ cookies })
const valid = validateAccessTokenServer(context);
const image = await caller.v1.bilder.getBase64({ uid })
if (!valid) {
throw new APIError({
code: "BAD_REQUEST",
message: "Invalid access token"
})
}
if (!image) {
return new Response("No image found", { status: 404 });
}
const image = await prisma.gebaeudeBilder.findUnique({
where: {
uid
}
})
const buffer = Buffer.from(image.base64, "base64");
if (!image) {
throw new APIError({
code: "NOT_FOUND",
message: "Image could not be found."
})
}
return new Response(buffer, {
headers: {
"Content-Type": "image/webp"
}
});
}
const path = fileURLToPath(new URL(`../../../../../persistent/images/${image.uid}.webp`, import.meta.url))
const base64 = fs.readFileSync(path, "base64")
const buffer = Buffer.from(base64, "base64");
return new Response(buffer, {
headers: {
"Content-Type": "image/webp"
}
});
},
})