// Define more types here const FORMAT_PDF = ['pdf']; const FORMAT_TEXT = ['txt']; const FORMAT_PHOTOSHOP = ['psd']; const FORMAT_WORD = ['doc', 'docx']; const FORMAT_EXCEL = ['xls', 'xlsx']; const FORMAT_ZIP = ['zip', 'rar', 'iso']; const FORMAT_ILLUSTRATOR = ['ai', 'esp']; const FORMAT_POWERPOINT = ['ppt', 'pptx']; const FORMAT_AUDIO = ['wav', 'aif', 'mp3', 'aac']; const FORMAT_IMG = ['jpg', 'jpeg', 'gif', 'bmp', 'png', 'svg', 'webp']; const FORMAT_VIDEO = ['m4v', 'avi', 'mpg', 'mp4', 'webm']; import IcAi from './icons/IcAi'; import IcAudio from './icons/IcAudio'; import IcExcel from './icons/IcExcel'; import IcFile from './icons/IcFile'; import IcFolder from './icons/IcFolder'; import IcImg from './icons/IcImg'; import IcPdf from './icons/IcPdf'; import IcPowerPoint from './icons/IcPower_point'; import IcPts from './icons/IcPts'; import IcTxt from './icons/IcTxt'; import IcVideo from './icons/IcVideo'; import IcWord from './icons/IcWord'; import IcZip from './icons/IcZip'; export function fileFormat(fileUrl: string) { let format; const fileByUrl = fileTypeByUrl(fileUrl); switch (fileUrl.includes(fileByUrl)) { case FORMAT_TEXT.includes(fileByUrl): format = 'txt'; break; case FORMAT_ZIP.includes(fileByUrl): format = 'zip'; break; case FORMAT_AUDIO.includes(fileByUrl): format = 'audio'; break; case FORMAT_IMG.includes(fileByUrl): format = 'image'; break; case FORMAT_VIDEO.includes(fileByUrl): format = 'video'; break; case FORMAT_WORD.includes(fileByUrl): format = 'word'; break; case FORMAT_EXCEL.includes(fileByUrl): format = 'excel'; break; case FORMAT_POWERPOINT.includes(fileByUrl): format = 'powerpoint'; break; case FORMAT_PDF.includes(fileByUrl): format = 'pdf'; break; case FORMAT_PHOTOSHOP.includes(fileByUrl): format = 'photoshop'; break; case FORMAT_ILLUSTRATOR.includes(fileByUrl): format = 'illustrator'; break; default: format = fileTypeByUrl(fileUrl); } return format; } export function fileThumb(fileUrl: string) { let thumb; switch (fileFormat(fileUrl)) { case 'folder': thumb = IcFolder; break; case 'txt': thumb = IcTxt; break; case 'zip': thumb = IcZip; break; case 'audio': thumb = IcAudio; break; case 'video': thumb = IcVideo; break; case 'word': thumb = IcWord; break; case 'excel': thumb = IcExcel; break; case 'powerpoint': thumb = IcPowerPoint; break; case 'pdf': thumb = IcPdf; break; case 'photoshop': thumb = IcPts; break; case 'illustrator': thumb = IcAi; break; case 'image': thumb = IcImg; break; default: thumb = IcFile; } return thumb; } // ---------------------------------------------------------------------- export function fileTypeByUrl(fileUrl: string) { return (fileUrl && fileUrl.split('.').pop()) || ''; } // ---------------------------------------------------------------------- export function fileNameByUrl(fileUrl: string) { return fileUrl.split('/').pop(); } // ---------------------------------------------------------------------- export function fileData(file: any) { // From url if (typeof file === 'string') { return { preview: file, name: fileNameByUrl(file), type: fileTypeByUrl(file), size: undefined, path: file, lastModified: undefined, lastModifiedDate: undefined }; } // From file return { name: file.name, size: file.size, path: file.path, type: file.type, preview: file.preview, lastModified: file.lastModified, lastModifiedDate: file.lastModifiedDate }; }