import { createCaller } from "#lib/caller";
import { APIRoute } from "astro";
import { validate } from "uuid";

export const get: APIRoute = async ({params, cookies}) => {
	const { uid } = params;

	if (!uid) {
		return new Response("No uid provided", { status: 400 });
	}

	if (!validate(uid)) {
		return new Response("Invalid uid", { status: 400 });
	}

	const caller = createCaller({ cookies })

	const image = await caller.v1.bilder.getBase64({ uid })

	if (!image) {
		return new Response("No image found", { status: 404 });
	}

	const buffer = Buffer.from(image.base64, "base64");

	return new Response(buffer, {
		headers: {
			"Content-Type": "image/webp"
		}
	});
}