🐛 Fixed HTTP 500 error when image processing fails during upload

fixes ENG-740
fixes https://linear.app/tryghost/issue/ENG-740/http-500-error-when-image-processing-fails

- in the event the image transform library throws (which can happen for
  many reasons; sharp/libvips can come across a number of errors), we
  currently return this as a HTTP 500 error to the user
- in this case, we should just try-catch the call and jump to the
  non-processing flow where it just saves the original image
- also added breaking test
This commit is contained in:
Daniel Lockyer 2024-03-12 15:21:57 +01:00 committed by Daniel Lockyer
parent ef143978e7
commit 4aad551c72
2 changed files with 16 additions and 1 deletions

View File

@ -33,7 +33,12 @@ module.exports = {
width: config.get('imageOptimization:defaultMaxWidth')
}, imageOptimizationOptions);
await imageTransform.resizeFromPath(options);
try {
await imageTransform.resizeFromPath(options);
} catch (err) {
// If the image processing fails, we just want to store the original image
return store.save(frame.file);
}
// Store the processed/optimized image
const processedImageUrl = await store.save({

View File

@ -301,4 +301,14 @@ describe('Images API', function () {
await uploadImageCheck({path: originalFilePath, filename: 'a.png', contentType: 'image/png'});
clock.restore();
});
it('Does not return HTTP 500 when image processing fails', async function () {
sinon.stub(imageTransform, 'resizeFromPath').rejects(new Error('Image processing failed'));
const originalFilePath = p.join(__dirname, '/../../utils/fixtures/images/ghost-logo.png');
const fileContents = await fs.readFile(originalFilePath);
await uploadImageRequest({fileContents, filename: 'test.png', contentType: 'image/png'})
.expectStatus(201);
});
});