Skip to content

Commit

Permalink
MDL-60427 dndupload: detect attempts to upload folders
Browse files Browse the repository at this point in the history
  • Loading branch information
davosmith committed Jan 3, 2018
1 parent 3fa531e commit 4e737cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
29 changes: 23 additions & 6 deletions course/dndupload.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
},

/**
Expand Down
1 change: 1 addition & 0 deletions course/dnduploadlib.php
Expand Up @@ -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')
),
Expand Down
1 change: 1 addition & 0 deletions lang/en/moodle.php
Expand Up @@ -799,6 +799,7 @@
$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';
Expand Down

0 comments on commit 4e737cf

Please sign in to comment.