From 4e737cf37f2ac658732ed6db15bbb844e2da2bed Mon Sep 17 00:00:00 2001 From: Davo Smith Date: Wed, 6 Dec 2017 09:33:44 +0000 Subject: [PATCH] MDL-60427 dndupload: detect attempts to upload folders --- course/dndupload.js | 29 +++++++++++++++++++++++------ course/dnduploadlib.php | 1 + lang/en/moodle.php | 1 + 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/course/dndupload.js b/course/dndupload.js index 7c60ee00a1b..f15f6a4a9c5 100644 --- a/course/dndupload.js +++ b/course/dndupload.js @@ -410,12 +410,12 @@ M.course_dndupload = { * @return false to prevent the event from continuing to be processed */ drop: function(e) { + this.hide_preview_element(); + if (!(type = this.check_drag(e))) { return false; } - this.hide_preview_element(); - // Work out the number of the section we are on (from its id) var section = this.get_section(e.currentTarget); var sectionnumber = this.get_section_number(section); @@ -792,16 +792,33 @@ M.course_dndupload = { // Prepare the data to send var formData = new FormData(); - formData.append('repo_upload_file', file); + try { + formData.append('repo_upload_file', file); + } catch (e) { + // Edge throws an error at this point if we try to upload a folder. + resel.parent.removeChild(resel.li); + new M.core.alert({message: M.util.get_string('filereaderror', 'moodle', file.name)}); + return; + } formData.append('sesskey', M.cfg.sesskey); formData.append('course', this.courseid); formData.append('section', sectionnumber); formData.append('module', module); formData.append('type', 'Files'); - // Send the AJAX call - xhr.open("POST", this.url, true); - xhr.send(formData); + // Try reading the file to check it is not a folder, before sending it to the server. + var reader = new FileReader(); + reader.onload = function() { + // File was read OK - send it to the server. + xhr.open("POST", self.url, true); + xhr.send(formData); + }; + reader.onerror = function() { + // Unable to read the file (it is probably a folder) - display an error message. + resel.parent.removeChild(resel.li); + new M.core.alert({message: M.util.get_string('filereaderror', 'moodle', file.name)}); + }; + reader.readAsText(file.slice(0, 5)); // Try reading the first few bytes of the file. }, /** diff --git a/course/dnduploadlib.php b/course/dnduploadlib.php index 73ecfc89d38..1e1210570d2 100644 --- a/course/dnduploadlib.php +++ b/course/dnduploadlib.php @@ -64,6 +64,7 @@ function dndupload_add_to_course($course, $modnames) { array('namedfiletoolarge', 'moodle'), array('actionchoice', 'moodle'), array('servererror', 'moodle'), + array('filereaderror', 'moodle'), array('upload', 'moodle'), array('cancel', 'moodle') ), diff --git a/lang/en/moodle.php b/lang/en/moodle.php index f1eb2524416..99db34be552 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -799,6 +799,7 @@ $string['feedback'] = 'Feedback'; $string['file'] = 'File'; $string['fileexists'] = 'There is already a file called {$a}'; $string['filemissing'] = '{$a} is missing'; +$string['filereaderror'] = 'Unable to read the file \'{$a}\' - please check this really is a file and not a folder'; $string['files'] = 'Files'; $string['filesanduploads'] = 'Files and uploads'; $string['filesfolders'] = 'Files/folders';