Cache bounded Discord avatars and coalesce downloads

This commit is contained in:
syntaxbullet
2026-09-08 16:15:08 +02:00
parent 17190219fa
commit e3e07404f8
3 changed files with 102 additions and 8 deletions

View File

@@ -1,4 +1,5 @@
import { createCanvas, GlobalFonts, loadImage } from "@napi-rs/canvas";
import { cachedAvatar } from './avatar-cache';
import { createCanvas, GlobalFonts } from "@napi-rs/canvas";
import { existsSync } from "node:fs";
import { eq } from "drizzle-orm";
import type { AppDatabase } from "../auth";
@@ -52,13 +53,7 @@ export async function renderLiveCard(data: LiveImageData, theme: Theme) {
const canvas = createCanvas(layout.width, layout.height);
const ctx = canvas.getContext('2d');
const colors = themePalette[theme];
let avatar: Awaited<ReturnType<typeof loadImage>> | null = null;
if (data.avatarUrl) {
try {
const response = await fetch(data.avatarUrl, { redirect: 'error', signal: AbortSignal.timeout(3000) });
if (response.ok) avatar = await loadImage(Buffer.from(await response.arrayBuffer()));
} catch { /* Match the snapshot card's neutral avatar fallback. */ }
}
const avatar = data.avatarUrl ? await cachedAvatar(data.avatarUrl) : null;
const rect = (x: number, y: number, w: number, h: number, color: string, radius = 0) => {
ctx.fillStyle = color; ctx.beginPath(); ctx.roundRect(x, y, Math.max(0, w), Math.max(0, h), radius); ctx.fill();
};