4.6.4-alpha (#582)
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { GET, POST, PUT, DELETE } from '@/web/common/api/request';
|
||||
import type { UploadImgProps } from '@fastgpt/global/common/file/api.d';
|
||||
import { AxiosProgressEvent } from 'axios';
|
||||
|
||||
export const postUploadImg = (base64Img: string, expiredTime?: Date) =>
|
||||
POST<string>('/common/file/uploadImage', { base64Img, expiredTime });
|
||||
export const postUploadImg = (e: UploadImgProps) => POST<string>('/common/file/uploadImage', e);
|
||||
|
||||
export const postUploadFiles = (
|
||||
data: FormData,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { postUploadImg, postUploadFiles } from '@/web/common/file/api';
|
||||
import { UploadImgProps } from '@fastgpt/global/common/file/api';
|
||||
import { BucketNameEnum } from '@fastgpt/global/common/file/constants';
|
||||
|
||||
/**
|
||||
@@ -34,23 +35,24 @@ export const uploadFiles = ({
|
||||
* @param maxSize The max size of the compressed image
|
||||
*/
|
||||
export const compressBase64ImgAndUpload = ({
|
||||
base64,
|
||||
base64Img,
|
||||
maxW = 1080,
|
||||
maxH = 1080,
|
||||
maxSize = 1024 * 500, // 300kb
|
||||
expiredTime
|
||||
}: {
|
||||
base64: string;
|
||||
expiredTime,
|
||||
metadata,
|
||||
shareId
|
||||
}: UploadImgProps & {
|
||||
maxW?: number;
|
||||
maxH?: number;
|
||||
maxSize?: number;
|
||||
expiredTime?: Date;
|
||||
}) => {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const fileType = /^data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,/.exec(base64)?.[1] || 'image/jpeg';
|
||||
const fileType =
|
||||
/^data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,/.exec(base64Img)?.[1] || 'image/jpeg';
|
||||
|
||||
const img = new Image();
|
||||
img.src = base64;
|
||||
img.src = base64Img;
|
||||
img.onload = async () => {
|
||||
let width = img.width;
|
||||
let height = img.height;
|
||||
@@ -86,7 +88,12 @@ export const compressBase64ImgAndUpload = ({
|
||||
}
|
||||
|
||||
try {
|
||||
const src = await postUploadImg(compressedDataUrl, expiredTime);
|
||||
const src = await postUploadImg({
|
||||
shareId,
|
||||
base64Img: compressedDataUrl,
|
||||
expiredTime,
|
||||
metadata
|
||||
});
|
||||
resolve(src);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
@@ -100,18 +107,20 @@ export const compressImgFileAndUpload = async ({
|
||||
maxW,
|
||||
maxH,
|
||||
maxSize,
|
||||
expiredTime
|
||||
expiredTime,
|
||||
shareId
|
||||
}: {
|
||||
file: File;
|
||||
maxW?: number;
|
||||
maxH?: number;
|
||||
maxSize?: number;
|
||||
expiredTime?: Date;
|
||||
shareId?: string;
|
||||
}) => {
|
||||
const reader = new FileReader();
|
||||
reader.readAsDataURL(file);
|
||||
|
||||
const base64 = await new Promise<string>((resolve, reject) => {
|
||||
const base64Img = await new Promise<string>((resolve, reject) => {
|
||||
reader.onload = async () => {
|
||||
resolve(reader.result as string);
|
||||
};
|
||||
@@ -122,10 +131,11 @@ export const compressImgFileAndUpload = async ({
|
||||
});
|
||||
|
||||
return compressBase64ImgAndUpload({
|
||||
base64,
|
||||
base64Img,
|
||||
maxW,
|
||||
maxH,
|
||||
maxSize,
|
||||
expiredTime
|
||||
expiredTime,
|
||||
shareId
|
||||
});
|
||||
};
|
||||
|
||||
@@ -107,7 +107,7 @@ export const readPdfContent = (file: File) =>
|
||||
/**
|
||||
* read docx to markdown
|
||||
*/
|
||||
export const readDocContent = (file: File) =>
|
||||
export const readDocContent = (file: File, metadata: Record<string, any>) =>
|
||||
new Promise<string>((resolve, reject) => {
|
||||
try {
|
||||
const reader = new FileReader();
|
||||
@@ -120,7 +120,7 @@ export const readDocContent = (file: File) =>
|
||||
arrayBuffer: target.result as ArrayBuffer
|
||||
});
|
||||
|
||||
const rawText = await formatMarkdown(res?.value);
|
||||
const rawText = await formatMarkdown(res?.value, metadata);
|
||||
|
||||
resolve(rawText);
|
||||
} catch (error) {
|
||||
@@ -173,24 +173,25 @@ export const readCsvContent = async (file: File) => {
|
||||
* 1. upload base64
|
||||
* 2. replace \
|
||||
*/
|
||||
export const formatMarkdown = async (rawText: string = '') => {
|
||||
export const formatMarkdown = async (rawText: string = '', metadata: Record<string, any>) => {
|
||||
// match base64, upload and replace it
|
||||
const base64Regex = /data:image\/.*;base64,([^\)]+)/g;
|
||||
const base64Arr = rawText.match(base64Regex) || [];
|
||||
// upload base64 and replace it
|
||||
await Promise.all(
|
||||
base64Arr.map(async (base64) => {
|
||||
base64Arr.map(async (base64Img) => {
|
||||
try {
|
||||
const str = await compressBase64ImgAndUpload({
|
||||
base64,
|
||||
base64Img,
|
||||
maxW: 4329,
|
||||
maxH: 4329,
|
||||
maxSize: 1024 * 1024 * 5
|
||||
maxSize: 1024 * 1024 * 5,
|
||||
metadata
|
||||
});
|
||||
|
||||
rawText = rawText.replace(base64, str);
|
||||
rawText = rawText.replace(base64Img, str);
|
||||
} catch (error) {
|
||||
rawText = rawText.replace(base64, '');
|
||||
rawText = rawText.replace(base64Img, '');
|
||||
rawText = rawText.replace(/!\[.*\]\(\)/g, '');
|
||||
}
|
||||
})
|
||||
|
||||
@@ -4,7 +4,7 @@ export const useToast = (props?: UseToastOptions) => {
|
||||
const toast = uToast({
|
||||
position: 'top',
|
||||
duration: 2000,
|
||||
...props
|
||||
...(props && props)
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export enum EventNameEnum {
|
||||
guideClick = 'guideClick',
|
||||
sendQuestion = 'sendQuestion',
|
||||
editQuestion = 'editQuestion',
|
||||
updaterNode = 'updaterNode'
|
||||
}
|
||||
type EventNameType = `${EventNameEnum}`;
|
||||
|
||||
Reference in New Issue
Block a user