feat(F-060): completed feature
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user