mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-19 13:21:46 +00:00
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.
This commit is contained in:
@@ -27,14 +27,17 @@ export async function ensureLocalThumbnail(
|
|||||||
const meta = await sharp(thumbAbs).metadata()
|
const meta = await sharp(thumbAbs).metadata()
|
||||||
return { thumbnailRelPath: thumbRel, width: meta.width ?? 0, height: meta.height ?? 0 }
|
return { thumbnailRelPath: thumbRel, width: meta.width ?? 0, height: meta.height ?? 0 }
|
||||||
}
|
}
|
||||||
} catch { /* regenerate */ }
|
|
||||||
|
|
||||||
await fs.mkdir(path.dirname(thumbAbs), { recursive: true })
|
await fs.mkdir(path.dirname(thumbAbs), { recursive: true })
|
||||||
await sharp(originalAbs)
|
await sharp(originalAbs)
|
||||||
.rotate()
|
.rotate()
|
||||||
.resize({ width: THUMB_MAX, height: THUMB_MAX, fit: 'inside', withoutEnlargement: true })
|
.resize({ width: THUMB_MAX, height: THUMB_MAX, fit: 'inside', withoutEnlargement: true })
|
||||||
.webp({ quality: THUMB_QUALITY })
|
.webp({ quality: THUMB_QUALITY })
|
||||||
.toFile(thumbAbs)
|
.toFile(thumbAbs)
|
||||||
const meta = await sharp(thumbAbs).metadata()
|
const meta = await sharp(thumbAbs).metadata()
|
||||||
return { thumbnailRelPath: thumbRel, width: meta.width ?? 0, height: meta.height ?? 0 }
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user