@midwayjs/upload 3.6.0 → 3.6.2

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.
@@ -3,11 +3,12 @@ import { UploadOptions } from '.';
3
3
  export declare class UploadMiddleware implements IMiddleware<any, any> {
4
4
  upload: UploadOptions;
5
5
  logger: IMidwayLogger;
6
+ private uploadWhiteListMap;
6
7
  resolve(app: any): (req: any, res: any, next: any) => Promise<any>;
7
8
  execUpload(ctx: any, req: any, res: any, next: any, isExpress: any): Promise<any>;
8
9
  getUploadBoundary(request: any): false | string;
9
10
  isReadableStream(req: any, isExpress: any): boolean;
10
- checkExt(filename: any): boolean;
11
+ checkExt(filename: any): string | boolean;
11
12
  static getName(): string;
12
13
  }
13
14
  //# sourceMappingURL=middleware.d.ts.map
@@ -19,7 +19,15 @@ const parse_1 = require("./parse");
19
19
  const getRawBody = require("raw-body");
20
20
  const { unlink, writeFile } = fs_1.promises;
21
21
  let UploadMiddleware = class UploadMiddleware {
22
+ constructor() {
23
+ this.uploadWhiteListMap = {};
24
+ }
22
25
  resolve(app) {
26
+ if (Array.isArray(this.upload.whitelist)) {
27
+ for (const whiteExt of this.upload.whitelist) {
28
+ this.uploadWhiteListMap[whiteExt] = true;
29
+ }
30
+ }
23
31
  if (app.getFrameworkType() === core_1.MidwayFrameworkType.WEB_EXPRESS) {
24
32
  return async (req, res, next) => {
25
33
  return this.execUpload(req, req, res, next, true);
@@ -64,10 +72,12 @@ let UploadMiddleware = class UploadMiddleware {
64
72
  if (this.isReadableStream(req, isExpress)) {
65
73
  if (mode === 'stream') {
66
74
  const { fields, fileInfo } = await (0, parse_1.parseFromReadableStream)(req, boundary);
67
- if (!this.checkExt(fileInfo.filename)) {
75
+ const ext = this.checkExt(fileInfo.filename);
76
+ if (!ext) {
68
77
  throw new _1.MultipartInvalidFilenameError(fileInfo.filename);
69
78
  }
70
79
  else {
80
+ fileInfo['_ext'] = ext;
71
81
  ctx.fields = fields;
72
82
  ctx.files = [fileInfo];
73
83
  return next();
@@ -93,9 +103,11 @@ let UploadMiddleware = class UploadMiddleware {
93
103
  const requireId = `upload_${Date.now()}.${Math.random()}`;
94
104
  const files = data.files;
95
105
  const notCheckFile = files.find(fileInfo => {
96
- if (!this.checkExt(fileInfo.filename)) {
106
+ const ext = this.checkExt(fileInfo.filename);
107
+ if (!ext) {
97
108
  return fileInfo;
98
109
  }
110
+ fileInfo['_ext'] = ext;
99
111
  });
100
112
  if (notCheckFile) {
101
113
  throw new _1.MultipartInvalidFilenameError(notCheckFile.filename);
@@ -103,7 +115,7 @@ let UploadMiddleware = class UploadMiddleware {
103
115
  ctx.files = await Promise.all(files.map(async (file, index) => {
104
116
  const { data, filename } = file;
105
117
  if (mode === 'file') {
106
- const ext = (0, path_1.extname)(filename);
118
+ const ext = file['_ext'] || (0, path_1.extname)(filename);
107
119
  const tmpFileName = (0, path_1.resolve)(tmpdir, `${requireId}.${index}${ext}`);
108
120
  await writeFile(tmpFileName, data, 'binary');
109
121
  file.data = tmpFileName;
@@ -156,12 +168,15 @@ let UploadMiddleware = class UploadMiddleware {
156
168
  return false;
157
169
  }
158
170
  checkExt(filename) {
159
- const ext = (0, path_1.extname)(filename).toLowerCase();
160
- const { whitelist } = this.upload;
161
- if (!Array.isArray(whitelist)) {
162
- return true;
171
+ const lowerCaseFileNameList = filename.toLowerCase().split('.');
172
+ while (lowerCaseFileNameList.length) {
173
+ lowerCaseFileNameList.shift();
174
+ const curExt = `.${lowerCaseFileNameList.join('.')}`;
175
+ if (this.upload.whitelist === null || this.uploadWhiteListMap[curExt]) {
176
+ return curExt;
177
+ }
163
178
  }
164
- return whitelist.includes(ext);
179
+ return false;
165
180
  }
166
181
  static getName() {
167
182
  return 'upload';
package/dist/parse.js CHANGED
@@ -74,10 +74,6 @@ const parseFromReadableStream = (readStream, boundary) => {
74
74
  }
75
75
  // 正在传输中的话
76
76
  if (isTransformFileData) {
77
- if (lastChunk.length) {
78
- chunk = Buffer.concat([lastChunk, chunk]);
79
- lastChunk = emptyBuf;
80
- }
81
77
  const newPreChunk = Buffer.concat([preChunk, chunk]);
82
78
  const newBlockIndex = (0, exports.bufferIndexOf)(newPreChunk, bufferSeparator);
83
79
  // 存在新的块则代表已经结束了
@@ -122,7 +118,7 @@ const parseFromReadableStream = (readStream, boundary) => {
122
118
  resolve({ fileInfo, fields });
123
119
  break;
124
120
  }
125
- callback(null, emptyBuf);
121
+ callback(null, lastChunk);
126
122
  },
127
123
  });
128
124
  readStream.pipe(fileInfo.data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/upload",
3
- "version": "3.6.0",
3
+ "version": "3.6.2",
4
4
  "description": "Midway Component for upload",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -32,6 +32,5 @@
32
32
  "@midwayjs/mock": "^3.6.0",
33
33
  "@midwayjs/serverless-app": "^3.6.0",
34
34
  "@midwayjs/web": "^3.6.0"
35
- },
36
- "gitHead": "22643b0e8519766bb7c68b975930199fc136336e"
35
+ }
37
36
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2013 - Now midwayjs
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.