import { createAdminClient } from "@/lib/supabase/admin"; import { STANDARD_FARBE_PRIMAER } from "@/lib/firma/farben"; import { iconBuchstabe, iconQuelle, parseIconAnfrage, renderAppIcon, type IconBranding, } from "@/lib/pwa/app-icon"; // Bewusst oeffentlich (ohne Session): Browser und Betriebssystem laden die // Icons bei der Installation ohne Cookies. Es werden nur gerenderte PNGs // ausgeliefert, nie die Originaldateien. Der Admin-Client umgeht RLS, deshalb // wird "aktiv" explizit gefiltert. "version" dient nur dem Cache-Busting. export async function GET( _req: Request, { params }: { params: Promise<{ firma: string; version: string; datei: string }> } ) { const { firma, datei } = await params; const anfrage = parseIconAnfrage(firma, datei); if (!anfrage) return new Response("Not found", { status: 404 }); // Das Standard-Icon braucht keine Datenbank. const admin = anfrage.firmaId ? createAdminClient() : null; let branding: IconBranding | null = null; if (admin && anfrage.firmaId) { const { data } = await admin .from("firmen") .select("id, name, logo_pfad, app_icon_pfad, farbe_primaer") .eq("id", anfrage.firmaId) .eq("aktiv", true) .maybeSingle(); if (!data) return new Response("Not found", { status: 404 }); branding = data as IconBranding; } const farbe = branding?.farbe_primaer ?? STANDARD_FARBE_PRIMAER; const quelle = iconQuelle(branding); const buchstabe = iconBuchstabe(branding); let png: Buffer | null = null; if (admin && quelle.art !== "buchstabe") { const { data, error } = await admin.storage.from("firmen-logos").download(quelle.pfad); if (!error && data) { try { png = await renderAppIcon({ quelle: Buffer.from(await data.arrayBuffer()), art: quelle.art, farbe, groesse: anfrage.groesse, }); } catch { // Unlesbare Quelldatei: wie ein Download-Fehler behandeln. } } } // Download-/Lesefehler oder kein Bild hinterlegt: Buchstabe auf Firmenfarbe. png ??= await renderAppIcon({ quelle: null, art: "buchstabe", buchstabe, farbe, groesse: anfrage.groesse }); return new Response(new Uint8Array(png), { headers: { "Content-Type": "image/png", "Cache-Control": "public, max-age=31536000, immutable", "X-Content-Type-Options": "nosniff", }, }); }