mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
V4.6.6-1 (#656)
This commit is contained in:
66
packages/web/common/file/img.ts
Normal file
66
packages/web/common/file/img.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
export type CompressImgProps = {
|
||||
maxW?: number;
|
||||
maxH?: number;
|
||||
maxSize?: number;
|
||||
};
|
||||
|
||||
export const compressBase64ImgAndUpload = ({
|
||||
base64Img,
|
||||
maxW = 1080,
|
||||
maxH = 1080,
|
||||
maxSize = 1024 * 500, // 300kb
|
||||
uploadController
|
||||
}: CompressImgProps & {
|
||||
base64Img: string;
|
||||
uploadController: (base64: string) => Promise<string>;
|
||||
}) => {
|
||||
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 uploadController(compressedDataUrl);
|
||||
resolve(src);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
img.onerror = reject;
|
||||
});
|
||||
};
|
53
packages/web/common/file/read.ts
Normal file
53
packages/web/common/file/read.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { uploadMarkdownBase64 } from '@fastgpt/global/common/string/markdown';
|
||||
import { htmlStr2Md } from '../string/markdown';
|
||||
/**
|
||||
* read file raw text
|
||||
*/
|
||||
export const readFileRawText = (file: File) => {
|
||||
return new Promise((resolve: (_: string) => void, reject) => {
|
||||
try {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
resolve(reader.result as string);
|
||||
};
|
||||
reader.onerror = (err) => {
|
||||
console.log('error txt read:', err);
|
||||
reject('Read file error');
|
||||
};
|
||||
reader.readAsText(file);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const readMdFile = async ({
|
||||
file,
|
||||
uploadImgController
|
||||
}: {
|
||||
file: File;
|
||||
uploadImgController: (base64: string) => Promise<string>;
|
||||
}) => {
|
||||
const md = await readFileRawText(file);
|
||||
const rawText = await uploadMarkdownBase64({
|
||||
rawText: md,
|
||||
uploadImgController
|
||||
});
|
||||
return rawText;
|
||||
};
|
||||
|
||||
export const readHtmlFile = async ({
|
||||
file,
|
||||
uploadImgController
|
||||
}: {
|
||||
file: File;
|
||||
uploadImgController: (base64: string) => Promise<string>;
|
||||
}) => {
|
||||
const md = htmlStr2Md(await readFileRawText(file));
|
||||
const rawText = await uploadMarkdownBase64({
|
||||
rawText: md,
|
||||
uploadImgController
|
||||
});
|
||||
|
||||
return rawText;
|
||||
};
|
Reference in New Issue
Block a user