2026-04-23 12:19:00 -05:00
|
|
|
let _sharp: (typeof import("sharp") extends { default: infer D } ? D : never) | null = null;
|
|
|
|
|
let _sharpLoadAttempted = false;
|
|
|
|
|
|
|
|
|
|
async function getSharp(): Promise<typeof import("sharp") extends { default: infer D } ? D : never> {
|
|
|
|
|
if (_sharp) return _sharp;
|
|
|
|
|
if (_sharpLoadAttempted) throw new Error("sharp module unavailable");
|
|
|
|
|
_sharpLoadAttempted = true;
|
|
|
|
|
try {
|
|
|
|
|
_sharp = (await import("sharp")).default as any;
|
|
|
|
|
return _sharp!;
|
2026-04-27 21:52:35 -05:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error("[thumbnails] Failed to load sharp module. Thumbnails will be unavailable.", err);
|
|
|
|
|
throw err;
|
2026-04-17 13:49:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-22 23:21:27 -05:00
|
|
|
|
2026-04-17 13:49:06 -05:00
|
|
|
const THUMBNAIL_WIDTH = Math.max(64, Number(process.env.OVERLORD_THUMBNAIL_WIDTH || 640));
|
|
|
|
|
const THUMBNAIL_HEIGHT = Math.max(48, Number(process.env.OVERLORD_THUMBNAIL_HEIGHT || 360));
|
2026-03-22 23:21:27 -05:00
|
|
|
const THUMBNAIL_QUALITY = Math.min(90, Math.max(40, Number(process.env.OVERLORD_THUMBNAIL_QUALITY || 72)));
|
|
|
|
|
const MAX_THUMBNAIL_SOURCE_BYTES = Math.max(
|
|
|
|
|
256 * 1024,
|
|
|
|
|
Number(process.env.OVERLORD_THUMBNAIL_MAX_SOURCE_BYTES || 16 * 1024 * 1024),
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-05 00:04:26 -06:00
|
|
|
const thumbnails = new Map<string, string>();
|
2026-03-22 23:21:27 -05:00
|
|
|
const latestFrames = new Map<string, { bytes: Uint8Array; format: string; capturedAt: number }>();
|
2026-03-05 00:04:26 -06:00
|
|
|
const thumbnailRequests = new Map<string, number>();
|
|
|
|
|
|
|
|
|
|
export function setThumbnail(id: string, dataUrl: string) {
|
|
|
|
|
thumbnails.set(id, dataUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getThumbnail(id: string) {
|
|
|
|
|
return thumbnails.get(id) ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearThumbnail(id: string) {
|
|
|
|
|
thumbnails.delete(id);
|
|
|
|
|
latestFrames.delete(id);
|
|
|
|
|
thumbnailRequests.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setLatestFrame(id: string, bytes: Uint8Array, format: string) {
|
2026-03-22 23:21:27 -05:00
|
|
|
if (!bytes || bytes.byteLength === 0 || bytes.byteLength > MAX_THUMBNAIL_SOURCE_BYTES) {
|
|
|
|
|
latestFrames.delete(id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
latestFrames.set(id, { bytes, format, capturedAt: Date.now() });
|
2026-03-05 00:04:26 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-22 23:21:27 -05:00
|
|
|
async function buildThumbnailDataUrl(bytes: Uint8Array, format: string): Promise<string | null> {
|
|
|
|
|
if (!bytes || bytes.byteLength === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const inputFormat = format === "jpg" ? "jpeg" : format;
|
|
|
|
|
if (!["jpeg", "webp"].includes(inputFormat)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 13:49:06 -05:00
|
|
|
const sharp = await getSharp();
|
2026-03-22 23:21:27 -05:00
|
|
|
const output = await sharp(Buffer.from(bytes), { failOn: "none" })
|
|
|
|
|
.rotate()
|
|
|
|
|
.resize({
|
|
|
|
|
width: THUMBNAIL_WIDTH,
|
|
|
|
|
height: THUMBNAIL_HEIGHT,
|
|
|
|
|
fit: "inside",
|
|
|
|
|
withoutEnlargement: true,
|
|
|
|
|
fastShrinkOnLoad: true,
|
|
|
|
|
})
|
|
|
|
|
.webp({ quality: THUMBNAIL_QUALITY })
|
|
|
|
|
.toBuffer();
|
|
|
|
|
|
|
|
|
|
return `data:image/webp;base64,${output.toString("base64")}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function generateThumbnail(id: string): Promise<boolean> {
|
2026-03-05 00:04:26 -06:00
|
|
|
const frameData = latestFrames.get(id);
|
|
|
|
|
if (!frameData) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-22 23:21:27 -05:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const dataUrl = await buildThumbnailDataUrl(frameData.bytes, frameData.format);
|
|
|
|
|
if (!dataUrl) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
thumbnails.set(id, dataUrl);
|
|
|
|
|
latestFrames.delete(id);
|
|
|
|
|
return true;
|
2026-04-23 12:19:00 -05:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`[thumbnails] Failed to generate thumbnail for client ${id}:`, err);
|
2026-03-22 23:21:27 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-05 00:04:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function markThumbnailRequested(id: string) {
|
|
|
|
|
thumbnailRequests.set(id, Date.now());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function consumeThumbnailRequest(id: string, windowMs = 5000): boolean {
|
|
|
|
|
const ts = thumbnailRequests.get(id);
|
|
|
|
|
if (!ts) return false;
|
|
|
|
|
if (Date.now() - ts > windowMs) {
|
|
|
|
|
thumbnailRequests.delete(id);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
thumbnailRequests.delete(id);
|
|
|
|
|
return true;
|
|
|
|
|
}
|