From ed7e2badcae68b7790b3a5127b5455bbe88ec03a Mon Sep 17 00:00:00 2001 From: jubnl Date: Wed, 22 Apr 2026 16:11:38 +0200 Subject: [PATCH] fix: catch sharp errors in ensureLocalThumbnail and fall back to original Sharp throws on unsupported formats (HEIC, corrupt files, etc.) and the error was propagated outside the try/catch, crashing the server. Moved the mkdir + sharp pipeline inside the catch block so any failure returns null and streamPhoto falls through to serving the original file. --- .../src/services/memories/thumbnailService.ts | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/server/src/services/memories/thumbnailService.ts b/server/src/services/memories/thumbnailService.ts index 9a5d2278..765f5f95 100644 --- a/server/src/services/memories/thumbnailService.ts +++ b/server/src/services/memories/thumbnailService.ts @@ -27,14 +27,17 @@ export async function ensureLocalThumbnail( const meta = await sharp(thumbAbs).metadata() return { thumbnailRelPath: thumbRel, width: meta.width ?? 0, height: meta.height ?? 0 } } - } catch { /* regenerate */ } - await fs.mkdir(path.dirname(thumbAbs), { recursive: true }) - await sharp(originalAbs) - .rotate() - .resize({ width: THUMB_MAX, height: THUMB_MAX, fit: 'inside', withoutEnlargement: true }) - .webp({ quality: THUMB_QUALITY }) - .toFile(thumbAbs) - const meta = await sharp(thumbAbs).metadata() - return { thumbnailRelPath: thumbRel, width: meta.width ?? 0, height: meta.height ?? 0 } + await fs.mkdir(path.dirname(thumbAbs), { recursive: true }) + await sharp(originalAbs) + .rotate() + .resize({ width: THUMB_MAX, height: THUMB_MAX, fit: 'inside', withoutEnlargement: true }) + .webp({ quality: THUMB_QUALITY }) + .toFile(thumbAbs) + const meta = await sharp(thumbAbs).metadata() + return { thumbnailRelPath: thumbRel, width: meta.width ?? 0, height: meta.height ?? 0 } + } catch { + // Unsupported format, corrupt file, etc. — fall back to original in caller. + return null + } }