diff --git a/example/mock/file-upload.mock.ts b/example/mock/file-upload.mock.ts index 1da3b8d..8b1072a 100644 --- a/example/mock/file-upload.mock.ts +++ b/example/mock/file-upload.mock.ts @@ -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), } }, }) diff --git a/example/vite.config.ts b/example/vite.config.ts index 9049b4e..ec44b1a 100644 --- a/example/vite.config.ts +++ b/example/vite.config.ts @@ -12,10 +12,6 @@ export default defineConfig(({ mode }) => ({ formidableOptions: { // 配置上传资源存放目录 uploadDir: path.join(process.cwd(), '/uploads'), - // 可修改上传资源名称 - filename: (name, ext, part) => { - return part.originalFilename! - }, }, build: true, }), diff --git a/plugin/src/core/parseReqBody.ts b/plugin/src/core/parseReqBody.ts index 9ec120b..f47661e 100644 --- a/plugin/src/core/parseReqBody.ts +++ b/plugin/src/core/parseReqBody.ts @@ -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, @@ -48,7 +55,8 @@ async function parseMultipart( req: Connect.IncomingMessage, options: formidable.Options, ): Promise { - const form = formidable(options) + const form = formidable({ ...DEFAULT_FORMIDABLE_OPTIONS, ...options }) + return new Promise((resolve, reject) => { form.parse(req, (error, fields, files) => { if (error) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c6789bc..9a488aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -101,8 +101,8 @@ catalogs: specifier: ^3.3.3 version: 3.3.3 formidable: - specifier: 2.1.2 - version: 2.1.2 + specifier: 3.5.4 + version: 3.5.4 http-status: specifier: ^2.1.0 version: 2.1.0 @@ -233,7 +233,7 @@ importers: version: 3.3.3 formidable: specifier: catalog:prod - version: 2.1.2 + version: 3.5.4 http-status: specifier: catalog:prod version: 2.1.0 @@ -873,6 +873,10 @@ packages: '@napi-rs/wasm-runtime@0.2.9': resolution: {integrity: sha512-OKRBiajrrxB9ATokgEQoG87Z25c67pCpYcCwmXYX8PBftC9pBfN18gnm/fh1wurSLEKIAt+QRFLFCQISrb66Jg==} + '@noble/hashes@1.8.0': + resolution: {integrity: sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A==} + engines: {node: ^14.21.3 || >=16} + '@nodelib/fs.scandir@2.1.5': resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==} engines: {node: '>= 8'} @@ -892,6 +896,9 @@ packages: '@oxc-project/types@0.72.1': resolution: {integrity: sha512-qlvcDuCjISt4W7Izw0i5+GS3zCKJLXkoNDEc+E4ploage35SlZqxahpdKbHDX8uD70KDVNYWtupsHoNETy5kPQ==} + '@paralleldrive/cuid2@2.2.2': + resolution: {integrity: sha512-ZOBkgDwEdoYVlSeRbYYXs0S9MejQofiVYoTbKzy/6GQa39/q5tQU2IX46+shYnUkpEl3wc+J6wRlar7r2EK2xA==} + '@pengzhanbo/eslint-config@1.32.0': resolution: {integrity: sha512-HJg2NIyf+cRecJjklDLH2cf+KnWQS98RQTCWX+O6QWIHT2x585/bIPJ2Dh4AuHhzT6UFxJZKF2k9+5nbcmoSOw==} peerDependencies: @@ -2254,8 +2261,9 @@ packages: resolution: {integrity: sha512-wzsgA6WOq+09wrU1tsJ09udeR/YZRaeArL9e1wPbFg3GG2yDnC2ldKpxs4xunpFF9DgqCqOIra3bc1HWrJ37Ww==} engines: {node: '>=0.4.x'} - formidable@2.1.2: - resolution: {integrity: sha512-CM3GuJ57US06mlpQ47YcunuUZ9jpm8Vx+P2CGt2j7HpgkKZO/DJYQ0Bobim8G6PFQmK5lOqOOdUXboU+h73A4g==} + formidable@3.5.4: + resolution: {integrity: sha512-YikH+7CUTOtP44ZTnUhR7Ic2UASBPOqmaRkRKxRbywPTe5VxF7RRCck4af9wutiZ/QKM5nME9Bie2fFaPz5Gug==} + engines: {node: '>=14.0.0'} fsevents@2.3.3: resolution: {integrity: sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==} @@ -2353,10 +2361,6 @@ packages: hast-util-whitespace@3.0.0: resolution: {integrity: sha512-88JUN06ipLwsnv+dVn+OIYOvAuvBMy/Qoi6O7mQHxdPXpjy+Cd6xRkWwux7DKO+4sYILtLBRIKgsdpS2gQc7qw==} - hexoid@1.0.0: - resolution: {integrity: sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==} - engines: {node: '>=8'} - hookable@5.5.3: resolution: {integrity: sha512-Yc+BQe8SvoXH1643Qez1zqLRmbA5rCL+sSmk6TVos0LWVfNIB7PGncdlId77WzLGSIB5KaWgTaNTs2lNVEI6VQ==} @@ -4054,6 +4058,8 @@ snapshots: '@tybys/wasm-util': 0.9.0 optional: true + '@noble/hashes@1.8.0': {} + '@nodelib/fs.scandir@2.1.5': dependencies: '@nodelib/fs.stat': 2.0.5 @@ -4070,6 +4076,10 @@ snapshots: '@oxc-project/types@0.72.1': {} + '@paralleldrive/cuid2@2.2.2': + dependencies: + '@noble/hashes': 1.8.0 + '@pengzhanbo/eslint-config@1.32.0(@typescript-eslint/utils@8.32.0(eslint@9.28.0(jiti@2.4.2))(typescript@5.8.3))(eslint-plugin-vuejs-accessibility@2.4.1(eslint@9.28.0(jiti@2.4.2)))(eslint@9.28.0(jiti@2.4.2))(typescript@5.8.3)(vitest@3.1.4(@types/debug@4.1.12)(@types/node@22.15.29)(jiti@2.4.2)(yaml@2.8.0))': dependencies: '@antfu/install-pkg': 1.1.0 @@ -5498,12 +5508,11 @@ snapshots: format@0.2.2: {} - formidable@2.1.2: + formidable@3.5.4: dependencies: + '@paralleldrive/cuid2': 2.2.2 dezalgo: 1.0.4 - hexoid: 1.0.0 once: 1.4.0 - qs: 6.11.0 fsevents@2.3.3: optional: true @@ -5617,8 +5626,6 @@ snapshots: dependencies: '@types/hast': 3.0.4 - hexoid@1.0.0: {} - hookable@5.5.3: {} hosted-git-info@7.0.1: @@ -6968,14 +6975,14 @@ snapshots: vue-eslint-parser@9.4.3(eslint@9.28.0(jiti@2.4.2)): dependencies: - debug: 4.4.0 + debug: 4.4.1 eslint: 9.28.0(jiti@2.4.2) eslint-scope: 7.2.2 eslint-visitor-keys: 3.4.3 espree: 9.6.1 esquery: 1.6.0 lodash: 4.17.21 - semver: 7.7.1 + semver: 7.7.2 transitivePeerDependencies: - supports-color diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 2d35945..e63cb79 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -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