feat(F-059): completed feature
This commit is contained in:
@@ -16,6 +16,14 @@ const PEER_UPLOAD_DIRS = [
|
||||
|
||||
/** Thumbnail widths pre-generated for lists (40px) and previews (200px). */
|
||||
const THUMBNAIL_WIDTHS = [40, 200];
|
||||
/**
|
||||
* Maximum height ratio (h/w) for cached thumbnails. The thumbnail is scaled
|
||||
* to fit inside a `width × width * MAX_HEIGHT_RATIO` bounding box without
|
||||
* cropping. A non-square source produces a non-square thumbnail that preserves
|
||||
* the original aspect ratio, so the frontend can render it with
|
||||
* `object-contain` inside a matching box.
|
||||
*/
|
||||
const MAX_HEIGHT_RATIO = 1.4;
|
||||
|
||||
async function mirrorToPeers(filePath: string): Promise<void> {
|
||||
await Promise.allSettled(
|
||||
@@ -47,7 +55,10 @@ async function generateThumbnails(buffer: Buffer, filename: string): Promise<voi
|
||||
THUMBNAIL_WIDTHS.map(async (width) => {
|
||||
const thumbDir = path.join(dir, String(width));
|
||||
await mkdir(thumbDir, { recursive: true });
|
||||
const resized = await sharp(buffer).resize({ width, withoutEnlargement: true }).toBuffer();
|
||||
const maxHeight = Math.round(width * MAX_HEIGHT_RATIO);
|
||||
const resized = await sharp(buffer)
|
||||
.resize({ width, height: maxHeight, fit: 'inside', withoutEnlargement: true })
|
||||
.toBuffer();
|
||||
await writeFile(path.join(thumbDir, filename), resized);
|
||||
}),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user