From cff5ca7c2dcc0cc16243c3f58b49edf9630f2118 Mon Sep 17 00:00:00 2001 From: Tien Nguyen Date: Tue, 30 Nov 2021 16:05:08 +0700 Subject: [PATCH] MDL-73189 File upload: Fix bug can upload more than maximum no. of files --- lib/form/dndupload.js | 94 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 15 deletions(-) diff --git a/lib/form/dndupload.js b/lib/form/dndupload.js index 51f6b540b06..ebe96e6f903 100644 --- a/lib/form/dndupload.js +++ b/lib/form/dndupload.js @@ -56,6 +56,8 @@ M.form_dndupload.init = function(Y, options) { pageentercount: 0, // Holds the progress bar elements for each file. progressbars: {}, + // Number of request in queue and number of request uploading. + totalOfRequest: 0, // Number of request upload. numberOfRequestUpload: 0, @@ -334,6 +336,9 @@ M.form_dndupload.init = function(Y, options) { get: Y.bind('getNumberOfRequestUpload', this), increase: Y.bind('increaseNumberOfRequestUpload', this), decrease: Y.bind('decreaseNumberOfRequestUpload', this), + getTotal: Y.bind('getTotalRequestUpload', this), + increaseTotal: Y.bind('increaseTotalRequest', this), + reset: Y.bind('resetNumberOfRequestUpload', this) }, callbackClearProgress: Y.bind('clear_progress', this), callbackStartProgress: Y.bind('startProgress', this), @@ -356,6 +361,9 @@ M.form_dndupload.init = function(Y, options) { get: Y.bind('getNumberOfRequestUpload', this), increase: Y.bind('increaseNumberOfRequestUpload', this), decrease: Y.bind('decreaseNumberOfRequestUpload', this), + getTotal: Y.bind('getTotalRequestUpload', this), + increaseTotal: Y.bind('increaseTotalRequest', this), + reset: Y.bind('resetNumberOfRequestUpload', this) }, callbackClearProgress: Y.bind('clear_progress', this), callbackStartProgress: Y.bind('startProgress', this), @@ -376,6 +384,15 @@ M.form_dndupload.init = function(Y, options) { this.numberOfRequestUpload++; }, + /** + * Increase total request. + * + * @param {number} newFileCount Number of new files. + */ + increaseTotalRequest: function(newFileCount) { + this.totalOfRequest += newFileCount; + }, + /** * Decrease number of request upload. */ @@ -392,6 +409,25 @@ M.form_dndupload.init = function(Y, options) { return this.numberOfRequestUpload; }, + /** + * Return number of request upload. + * + * @returns {number} + */ + getTotalRequestUpload: function() { + return this.totalOfRequest; + }, + + /** + * Return number of request upload. + * + * @returns {number} + */ + resetNumberOfRequestUpload: function() { + this.numberOfRequestUpload = 0; + this.totalOfRequest = 0; + }, + /** * Check to see if the drag event has any files in it * @@ -544,6 +580,8 @@ M.form_dndupload.init = function(Y, options) { currentfiles: null, // Total number of files already uploaded (to check for exceeding limits). currentfilecount: 0, + // Number of new files will be upload in this dndupload (to check for exceeding limits). + newFileCount: 0, // Total size of the files present in the area. currentareasize: 0, // The list of files to upload. @@ -715,10 +753,11 @@ M.form_dndupload.init = function(Y, options) { */ add_to_upload_queue: function(file, filename, overwrite) { if (!overwrite) { - this.currentfilecount++; + this.newFileCount++; } + // The value for "unlimited files" is -1, so 0 should mean 0. - if (this.options.maxfiles >= 0 && this.currentfilecount > this.options.maxfiles) { + if (this.options.maxfiles >= 0 && this.getTotalNumberOfFiles() > this.options.maxfiles) { // Too many files - abort entire upload. this.uploadqueue = []; this.renamequeue = []; @@ -740,6 +779,21 @@ M.form_dndupload.init = function(Y, options) { return true; }, + /** + * Get total number of files: Number of uploaded files, number of files unloading in other dndupload, + * number of files need to be upload in this dndupload. + * @return number Total number of files. + */ + getTotalNumberOfFiles: function() { + // Get number of files we added into other dndupload. + let totalOfFiles = 0; + if(this.callbackNumberOfRequestUpload) { + totalOfFiles = this.callbackNumberOfRequestUpload.getTotal(); + } + + return this.currentfilecount + this.newFileCount + totalOfFiles; + }, + /** * Take the next file from the renamequeue and ask the user what to do with * it. Called recursively until the queue is empty, then calls do_upload. @@ -748,6 +802,9 @@ M.form_dndupload.init = function(Y, options) { process_renames: function() { if (this.renamequeue.length == 0) { // All rename processing complete - start the actual upload. + if(this.callbackNumberOfRequestUpload && this.uploadqueue.length > 0) { + this.callbackNumberOfRequestUpload.increaseTotal(this.newFileCount); + } this.do_upload(); return; } @@ -761,13 +818,15 @@ M.form_dndupload.init = function(Y, options) { // If the user has clicked on overwrite/rename ALL then process // this file, as appropriate, then process the rest of the queue. if (this.overwriteall) { - this.add_to_upload_queue(file, file.name, true); - this.process_renames(); + if (this.add_to_upload_queue(file, file.name, true)) { + this.process_renames(); + } return; } if (this.renameall) { - this.add_to_upload_queue(file, newname, false); - this.process_renames(); + if (this.add_to_upload_queue(file, newname, false)) { + this.process_renames(); + } return; } @@ -795,16 +854,18 @@ M.form_dndupload.init = function(Y, options) { node.one('.fp-dlg-butoverwrite').on('click', function(e) { e.preventDefault(); process_dlg.hide(); - self.add_to_upload_queue(file, file.name, true); - self.process_renames(); + if (self.add_to_upload_queue(file, file.name, true)) { + self.process_renames(); + } }, this); // Rename uploaded file. node.one('.fp-dlg-butrename').on('click', function(e) { e.preventDefault(); process_dlg.hide(); - self.add_to_upload_queue(file, newname, false); - self.process_renames(); + if (self.add_to_upload_queue(file, newname, false)) { + self.process_renames(); + } }, this); // Cancel all uploads. @@ -819,7 +880,7 @@ M.form_dndupload.init = function(Y, options) { }, this); // When we are at the file limit, only allow 'overwrite', not rename. - if (this.currentfilecount == this.options.maxfiles) { + if (this.getTotalNumberOfFiles() == this.options.maxfiles) { node.one('.fp-dlg-butrename').setStyle('display', 'none'); if (multiplefiles) { node.one('.fp-dlg-butrenameall').setStyle('display', 'none'); @@ -833,8 +894,9 @@ M.form_dndupload.init = function(Y, options) { e.preventDefault(); process_dlg.hide(); this.overwriteall = true; - self.add_to_upload_queue(file, file.name, true); - self.process_renames(); + if (self.add_to_upload_queue(file, file.name, true)) { + self.process_renames(); + } }, this); // Rename all new files. @@ -842,8 +904,9 @@ M.form_dndupload.init = function(Y, options) { e.preventDefault(); process_dlg.hide(); this.renameall = true; - self.add_to_upload_queue(file, newname, false); - self.process_renames(); + if (self.add_to_upload_queue(file, newname, false)) { + self.process_renames(); + } }, this); } node.one('.fp-dlg-text').setContent(M.util.get_string('fileexists', 'moodle', file.name)); @@ -939,6 +1002,7 @@ M.form_dndupload.init = function(Y, options) { * Run the callback to the filemanager/filepicker */ uploadfinished: function(lastresult) { + this.callbackNumberOfRequestUpload.reset(); this.callback(lastresult); },