From 935b0f6d49f3c68e34d11a3435e20472884a6fda Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 8 Jan 2019 11:00:15 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Use=20unoptimised=20image=20when?= =?UTF-8?q?=20possible=20for=20dynamic=20images=20(#10314)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../middlewares/image/handle-image-sizes.js | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/core/server/web/shared/middlewares/image/handle-image-sizes.js b/core/server/web/shared/middlewares/image/handle-image-sizes.js index 0251669809..f3bfb9cd8b 100644 --- a/core/server/web/shared/middlewares/image/handle-image-sizes.js +++ b/core/server/web/shared/middlewares/image/handle-image-sizes.js @@ -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); })