Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched to throwing error upon failed image processing #19843

Merged
merged 1 commit into from Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions ghost/core/core/server/api/endpoints/images.js
@@ -1,8 +1,10 @@
/* eslint-disable ghost/ghost-custom/max-api-complexity */
const storage = require('../../adapters/storage');
const path = require('path');
const errors = require('@tryghost/errors');
const imageTransform = require('@tryghost/image-transform');

const storage = require('../../adapters/storage');
const config = require('../../../shared/config');
const path = require('path');

module.exports = {
docName: 'images',
Expand Down Expand Up @@ -36,8 +38,12 @@ module.exports = {
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);
// If the image processing fails, we don't want to store the image because it's corrupted/invalid
throw new errors.BadRequestError({
message: 'Image processing failed',
context: err.message,
help: 'Please verify that the image is valid'
});
}

// Store the processed/optimized image
Expand Down
18 changes: 18 additions & 0 deletions ghost/core/test/e2e-api/admin/__snapshots__/images.test.js.snap
Expand Up @@ -72,6 +72,24 @@ Object {
}
`;

exports[`Images API Does not return HTTP 500 when image processing fails 1: [body] 1`] = `
Object {
"errors": Array [
Object {
"code": null,
"context": "Image processing failed Image processing failed",
"details": null,
"ghostErrorCode": null,
"help": "Please verify that the image is valid",
"id": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/,
"message": "Request not understood error, cannot upload image.",
"property": null,
"type": "BadRequestError",
},
],
}
`;

exports[`Images API Will error when filename is too long 1: [body] 1`] = `
Object {
"errors": Array [
Expand Down
9 changes: 8 additions & 1 deletion ghost/core/test/e2e-api/admin/images.test.js
Expand Up @@ -308,7 +308,14 @@ describe('Images API', function () {
const originalFilePath = p.join(__dirname, '/../../utils/fixtures/images/ghost-logo.png');
const fileContents = await fs.readFile(originalFilePath);

const loggingStub = sinon.stub(logging, 'error');
await uploadImageRequest({fileContents, filename: 'test.png', contentType: 'image/png'})
.expectStatus(201);
.expectStatus(400)
.matchBodySnapshot({
errors: [{
id: anyErrorId
}]
});
sinon.assert.calledOnce(loggingStub);
});
});