From 3e5e495b9fdf5dd8a7b67cea3a3152ca75fd615d Mon Sep 17 00:00:00 2001 From: Kirill Date: Fri, 15 May 2026 19:52:16 +0500 Subject: [PATCH] fix: reuse sharp instance and UPLOADS_DIR constant in eager processing --- server/src/lib/image-resize.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/lib/image-resize.js b/server/src/lib/image-resize.js index 4bd7c0f..e6378cc 100644 --- a/server/src/lib/image-resize.js +++ b/server/src/lib/image-resize.js @@ -77,13 +77,14 @@ export async function generateAllSizes(uuid, subdir, originalPath) { await fs.promises.mkdir(cacheDir, { recursive: true }) const sharp = (await import('sharp')).default + const source = sharp(originalPath) for (const width of VALID_WIDTHS) { for (const format of SUPPORTED_FORMATS) { const cacheFileName = `${uuid}_w${width}.${format}` const cachePath = path.join(CACHE_DIR, cacheSubdir, cacheFileName) - const pipeline = sharp(originalPath).resize(width, null, { withoutEnlargement: true }) + const pipeline = source.clone().resize(width, null, { withoutEnlargement: true }) const options = format === 'avif' ? { quality: 75, effort: 4 } : { quality: 80 } await pipeline[format](options).toFile(cachePath) } @@ -97,8 +98,7 @@ export async function generateAllSizes(uuid, subdir, originalPath) { * @returns {string} New URL path like `/uploads/.webp` */ export async function convertOriginalToWebp(uuid, subdir) { - const uploadsDir = path.join(process.cwd(), 'uploads') - const targetDir = subdir ? path.join(uploadsDir, subdir) : uploadsDir + const targetDir = subdir ? path.join(UPLOADS_DIR, subdir) : UPLOADS_DIR const originalPath = await findOriginalFile(uuid, subdir) if (!originalPath) {