forked from UKSOURCE/cms.hailearning.edu.vn
fix: fix resized image uploads
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user