mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
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
This commit is contained in:
parent
b12589ce6d
commit
69bc5a9dfd
2 changed files with 80 additions and 1 deletions
|
@ -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/theme-engine/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);
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
const should = require('should');
|
||||
const sinon = require('sinon');
|
||||
const storage = require('../../../../../core/server/adapters/storage');
|
||||
const activeTheme = require('../../../../../core/frontend/services/theme-engine/active');
|
||||
const handleImageSizes = require('../../../../../core/server/web/site/middleware/handle-image-sizes.js');
|
||||
|
||||
// @TODO make these tests lovely and non specific to implementation
|
||||
|
@ -41,4 +44,73 @@ describe('handleImageSizes middleware', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('file handling', function () {
|
||||
let dummyStorage;
|
||||
let dummyTheme;
|
||||
|
||||
this.beforeEach(function () {
|
||||
dummyStorage = {
|
||||
async exists() {
|
||||
return true;
|
||||
},
|
||||
|
||||
read() {
|
||||
return Buffer.from([]);
|
||||
},
|
||||
|
||||
async saveRaw(buf, url) {
|
||||
return url;
|
||||
}
|
||||
};
|
||||
|
||||
dummyTheme = {
|
||||
config(key) {
|
||||
if (key === 'image_sizes') {
|
||||
return {
|
||||
l: {
|
||||
width: 1000
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
sinon.stub(storage, 'getStorage').returns(dummyStorage);
|
||||
sinon.stub(activeTheme, 'get').returns(dummyTheme);
|
||||
});
|
||||
|
||||
this.afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it('returns original URL if file is empty', function (done) {
|
||||
dummyStorage.exists = async function (path) {
|
||||
if (path === '/blank_o.png') {
|
||||
return true;
|
||||
}
|
||||
if (path === '/size/w1000/blank.png') {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const fakeReq = {
|
||||
url: '/size/w1000/blank.png',
|
||||
originalUrl: '/size/w1000/blank.png'
|
||||
};
|
||||
const fakeRes = {
|
||||
redirect(url) {
|
||||
url.should.equal('/blank.png');
|
||||
done();
|
||||
}
|
||||
};
|
||||
|
||||
handleImageSizes(fakeReq, fakeRes, function next(err) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
done(new Error('Should not have called next'));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue