From db53e022cc841d23c4c5fe6a231f755db1531550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 11 Feb 2015 19:34:48 +0100 Subject: [PATCH] FEATURE: prevent upload of more than 10 files at a time --- .../javascripts/discourse/lib/utilities.js | 16 ---------------- .../javascripts/discourse/mixins/upload.js.es6 | 9 +++++++++ config/locales/client.en.yml | 1 + 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/utilities.js b/app/assets/javascripts/discourse/lib/utilities.js index 243910e019b..266ed03ba64 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js +++ b/app/assets/javascripts/discourse/lib/utilities.js @@ -146,16 +146,9 @@ Discourse.Utilities = { } }, - /** - Validate a list of files to be uploaded - - @method validateUploadedFiles - @param {Array} files The list of files we want to upload - **/ validateUploadedFiles: function(files, bypassNewUserRestriction) { if (!files || files.length === 0) { return false; } - // can only upload one file at a time if (files.length > 1) { bootbox.alert(I18n.t('post.errors.too_many_uploads')); return false; @@ -173,15 +166,6 @@ Discourse.Utilities = { return Discourse.Utilities.validateUploadedFile(upload, type, bypassNewUserRestriction); }, - /** - Validate a file to be uploaded - - @method validateUploadedFile - @param {File} file The file to be uploaded - @param {string} type The type of the upload (image, attachment) - @params {bool} bypassNewUserRestriction - @returns true whenever the upload is valid - **/ validateUploadedFile: function(file, type, bypassNewUserRestriction) { // check that the uploaded file is authorized if (!Discourse.Utilities.authorizesAllExtensions() && diff --git a/app/assets/javascripts/discourse/mixins/upload.js.es6 b/app/assets/javascripts/discourse/mixins/upload.js.es6 index dceab04b34b..ac91a056de1 100644 --- a/app/assets/javascripts/discourse/mixins/upload.js.es6 +++ b/app/assets/javascripts/discourse/mixins/upload.js.es6 @@ -22,6 +22,15 @@ export default Em.Mixin.create({ pasteZone: $upload }); + $upload.on("fileuploaddrop", function (e, data) { + if (data.files.length > 10) { + bootbox.alert(I18n.t("post.errors.too_many_dragged_and_dropped_files")); + return false; + } else { + return true; + } + }); + $upload.on('fileuploadsubmit', function (e, data) { var isValid = Discourse.Utilities.validateUploadedFiles(data.files, true); var form = { image_type: self.get('type') }; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index b4b708d795d..0d89d9b019a 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1106,6 +1106,7 @@ en: attachment_too_large: "Sorry, the file you are trying to upload is too big (maximum size is {{max_size_kb}}kb)." file_too_large: "Sorry, the file you are trying to upload is too big (maximum size is {{max_size_kb}}kb)" too_many_uploads: "Sorry, you can only upload one file at a time." + too_many_dragged_and_dropped_files: "Sorry, you can only drag & drop up to 10 files at a time." upload_not_authorized: "Sorry, the file you are trying to upload is not authorized (authorized extension: {{authorized_extensions}})." image_upload_not_allowed_for_new_user: "Sorry, new users can not upload images." attachment_upload_not_allowed_for_new_user: "Sorry, new users can not upload attachments."