Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
f1253a1f
authored
Nov 28, 2025
by
Roy
Committed by
GitHub
Nov 28, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: get file buffer and upload file to S3 (#6004)
parent
ac1a07d9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
38 deletions
+58
-38
packages/service/common/file/multer.ts
+38
-1
projects/app/src/pages/api/core/dataset/collection/create/localFile.ts
+20
-37
No files found.
packages/service/common/file/multer.ts
View file @
f1253a1f
...
...
@@ -16,7 +16,7 @@ export type FileType = {
size
:
number
;
};
/*
/*
maxSize: File max size (MB)
*/
export
const
getUploadModel
=
({
maxSize
=
500
}:
{
maxSize
?:
number
})
=>
{
...
...
@@ -146,6 +146,43 @@ export const getUploadModel = ({ maxSize = 500 }: { maxSize?: number }) => {
});
});
}
uploaderMemory
=
multer
({
storage
:
multer
.
memoryStorage
(),
limits
:
{
fileSize
:
maxSize
}
}).
single
(
'file'
);
async
getFileBuffer
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
return
new
Promise
<
{
buffer
:
Buffer
;
originalname
:
string
;
encoding
:
string
;
mimetype
:
string
;
}
>
((
resolve
,
reject
)
=>
{
// @ts-ignore
this
.
uploaderMemory
(
req
,
res
,
(
error
)
=>
{
if
(
error
)
{
return
reject
(
error
);
}
// @ts-ignore
const
file
=
req
.
file
;
if
(
!
file
?.
buffer
)
{
return
reject
(
new
Error
(
'File empty'
));
}
resolve
({
buffer
:
file
.
buffer
,
originalname
:
decodeURIComponent
(
file
.
originalname
),
encoding
:
file
.
encoding
,
mimetype
:
file
.
mimetype
});
});
});
}
}
return
new
UploadModel
();
...
...
projects/app/src/pages/api/core/dataset/collection/create/localFile.ts
View file @
f1253a1f
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
uploadFile
}
from
'@fastgpt/service/common/file/gridfs/controller'
;
import
{
getUploadModel
}
from
'@fastgpt/service/common/file/multer'
;
import
{
authDataset
}
from
'@fastgpt/service/support/permission/dataset/auth'
;
import
{
type
FileCreateDatasetCollectionParams
}
from
'@fastgpt/global/core/dataset/api'
;
import
{
removeFilesByPaths
}
from
'@fastgpt/service/common/file/utils'
;
import
{
createCollectionAndInsertData
}
from
'@fastgpt/service/core/dataset/collection/controller'
;
import
{
DatasetCollectionTypeEnum
}
from
'@fastgpt/global/core/dataset/constants'
;
import
{
getNanoid
}
from
'@fastgpt/global/common/string/tools'
;
import
{
BucketNameEnum
}
from
'@fastgpt/global/common/file/constants'
;
import
{
NextAPI
}
from
'@/service/middleware/entry'
;
import
{
WritePermissionVal
}
from
'@fastgpt/global/support/permission/constant'
;
import
{
type
CreateCollectionResponse
}
from
'@/global/core/dataset/api'
;
import
{
getS3DatasetSource
}
from
'@fastgpt/service/common/s3/sources/dataset'
;
import
{
removeS3TTL
}
from
'@fastgpt/service/common/s3/utils'
;
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
):
CreateCollectionResponse
{
let
filePaths
:
string
[]
=
[];
try
{
// Create multer uploader
const
upload
=
getUploadModel
({
maxSize
:
global
.
feConfigs
?.
uploadFileMaxSize
});
const
{
file
,
data
,
bucketName
}
=
await
upload
.
getUploadFile
<
FileCreateDatasetCollectionParams
>
(
req
,
res
,
BucketNameEnum
.
dataset
);
filePaths
=
[
file
.
path
];
if
(
!
file
||
!
bucketName
)
{
throw
new
Error
(
'file is empty'
);
}
const
upload
=
getUploadModel
({
maxSize
:
global
.
feConfigs
?.
uploadFileMaxSize
});
const
{
buffer
,
originalname
}
=
await
upload
.
getFileBuffer
(
req
,
res
);
const
data
=
(()
=>
{
try
{
return
JSON
.
parse
(
req
.
body
?.
data
||
'{}'
);
}
catch
(
error
)
{
return
{};
}
})()
as
FileCreateDatasetCollectionParams
;
const
{
teamId
,
tmbId
,
dataset
}
=
await
authDataset
({
req
,
...
...
@@ -40,29 +30,24 @@ async function handler(req: NextApiRequest, res: NextApiResponse<any>): CreateCo
datasetId
:
data
.
datasetId
});
const
{
fileMetadata
,
collectionMetadata
,
...
collectionData
}
=
data
;
const
collectionName
=
file
.
originalname
;
const
s3DatasetSource
=
getS3DatasetSource
();
// 1. upload file
const
fileId
=
await
uploadFile
({
teamId
,
uid
:
tmbId
,
bucketName
,
path
:
file
.
path
,
filename
:
file
.
originalname
,
contentType
:
file
.
mimetype
,
metadata
:
fileMetadata
const
fileId
=
await
s3DatasetSource
.
uploadDatasetFileByBuffer
({
datasetId
:
String
(
dataset
.
_id
),
buffer
,
filename
:
originalname
});
// 2. delete tmp file
removeFilesByPaths
(
filePaths
);
await
removeS3TTL
({
key
:
fileId
,
bucketName
:
'private'
});
const
{
fileMetadata
,
collectionMetadata
,
...
collectionData
}
=
data
;
// 3. Create collection
const
{
collectionId
,
insertResults
}
=
await
createCollectionAndInsertData
({
dataset
,
createCollectionParams
:
{
...
collectionData
,
name
:
collectionN
ame
,
name
:
originaln
ame
,
teamId
,
tmbId
,
type
:
DatasetCollectionTypeEnum
.
file
,
...
...
@@ -76,8 +61,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse<any>): CreateCo
return
{
collectionId
,
results
:
insertResults
};
}
catch
(
error
)
{
removeFilesByPaths
(
filePaths
);
return
Promise
.
reject
(
error
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment