🎨 Use unoptimised image when possible for dynamic images (#10314)

closes #10283 

Updated middleware for dynamic image sizes to attempt to read the unoptimized image first, taking into account the `-n` suffix for duplicate image names, by using a regex.
This commit is contained in:
Fabien O'Carroll 2019-01-08 11:00:15 +01:00 committed by Kevin Ansfield
parent df1ba8aee1
commit 935b0f6d49

View File

@ -53,9 +53,27 @@ module.exports = function (req, res, next) {
return;
}
const originalImagePath = path.relative(sizeImageDir, req.url);
const imagePath = path.relative(sizeImageDir, req.url);
const {dir, name, ext} = path.parse(imagePath);
const [imageNameMatched, imageName, imageNumber] = name.match(/^(.+?)(-\d+)?$/) || [null];
return storageInstance.read({path: originalImagePath})
if (!imageNameMatched) {
// CASE: Image name does not contain any characters?
// RESULT: Hand off to `next()` which will 404
return;
}
const unoptimizedImagePath = path.join(dir, `${imageName}_o${imageNumber || ''}${ext}`);
return storageInstance.exists(unoptimizedImagePath)
.then((unoptimizedImageExists) => {
if (unoptimizedImageExists) {
return unoptimizedImagePath;
}
return imagePath;
})
.then((path) => {
return storageInstance.read({path});
})
.then((originalImageBuffer) => {
return image.manipulator.resizeImage(originalImageBuffer, imageDimensionConfig);
})