Skip to content

A platform for community discussion. Free, open, simple.

DEV: Fix uploadHandler impl. in composer-upload-uppy mixin (#15105)

Martin Brennane46495833309
diff --git a/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js b/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js
index 316518f9dee..a79b742a717 100644
--- a/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js
+++ b/app/assets/javascripts/discourse/app/mixins/composer-upload-uppy.js
@@ -140,24 +140,36 @@ export default Mixin.create(ExtendableUploader, UppyS3Multipart, {
},
onBeforeUpload: (files) => {
- const fileCount = Object.keys(files).length;
const maxFiles = this.siteSettings.simultaneous_uploads;
// Look for a matching file upload handler contributed from a plugin.
- // It is not ideal that this only works for single file uploads, but
- // at this time it is all we need. 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.
- if (fileCount === 1) {
- const file = Object.values(files)[0];
+ // 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 = {};
+ for (const [fileId, file] of Object.entries(files)) {
const matchingHandler = this._findMatchingUploadHandler(file.name);
+ if (matchingHandler) {
+ uploadHandlerFiles.push([file, matchingHandler]);
+ } 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)) {
return this._abortAndReset();
}
- }
+ });
- // Limit the number of simultaneous uploads
+ // Limit the number of simultaneous uploads, for files which have
+ // _not_ been handled by an upload handler.
+ const fileCount = Object.keys(unhandledFiles).length;
if (maxFiles > 0 && fileCount > maxFiles) {
bootbox.alert(
I18n.t("post.errors.too_many_dragged_and_dropped_files", {
@@ -166,6 +178,9 @@ export default Mixin.create(ExtendableUploader, UppyS3Multipart, {
);
return this._abortAndReset();
}
+
+ // uppy uses this new object to track progress of remaining files
+ return unhandledFiles;
},
});
@@ -196,14 +211,14 @@ export default Mixin.create(ExtendableUploader, UppyS3Multipart, {
});
this._uppyInstance.on("file-removed", (file, reason) => {
- file.meta.cancelled = true;
-
// we handle the cancel-all event specifically, so no need
- // to do anything here
+ // to do anything here. this event is also fired when some files
+ // are handled by an upload handler
if (reason === "cancel-all") {
return;
}
+ file.meta.cancelled = true;
this._removeInProgressUpload(file.id);
this._resetUpload(file, { removePlaceholder: true });
if (this.inProgressUploads.length === 0) {