fix: fix resized image uploads

This commit is contained in:
Tống Thành Đạt
2026-04-11 11:09:16 +07:00
parent c6a2d4a55d
commit e83f1827b8
2 changed files with 16 additions and 4 deletions

View File

@@ -66,17 +66,21 @@ async function finalizeUploadedImage(file, req, resizePreset) {
}
const { finalFileName, finalPath } = getFinalUploadTarget(file, req, true);
const finalPathMatchesInput = path.resolve(file.path) === path.resolve(finalPath);
await sharp(file.path)
const sourceBuffer = fs.readFileSync(file.path);
const optimizedBuffer = await sharp(sourceBuffer)
.resize(preset.width, preset.height, {
fit: 'contain',
background: { r: 0, g: 0, b: 0, alpha: 0 },
withoutEnlargement: true,
})
.webp({ quality: preset.quality })
.toFile(finalPath);
.toBuffer();
if (fs.existsSync(file.path)) {
fs.writeFileSync(finalPath, optimizedBuffer);
if (!finalPathMatchesInput && fs.existsSync(file.path)) {
try {
fs.unlinkSync(file.path);
} catch (cleanupError) {