import { createClient } from "@/lib/supabase/server"; import { STANDARD_FOTO_POSITIONEN } from "./foto-positionen"; export type AuftragFoto = { position: string; label: string; url: string; downloadUrl: string; beschreibung: string | null; }; export type FotoZeile = { position: string; storage_path: string; uploaded_at: string; beschreibung: string | null; }; export function sortiereUndBeschrifteFotos(fotos: FotoZeile[]): (FotoZeile & { label: string })[] { const pflichtFotos = STANDARD_FOTO_POSITIONEN.map((position) => fotos.find((foto) => foto.position === position) ).filter((foto): foto is FotoZeile => foto !== undefined); const schadenFotos = fotos .filter((foto) => foto.position === "schaden") .sort((a, b) => a.uploaded_at.localeCompare(b.uploaded_at)); const beschriftetePflichtFotos = pflichtFotos.map((foto) => ({ ...foto, label: foto.position.replaceAll("_", " "), })); const beschrifteteSchadenFotos = schadenFotos.map((foto, index) => ({ ...foto, label: schadenFotos.length > 1 ? `Schaden ${index + 1}` : "Schaden", })); return [...beschriftetePflichtFotos, ...beschrifteteSchadenFotos]; } // Baut aus einer von Supabase Storage signierten URL (die auf die // interne/Docker-Kong-Basis zeigen kann, siehe // docs/superpowers/specs/2026-08-18-auftrag-foto-galerie-design.md Abschnitt // 2) die von aussen erreichbare oeffentliche URL, unter Beibehaltung von // Pfad und Query-String (Signatur-Token). function toPublicUrl(signedUrl: string): string { const publicUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!; const geparst = new URL(signedUrl); return `${publicUrl}${geparst.pathname}${geparst.search}`; } export async function loadAuftragFotos(auftragId: string): Promise { const supabase = await createClient(); const { data } = await supabase .from("auftrag_fotos") .select("position, storage_path, uploaded_at, beschreibung") .eq("auftrag_id", auftragId); if (!data || data.length === 0) { return []; } const sortiert = sortiereUndBeschrifteFotos(data); const fotos = await Promise.all( sortiert.map(async (foto): Promise => { const { data: signiert } = await supabase.storage .from("auftrag-fotos") .createSignedUrl(foto.storage_path, 3600); if (!signiert) { return null; } const url = toPublicUrl(signiert.signedUrl); const dateiname = foto.storage_path.split("/").pop() ?? `${foto.position}.jpg`; const downloadUrl = `${url}&download=${encodeURIComponent(dateiname)}`; return { position: foto.position, label: foto.label, url, downloadUrl, beschreibung: foto.beschreibung }; }) ); return fotos.filter((foto): foto is AuftragFoto => foto !== null); } // Signierte URL fuer die Unterschriftsgrafik eines abgeschlossenen Auftrags. // null, wenn keine Unterschrift hochgeladen wurde (haeufigster Fall, da // optional) oder das Signieren fehlschlaegt. export async function loadUnterschriftUrl(storagePath: string | null): Promise { if (!storagePath) { return null; } const supabase = await createClient(); const { data: signiert } = await supabase.storage .from("auftrag-unterschriften") .createSignedUrl(storagePath, 3600); if (!signiert) { return null; } return toPublicUrl(signiert.signedUrl); } // Laedt die Rohdaten (nicht signierte URLs) aller Fotos eines Auftrags fuer // den PDF-Export -- @react-pdf/renderer bettet Bilder als Binaerdaten ein, // nicht als externe Links (siehe ProtokollDocument.tsx). export async function loadAuftragFotoBuffers( auftragId: string ): Promise<{ label: string; beschreibung: string | null; buffer: Buffer }[]> { const supabase = await createClient(); const { data } = await supabase .from("auftrag_fotos") .select("position, storage_path, uploaded_at, beschreibung") .eq("auftrag_id", auftragId); if (!data || data.length === 0) { return []; } const sortiert = sortiereUndBeschrifteFotos(data); const fotos = await Promise.all( sortiert.map(async (foto): Promise<{ label: string; beschreibung: string | null; buffer: Buffer } | null> => { const { data: blob } = await supabase.storage.from("auftrag-fotos").download(foto.storage_path); if (!blob) { return null; } const buffer = Buffer.from(await blob.arrayBuffer()); return { label: foto.label, beschreibung: foto.beschreibung, buffer }; }) ); return fotos.filter((foto): foto is { label: string; beschreibung: string | null; buffer: Buffer } => foto !== null); } // Analoger Rohdaten-Loader fuer die Unterschrift (siehe loadUnterschriftUrl // oben fuer die signierte URL-Variante, die fuer die Web-Anzeige genutzt // wird -- der PDF-Export braucht stattdessen die Bytes direkt). export async function loadUnterschriftBuffer(storagePath: string | null): Promise { if (!storagePath) { return null; } const supabase = await createClient(); const { data: blob } = await supabase.storage.from("auftrag-unterschriften").download(storagePath); if (!blob) { return null; } return Buffer.from(await blob.arrayBuffer()); }