mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-02 20:58:12 +00:00
V4.6.6-1 (#656)
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { postUploadImg, postUploadFiles } from '@/web/common/file/api';
|
||||
import { UploadImgProps } from '@fastgpt/global/common/file/api';
|
||||
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
||||
import {
|
||||
compressBase64ImgAndUpload as compressBase64ImgAndUploadControl,
|
||||
type CompressImgProps
|
||||
} from '@fastgpt/web/common/file/img';
|
||||
|
||||
/**
|
||||
* upload file to mongo gridfs
|
||||
@@ -30,76 +34,40 @@ export const uploadFiles = ({
|
||||
});
|
||||
};
|
||||
|
||||
export const getUploadMdImgController = ({
|
||||
base64Img,
|
||||
metadata
|
||||
}: {
|
||||
base64Img: string;
|
||||
metadata: Record<string, any>;
|
||||
}) =>
|
||||
compressBase64ImgAndUpload({
|
||||
base64Img,
|
||||
maxW: 4000,
|
||||
maxH: 4000,
|
||||
maxSize: 1024 * 1024 * 5,
|
||||
metadata
|
||||
});
|
||||
|
||||
/**
|
||||
* compress image. response base64
|
||||
* @param maxSize The max size of the compressed image
|
||||
*/
|
||||
export const compressBase64ImgAndUpload = ({
|
||||
base64Img,
|
||||
maxW = 1080,
|
||||
maxH = 1080,
|
||||
maxSize = 1024 * 500, // 300kb
|
||||
expiredTime,
|
||||
metadata,
|
||||
shareId
|
||||
}: UploadImgProps & {
|
||||
maxW?: number;
|
||||
maxH?: number;
|
||||
maxSize?: number;
|
||||
}) => {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const fileType =
|
||||
/^data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,/.exec(base64Img)?.[1] || 'image/jpeg';
|
||||
|
||||
const img = new Image();
|
||||
img.src = base64Img;
|
||||
img.onload = async () => {
|
||||
let width = img.width;
|
||||
let height = img.height;
|
||||
|
||||
if (width > height) {
|
||||
if (width > maxW) {
|
||||
height *= maxW / width;
|
||||
width = maxW;
|
||||
}
|
||||
} else {
|
||||
if (height > maxH) {
|
||||
width *= maxH / height;
|
||||
height = maxH;
|
||||
}
|
||||
}
|
||||
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
if (!ctx) {
|
||||
return reject('压缩图片异常');
|
||||
}
|
||||
|
||||
ctx.drawImage(img, 0, 0, width, height);
|
||||
const compressedDataUrl = canvas.toDataURL(fileType, 1);
|
||||
// 移除 canvas 元素
|
||||
canvas.remove();
|
||||
|
||||
if (compressedDataUrl.length > maxSize) {
|
||||
return reject('图片太大了');
|
||||
}
|
||||
|
||||
try {
|
||||
const src = await postUploadImg({
|
||||
shareId,
|
||||
base64Img: compressedDataUrl,
|
||||
expiredTime,
|
||||
metadata
|
||||
});
|
||||
resolve(src);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
img.onerror = reject;
|
||||
shareId,
|
||||
...props
|
||||
}: UploadImgProps & CompressImgProps) => {
|
||||
return compressBase64ImgAndUploadControl({
|
||||
...props,
|
||||
uploadController: (base64Img) =>
|
||||
postUploadImg({
|
||||
shareId,
|
||||
base64Img,
|
||||
expiredTime,
|
||||
metadata
|
||||
})
|
||||
});
|
||||
};
|
||||
export const compressImgFileAndUpload = async ({
|
||||
|
Reference in New Issue
Block a user