Cache bounded Discord avatars and coalesce downloads
This commit is contained in:
@@ -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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user