feat(F-060): completed feature

This commit is contained in:
chattie
2026-08-19 16:49:16 +02:00
parent 72ba84456c
commit 5e2e935f76
8 changed files with 201 additions and 16 deletions

View File

@@ -11,6 +11,16 @@ const sharp = require(process.env.SHARP_PATH || '/Users/chattie/git/mercadodevid
const PROJECT_DIR = join(import.meta.dirname, '..');
const SIZES = [40, 200];
/**
* Maximum height ratio (h/w) for cached thumbnails. Mirrors the resize
* contract used by the upload route and the dynamic `/uploads/[...path]`
* handler so that batch regeneration and on-demand generation produce the
* same shape. The thumbnail fits inside a `width × width * MAX_HEIGHT_RATIO`
* bounding box without cropping, preserving the source aspect ratio.
*/
const MAX_HEIGHT_RATIO = 1.4;
/** Set FORCE=1 to regenerate thumbnails even when the cached file exists. */
const FORCE = process.env.FORCE === '1' || process.env.FORCE === 'true';
const APP_DIRS = [
join(PROJECT_DIR, 'apps', 'admin'),
join(PROJECT_DIR, 'frontend'),
@@ -58,15 +68,18 @@ async function main() {
const thumbDir = join(appDir, 'public', 'uploads', String(size));
const thumbPath = join(thumbDir, filename);
// Skip if already cached
try {
const s = await stat(thumbPath);
if (s.isFile() && s.size > 0) continue;
} catch {}
// Skip if already cached (unless FORCE=1 to regenerate)
if (!FORCE) {
try {
const s = await stat(thumbPath);
if (s.isFile() && s.size > 0) continue;
} catch {}
}
await mkdir(thumbDir, { recursive: true });
const maxHeight = Math.round(size * MAX_HEIGHT_RATIO);
await sharp(src)
.resize({ width: size, withoutEnlargement: true })
.resize({ width: size, height: maxHeight, fit: 'inside', withoutEnlargement: true })
.jpeg({ quality: 80 })
.toFile(thumbPath);
generated += 1;