diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6 index 9f06d80b7f7..2c6e4ea6404 100644 --- a/app/assets/javascripts/discourse/components/composer-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6 @@ -493,7 +493,7 @@ export default Ember.Component.extend({ this._xhr = null; if (!userCancelled) { - displayErrorForUpload(data.jqXHR.responseJSON); + displayErrorForUpload(data); } }); diff --git a/app/assets/javascripts/discourse/lib/utilities.js.es6 b/app/assets/javascripts/discourse/lib/utilities.js.es6 index dde738d9a83..33558befacb 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js.es6 +++ b/app/assets/javascripts/discourse/lib/utilities.js.es6 @@ -334,27 +334,27 @@ export function getUploadMarkdown(upload) { } export function displayErrorForUpload(data) { - // deal with meaningful errors first if (data.jqXHR) { switch (data.jqXHR.status) { // cancelled by the user - case 0: return; + case 0: + return; - // entity too large, usually returned from the web server + // entity too large, usually returned from the web server case 413: - var type = uploadTypeFromFileName(data.files[0].name); - var maxSizeKB = Discourse.SiteSettings['max_' + type + '_size_kb']; - bootbox.alert(I18n.t('post.errors.file_too_large', { max_size_kb: maxSizeKB })); - return; + const type = uploadTypeFromFileName(data.files[0].name); + const max_size_kb = Discourse.SiteSettings[`max_${type}_size_kb`]; + bootbox.alert(I18n.t('post.errors.file_too_large', { max_size_kb })); + return; - // the error message is provided by the server + // the error message is provided by the server case 422: - if (data.jqXHR.responseJSON.message) { - bootbox.alert(data.jqXHR.responseJSON.message); - } else { - bootbox.alert(data.jqXHR.responseJSON.join("\n")); - } - return; + if (data.jqXHR.responseJSON.message) { + bootbox.alert(data.jqXHR.responseJSON.message); + } else { + bootbox.alert(data.jqXHR.responseJSON.join("\n")); + } + return; } } else if (data.errors && data.errors.length > 0) { bootbox.alert(data.errors.join("\n")); diff --git a/app/assets/javascripts/discourse/mixins/upload.js.es6 b/app/assets/javascripts/discourse/mixins/upload.js.es6 index 1dc55eeb851..eec73d8c3d1 100644 --- a/app/assets/javascripts/discourse/mixins/upload.js.es6 +++ b/app/assets/javascripts/discourse/mixins/upload.js.es6 @@ -56,7 +56,7 @@ export default Em.Mixin.create({ }); $upload.on("fileuploadfail", (e, data) => { - displayErrorForUpload(data.jqXHR.responseJSON); + displayErrorForUpload(data); reset(); }); }.on("didInsertElement"), diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 8ea6c766eb2..dcfaa4ed3c5 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -26,17 +26,22 @@ class UploadsController < ApplicationController # note, atm hijack is processed in its own context and has not access to controller # longer term we may change this hijack do - info = UploadsController.create_upload( - current_user: me, - file: file, - url: url, - type: type, - for_private_message: for_private_message, - pasted: pasted, - is_api: is_api, - retain_hours: retain_hours - ) - render json: UploadsController.serialize_upload(info), status: Upload === info ? 200 : 422 + begin + info = UploadsController.create_upload( + current_user: me, + file: file, + url: url, + type: type, + for_private_message: for_private_message, + pasted: pasted, + is_api: is_api, + retain_hours: retain_hours + ) + rescue => e + render json: failed_json.merge(message: e.message&.split("\n")&.first), status: 422 + else + render json: UploadsController.serialize_upload(info), status: Upload === info ? 200 : 422 + end end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index a44704fd897..9d2ce6e56f3 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2873,6 +2873,7 @@ en: store_failure: "Failed to store upload #%{upload_id} for user #%{user_id}." file_missing: "Sorry, you must provide a file to upload." empty: "Sorry, but the file you provided is empty." + png_to_jpg_conversion_failure_message: "An error happened when converting from PNG to JPG." attachments: too_large: "Sorry, the file you are trying to upload is too big (maximum size is %{max_size_kb}KB)." images: diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index 75f4b3cff6e..71057483203 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -163,7 +163,7 @@ class UploadCreator command << '-debug' << 'all' if debug command << output_file.path - Discourse::Utils.execute_command(*command, failure_message: "failed to convert png to jpg") + Discourse::Utils.execute_command(*command, failure_message: I18n.t("upload.png_to_jpg_conversion_failure_message")) end def should_downsize?