A platform for community discussion. Free, open, simple.
DEV: Send multiple files in batches to composer upload handlers when using uppy (#15124)
Martin Brennan20b2a42f491c
diff --git a/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js b/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.jsindex a79b742a717..c623a92388c 100644--- a/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js+++ b/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js@@ -146,26 +146,35 @@ export default Mixin.create(ExtendableUploader, UppyS3Multipart, {// In future we may want to devise a nicer way of doing this.// Uppy plugins are out of the question because there is no way to// define which uploader plugin handles which file extensions at this time.- const uploadHandlerFiles = [];const unhandledFiles = {};+ const handlerBuckets = {};+for (const [fileId, file] of Object.entries(files)) {const matchingHandler = this._findMatchingUploadHandler(file.name);if (matchingHandler) {- uploadHandlerFiles.push([file, matchingHandler]);+ // the function signature will be converted to a string for the+ // object key, so we can send multiple files at once to each handler+ if (handlerBuckets[matchingHandler.method]) {+ handlerBuckets[matchingHandler.method].files.push(file);+ } else {+ handlerBuckets[matchingHandler.method] = {+ fn: matchingHandler.method,+ files: [file],+ };+ }} else {unhandledFiles[fileId] = { ...files[fileId] };}}- // This replicates what the old composer upload handler did; because- // jQuery file uploader passed through a single file at a time to- // the upload handlers.- uploadHandlerFiles.forEach((fileWithHandler) => {- const [file, matchingHandler] = fileWithHandler;- if (matchingHandler && !matchingHandler.method(file.data, this)) {+ // Send the collected array of files to each matching handler,+ // rather than the old jQuery file uploader method of sending+ // a single file at a time through to the handler.+ for (const bucket of Object.values(handlerBuckets)) {+ if (!bucket.fn(bucket.files, this)) {return this._abortAndReset();}- });+ }// Limit the number of simultaneous uploads, for files which have// _not_ been handled by an upload handler.diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-uploads-uppy-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-uploads-uppy-test.jsindex d0a0847c710..6671bc8a925 100644--- a/app/assets/javascripts/discourse/tests/acceptance/composer-uploads-uppy-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/composer-uploads-uppy-test.js@@ -234,7 +234,8 @@ acceptance("Uppy Composer Attachment - Upload Handler", function (needs) {});needs.hooks.beforeEach(() => {withPluginApi("0.8.14", (api) => {- api.addComposerUploadHandler(["png"], (file) => {+ api.addComposerUploadHandler(["png"], (files) => {+ const file = files[0];bootbox.alert(`This is an upload handler test for ${file.name}`);});});