From 041ed9043b4a218c85e73e8a4d121bc203f773be Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Mon, 29 Jul 2024 21:54:11 +0100 Subject: [PATCH] MDL-82177 file upload: default to path '/'when things go wrong This 'fix' is basically a work-around, but one that is already used in some places. Now it is used in all similar places. Exactly what causes the current file path in a file manager to become unset is still unclear to me, but it seems to be an obscure race condition that is very hard to reproduce. However, when it happens, currently we are effectively defaulting to path '/undefined/'. Using '/' is never worse, and is infinitely better in the case where the filepicker does not allow folders to be used (which includes important cases like essay questions in a quiz, which some people use for exams!). In that case, falling back to non-top-level path leads to dataloss. Also, this is all historic JavaScript which will get replaced in the grand de-YUI-fication, so I think a pragmatic fix is justified here. --- lib/form/dndupload.js | 4 +++- lib/form/filemanager.js | 2 +- repository/filepicker.js | 9 +++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/form/dndupload.js b/lib/form/dndupload.js index ebe96e6f903..8c612eb661e 100644 --- a/lib/form/dndupload.js +++ b/lib/form/dndupload.js @@ -1084,8 +1084,10 @@ M.form_dndupload.init = function(Y, options) { if (this.options.author) { formdata.append('author', this.options.author); } - if (this.options.filemanager) { // Filepickers do not have folders + if (this.options.filemanager && this.options.filemanager.currentpath) { // Filepickers do not have folders formdata.append('savepath', this.options.filemanager.currentpath); + } else { + formdata.append('savepath', '/'); } formdata.append('title', filename); if (overwrite) { diff --git a/lib/form/filemanager.js b/lib/form/filemanager.js index 8f9e5cba4d7..5711dfe57b0 100644 --- a/lib/form/filemanager.js +++ b/lib/form/filemanager.js @@ -159,7 +159,7 @@ M.form_filemanager.init = function(Y, options) { } params['sesskey'] = M.cfg.sesskey; params['client_id'] = this.client_id; - params['filepath'] = this.currentpath; + params['filepath'] = this.currentpath || '/'; params['itemid'] = this.options.itemid?this.options.itemid:0; if (args['params']) { for (i in args['params']) { diff --git a/repository/filepicker.js b/repository/filepicker.js index 3dd8d94c919..5b703bade03 100644 --- a/repository/filepicker.js +++ b/repository/filepicker.js @@ -1281,7 +1281,12 @@ M.core_filepicker.init = function(Y, options) { var title = selectnode.one('.fp-saveas input').get('value'); var filesource = selectnode.one('form #filesource-'+client_id).get('value'); var filesourcekey = selectnode.one('form #filesourcekey-'+client_id).get('value'); - var params = {'title':title, 'source':filesource, 'savepath': this.options.savepath, sourcekey: filesourcekey}; + var params = { + 'title': title, + 'source': filesource, + 'savepath': this.options.savepath || '/', + 'sourcekey': filesourcekey, + }; var license = selectnode.one('.fp-setlicense select'); if (license) { params['license'] = license.get('value'); @@ -1886,7 +1891,7 @@ M.core_filepicker.init = function(Y, options) { scope: scope, action:'upload', client_id: client_id, - params: {'savepath':scope.options.savepath}, + params: {'savepath': scope.options.savepath || '/'}, repository_id: scope.active_repo.id, form: {id: id, upload:true}, onerror: function(id, o, args) {