simple-uploader.js
1 import Base64UploadAdapter from "@ckeditor/ckeditor5-upload/src/adapters/base64uploadadapter"; 2 3 export default class SimpleUploader extends Base64UploadAdapter { 4 constructor(loader) { 5 // The file loader instance to use during the upload. 6 super(); 7 this.loader = loader; 8 } 9 10 /** 11 * Starts the upload process. 12 * 13 * @see module:upload/filerepository~UploadAdapter#upload 14 * @returns {Promise} 15 */ 16 upload() { 17 return new Promise((resolve, reject) => { 18 const reader = this.reader = new window.FileReader(); 19 reader.addEventListener('load', () => { 20 resolve({ default: reader.result }); 21 }); 22 reader.addEventListener('error', err => { 23 reject(err); 24 }); 25 reader.addEventListener('abort', () => { 26 reject(); 27 }); 28 this.loader.file.then(file => { 29 30 //File size 31 // let fileSize = file.size / 1024; 32 33 //LongText in mysql is 4GB, sizeInMB is the limit allowed 34 // const sizeInGB = 4; 35 const sizeInMB = 16; 36 37 //LongText in mysql in Bytes is maxSizeInBytes 38 // const maxSizeInBytes = sizeInGB * 1024 * 1024 * 1024 39 const maxSizeInBytes = sizeInMB * 1024 * 1024; 40 41 //File size in base64 42 const fileSizeInBase64 = Math.ceil((file.size / 3) * 4); 43 44 //Max size in base64 where maxSizeInBytes 45 const maxSizeInBase64 = Math.ceil((maxSizeInBytes / 3) * 4); 46 47 if (fileSizeInBase64 > maxSizeInBase64) { 48 window.$wireui.notify({ 49 title: 'Image exceeds', 50 description: 'Image exceeds the allowed limit', 51 icon: 'error' 52 }); 53 reject(); 54 } 55 else { 56 reader.readAsDataURL(file); 57 } 58 }); 59 }); 60 } 61 }