Skip to content

Commit 6d6461f

Browse files
authored
feat: upgrade formidable to v3, close #112 (#114)
1 parent d0015ca commit 6d6461f

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

example/mock/file-upload.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default defineMock({
88
const files = Array.isArray(body.files) ? body.files : [body.files]
99
return {
1010
name: body.name,
11-
files: files.map((file: any) => file.originalFilename),
11+
files: files.filter(Boolean).map((file: any) => file.originalFilename),
1212
}
1313
},
1414
})

example/vite.config.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export default defineConfig(({ mode }) => ({
1212
formidableOptions: {
1313
// 配置上传资源存放目录
1414
uploadDir: path.join(process.cwd(), '/uploads'),
15-
// 可修改上传资源名称
16-
filename: (name, ext, part) => {
17-
return part.originalFilename!
18-
},
1915
},
2016
build: true,
2117
}),

plugin/src/core/parseReqBody.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import type { BodyParserOptions } from '../types'
33
import bodyParser from 'co-body'
44
import formidable from 'formidable'
55

6+
const DEFAULT_FORMIDABLE_OPTIONS: formidable.Options = {
7+
keepExtensions: true,
8+
filename(name, ext, part) {
9+
return part?.originalFilename || `${name}.${Date.now()}${ext ? `.${ext}` : ''}`
10+
},
11+
}
12+
613
export async function parseReqBody(
714
req: Connect.IncomingMessage,
815
formidableOptions: formidable.Options,
@@ -48,7 +55,8 @@ async function parseMultipart(
4855
req: Connect.IncomingMessage,
4956
options: formidable.Options,
5057
): Promise<any> {
51-
const form = formidable(options)
58+
const form = formidable({ ...DEFAULT_FORMIDABLE_OPTIONS, ...options })
59+
5260
return new Promise((resolve, reject) => {
5361
form.parse(req, (error, fields, files) => {
5462
if (error) {

pnpm-lock.yaml

Lines changed: 23 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ catalogs:
3636
cors: ^2.8.5
3737
debug: ^4.4.1
3838
fast-glob: ^3.3.3
39-
formidable: 2.1.2
39+
formidable: 3.5.4
4040
http-status: ^2.1.0
4141
is-core-module: ^2.16.1
4242
json5: ^2.2.3

0 commit comments

Comments
 (0)