From 54b537debae859d4e66921fa4ff2fede11ca4335 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Wed, 7 Jul 2021 19:11:19 +0100 Subject: [PATCH] Fixed error when requesting resize of a blank image closes https://github.com/TryGhost/Team/issues/819 - adds guard for an empty buffer when reading file from storage for resizing, if a blank image is loaded then redirect to the original file --- core/server/web/site/middleware/handle-image-sizes.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/core/server/web/site/middleware/handle-image-sizes.js b/core/server/web/site/middleware/handle-image-sizes.js index 0e035999ad..1814811bbf 100644 --- a/core/server/web/site/middleware/handle-image-sizes.js +++ b/core/server/web/site/middleware/handle-image-sizes.js @@ -1,5 +1,6 @@ const _ = require('lodash'); const path = require('path'); +const {GhostError} = require('@tryghost/errors'); const imageTransform = require('@tryghost/image-transform'); const storage = require('../../../adapters/storage'); const activeTheme = require('../../../../frontend/services/themes/active'); @@ -100,6 +101,12 @@ module.exports = function (req, res, next) { return storageInstance.read({path: storagePath}); }) .then((originalImageBuffer) => { + if (originalImageBuffer.length <= 0) { + throw new GhostError({ + errorType: 'NoContentError', + statusCode: 204 + }); + } return imageTransform.resizeFromBuffer(originalImageBuffer, imageDimensionConfig); }) .then((resizedImageBuffer) => { @@ -108,7 +115,7 @@ module.exports = function (req, res, next) { }).then(() => { next(); }).catch(function (err) { - if (err.code === 'SHARP_INSTALLATION') { + if (err.code === 'SHARP_INSTALLATION' || err.errorType === 'NoContentError') { return redirectToOriginal(); } next(err);