1
0
Fork 0

fix: App-Icon-Route Fallback-Kette, Cache nur bei Erfolg, Schrift im Docker-Image

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jonny 2026-10-28 11:16:52 +01:00
parent eb20d421b6
commit befec482f9
4 changed files with 36 additions and 19 deletions

View File

@ -16,6 +16,8 @@ RUN npm run build
FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
# Schrift fuer den Buchstaben im App-Icon (sharp/librsvg); alpine bringt keine mit.
RUN apk add --no-cache fontconfig font-dejavu
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

View File

@ -2,7 +2,6 @@ import { createAdminClient } from "@/lib/supabase/admin";
import { STANDARD_FARBE_PRIMAER } from "@/lib/firma/farben";
import {
iconBuchstabe,
iconQuelle,
parseIconAnfrage,
renderAppIcon,
type IconBranding,
@ -35,32 +34,38 @@ export async function GET(
}
const farbe = branding?.farbe_primaer ?? STANDARD_FARBE_PRIMAER;
const quelle = iconQuelle(branding);
const buchstabe = iconBuchstabe(branding);
// Fallback-Kette App-Icon -> Logo -> Buchstabe; bei Fehlern naechste Stufe.
const stufen: { art: "app-icon" | "logo"; pfad: string }[] = [];
if (branding?.app_icon_pfad) stufen.push({ art: "app-icon", pfad: branding.app_icon_pfad });
if (branding?.logo_pfad) stufen.push({ art: "logo", pfad: branding.logo_pfad });
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) {
let fallback = false;
for (const stufe of stufen) {
if (!admin) break;
try {
const { data, error } = await admin.storage.from("firmen-logos").download(stufe.pfad);
if (error || !data) throw error ?? new Error("leer");
png = await renderAppIcon({
quelle: Buffer.from(await data.arrayBuffer()),
art: quelle.art,
art: stufe.art,
farbe,
groesse: anfrage.groesse,
});
break;
} catch {
// Unlesbare Quelldatei: wie ein Download-Fehler behandeln.
fallback = true;
}
}
}
// 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",
// Nach einem Fallback nicht ein Jahr festschreiben (z.B. voruebergehender Storage-Fehler).
"Cache-Control": fallback ? "no-store" : "public, max-age=31536000, immutable",
"X-Content-Type-Options": "nosniff",
},
});

View File

@ -114,4 +114,14 @@ describe("renderAppIcon", () => {
const data = await sharp(buf).raw().toBuffer();
expect([data[0], data[1], data[2]]).toEqual([0x00, 0x71, 0xed]);
});
it("Buchstaben-Icon hat Vordergrundpixel in der Mitte", async () => {
const buf = await renderAppIcon({ quelle: null, art: "buchstabe", buchstabe: "W", farbe: "#0071ed", groesse: "512" });
const mitte = await sharp(buf).extract({ left: 156, top: 156, width: 200, height: 200 }).raw().toBuffer();
let abweichend = 0;
for (let i = 0; i < mitte.length; i += 3) {
if (mitte[i] !== 0x00 || mitte[i + 1] !== 0x71 || mitte[i + 2] !== 0xed) abweichend++;
}
expect(abweichend).toBeGreaterThan(500);
});
});

View File

@ -48,7 +48,7 @@ export function parseIconAnfrage(
}
export function iconBuchstabe(f: IconBranding | null): string {
return (f?.name ?? "").trim().charAt(0).toUpperCase() || "A";
return [...(f?.name ?? "").trim()][0]?.toUpperCase() || "A";
}
export function iconQuelle(
@ -66,7 +66,7 @@ function buchstabeSvg(buchstabe: string, n: number): Buffer {
return Buffer.from(
`<svg xmlns="http://www.w3.org/2000/svg" width="${n}" height="${n}">` +
`<text x="50%" y="50%" dy="0.35em" text-anchor="middle" fill="${WEISS}" ` +
`font-family="Inter, Arial, Helvetica, sans-serif" font-weight="700" font-size="${Math.round(n * 0.6)}">${zeichen}</text></svg>`
`font-family="DejaVu Sans, Inter, Arial, sans-serif" font-weight="700" font-size="${Math.round(n * 0.6)}">${zeichen}</text></svg>`
);
}