@midwayjs/upload 3.0.0-beta.11 → 3.0.0-beta.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.0.0-beta.12](https://github.com/midwayjs/midway/compare/v3.0.0-beta.11...v3.0.0-beta.12) (2021-12-28)
7
+
8
+
9
+ ### Features
10
+
11
+ * custom error code & add @Files/@Fields ([#1438](https://github.com/midwayjs/midway/issues/1438)) ([b0032af](https://github.com/midwayjs/midway/commit/b0032afd2fa9ea0416fe69f4bd0c1a58bea5314e))
12
+ * support throw err status ([#1440](https://github.com/midwayjs/midway/issues/1440)) ([7b98110](https://github.com/midwayjs/midway/commit/7b98110d65c5287a8fcb3eb5356dea2d7a32cee9))
13
+
14
+
15
+
16
+
17
+
6
18
  # [3.0.0-beta.11](https://github.com/midwayjs/midway/compare/v3.0.0-beta.10...v3.0.0-beta.11) (2021-12-21)
7
19
 
8
20
  **Note:** Version bump only for package @midwayjs/upload
@@ -10,21 +10,12 @@ tnpm i @midwayjs/upload --save
10
10
  ```
11
11
  2. 在 configuration 中引入组件,
12
12
  ```ts
13
- import * as upload from '../../../../src';
13
+ import * as upload from '@midwayjs/upload';
14
14
  @Configuration({
15
15
  imports: [
16
16
  // ...other components
17
17
  upload
18
18
  ],
19
- importConfigs: [
20
- {
21
- default: {
22
- upload: { // 上传组件的配置
23
- mode: upload.UploadMode.File, // 默认为 file 模式
24
- }
25
- }
26
- }
27
- ]
28
19
  })
29
20
  export class AutoConfiguration {}
30
21
  ```
@@ -38,20 +29,19 @@ export class HomeController {
38
29
  ctx;
39
30
 
40
31
  @Post('/upload')
41
- async upload() {
42
- const { files, fields } = this.ctx;
32
+ async upload(@Files() files: upload.UploadFileInfo[], @Fields() fields) {
43
33
  /*
44
34
  files = [
45
35
  {
46
36
  filename: 'test.pdf', // 文件原名
47
37
  data: '/var/tmp/xxx.pdf', // mode 为 file 时为服务器临时文件地址
48
- fieldname: 'test1', // 表单 field 名
38
+ fieldName: 'test1', // 表单 field 名
49
39
  mimeType: 'application/pdf', // mime
50
40
  },
51
41
  {
52
42
  filename: 'test.pdf', // 文件原名
53
43
  data: ReadStream, // mode 为 stream 时为服务器临时文件地址
54
- fieldname: 'test2', // 表单 field 名
44
+ fieldName: 'test2', // 表单 field 名
55
45
  mimeType: 'application/pdf', // mime
56
46
  },
57
47
  // ...file 下支持同时上传多个文件
@@ -79,4 +69,4 @@ export const upload = {
79
69
  // tmpdir: string,上传的文件临时存储路径
80
70
  tmpdir: join(tmpdir(), 'midway-upload-files'),
81
71
  }
82
- ```
72
+ ```
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.upload = void 0;
4
- const interface_1 = require("../interface");
5
4
  const path_1 = require("path");
6
5
  const os_1 = require("os");
7
6
  exports.upload = {
8
- mode: interface_1.UploadMode.File,
7
+ mode: 'file',
9
8
  fileSize: '10mb',
10
9
  whitelist: [
11
10
  // images
@@ -0,0 +1,5 @@
1
+ import { httpError } from '@midwayjs/core';
2
+ export declare class MultipartInvalidFilenameError extends httpError.BadRequestError {
3
+ constructor(filename: string);
4
+ }
5
+ //# sourceMappingURL=error.d.ts.map
package/dist/error.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MultipartInvalidFilenameError = void 0;
4
+ const core_1 = require("@midwayjs/core");
5
+ class MultipartInvalidFilenameError extends core_1.httpError.BadRequestError {
6
+ constructor(filename) {
7
+ super(`Invalid update file name ${filename}, please check it`);
8
+ }
9
+ }
10
+ exports.MultipartInvalidFilenameError = MultipartInvalidFilenameError;
11
+ //# sourceMappingURL=error.js.map
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { UploadConfiguration as Configuration } from './configuration';
2
2
  export * from './interface';
3
3
  export * from './middleware';
4
+ export * from './error';
4
5
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -15,4 +15,5 @@ var configuration_1 = require("./configuration");
15
15
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.UploadConfiguration; } });
16
16
  __exportStar(require("./interface"), exports);
17
17
  __exportStar(require("./middleware"), exports);
18
+ __exportStar(require("./error"), exports);
18
19
  //# sourceMappingURL=index.js.map
@@ -1,20 +1,16 @@
1
1
  /// <reference types="node" />
2
2
  import { Readable } from "stream";
3
- export declare enum UploadMode {
4
- Stream = "stream",
5
- File = "file",
6
- Buffer = "buffer"
7
- }
3
+ export declare type UploadMode = 'stream' | 'file';
8
4
  export interface UploadOptions {
9
5
  mode?: UploadMode;
10
6
  fileSize?: string;
11
7
  whitelist?: string[];
12
8
  tmpdir?: string;
13
9
  }
14
- export interface UploadFileInfo {
10
+ export interface UploadFileInfo<T> {
15
11
  filename: string;
16
- fieldname: string;
12
+ fieldName: string;
17
13
  mimeType: string;
18
- data: Buffer | Readable | string;
14
+ data: T extends string ? string : Readable;
19
15
  }
20
16
  //# sourceMappingURL=interface.d.ts.map
package/dist/interface.js CHANGED
@@ -1,10 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.UploadMode = void 0;
4
- var UploadMode;
5
- (function (UploadMode) {
6
- UploadMode["Stream"] = "stream";
7
- UploadMode["File"] = "file";
8
- UploadMode["Buffer"] = "buffer";
9
- })(UploadMode = exports.UploadMode || (exports.UploadMode = {}));
10
3
  //# sourceMappingURL=interface.js.map
@@ -8,6 +8,6 @@ export declare class UploadMiddleware implements IMiddleware<any, any> {
8
8
  getUploadBoundary(request: any): false | string;
9
9
  isReadableStream(req: any, isExpress: any): boolean;
10
10
  checkExt(filename: any): boolean;
11
- getName(): string;
11
+ static getName(): string;
12
12
  }
13
13
  //# sourceMappingURL=middleware.d.ts.map
@@ -41,17 +41,10 @@ let UploadMiddleware = class UploadMiddleware {
41
41
  ctx.fields = {};
42
42
  let body;
43
43
  if (this.isReadableStream(req, isExpress)) {
44
- if (mode === _1.UploadMode.Stream) {
44
+ if (mode === 'stream') {
45
45
  const { fields, fileInfo } = await (0, upload_1.parseFromReadableStream)(req, boundary);
46
46
  if (!this.checkExt(fileInfo.filename)) {
47
- res.status = 400;
48
- const errorMessage = 'Invalid filename: ' + fileInfo.filename;
49
- const err = new Error(errorMessage);
50
- this.logger.error(err);
51
- if (isExpress) {
52
- return res.sendStatus(400);
53
- }
54
- return;
47
+ throw new _1.MultipartInvalidFilenameError(fileInfo.filename);
55
48
  }
56
49
  else {
57
50
  ctx.fields = fields;
@@ -79,36 +72,26 @@ let UploadMiddleware = class UploadMiddleware {
79
72
  }
80
73
  });
81
74
  if (notCheckFile) {
82
- res.status = 400;
83
- const errorMessage = 'Invalid filename: ' + notCheckFile.filename;
84
- const err = new Error(errorMessage);
85
- this.logger.error(err);
86
- if (isExpress) {
87
- return res.sendStatus(400);
88
- }
89
- return;
75
+ throw new _1.MultipartInvalidFilenameError(notCheckFile.filename);
90
76
  }
91
- ctx.files =
92
- mode === 'buffer'
93
- ? files
94
- : files.map((file, index) => {
95
- const { data, filename } = file;
96
- if (mode === _1.UploadMode.File) {
97
- const ext = (0, path_1.extname)(filename);
98
- const tmpFileName = (0, path_1.resolve)(tmpdir, `${requireId}.${index}${ext}`);
99
- (0, fs_1.writeFileSync)(tmpFileName, data, 'binary');
100
- file.data = tmpFileName;
101
- }
102
- else if (mode === _1.UploadMode.Stream) {
103
- file.data = new stream_1.Readable({
104
- read() {
105
- this.push(data);
106
- this.push(null);
107
- },
108
- });
109
- }
110
- return file;
77
+ ctx.files = files.map((file, index) => {
78
+ const { data, filename } = file;
79
+ if (mode === 'file') {
80
+ const ext = (0, path_1.extname)(filename);
81
+ const tmpFileName = (0, path_1.resolve)(tmpdir, `${requireId}.${index}${ext}`);
82
+ (0, fs_1.writeFileSync)(tmpFileName, data, 'binary');
83
+ file.data = tmpFileName;
84
+ }
85
+ else if (mode === 'stream') {
86
+ file.data = new stream_1.Readable({
87
+ read() {
88
+ this.push(data);
89
+ this.push(null);
90
+ },
111
91
  });
92
+ }
93
+ return file;
94
+ });
112
95
  return next();
113
96
  }
114
97
  getUploadBoundary(request) {
@@ -148,7 +131,7 @@ let UploadMiddleware = class UploadMiddleware {
148
131
  }
149
132
  return whitelist.includes(ext);
150
133
  }
151
- getName() {
134
+ static getName() {
152
135
  return 'upload';
153
136
  }
154
137
  };
package/dist/upload.d.ts CHANGED
@@ -7,7 +7,7 @@ export declare const parseMultipart: (body: any, boundary: string) => Promise<{
7
7
  }>;
8
8
  export declare const parseFromReadableStream: (readStream: Readable, boundary: any) => Promise<{
9
9
  fields: any;
10
- fileInfo: UploadFileInfo;
10
+ fileInfo: UploadFileInfo<Readable>;
11
11
  }>;
12
12
  export declare const bufferIndexOf: (buffer: Buffer, search: Buffer, offset?: number) => number;
13
13
  export declare const bufferSplit: (buffer: Buffer, separator: Buffer, limit?: number) => Buffer[];
package/dist/upload.js CHANGED
@@ -25,7 +25,7 @@ const parseMultipart = async (body, boundary) => {
25
25
  files.push({
26
26
  filename: head['content-disposition'].filename,
27
27
  data,
28
- fieldname: head['content-disposition'].name,
28
+ fieldName: head['content-disposition'].name,
29
29
  mimeType: head['content-type'],
30
30
  });
31
31
  });
@@ -42,7 +42,7 @@ const parseFromReadableStream = (readStream, boundary) => {
42
42
  const fileInfo = {
43
43
  filename: '',
44
44
  data: null,
45
- fieldname: '',
45
+ fieldName: '',
46
46
  mimeType: '',
47
47
  };
48
48
  const emptyBuf = Buffer.alloc(0);
@@ -108,7 +108,7 @@ const parseFromReadableStream = (readStream, boundary) => {
108
108
  continue;
109
109
  }
110
110
  fileInfo.filename = head['content-disposition'].filename;
111
- fileInfo.fieldname = head['content-disposition'].name;
111
+ fileInfo.fieldName = head['content-disposition'].name;
112
112
  fileInfo.mimeType = head['content-type'];
113
113
  isTransformFileData = true;
114
114
  lastChunk = data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/upload",
3
- "version": "3.0.0-beta.11",
3
+ "version": "3.0.0-beta.12",
4
4
  "description": "Midway Component for upload",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -25,14 +25,14 @@
25
25
  "raw-body": "^2.4.1"
26
26
  },
27
27
  "devDependencies": {
28
- "@midwayjs/core": "^3.0.0-beta.11",
29
- "@midwayjs/decorator": "^3.0.0-beta.11",
30
- "@midwayjs/express": "^3.0.0-beta.11",
31
- "@midwayjs/faas": "^3.0.0-beta.11",
32
- "@midwayjs/koa": "^3.0.0-beta.11",
33
- "@midwayjs/mock": "^3.0.0-beta.11",
34
- "@midwayjs/serverless-app": "^3.0.0-beta.11",
35
- "@midwayjs/web": "^3.0.0-beta.11"
28
+ "@midwayjs/core": "^3.0.0-beta.12",
29
+ "@midwayjs/decorator": "^3.0.0-beta.12",
30
+ "@midwayjs/express": "^3.0.0-beta.12",
31
+ "@midwayjs/faas": "^3.0.0-beta.12",
32
+ "@midwayjs/koa": "^3.0.0-beta.12",
33
+ "@midwayjs/mock": "^3.0.0-beta.12",
34
+ "@midwayjs/serverless-app": "^3.0.0-beta.12",
35
+ "@midwayjs/web": "^3.0.0-beta.12"
36
36
  },
37
- "gitHead": "a2b066fd500e815e799e7f421cd216f12e0e9f8e"
37
+ "gitHead": "1c46e53eb934248007eeb7fe3920f5ac24e272c6"
38
38
  }