-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
257 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { customAlphabet } from 'nanoid'; | ||
import multer from 'multer'; | ||
import path from 'path'; | ||
import { BucketNameEnum } from '@fastgpt/global/common/file/constants'; | ||
|
||
const nanoid = customAlphabet('1234567890abcdef', 12); | ||
|
||
type FileType = { | ||
fieldname: string; | ||
originalname: string; | ||
encoding: string; | ||
mimetype: string; | ||
filename: string; | ||
path: string; | ||
size: number; | ||
}; | ||
|
||
export function getUploadModel({ maxSize = 500 }: { maxSize?: number }) { | ||
maxSize *= 1024 * 1024; | ||
class UploadModel { | ||
uploader = multer({ | ||
limits: { | ||
fieldSize: maxSize | ||
}, | ||
preservePath: true, | ||
storage: multer.diskStorage({ | ||
filename: (_req, file, cb) => { | ||
const { ext } = path.parse(decodeURIComponent(file.originalname)); | ||
cb(null, nanoid() + ext); | ||
} | ||
}) | ||
}).any(); | ||
|
||
async doUpload(req: NextApiRequest, res: NextApiResponse) { | ||
return new Promise<{ | ||
files: FileType[]; | ||
metadata: Record<string, any>; | ||
bucketName?: `${BucketNameEnum}`; | ||
}>((resolve, reject) => { | ||
// @ts-ignore | ||
this.uploader(req, res, (error) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
|
||
resolve({ | ||
...req.body, | ||
files: | ||
// @ts-ignore | ||
req.files?.map((file) => ({ | ||
...file, | ||
originalname: decodeURIComponent(file.originalname) | ||
})) || [], | ||
metadata: (() => { | ||
if (!req.body?.metadata) return {}; | ||
try { | ||
return JSON.parse(req.body.metadata); | ||
} catch (error) { | ||
console.log(error); | ||
return {}; | ||
} | ||
})() | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
return new UploadModel(); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
projects/app/src/components/Icon/icons/core/chat/recordFill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions
8
projects/app/src/components/Icon/icons/core/chat/stopSpeechFill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.