MDL-78096 editor_tiny: Convert error to JSON and send the accepted types
This commit is contained in:
+1
-1
@@ -1,3 +1,3 @@
|
||||
define("editor_tiny/uploader",["exports","core_form/events","editor_tiny/options"],(function(_exports,_events,_options){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0;return _exports.default=(editor,filePickerType,blob,fileName,progress)=>new Promise(((resolve,reject)=>{var _options$savepath;(0,_events.notifyUploadStarted)(editor.targetElm.id);const xhr=new XMLHttpRequest;xhr.upload.addEventListener("progress",(e=>{progress(e.loaded/e.total*100)})),xhr.addEventListener("load",(()=>{if(403===xhr.status)return void reject({message:"HTTP error: ".concat(xhr.status),remove:!0});if(xhr.status<200||xhr.status>=300)return void reject("HTTP Error: ".concat(xhr.status));const response=JSON.parse(xhr.responseText);if(!response)return void reject("Invalid JSON: ".concat(xhr.responseText));let location;(0,_events.notifyUploadCompleted)(editor.targetElm.id),response.url?location=response.url:response.event&&"fileexists"===response.event&&response.newfile&&(location=response.newfile.url),location&&"string"==typeof location?resolve(location):reject("Unable to handle file result: ".concat(xhr.responseText))})),xhr.addEventListener("error",(()=>{reject({message:"Upload failed due to an XHR transport error. Code: ".concat(xhr.status),remove:!0})}));const formData=new FormData,options=(0,_options.getFilePicker)(editor,filePickerType);formData.append("repo_upload_file",blob,fileName),formData.append("itemid",options.itemid),Object.values(options.repositories).some((repository=>"upload"===repository.type&&(formData.append("repo_id",repository.id),!0))),formData.append("env",options.env),formData.append("sesskey",M.cfg.sesskey),formData.append("client_id",options.client_id),formData.append("savepath",null!==(_options$savepath=options.savepath)&&void 0!==_options$savepath?_options$savepath:"/"),formData.append("ctx_id",options.context.id),xhr.open("POST","".concat(M.cfg.wwwroot,"/repository/repository_ajax.php?action=upload"),!0),xhr.send(formData)})),_exports.default}));
|
||||
define("editor_tiny/uploader",["exports","core_form/events","editor_tiny/options"],(function(_exports,_events,_options){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0;return _exports.default=(editor,filePickerType,blob,fileName,progress)=>new Promise(((resolve,reject)=>{var _options$savepath;(0,_events.notifyUploadStarted)(editor.targetElm.id);const xhr=new XMLHttpRequest;xhr.upload.addEventListener("progress",(e=>{progress(e.loaded/e.total*100)})),xhr.addEventListener("load",(()=>{if(403===xhr.status)return void reject({message:"HTTP error: ".concat(xhr.status),remove:!0});if(xhr.status<200||xhr.status>=300)return void reject("HTTP Error: ".concat(xhr.status));const response=JSON.parse(xhr.responseText);if(!response)return void reject("Invalid JSON: ".concat(xhr.responseText));let location;if((0,_events.notifyUploadCompleted)(editor.targetElm.id),response.url?location=response.url:response.event&&"fileexists"===response.event&&response.newfile&&(location=response.newfile.url),location&&"string"==typeof location)return void resolve(location);const errorString=xhr.responseText,cleanedErrorString=errorString.replace(/\\/g,"");let output="";try{output=JSON.parse(cleanedErrorString)}catch(error){output=errorString}reject(output)})),xhr.addEventListener("error",(()=>{reject({message:"Upload failed due to an XHR transport error. Code: ".concat(xhr.status),remove:!0})}));const formData=new FormData,options=(0,_options.getFilePicker)(editor,filePickerType);formData.append("repo_upload_file",blob,fileName),formData.append("itemid",options.itemid),Object.values(options.repositories).some((repository=>"upload"===repository.type&&(formData.append("repo_id",repository.id),!0))),formData.append("env",options.env),formData.append("sesskey",M.cfg.sesskey),formData.append("client_id",options.client_id),formData.append("savepath",null!==(_options$savepath=options.savepath)&&void 0!==_options$savepath?_options$savepath:"/"),formData.append("ctx_id",options.context.id);const acceptedTypes=options.accepted_types;Array.isArray(acceptedTypes)?acceptedTypes.forEach((function(type){formData.append("accepted_types[]",type)})):formData.append("accepted_types[]",acceptedTypes),xhr.open("POST","".concat(M.cfg.wwwroot,"/repository/repository_ajax.php?action=upload"),!0),xhr.send(formData)})),_exports.default}));
|
||||
|
||||
//# sourceMappingURL=uploader.min.js.map
|
||||
File diff suppressed because one or more lines are too long
@@ -76,7 +76,19 @@ export default (editor, filePickerType, blob, fileName, progress) => new Promise
|
||||
return;
|
||||
}
|
||||
|
||||
reject(`Unable to handle file result: ${xhr.responseText}`);
|
||||
const errorString = xhr.responseText;
|
||||
// Remove backslashes and escape characters in the stack trace.
|
||||
const cleanedErrorString = errorString.replace(/\\/g, '');
|
||||
// Try to parse the cleaned string into a JSON object.
|
||||
let output = '';
|
||||
try {
|
||||
output = JSON.parse(cleanedErrorString);
|
||||
} catch (error) {
|
||||
// If the JSON parsing process returns an error, then it returns the original.
|
||||
output = errorString;
|
||||
}
|
||||
|
||||
reject(output);
|
||||
});
|
||||
|
||||
xhr.addEventListener('error', () => {
|
||||
@@ -105,6 +117,17 @@ export default (editor, filePickerType, blob, fileName, progress) => new Promise
|
||||
formData.append('savepath', options.savepath ?? '/');
|
||||
formData.append('ctx_id', options.context.id);
|
||||
|
||||
// Accepted types can be either a string or an array, but an array is
|
||||
// expected in the processing script, so make sure we are sending an array.
|
||||
const acceptedTypes = options.accepted_types;
|
||||
if (Array.isArray(acceptedTypes)) {
|
||||
acceptedTypes.forEach(function(type) {
|
||||
formData.append('accepted_types[]', type);
|
||||
});
|
||||
} else {
|
||||
formData.append('accepted_types[]', acceptedTypes);
|
||||
}
|
||||
|
||||
xhr.open('POST', `${M.cfg.wwwroot}/repository/repository_ajax.php?action=upload`, true);
|
||||
xhr.send(formData);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user