1
0
Fork 0
fahrPool/web/lib/pwa/app-icon.ts

107 lines
4.2 KiB
TypeScript

import { createHash } from "node:crypto";
import sharp from "sharp";
import { validateLogo, type LogoEndung } from "@/lib/firma/logo";
export type IconGroesse = "180" | "192" | "512" | "512-maskable";
export type IconBranding = {
id: string;
name: string;
logo_pfad: string | null;
app_icon_pfad: string | null;
farbe_primaer: string | null;
};
const GROESSEN: readonly IconGroesse[] = ["180", "192", "512", "512-maskable"];
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
// Zeitstempel im Namen: ein neues App-Icon hat einen neuen Pfad und damit eine neue Version.
export function appIconPfad(firmaId: string, endung: LogoEndung, jetzt: number = Date.now()): string {
return `${firmaId}/app-icon-${jetzt}.${endung}`;
}
export function validateAppIcon(datei: { type: string; size: number }): ReturnType<typeof validateLogo> {
const r = validateLogo(datei);
return r.valid ? r : { valid: false, error: r.error.replace("Das Logo", "Das App-Icon") };
}
export function iconVersion(f: IconBranding | null): string {
if (!f) return "standard";
const roh = [f.name, f.logo_pfad, f.app_icon_pfad, f.farbe_primaer].map((v) => v ?? "").join("|");
return createHash("sha1").update(roh).digest("hex").slice(0, 10);
}
export function iconUrl(f: IconBranding | null, groesse: IconGroesse): string {
return `/app-icon/${f ? f.id : "standard"}/${iconVersion(f)}/${groesse}.png`;
}
export function parseIconAnfrage(
firma: string,
datei: string
): { firmaId: string | null; groesse: IconGroesse } | null {
if (!datei.endsWith(".png")) return null;
const groesse = datei.slice(0, -4) as IconGroesse;
if (!GROESSEN.includes(groesse)) return null;
if (firma === "standard") return { firmaId: null, groesse };
if (!UUID_REGEX.test(firma)) return null;
return { firmaId: firma, groesse };
}
export function iconBuchstabe(f: IconBranding | null): string {
return (f?.name ?? "").trim().charAt(0).toUpperCase() || "A";
}
export function iconQuelle(
f: IconBranding | null
): { art: "app-icon"; pfad: string } | { art: "logo"; pfad: string } | { art: "buchstabe"; buchstabe: string } {
if (f?.app_icon_pfad) return { art: "app-icon", pfad: f.app_icon_pfad };
if (f?.logo_pfad) return { art: "logo", pfad: f.logo_pfad };
return { art: "buchstabe", buchstabe: iconBuchstabe(f) };
}
const WEISS = "#ffffff";
function buchstabeSvg(buchstabe: string, n: number): Buffer {
const zeichen = buchstabe.replace(/[<>&"']/g, "") || "A";
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>`
);
}
export async function renderAppIcon(params: {
quelle: Buffer | null;
art: "app-icon" | "logo" | "buchstabe";
buchstabe?: string;
farbe: string;
groesse: IconGroesse;
}): Promise<Buffer> {
const { quelle, art, farbe, groesse } = params;
const maskable = groesse === "512-maskable";
const kante = groesse === "180" ? 180 : groesse === "192" ? 192 : 512;
// Maskable: Inhalt in der Safe Zone (60 %), der Rest ist Hintergrund.
const innen = maskable ? Math.round(kante * 0.6) : kante;
// sharp/librsvg fuehrt kein Script aus und laedt keine externen Ressourcen.
const eingabe = (b: Buffer) => sharp(b, { limitInputPixels: 4096 * 4096 });
let hintergrund: string = farbe;
let inhalt: Buffer;
if (art === "app-icon" && quelle) {
hintergrund = WEISS;
inhalt = await eingabe(quelle).resize(innen, innen, { fit: "cover" }).flatten({ background: WEISS }).png().toBuffer();
} else if (art === "logo" && quelle) {
const breite = Math.round(innen * 0.7);
inhalt = await eingabe(quelle).resize(breite, breite, { fit: "inside" }).png().toBuffer();
} else {
inhalt = await eingabe(buchstabeSvg(params.buchstabe ?? "A", innen)).resize(innen, innen).png().toBuffer();
}
return sharp({ create: { width: kante, height: kante, channels: 4, background: hintergrund } })
.composite([{ input: inhalt, gravity: "centre" }])
.flatten({ background: hintergrund })
.png()
.toBuffer();
}