@midwayjs/upload 3.15.6 → 3.15.8
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/dist/interface.d.ts +3 -3
- package/dist/middleware.d.ts +12 -4
- package/dist/middleware.js +38 -10
- package/package.json +8 -8
package/dist/interface.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Readable } from 'stream';
|
|
3
|
-
import { IgnoreMatcher } from '@midwayjs/core';
|
|
3
|
+
import { IgnoreMatcher, IMidwayContext } from '@midwayjs/core';
|
|
4
4
|
export type UploadMode = 'stream' | 'file';
|
|
5
5
|
export interface UploadOptions {
|
|
6
6
|
/**
|
|
@@ -14,7 +14,7 @@ export interface UploadOptions {
|
|
|
14
14
|
/**
|
|
15
15
|
* The white ext file names
|
|
16
16
|
*/
|
|
17
|
-
whitelist?: string[] | null;
|
|
17
|
+
whitelist?: string[] | null | ((ctx: IMidwayContext<any>) => string[]);
|
|
18
18
|
/**
|
|
19
19
|
* Temporary file directory
|
|
20
20
|
*/
|
|
@@ -38,7 +38,7 @@ export interface UploadOptions {
|
|
|
38
38
|
/**
|
|
39
39
|
* Mime type white list
|
|
40
40
|
*/
|
|
41
|
-
mimeTypeWhiteList?: Record<string, string | string[]
|
|
41
|
+
mimeTypeWhiteList?: Record<string, string | string[]> | ((ctx: IMidwayContext<any>) => string | string[]);
|
|
42
42
|
}
|
|
43
43
|
export interface UploadFileInfo<T> {
|
|
44
44
|
filename: string;
|
package/dist/middleware.d.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { IMiddleware,
|
|
2
|
+
import { IMiddleware, ILogger, IgnoreMatcher } from '@midwayjs/core';
|
|
3
3
|
import { UploadOptions } from '.';
|
|
4
4
|
export declare class UploadMiddleware implements IMiddleware<any, any> {
|
|
5
5
|
uploadConfig: UploadOptions;
|
|
6
|
-
logger:
|
|
6
|
+
logger: ILogger;
|
|
7
|
+
/**
|
|
8
|
+
* cache global upload white list when uploadConfig.whitelist is set
|
|
9
|
+
* @private
|
|
10
|
+
*/
|
|
7
11
|
private uploadWhiteListMap;
|
|
12
|
+
/**
|
|
13
|
+
* cache global upload mime type white list when uploadConfig.mimeTypeWhiteList is set
|
|
14
|
+
* @private
|
|
15
|
+
*/
|
|
8
16
|
private uploadFileMimeTypeMap;
|
|
9
17
|
match: IgnoreMatcher<any>[];
|
|
10
18
|
ignore: IgnoreMatcher<any>[];
|
|
@@ -13,8 +21,8 @@ export declare class UploadMiddleware implements IMiddleware<any, any> {
|
|
|
13
21
|
execUpload(ctx: any, req: any, res: any, next: any, isExpress: any): Promise<any>;
|
|
14
22
|
getUploadBoundary(request: any): false | string;
|
|
15
23
|
isReadableStream(req: any, isExpress: any): boolean;
|
|
16
|
-
checkAndGetExt(filename: any): string | boolean;
|
|
17
|
-
checkFileType(ext: string, data: Buffer): Promise<{
|
|
24
|
+
checkAndGetExt(filename: any, whiteListMap?: Map<string, string>): string | boolean;
|
|
25
|
+
checkFileType(ext: string, data: Buffer, uploadFileMimeTypeMap?: Map<string, string[]>): Promise<{
|
|
18
26
|
passed: boolean;
|
|
19
27
|
mime?: string;
|
|
20
28
|
current?: string;
|
package/dist/middleware.js
CHANGED
|
@@ -22,7 +22,15 @@ const utils_1 = require("./utils");
|
|
|
22
22
|
const { unlink, writeFile } = fs_1.promises;
|
|
23
23
|
let UploadMiddleware = class UploadMiddleware {
|
|
24
24
|
constructor() {
|
|
25
|
+
/**
|
|
26
|
+
* cache global upload white list when uploadConfig.whitelist is set
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
25
29
|
this.uploadWhiteListMap = new Map();
|
|
30
|
+
/**
|
|
31
|
+
* cache global upload mime type white list when uploadConfig.mimeTypeWhiteList is set
|
|
32
|
+
* @private
|
|
33
|
+
*/
|
|
26
34
|
this.uploadFileMimeTypeMap = new Map();
|
|
27
35
|
}
|
|
28
36
|
async init() {
|
|
@@ -34,7 +42,8 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
34
42
|
}
|
|
35
43
|
}
|
|
36
44
|
resolve(app) {
|
|
37
|
-
if (
|
|
45
|
+
if (this.uploadConfig.whitelist &&
|
|
46
|
+
Array.isArray(this.uploadConfig.whitelist)) {
|
|
38
47
|
for (const whiteExt of this.uploadConfig.whitelist) {
|
|
39
48
|
this.uploadWhiteListMap.set(whiteExt, whiteExt);
|
|
40
49
|
}
|
|
@@ -85,11 +94,30 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
85
94
|
}
|
|
86
95
|
}));
|
|
87
96
|
};
|
|
97
|
+
// create new map include custom white list
|
|
98
|
+
const currentContextWhiteListMap = new Map([
|
|
99
|
+
...this.uploadWhiteListMap.entries(),
|
|
100
|
+
]);
|
|
101
|
+
if (typeof this.uploadConfig.whitelist === 'function') {
|
|
102
|
+
const whiteListArray = this.uploadConfig.whitelist.call(this, ctx);
|
|
103
|
+
whiteListArray.forEach(ext => currentContextWhiteListMap.set(ext, ext));
|
|
104
|
+
}
|
|
105
|
+
// create new map include custom mime type white list
|
|
106
|
+
const currentContextMimeTypeWhiteListMap = new Map([
|
|
107
|
+
...this.uploadFileMimeTypeMap.entries(),
|
|
108
|
+
]);
|
|
109
|
+
if (typeof this.uploadConfig.mimeTypeWhiteList === 'function') {
|
|
110
|
+
const mimeTypeWhiteList = this.uploadConfig.mimeTypeWhiteList.call(this, ctx);
|
|
111
|
+
for (const ext in mimeTypeWhiteList) {
|
|
112
|
+
const mime = [].concat(mimeTypeWhiteList[ext]);
|
|
113
|
+
currentContextMimeTypeWhiteListMap.set(ext, mime);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
88
116
|
let body;
|
|
89
117
|
if (this.isReadableStream(req, isExpress)) {
|
|
90
118
|
if (mode === 'stream') {
|
|
91
119
|
const { fields, fileInfo } = await (0, parse_1.parseFromReadableStream)(req, boundary);
|
|
92
|
-
const ext = this.checkAndGetExt(fileInfo.filename);
|
|
120
|
+
const ext = this.checkAndGetExt(fileInfo.filename, currentContextWhiteListMap);
|
|
93
121
|
if (!ext) {
|
|
94
122
|
throw new _1.MultipartInvalidFilenameError(fileInfo.filename);
|
|
95
123
|
}
|
|
@@ -120,11 +148,11 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
120
148
|
const requireId = `upload_${Date.now()}.${Math.random()}`;
|
|
121
149
|
const files = data.files;
|
|
122
150
|
for (const fileInfo of files) {
|
|
123
|
-
const ext = this.checkAndGetExt(fileInfo.filename);
|
|
151
|
+
const ext = this.checkAndGetExt(fileInfo.filename, currentContextWhiteListMap);
|
|
124
152
|
if (!ext) {
|
|
125
153
|
throw new _1.MultipartInvalidFilenameError(fileInfo.filename);
|
|
126
154
|
}
|
|
127
|
-
const { passed, mime, current } = await this.checkFileType(ext, fileInfo.data);
|
|
155
|
+
const { passed, mime, current } = await this.checkFileType(ext, fileInfo.data, currentContextMimeTypeWhiteListMap);
|
|
128
156
|
if (!passed) {
|
|
129
157
|
throw new _1.MultipartInvalidFileTypeError(fileInfo.filename, current, mime);
|
|
130
158
|
}
|
|
@@ -185,8 +213,8 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
185
213
|
}
|
|
186
214
|
return false;
|
|
187
215
|
}
|
|
188
|
-
// check
|
|
189
|
-
checkAndGetExt(filename) {
|
|
216
|
+
// check extensions
|
|
217
|
+
checkAndGetExt(filename, whiteListMap = this.uploadWhiteListMap) {
|
|
190
218
|
const lowerCaseFileNameList = filename.toLowerCase().split('.');
|
|
191
219
|
while (lowerCaseFileNameList.length) {
|
|
192
220
|
lowerCaseFileNameList.shift();
|
|
@@ -194,20 +222,20 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
194
222
|
if (this.uploadConfig.whitelist === null) {
|
|
195
223
|
return (0, utils_1.formatExt)(curExt);
|
|
196
224
|
}
|
|
197
|
-
if (
|
|
225
|
+
if (whiteListMap.has(curExt)) {
|
|
198
226
|
// Avoid the presence of hidden characters and return extensions in the white list.
|
|
199
|
-
return
|
|
227
|
+
return whiteListMap.get(curExt);
|
|
200
228
|
}
|
|
201
229
|
}
|
|
202
230
|
return false;
|
|
203
231
|
}
|
|
204
232
|
// check file-type
|
|
205
|
-
async checkFileType(ext, data) {
|
|
233
|
+
async checkFileType(ext, data, uploadFileMimeTypeMap = this.uploadFileMimeTypeMap) {
|
|
206
234
|
// fileType == null, pass check
|
|
207
235
|
if (!this.uploadConfig.mimeTypeWhiteList) {
|
|
208
236
|
return { passed: true };
|
|
209
237
|
}
|
|
210
|
-
const mime =
|
|
238
|
+
const mime = uploadFileMimeTypeMap.get(ext);
|
|
211
239
|
if (!mime) {
|
|
212
240
|
return { passed: false, mime: ext };
|
|
213
241
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/upload",
|
|
3
|
-
"version": "3.15.
|
|
3
|
+
"version": "3.15.8",
|
|
4
4
|
"description": "Midway Component for upload",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"raw-body": "2.5.2"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@midwayjs/core": "^3.15.
|
|
30
|
-
"@midwayjs/express": "^3.15.
|
|
31
|
-
"@midwayjs/faas": "^3.15.
|
|
32
|
-
"@midwayjs/koa": "^3.15.
|
|
33
|
-
"@midwayjs/mock": "^3.15.
|
|
34
|
-
"@midwayjs/web": "^3.15.
|
|
29
|
+
"@midwayjs/core": "^3.15.8",
|
|
30
|
+
"@midwayjs/express": "^3.15.8",
|
|
31
|
+
"@midwayjs/faas": "^3.15.8",
|
|
32
|
+
"@midwayjs/koa": "^3.15.8",
|
|
33
|
+
"@midwayjs/mock": "^3.15.8",
|
|
34
|
+
"@midwayjs/web": "^3.15.8"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "b9790dce4fac050c747893bc86dda0f044d827a9"
|
|
37
37
|
}
|