mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
🎨 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:
parent
df1ba8aee1
commit
935b0f6d49
1 changed files with 20 additions and 2 deletions
|
@ -53,9 +53,27 @@ module.exports = function (req, res, next) {
|
||||||
return;
|
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) => {
|
.then((originalImageBuffer) => {
|
||||||
return image.manipulator.resizeImage(originalImageBuffer, imageDimensionConfig);
|
return image.manipulator.resizeImage(originalImageBuffer, imageDimensionConfig);
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue