Skip to content

Commit

Permalink
🎨 Use unoptimised image when possible for dynamic images (#10314)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
allouis authored and kevinansfield committed Jan 8, 2019
1 parent df1ba8a commit 935b0f6
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions core/server/web/shared/middlewares/image/handle-image-sizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down

0 comments on commit 935b0f6

Please sign in to comment.