| | import { FileMimeType } from '@/constants/common'; |
| | import fileManagerService from '@/services/file-manager-service'; |
| | import { UploadFile } from 'antd'; |
| |
|
| | export const transformFile2Base64 = (val: any): Promise<any> => { |
| | return new Promise((resolve, reject) => { |
| | const reader = new FileReader(); |
| | reader.readAsDataURL(val); |
| | reader.onload = (): void => { |
| | |
| | const img = new Image(); |
| | img.src = reader.result as string; |
| |
|
| | img.onload = () => { |
| | |
| | const canvas = document.createElement('canvas'); |
| | const ctx = canvas.getContext('2d'); |
| |
|
| | |
| | let width = img.width; |
| | let height = img.height; |
| | const maxSize = 100; |
| |
|
| | if (width > height && width > maxSize) { |
| | height = (height * maxSize) / width; |
| | width = maxSize; |
| | } else if (height > maxSize) { |
| | width = (width * maxSize) / height; |
| | height = maxSize; |
| | } |
| |
|
| | |
| | canvas.width = width; |
| | canvas.height = height; |
| |
|
| | |
| | ctx?.drawImage(img, 0, 0, width, height); |
| |
|
| | |
| | const compressedBase64 = canvas.toDataURL('image/png'); |
| | resolve(compressedBase64); |
| | }; |
| |
|
| | img.onerror = reject; |
| | }; |
| | reader.onerror = reject; |
| | }); |
| | }; |
| |
|
| | export const transformBase64ToFile = ( |
| | dataUrl: string, |
| | filename: string = 'file', |
| | ) => { |
| | let arr = dataUrl.split(','), |
| | bstr = atob(arr[1]), |
| | n = bstr.length, |
| | u8arr = new Uint8Array(n); |
| |
|
| | const mime = arr[0].match(/:(.*?);/); |
| | const mimeType = mime ? mime[1] : 'image/png'; |
| |
|
| | while (n--) { |
| | u8arr[n] = bstr.charCodeAt(n); |
| | } |
| | return new File([u8arr], filename, { type: mimeType }); |
| | }; |
| |
|
| | export const normFile = (e: any) => { |
| | if (Array.isArray(e)) { |
| | return e; |
| | } |
| | return e?.fileList; |
| | }; |
| |
|
| | export const getUploadFileListFromBase64 = (avatar: string) => { |
| | let fileList: UploadFile[] = []; |
| |
|
| | if (avatar) { |
| | fileList = [{ uid: '1', name: 'file', thumbUrl: avatar, status: 'done' }]; |
| | } |
| |
|
| | return fileList; |
| | }; |
| |
|
| | export const getBase64FromUploadFileList = async (fileList?: UploadFile[]) => { |
| | if (Array.isArray(fileList) && fileList.length > 0) { |
| | const file = fileList[0]; |
| | const originFileObj = file.originFileObj; |
| | if (originFileObj) { |
| | const base64 = await transformFile2Base64(originFileObj); |
| | return base64; |
| | } else { |
| | return file.thumbUrl; |
| | } |
| | |
| | } |
| |
|
| | return ''; |
| | }; |
| |
|
| | export const downloadFileFromBlob = (blob: Blob, name?: string) => { |
| | const url = window.URL.createObjectURL(blob); |
| | const a = document.createElement('a'); |
| | a.href = url; |
| | if (name) { |
| | a.download = name; |
| | } |
| | a.click(); |
| | window.URL.revokeObjectURL(url); |
| | }; |
| |
|
| | export const downloadDocument = async ({ |
| | id, |
| | filename, |
| | }: { |
| | id: string; |
| | filename?: string; |
| | }) => { |
| | const response = await fileManagerService.getDocumentFile({}, id); |
| | const blob = new Blob([response.data], { type: response.data.type }); |
| | downloadFileFromBlob(blob, filename); |
| | }; |
| |
|
| | const Units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
| |
|
| | export const formatBytes = (x: string | number) => { |
| | let l = 0, |
| | n = (typeof x === 'string' ? parseInt(x, 10) : x) || 0; |
| |
|
| | while (n >= 1024 && ++l) { |
| | n = n / 1024; |
| | } |
| |
|
| | return n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + Units[l]; |
| | }; |
| |
|
| | export const downloadJsonFile = async ( |
| | data: Record<string, any>, |
| | fileName: string, |
| | ) => { |
| | const blob = new Blob([JSON.stringify(data)], { type: FileMimeType.Json }); |
| | downloadFileFromBlob(blob, fileName); |
| | }; |
| |
|