AutorÃa | Ultima modificación | Ver Log |
{"version":3,"file":"uploader.min.js","sources":["../src/uploader.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Tiny Media plugin for Moodle.\n *\n * @module editor_tiny/uploader\n * @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport {\n not
ifyUploadStarted,\n notifyUploadCompleted,\n} from 'core_form/events';\nimport {getFilePicker} from 'editor_tiny/options';\n\n// This image uploader is based on advice given at:\n// https://www.tiny.cloud/docs/tinymce/6/upload-images/\nexport default (editor, filePickerType, blob, fileName, progress) => new Promise((resolve, reject) => {\n notifyUploadStarted(editor.targetElm.id);\n\n const xhr = new XMLHttpRequest();\n\n // Add the progress handler.\n xhr.upload.addEventListener('progress', (e) => {\n progress(e.loaded / e.total * 100);\n });\n\n xhr.addEventListener('load', () => {\n if (xhr.status === 403) {\n reject({\n message: `HTTP error: ${xhr.status}`,\n remove: true,\n });\n return;\n }\n\n if (xhr.status < 200 || xhr.status >= 300) {\n reject(`HTTP Error: ${xhr.status}`);\n return;\n }\n\n const response = JSON.parse(xhr.responseText);\n\n if (!
response) {\n reject(`Invalid JSON: ${xhr.responseText}`);\n return;\n }\n\n notifyUploadCompleted(editor.targetElm.id);\n\n let location;\n if (response.url) {\n location = response.url;\n } else if (response.event && response.event === 'fileexists' && response.newfile) {\n // A file with this name is already in use here - rename to avoid conflict.\n // Chances are, it's a different image (stored in a different folder on the user's computer).\n // If the user wants to reuse an existing image, they can copy/paste it within the editor.\n location = response.newfile.url;\n }\n\n if (location && typeof location === 'string') {\n resolve(location);\n return;\n }\n\n // Try to parse the error response into a JSON object.\n const errorString = xhr.responseText;\n let output = '';\n try {\n output = JSON.parse(errorString);\
n } catch (error) {\n // If the JSON parsing process returns an error, then it returns the original.\n output = errorString;\n }\n\n reject(output);\n });\n\n xhr.addEventListener('error', () => {\n reject({\n message: `Upload failed due to an XHR transport error. Code: ${xhr.status}`,\n remove: true,\n });\n });\n\n const formData = new FormData();\n const options = getFilePicker(editor, filePickerType);\n\n formData.append('repo_upload_file', blob, fileName);\n formData.append('itemid', options.itemid);\n Object.values(options.repositories).some((repository) => {\n if (repository.type === 'upload') {\n formData.append('repo_id', repository.id);\n return true;\n }\n return false;\n });\n\n formData.append('env', options.env);\n formData.append('sesskey', M.cfg.sesskey);\n formData.append('client_id', options.client_id);\n formData.append('savepath',
options.savepath ?? '/');\n formData.append('ctx_id', options.context.id);\n\n // Accepted types can be either a string or an array, but an array is\n // expected in the processing script, so make sure we are sending an array.\n const acceptedTypes = options.accepted_types;\n if (Array.isArray(acceptedTypes)) {\n acceptedTypes.forEach(function(type) {\n formData.append('accepted_types[]', type);\n });\n } else {\n formData.append('accepted_types[]', acceptedTypes);\n }\n\n xhr.open('POST', `${M.cfg.wwwroot}/repository/repository_ajax.php?action=upload`, true);\n xhr.send(formData);\n});\n"],"names":["editor","filePickerType","blob","fileName","progress","Promise","resolve","reject","targetElm","id","xhr","XMLHttpRequest","upload","addEventListener","e","loaded","total","status","message","remove","response","JSON","parse","responseText","location","url","event","newfile","errorString","output","error","formData","FormData","options","append","item
id","Object","values","repositories","some","repository","type","env","M","cfg","sesskey","client_id","savepath","context","acceptedTypes","accepted_types","Array","isArray","forEach","open","wwwroot","send"],"mappings":"gOA8Be,CAACA,OAAQC,eAAgBC,KAAMC,SAAUC,WAAa,IAAIC,SAAQ,CAACC,QAASC,gEACnEP,OAAOQ,UAAUC,UAE/BC,IAA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