mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-22 07:15:47 +00:00
7a7a9f5e6b
111 of 122 top-level src/utils/ files move into 16 topic folders (audio, cache, cover, share, server, playback, playlist, deviceSync, waveform, mix, format, export, changelog, ui, perf, componentHelpers). True singletons with no cluster stay at the utils/ root. Pure file-move: a path-aware codemod rewrote 539 relative-import specifiers across 275 files; no logic touched. The hot-path coverage gate list (.github/frontend-hot-path-files.txt) is updated to the new paths for the 11 gated utils files — a mechanical consequence of the move, not a CI change. tsc is green.
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
/**
|
|
* Client-side thumbnail path when a larger cover blob is already in cache:
|
|
* avoids a second HTTP getCoverArt for a smaller `size=` variant.
|
|
*/
|
|
|
|
export async function downscaleCoverBlob(
|
|
blob: Blob,
|
|
maxPx: number,
|
|
signal?: AbortSignal,
|
|
): Promise<Blob | null> {
|
|
if (typeof document === 'undefined' || signal?.aborted || maxPx < 16) return null;
|
|
|
|
let bmp: ImageBitmap | undefined;
|
|
try {
|
|
bmp = await createImageBitmap(blob);
|
|
if (signal?.aborted) return null;
|
|
const w = bmp.width;
|
|
const h = bmp.height;
|
|
if (!w || !h || !Number.isFinite(w) || !Number.isFinite(h)) return null;
|
|
|
|
const maxDim = Math.max(w, h);
|
|
/** Skip encode work when already suitable for tiny list thumbs. */
|
|
if (maxDim <= maxPx * 1.02) return null;
|
|
|
|
const scale = maxPx / maxDim;
|
|
const dw = Math.max(1, Math.round(w * scale));
|
|
const dh = Math.max(1, Math.round(h * scale));
|
|
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = dw;
|
|
canvas.height = dh;
|
|
const ctx = canvas.getContext('2d');
|
|
if (!ctx) return null;
|
|
ctx.imageSmoothingEnabled = true;
|
|
ctx.imageSmoothingQuality = 'high';
|
|
ctx.drawImage(bmp, 0, 0, dw, dh);
|
|
|
|
if (signal?.aborted) return null;
|
|
|
|
return await new Promise<Blob | null>(resolve => {
|
|
if (signal?.aborted) {
|
|
resolve(null);
|
|
return;
|
|
}
|
|
const finish = (b: Blob | null) => {
|
|
signal?.removeEventListener('abort', onAbort);
|
|
resolve(signal?.aborted ? null : b);
|
|
};
|
|
const onAbort = () => finish(null);
|
|
signal?.addEventListener('abort', onAbort, { once: true });
|
|
canvas.toBlob(b => finish(b ?? null), 'image/jpeg', 0.88);
|
|
});
|
|
} catch {
|
|
return null;
|
|
} finally {
|
|
try {
|
|
bmp?.close();
|
|
} catch {
|
|
/* noop */
|
|
}
|
|
}
|
|
}
|