Skip to content

Commit

Permalink
Add img extension (#2172)
Browse files Browse the repository at this point in the history
* perf: mongo log

* perf: img read

* doc
  • Loading branch information
c121914yu authored Jul 26, 2024
1 parent 2d1e53c commit 8d25a1d
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"i18n-ally.pathMatcher": "{locale}/{namespaces}.json",
"i18n-ally.extract.targetPickingStrategy": "most-similar-by-key",
"[typescript]": {
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint"
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
17 changes: 8 additions & 9 deletions docSite/content/zh-cn/docs/development/upgrading/488.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ weight: 816
从任意终端,发起 1 个 HTTP 请求。其中 {{rootkey}} 替换成环境变量里的 `rootkey`;{{host}} 替换成**FastGPT 域名**

```bash
curl --location --request POST 'https://{{host}}/api/admin/initv48 8' \
curl --location --request POST 'https://{{host}}/api/admin/initv488' \
--header 'rootkey: {{rootkey}}' \
--header 'Content-Type: application/json'
```
Expand All @@ -40,11 +40,10 @@ curl --location --request POST 'https://{{host}}/api/admin/initv48 8' \
6. 优化 - 移动端快速切换应用交互。
7. 优化 - 节点图标。
8. 优化 - 对话框引用增加额外复制案件,便于复制。增加引用内容折叠。
9. 优化 - 对话框底部增加复制,简便复制交互,无需滚动到消息开头。
10. 优化 - OpenAI sdk 升级,并自定义了 whisper 模型接口(未仔细查看 sdk 实现,但 sdk 中 whisper 接口,似乎无法适配一般 fastapi 接口)
11. 修复 - Permission 表声明问题。
12. 修复 - 并行执行节点,运行时间未正确记录。
13. 修复 - 运行详情未正确展示嵌套节点信息。
14. 修复 - 简易模式,首次进入,无法正确获取知识库配置。
15. 修复 - Log debug level 配置无效。
16. 修复 - 插件独立运行时,会将插件输入的值进行变量替换,可能导致后续节点变量异常。
9. 优化 - OpenAI sdk 升级,并自定义了 whisper 模型接口(未仔细查看 sdk 实现,但 sdk 中 whisper 接口,似乎无法适配一般 fastapi 接口)
10. 修复 - Permission 表声明问题。
11. 修复 - 并行执行节点,运行时间未正确记录。
12. 修复 - 运行详情未正确展示嵌套节点信息。
13. 修复 - 简易模式,首次进入,无法正确获取知识库配置。
14. 修复 - Log debug level 配置无效。
15. 修复 - 插件独立运行时,会将插件输入的值进行变量替换,可能导致后续节点变量异常。
1 change: 1 addition & 0 deletions packages/service/common/file/gridfs/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export const readFileContentFromMongo = async ({

const encoding = file?.metadata?.encoding || detectFileEncoding(fileBuffers);

// Get raw text
const { rawText } = await readRawContentByFileBuffer({
extension,
isQAImport,
Expand Down
21 changes: 14 additions & 7 deletions packages/service/common/file/image/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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({
Expand Down
5 changes: 3 additions & 2 deletions packages/service/common/file/read/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions packages/service/common/mongo/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions packages/service/core/chat/chatSchema.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/web/components/common/Icon/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const iconPaths = {
'common/leftArrowLight': () => import('./icons/common/leftArrowLight.svg'),
'common/linkBlue': () => import('./icons/common/linkBlue.svg'),
'common/loading': () => import('./icons/common/loading.svg'),
'common/logLight': () => import('./icons/common/logLight.svg'),
'common/navbar/pluginFill': () => import('./icons/common/navbar/pluginFill.svg'),
'common/navbar/pluginLight': () => import('./icons/common/navbar/pluginLight.svg'),
'common/openai': () => import('./icons/common/openai.svg'),
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion projects/app/src/pages/api/common/file/previewContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Read db file content and response 3000 words
*/
import type { NextApiResponse } from 'next';
import { jsonRes } from '@fastgpt/service/common/response';
import { authFile } from '@fastgpt/service/support/permission/auth/file';
import { NextAPI } from '@/service/middleware/entry';
import { DatasetSourceReadTypeEnum } from '@fastgpt/global/core/dataset/constants';
Expand Down
8 changes: 2 additions & 6 deletions projects/app/src/pages/api/system/img/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@fastgpt/service/common/response';
import { connectToDatabase } from '@/service/mongo';
import { readMongoImg } from '@fastgpt/service/common/file/image/controller';
import { guessBase64ImageType } from '@fastgpt/service/common/file/utils';

// get the models available to the system
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await connectToDatabase();
const { id } = req.query as { id: string };

const { binary, metadata } = await readMongoImg({ id });
const { binary, mime } = await readMongoImg({ id });

res.setHeader(
'Content-Type',
metadata?.mime ?? guessBase64ImageType(binary.toString('base64'))
);
res.setHeader('Content-Type', mime);
res.send(binary);
} catch (error) {
jsonRes(res, {
Expand Down

0 comments on commit 8d25a1d

Please sign in to comment.