Skip to content

Commit

Permalink
馃悰 Fixed HTTP 500 error when image processing fails during upload
Browse files Browse the repository at this point in the history
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
  • Loading branch information
daniellockyer committed Mar 12, 2024
1 parent ef14397 commit 4aad551
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ghost/core/core/server/api/endpoints/images.js
Expand Up @@ -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({
Expand Down
10 changes: 10 additions & 0 deletions ghost/core/test/e2e-api/admin/images.test.js
Expand Up @@ -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);
});
});

0 comments on commit 4aad551

Please sign in to comment.