Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/mock/file-upload.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineMock({
const files = Array.isArray(body.files) ? body.files : [body.files]
return {
name: body.name,
files: files.map((file: any) => file.originalFilename),
files: files.filter(Boolean).map((file: any) => file.originalFilename),
}
},
})
4 changes: 0 additions & 4 deletions example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ export default defineConfig(({ mode }) => ({
formidableOptions: {
// 配置上传资源存放目录
uploadDir: path.join(process.cwd(), '/uploads'),
// 可修改上传资源名称
filename: (name, ext, part) => {
return part.originalFilename!
},
},
build: true,
}),
Expand Down
10 changes: 9 additions & 1 deletion plugin/src/core/parseReqBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import type { BodyParserOptions } from '../types'
import bodyParser from 'co-body'
import formidable from 'formidable'

const DEFAULT_FORMIDABLE_OPTIONS: formidable.Options = {
keepExtensions: true,
filename(name, ext, part) {
return part?.originalFilename || `${name}.${Date.now()}${ext ? `.${ext}` : ''}`
},
}

export async function parseReqBody(
req: Connect.IncomingMessage,
formidableOptions: formidable.Options,
Expand Down Expand Up @@ -48,7 +55,8 @@ async function parseMultipart(
req: Connect.IncomingMessage,
options: formidable.Options,
): Promise<any> {
const form = formidable(options)
const form = formidable({ ...DEFAULT_FORMIDABLE_OPTIONS, ...options })

return new Promise((resolve, reject) => {
form.parse(req, (error, fields, files) => {
if (error) {
Expand Down
39 changes: 23 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ catalogs:
cors: ^2.8.5
debug: ^4.4.1
fast-glob: ^3.3.3
formidable: 2.1.2
formidable: 3.5.4
http-status: ^2.1.0
is-core-module: ^2.16.1
json5: ^2.2.3
Expand Down