2022-06-22 14:57:09 +08:00
|
|
|
import type { InternalUploadFile, RcFile, UploadFile } from './interface';
|
2017-11-20 16:43:25 +08:00
|
|
|
|
2021-04-28 22:52:31 +08:00
|
|
|
export function file2Obj(file: RcFile): InternalUploadFile {
|
2021-03-13 23:46:32 +08:00
|
|
|
return {
|
|
|
|
...file,
|
2018-05-02 13:56:54 +08:00
|
|
|
lastModified: file.lastModified,
|
|
|
|
lastModifiedDate: file.lastModifiedDate,
|
|
|
|
name: file.name,
|
|
|
|
size: file.size,
|
|
|
|
type: file.type,
|
|
|
|
uid: file.uid,
|
2017-03-17 11:47:05 +08:00
|
|
|
percent: 0,
|
2018-04-19 11:54:28 +08:00
|
|
|
originFileObj: file,
|
2021-02-26 15:18:37 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
/** Upload fileList. Replace file if exist or just push into it. */
|
2023-02-07 14:09:45 +08:00
|
|
|
export function updateFileList(file: UploadFile, fileList: (UploadFile | Readonly<UploadFile>)[]) {
|
2021-03-04 16:59:06 +08:00
|
|
|
const nextFileList = [...fileList];
|
2023-02-07 14:09:45 +08:00
|
|
|
const fileIndex = nextFileList.findIndex(({ uid }) => uid === file.uid);
|
2021-03-04 16:59:06 +08:00
|
|
|
if (fileIndex === -1) {
|
|
|
|
nextFileList.push(file);
|
|
|
|
} else {
|
|
|
|
nextFileList[fileIndex] = file;
|
|
|
|
}
|
|
|
|
return nextFileList;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:09:45 +08:00
|
|
|
export function getFileItem(file: RcFile, fileList: (UploadFile | Readonly<UploadFile>)[]) {
|
2017-08-20 15:39:02 +08:00
|
|
|
const matchKey = file.uid !== undefined ? 'uid' : 'name';
|
2022-11-19 13:47:33 +08:00
|
|
|
return fileList.filter((item) => item[matchKey] === file[matchKey])[0];
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:09:45 +08:00
|
|
|
export function removeFileItem(file: UploadFile, fileList: (UploadFile | Readonly<UploadFile>)[]) {
|
2017-08-20 15:39:02 +08:00
|
|
|
const matchKey = file.uid !== undefined ? 'uid' : 'name';
|
2022-11-19 13:47:33 +08:00
|
|
|
const removed = fileList.filter((item) => item[matchKey] !== file[matchKey]);
|
2017-03-17 11:47:05 +08:00
|
|
|
if (removed.length === fileList.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
// ==================== Default Image Preview ====================
|
2019-09-29 12:33:24 +08:00
|
|
|
const extname = (url: string = '') => {
|
2019-05-06 12:04:39 +08:00
|
|
|
const temp = url.split('/');
|
|
|
|
const filename = temp[temp.length - 1];
|
|
|
|
const filenameWithoutSuffix = filename.split(/#|\?/)[0];
|
|
|
|
return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
|
|
|
|
};
|
2019-09-29 12:33:24 +08:00
|
|
|
|
2020-02-19 20:12:44 +08:00
|
|
|
const isImageFileType = (type: string): boolean => type.indexOf('image/') === 0;
|
2019-09-29 12:33:24 +08:00
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
export const isImageUrl = (file: UploadFile): boolean => {
|
2020-07-06 21:47:01 +08:00
|
|
|
if (file.type && !file.thumbUrl) {
|
2020-02-19 20:12:44 +08:00
|
|
|
return isImageFileType(file.type);
|
2019-05-06 12:04:39 +08:00
|
|
|
}
|
2021-04-20 09:58:02 +08:00
|
|
|
const url: string = (file.thumbUrl || file.url || '') as string;
|
2019-05-06 12:04:39 +08:00
|
|
|
const extension = extname(url);
|
2019-12-22 17:23:08 +08:00
|
|
|
if (
|
|
|
|
/^data:image\//.test(url) ||
|
2022-09-23 10:44:55 +08:00
|
|
|
/(webp|svg|png|gif|jpg|jpeg|jfif|bmp|dpg|ico|heic|heif)$/i.test(extension)
|
2019-12-22 17:23:08 +08:00
|
|
|
) {
|
2019-05-06 12:04:39 +08:00
|
|
|
return true;
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
if (/^data:/.test(url)) {
|
2019-05-06 12:04:39 +08:00
|
|
|
// other file types of base64
|
|
|
|
return false;
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
if (extension) {
|
2019-05-06 12:04:39 +08:00
|
|
|
// other file types which have extension
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const MEASURE_SIZE = 200;
|
2023-02-07 14:09:45 +08:00
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
export function previewImage(file: File | Blob): Promise<string> {
|
2022-11-19 13:47:33 +08:00
|
|
|
return new Promise((resolve) => {
|
2020-02-19 20:12:44 +08:00
|
|
|
if (!file.type || !isImageFileType(file.type)) {
|
2019-05-06 12:04:39 +08:00
|
|
|
resolve('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
canvas.width = MEASURE_SIZE;
|
|
|
|
canvas.height = MEASURE_SIZE;
|
|
|
|
canvas.style.cssText = `position: fixed; left: 0; top: 0; width: ${MEASURE_SIZE}px; height: ${MEASURE_SIZE}px; z-index: 9999; display: none;`;
|
|
|
|
document.body.appendChild(canvas);
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
const img = new Image();
|
2019-08-05 18:38:10 +08:00
|
|
|
img.onload = () => {
|
2019-05-06 12:04:39 +08:00
|
|
|
const { width, height } = img;
|
|
|
|
|
|
|
|
let drawWidth = MEASURE_SIZE;
|
|
|
|
let drawHeight = MEASURE_SIZE;
|
|
|
|
let offsetX = 0;
|
|
|
|
let offsetY = 0;
|
|
|
|
|
2020-10-23 11:18:57 +08:00
|
|
|
if (width > height) {
|
2019-05-06 12:04:39 +08:00
|
|
|
drawHeight = height * (MEASURE_SIZE / width);
|
|
|
|
offsetY = -(drawHeight - drawWidth) / 2;
|
|
|
|
} else {
|
|
|
|
drawWidth = width * (MEASURE_SIZE / height);
|
|
|
|
offsetX = -(drawWidth - drawHeight) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx!.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
|
|
|
|
const dataURL = canvas.toDataURL();
|
|
|
|
document.body.removeChild(canvas);
|
2023-08-09 15:19:25 +08:00
|
|
|
window.URL.revokeObjectURL(img.src);
|
2019-05-06 12:04:39 +08:00
|
|
|
resolve(dataURL);
|
|
|
|
};
|
2022-07-25 13:09:39 +08:00
|
|
|
img.crossOrigin = 'anonymous';
|
|
|
|
if (file.type.startsWith('image/svg+xml')) {
|
2022-07-14 11:25:57 +08:00
|
|
|
const reader = new FileReader();
|
2023-08-09 15:19:25 +08:00
|
|
|
reader.onload = () => {
|
2022-07-14 11:25:57 +08:00
|
|
|
if (reader.result) img.src = reader.result as string;
|
2023-08-09 15:19:25 +08:00
|
|
|
};
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
} else if (file.type.startsWith('image/gif')) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => {
|
|
|
|
if (reader.result) resolve(reader.result as string);
|
|
|
|
};
|
2022-07-14 11:25:57 +08:00
|
|
|
reader.readAsDataURL(file);
|
|
|
|
} else {
|
|
|
|
img.src = window.URL.createObjectURL(file);
|
|
|
|
}
|
2019-05-06 12:04:39 +08:00
|
|
|
});
|
|
|
|
}
|