/** * @module modules/uploader */ import type { HandlerError, HandlerSuccess, IUploader, MediaUploadType } from 'jodit/types'; import { error, isFunction, isPlainObject, toArray } from 'jodit/core/helpers'; import { send } from 'jodit/modules/uploader/helpers/send'; /** * Send files to server */ export function sendFiles( uploader: IUploader, files: FileList | File[] | null, handlerSuccess?: HandlerSuccess, handlerError?: HandlerError, process?: (form: FormData) => void, mediaUploadType?: MediaUploadType ): Promise { // console.log(''); // console.log( // '======================== sendFiles() ========================' // ); // console.log('uploader: ', uploader); if (!files) { return Promise.reject(error('Need files')); } const { o } = uploader; const callError = (error: any): void => { o.defaultHandlerError.call(uploader, error); o.error.call(uploader, error); }; let fileList: File[] = toArray(files); if (!fileList.length) { return Promise.reject(error('Need files')); } const promises: Array> = []; fileList = fileList.filter(a => a); if (!fileList.length) return Promise.all(promises); if (mediaUploadType === 'image' && fileList.length > 1) { uploader.j.e.fire('multiImageUploaded', fileList); return Promise.all(promises); } // Save files for (let i = 0; i < fileList.length; i += 1) { const form = new FormData(); const file: File = fileList[i]; if (!file) continue; const hasRealExtension = /\.[\d\w]+$/.test(file.name); const match_type = file.type.match(/([a-z0-9]+)\//i) as string[]; const match_extension = file.type.match(/\/([a-z0-9]+)/i) as string[]; const fileType: string = match_type && match_type[1] ? match_type[1].toLowerCase() : ''; const fileExtension: string = match_extension && match_extension[1] ? match_extension[1].toLowerCase() : ''; const time = String(new Date().getTime()); let newName = file.name ? `${time}_${file.name}` : time; if (!hasRealExtension && fileExtension) { let extForReg = fileExtension; if (['jpeg', 'jpg'].includes(extForReg)) { extForReg = 'jpeg|jpg'; } const reEnd = new RegExp('.(' + extForReg + ')$', 'i'); if (!reEnd.test(newName)) { newName += '.' + fileExtension; } } const [key, iFile, name] = o.processFileName.call( uploader, o.filesVariableName(fileType, fileExtension), fileList[i], newName ); if (iFile.size > 104857600) { const error = { error: 'File size is too big', // message: 'File size is too big', statusCode: 413, fileName: iFile.name, mediaUploadType }; callError(error); continue; } // Add file form.append(key, iFile, name); // console.log({ key, iFile, name }); if (process) { process(form); } if (o.data && isPlainObject(o.data)) { Object.keys(o.data).forEach((key: string) => { form.append(key, (o.data as any)[key]); }); } o.prepareData.call(uploader, form); // const uploaderPayload = iFile as IUploaderPayload; // uploaderPayload.uploadType === 'file'; // Object.assign(iFile, { uploadType: 'file' }); // console.log(iFile); promises.push( send(uploader, form) .then(resp => { // console.log('send-files.ts send() response: ', resp); const handler = isFunction(handlerSuccess) ? handlerSuccess : o.defaultHandlerSuccess; handler.call( uploader, o.process.call(uploader, resp), undefined, iFile ); return resp; }) .then(() => { uploader.j.events && uploader.j.e.fire('filesWereUploaded'); }) .catch(async e => { const error = await e.json(); const newError = { ...error, fileName: iFile.name, mediaUploadType }; callError(newError); }) ); } return Promise.all(promises); }