Skip to content

Commit d9a4a5f

Browse files
authored
fix: mongodb file oversize (#4594)
1 parent a18d34e commit d9a4a5f

File tree

4 files changed

+82
-52
lines changed

4 files changed

+82
-52
lines changed

docSite/content/zh-cn/docs/development/upgrading/497.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ weight: 793
1616

1717
## 🐛 修复
1818

19+
1. 文件上传分块大小限制,避免超出 MongoDB 限制。
1920

packages/service/common/file/gridfs/controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ export async function uploadFile({
6565
const bucket = getGridBucket(bucketName);
6666

6767
const fileSize = stats.size;
68+
// 单块大小:尽可能大,但不超过 14MB,不小于512KB
6869
const chunkSizeBytes = (() => {
69-
// 计算理想块大小:文件大小 ÷ 目标块数(10)
70-
const idealChunkSize = Math.ceil(fileSize / 10);
70+
// 计算理想块大小:文件大小 ÷ 目标块数(10)。 并且每个块需要小于 14MB
71+
const idealChunkSize = Math.min(Math.ceil(fileSize / 10), 14 * 1024 * 1024);
7172

7273
// 确保块大小至少为512KB
7374
const minChunkSize = 512 * 1024; // 512KB

projects/app/src/pageComponents/dataset/detail/Import/components/FileSelector.tsx

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ImportSourceItemType } from '@/web/core/dataset/type';
1616
import { useI18n } from '@/web/context/I18n';
1717
import { useContextSelector } from 'use-context-selector';
1818
import { DatasetPageContext } from '@/web/core/dataset/context/datasetPageContext';
19+
import { getErrText } from '@fastgpt/global/common/error/utils';
1920

2021
export type SelectFileItemType = {
2122
fileId: string;
@@ -71,39 +72,53 @@ const FileSelector = ({
7172
{
7273
await Promise.all(
7374
files.map(async ({ fileId, file }) => {
74-
const { fileId: uploadFileId } = await uploadFile2DB({
75-
file,
76-
bucketName: BucketNameEnum.dataset,
77-
data: {
78-
datasetId
79-
},
80-
percentListen: (e) => {
81-
setSelectFiles((state) =>
82-
state.map((item) =>
83-
item.id === fileId
84-
? {
85-
...item,
86-
uploadedFileRate: item.uploadedFileRate
87-
? Math.max(e, item.uploadedFileRate)
88-
: e
89-
}
90-
: item
91-
)
92-
);
93-
}
94-
});
95-
setSelectFiles((state) =>
96-
state.map((item) =>
97-
item.id === fileId
98-
? {
99-
...item,
100-
dbFileId: uploadFileId,
101-
isUploading: false,
102-
uploadedFileRate: 100
103-
}
104-
: item
105-
)
106-
);
75+
try {
76+
const { fileId: uploadFileId } = await uploadFile2DB({
77+
file,
78+
bucketName: BucketNameEnum.dataset,
79+
data: {
80+
datasetId
81+
},
82+
percentListen: (e) => {
83+
setSelectFiles((state) =>
84+
state.map((item) =>
85+
item.id === fileId
86+
? {
87+
...item,
88+
uploadedFileRate: item.uploadedFileRate
89+
? Math.max(e, item.uploadedFileRate)
90+
: e
91+
}
92+
: item
93+
)
94+
);
95+
}
96+
});
97+
setSelectFiles((state) =>
98+
state.map((item) =>
99+
item.id === fileId
100+
? {
101+
...item,
102+
dbFileId: uploadFileId,
103+
isUploading: false,
104+
uploadedFileRate: 100
105+
}
106+
: item
107+
)
108+
);
109+
} catch (error) {
110+
setSelectFiles((state) =>
111+
state.map((item) =>
112+
item.id === fileId
113+
? {
114+
...item,
115+
isUploading: false,
116+
errorMsg: getErrText(error)
117+
}
118+
: item
119+
)
120+
);
121+
}
107122
})
108123
);
109124
}

projects/app/src/pageComponents/dataset/detail/Import/components/RenderFiles.tsx

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import {
99
Td,
1010
Tbody,
1111
Progress,
12-
IconButton
12+
IconButton,
13+
Box
1314
} from '@chakra-ui/react';
1415
import { ImportSourceItemType } from '@/web/core/dataset/type.d';
1516
import MyIcon from '@fastgpt/web/components/common/Icon';
1617
import { useTranslation } from 'next-i18next';
1718
import { useI18n } from '@/web/context/I18n';
19+
import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
20+
import MyTag from '@fastgpt/web/components/common/Tag/index';
21+
import { QuestionOutlineIcon } from '@chakra-ui/icons';
1822

1923
export const RenderUploadFiles = ({
2024
files,
@@ -56,22 +60,31 @@ export const RenderUploadFiles = ({
5660
</Flex>
5761
</Td>
5862
<Td>
59-
<Flex alignItems={'center'} fontSize={'xs'}>
60-
<Progress
61-
value={item.uploadedFileRate}
62-
h={'6px'}
63-
w={'100%'}
64-
maxW={'210px'}
65-
size="sm"
66-
borderRadius={'20px'}
67-
colorScheme={(item.uploadedFileRate || 0) >= 100 ? 'green' : 'blue'}
68-
bg="myGray.200"
69-
hasStripe
70-
isAnimated
71-
mr={2}
72-
/>
73-
{`${item.uploadedFileRate}%`}
74-
</Flex>
63+
{item.errorMsg ? (
64+
<MyTooltip label={item.errorMsg}>
65+
<MyTag colorSchema={'red'}>
66+
<Box mr={1}>{t('common:common.Error')}</Box>
67+
<MyIcon name={'help'} w={'0.9rem'} color={'red.500'} />
68+
</MyTag>
69+
</MyTooltip>
70+
) : (
71+
<Flex alignItems={'center'} fontSize={'xs'}>
72+
<Progress
73+
value={item.uploadedFileRate}
74+
h={'6px'}
75+
w={'100%'}
76+
maxW={'210px'}
77+
size="sm"
78+
borderRadius={'20px'}
79+
colorScheme={(item.uploadedFileRate || 0) >= 100 ? 'green' : 'blue'}
80+
bg="myGray.200"
81+
hasStripe
82+
isAnimated
83+
mr={2}
84+
/>
85+
{`${item.uploadedFileRate}%`}
86+
</Flex>
87+
)}
7588
</Td>
7689
<Td>{item.sourceSize}</Td>
7790
<Td>

0 commit comments

Comments
 (0)