import axios from "axios"; import route from "../ts/modules/route"; class ckUploadAdapter { constructor(loader, folder) { // The file loader instance to use during the upload. this.loader = loader; this.folder = folder; } // Starts the upload process. upload() { return this.loader.file.then( (file) => new Promise((resolve, reject) => { this._initRequest(); this._sendRequest(file, resolve, reject); }), ); } // Aborts the upload process. abort() { if (this.cancel) { this.cancel.abort(); } } // Initializes the XMLHttpRequest object using the URL passed to the constructor. _initRequest() { this.cancel = axios.CancelToken.source(); this.route = route.methods.route("bo.ckeditor.upload"); } // Prepares the data and sends the request. _sendRequest(file, resolve, reject) { // Prepare the form data. const data = new FormData(); data.append("upload", file); data.append("folder", this.folder); axios .post(this.route, data, { headers: { "Content-Type": "multipart/form-data", }, onUploadProgress: (evt) => { this.loader.uploadTotal = evt.total; this.loader.uploaded = evt.loaded; }, }) .then((response) => { resolve({ default: response.data.url, }); }) .catch((error) => { console.error(error); reject( "Le fichier n'a pas pu être téléchargé. Veuillez réessayer ultérieurement et contacter le support si le problème persiste.", ); }); } } function CkUploadAdapterPlugin(editor) { editor.plugins.get("FileRepository").createUploadAdapter = (loader) => { return new ckUploadAdapter(loader, editor.config._config.folder); }; } export default CkUploadAdapterPlugin;