Add img extension (#2172)

* perf: mongo log

* perf: img read

* doc
This commit is contained in:
Archer
2024-07-26 12:46:07 +08:00
committed by GitHub
parent 2d1e53c3b5
commit 8d25a1d3ec
11 changed files with 35 additions and 31 deletions

View File

@@ -163,6 +163,7 @@ export const readFileContentFromMongo = async ({
const encoding = file?.metadata?.encoding || detectFileEncoding(fileBuffers);
// Get raw text
const { rawText } = await readRawContentByFileBuffer({
extension,
isQAImport,

View File

@@ -2,9 +2,10 @@ import { UploadImgProps } from '@fastgpt/global/common/file/api';
import { imageBaseUrl } from '@fastgpt/global/common/file/image/constants';
import { MongoImage } from './schema';
import { ClientSession } from '../../../common/mongo';
import { guessBase64ImageType } from '../utils';
export function getMongoImgUrl(id: string) {
return `${imageBaseUrl}${id}`;
export function getMongoImgUrl(id: string, extension: string) {
return `${imageBaseUrl}${id}.${extension}`;
}
export const maxImgSize = 1024 * 1024 * 12;
@@ -23,9 +24,10 @@ export async function uploadMongoImg({
return Promise.reject('Image too large');
}
const [base64Mime, base64Data] = base64Img.split(',')
const mime = `image/${base64Mime.match(base64MimeRegex)?.[1] ?? 'jpeg'}`
const [base64Mime, base64Data] = base64Img.split(',');
const mime = `image/${base64Mime.match(base64MimeRegex)?.[1] ?? 'image/jpeg'}`;
const binary = Buffer.from(base64Data, 'base64');
const extension = mime.split('/')[1];
const { _id } = await MongoImage.create({
type,
@@ -36,15 +38,20 @@ export async function uploadMongoImg({
shareId
});
return getMongoImgUrl(String(_id));
return getMongoImgUrl(String(_id), extension);
}
export async function readMongoImg({ id }: { id: string }) {
const data = await MongoImage.findById(id);
const formatId = id.replace(/\.[^/.]+$/, '');
const data = await MongoImage.findById(formatId);
if (!data) {
return Promise.reject('Image not found');
}
return data;
return {
binary: data.binary,
mime: data.metadata?.mime ?? guessBase64ImageType(data.binary.toString('base64'))
};
}
export async function delImgByRelatedId({

View File

@@ -8,7 +8,8 @@ import fs from 'fs';
import { detectFileEncoding } from '@fastgpt/global/common/file/tools';
import type { ReadFileResponse } from '../../../worker/readFile/type';
export const initMarkdownText = ({
// match md img text and upload to db
export const matchMdImgTextAndUpload = ({
teamId,
md,
metadata
@@ -79,7 +80,7 @@ export const readRawContentByFileBuffer = async ({
// markdown data format
if (['md', 'html', 'docx'].includes(extension)) {
rawText = await initMarkdownText({
rawText = await matchMdImgTextAndUpload({
teamId: teamId,
md: rawText,
metadata: metadata

View File

@@ -46,12 +46,11 @@ export async function connectMongo(): Promise<Mongoose> {
});
console.log('mongo connected');
return connectionMongo;
} catch (error) {
addLog.error('mongo connect error', error);
await connectionMongo.disconnect();
await delay(1000);
connectMongo();
return connectMongo();
}
return connectionMongo;
}

View File

@@ -1,5 +1,5 @@
import { connectionMongo, getMongoModel, type Model } from '../../common/mongo';
const { Schema, model, models } = connectionMongo;
import { connectionMongo, getMongoModel } from '../../common/mongo';
const { Schema } = connectionMongo;
import { ChatSchema as ChatType } from '@fastgpt/global/core/chat/type.d';
import { ChatSourceMap } from '@fastgpt/global/core/chat/constants';
import {