@midwayjs/upload 3.0.0-beta.13 → 3.0.0-beta.14
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 +12 -0
- package/README.md +2 -0
- package/dist/config/config.default.js +3 -32
- package/dist/configuration.d.ts +3 -1
- package/dist/configuration.js +9 -3
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +36 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interface.d.ts +1 -0
- package/dist/middleware.js +26 -6
- package/dist/{upload.d.ts → parse.d.ts} +1 -1
- package/dist/{upload.js → parse.js} +1 -1
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +71 -0
- package/package.json +11 -12
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.14](https://github.com/midwayjs/midway/compare/v3.0.0-beta.13...v3.0.0-beta.14) (2022-01-04)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* cos config definition & 3.x doc update ([#1515](https://github.com/midwayjs/midway/issues/1515)) ([0ac7ac5](https://github.com/midwayjs/midway/commit/0ac7ac5805b7ab8873f8792fc1712a74e3223172))
|
|
12
|
+
* upload support auto clean and whitelist ([#1484](https://github.com/midwayjs/midway/issues/1484)) ([7daa037](https://github.com/midwayjs/midway/commit/7daa0371f4a3e79cd4789959d8427fc5b6c91d08))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
6
18
|
# [3.0.0-beta.13](https://github.com/midwayjs/midway/compare/v3.0.0-beta.12...v3.0.0-beta.13) (2021-12-30)
|
|
7
19
|
|
|
8
20
|
**Note:** Version bump only for package @midwayjs/upload
|
package/README.md
CHANGED
|
@@ -3,41 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.upload = void 0;
|
|
4
4
|
const path_1 = require("path");
|
|
5
5
|
const os_1 = require("os");
|
|
6
|
+
const constants_1 = require("../constants");
|
|
6
7
|
exports.upload = {
|
|
7
8
|
mode: 'file',
|
|
8
9
|
fileSize: '10mb',
|
|
9
|
-
whitelist:
|
|
10
|
-
// images
|
|
11
|
-
'.jpg',
|
|
12
|
-
'.jpeg',
|
|
13
|
-
'.png',
|
|
14
|
-
'.gif',
|
|
15
|
-
'.bmp',
|
|
16
|
-
'.wbmp',
|
|
17
|
-
'.webp',
|
|
18
|
-
'.tif',
|
|
19
|
-
'.psd',
|
|
20
|
-
// text
|
|
21
|
-
'.svg',
|
|
22
|
-
'.js',
|
|
23
|
-
'.jsx',
|
|
24
|
-
'.json',
|
|
25
|
-
'.css',
|
|
26
|
-
'.less',
|
|
27
|
-
'.html',
|
|
28
|
-
'.htm',
|
|
29
|
-
'.xml',
|
|
30
|
-
'.pdf',
|
|
31
|
-
// tar
|
|
32
|
-
'.zip',
|
|
33
|
-
'.gz',
|
|
34
|
-
'.tgz',
|
|
35
|
-
'.gzip',
|
|
36
|
-
// video
|
|
37
|
-
'.mp3',
|
|
38
|
-
'.mp4',
|
|
39
|
-
'.avi',
|
|
40
|
-
],
|
|
10
|
+
whitelist: constants_1.uploadWhiteList,
|
|
41
11
|
tmpdir: (0, path_1.join)((0, os_1.tmpdir)(), 'midway-upload-files'),
|
|
12
|
+
cleanTimeout: 5 * 60 * 1000,
|
|
42
13
|
};
|
|
43
14
|
//# sourceMappingURL=config.default.js.map
|
package/dist/configuration.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { MidwayApplicationManager } from '@midwayjs/core';
|
|
2
|
+
import { UploadOptions } from './interface';
|
|
2
3
|
export declare class UploadConfiguration {
|
|
3
4
|
applicationManager: MidwayApplicationManager;
|
|
4
|
-
uploadConfig:
|
|
5
|
+
uploadConfig: UploadOptions;
|
|
5
6
|
onReady(): Promise<void>;
|
|
7
|
+
onStop(): Promise<void>;
|
|
6
8
|
}
|
|
7
9
|
//# sourceMappingURL=configuration.d.ts.map
|
package/dist/configuration.js
CHANGED
|
@@ -14,12 +14,15 @@ const decorator_1 = require("@midwayjs/decorator");
|
|
|
14
14
|
const DefaultConfig = require("./config/config.default");
|
|
15
15
|
const core_1 = require("@midwayjs/core");
|
|
16
16
|
const middleware_1 = require("./middleware");
|
|
17
|
-
const
|
|
17
|
+
const utils_1 = require("./utils");
|
|
18
18
|
let UploadConfiguration = class UploadConfiguration {
|
|
19
19
|
async onReady() {
|
|
20
|
-
const { tmpdir } = this.uploadConfig;
|
|
20
|
+
const { tmpdir, cleanTimeout } = this.uploadConfig;
|
|
21
21
|
if (tmpdir) {
|
|
22
|
-
await (0,
|
|
22
|
+
await (0, utils_1.ensureDir)(tmpdir);
|
|
23
|
+
if (cleanTimeout) {
|
|
24
|
+
(0, utils_1.autoRemoveUploadTmpFile)(tmpdir, cleanTimeout);
|
|
25
|
+
}
|
|
23
26
|
}
|
|
24
27
|
this.applicationManager
|
|
25
28
|
.getApplications(['koa', 'faas', 'express', 'egg'])
|
|
@@ -27,6 +30,9 @@ let UploadConfiguration = class UploadConfiguration {
|
|
|
27
30
|
app.useMiddleware(middleware_1.UploadMiddleware);
|
|
28
31
|
});
|
|
29
32
|
}
|
|
33
|
+
async onStop() {
|
|
34
|
+
await (0, utils_1.stopAutoRemoveUploadTmpFile)();
|
|
35
|
+
}
|
|
30
36
|
};
|
|
31
37
|
__decorate([
|
|
32
38
|
(0, decorator_1.Inject)(),
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uploadWhiteList = void 0;
|
|
4
|
+
exports.uploadWhiteList = [
|
|
5
|
+
// images
|
|
6
|
+
'.jpg',
|
|
7
|
+
'.jpeg',
|
|
8
|
+
'.png',
|
|
9
|
+
'.gif',
|
|
10
|
+
'.bmp',
|
|
11
|
+
'.wbmp',
|
|
12
|
+
'.webp',
|
|
13
|
+
'.tif',
|
|
14
|
+
'.psd',
|
|
15
|
+
// text
|
|
16
|
+
'.svg',
|
|
17
|
+
'.js',
|
|
18
|
+
'.jsx',
|
|
19
|
+
'.json',
|
|
20
|
+
'.css',
|
|
21
|
+
'.less',
|
|
22
|
+
'.html',
|
|
23
|
+
'.htm',
|
|
24
|
+
'.xml',
|
|
25
|
+
'.pdf',
|
|
26
|
+
// tar
|
|
27
|
+
'.zip',
|
|
28
|
+
'.gz',
|
|
29
|
+
'.tgz',
|
|
30
|
+
'.gzip',
|
|
31
|
+
// video
|
|
32
|
+
'.mp3',
|
|
33
|
+
'.mp4',
|
|
34
|
+
'.avi',
|
|
35
|
+
];
|
|
36
|
+
//# sourceMappingURL=constants.js.map
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,4 +16,5 @@ Object.defineProperty(exports, "Configuration", { enumerable: true, get: functio
|
|
|
16
16
|
__exportStar(require("./interface"), exports);
|
|
17
17
|
__exportStar(require("./middleware"), exports);
|
|
18
18
|
__exportStar(require("./error"), exports);
|
|
19
|
+
__exportStar(require("./constants"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/dist/interface.d.ts
CHANGED
package/dist/middleware.js
CHANGED
|
@@ -15,8 +15,9 @@ const path_1 = require("path");
|
|
|
15
15
|
const fs_1 = require("fs");
|
|
16
16
|
const stream_1 = require("stream");
|
|
17
17
|
const _1 = require(".");
|
|
18
|
-
const
|
|
18
|
+
const parse_1 = require("./parse");
|
|
19
19
|
const getRawBody = require("raw-body");
|
|
20
|
+
const { unlink, writeFile } = fs_1.promises;
|
|
20
21
|
let UploadMiddleware = class UploadMiddleware {
|
|
21
22
|
resolve(app) {
|
|
22
23
|
if (app.getFrameworkType() === decorator_1.MidwayFrameworkType.WEB_EXPRESS) {
|
|
@@ -39,10 +40,29 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
39
40
|
return next();
|
|
40
41
|
}
|
|
41
42
|
ctx.fields = {};
|
|
43
|
+
ctx.files = [];
|
|
44
|
+
ctx.cleanupRequestFiles = async () => {
|
|
45
|
+
var _a;
|
|
46
|
+
if (!((_a = ctx.files) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
return Promise.all(ctx.files.map(async (fileInfo) => {
|
|
50
|
+
if (typeof fileInfo.data !== 'string') {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
await unlink(fileInfo.data);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
catch (_a) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
};
|
|
42
62
|
let body;
|
|
43
63
|
if (this.isReadableStream(req, isExpress)) {
|
|
44
64
|
if (mode === 'stream') {
|
|
45
|
-
const { fields, fileInfo } = await (0,
|
|
65
|
+
const { fields, fileInfo } = await (0, parse_1.parseFromReadableStream)(req, boundary);
|
|
46
66
|
if (!this.checkExt(fileInfo.filename)) {
|
|
47
67
|
throw new _1.MultipartInvalidFilenameError(fileInfo.filename);
|
|
48
68
|
}
|
|
@@ -59,7 +79,7 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
59
79
|
else {
|
|
60
80
|
body = req.body;
|
|
61
81
|
}
|
|
62
|
-
const data = await (0,
|
|
82
|
+
const data = await (0, parse_1.parseMultipart)(body, boundary);
|
|
63
83
|
if (!data) {
|
|
64
84
|
return next();
|
|
65
85
|
}
|
|
@@ -74,12 +94,12 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
74
94
|
if (notCheckFile) {
|
|
75
95
|
throw new _1.MultipartInvalidFilenameError(notCheckFile.filename);
|
|
76
96
|
}
|
|
77
|
-
ctx.files = files.map((file, index) => {
|
|
97
|
+
ctx.files = await Promise.all(files.map(async (file, index) => {
|
|
78
98
|
const { data, filename } = file;
|
|
79
99
|
if (mode === 'file') {
|
|
80
100
|
const ext = (0, path_1.extname)(filename);
|
|
81
101
|
const tmpFileName = (0, path_1.resolve)(tmpdir, `${requireId}.${index}${ext}`);
|
|
82
|
-
|
|
102
|
+
await writeFile(tmpFileName, data, 'binary');
|
|
83
103
|
file.data = tmpFileName;
|
|
84
104
|
}
|
|
85
105
|
else if (mode === 'stream') {
|
|
@@ -91,7 +111,7 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
91
111
|
});
|
|
92
112
|
}
|
|
93
113
|
return file;
|
|
94
|
-
});
|
|
114
|
+
}));
|
|
95
115
|
return next();
|
|
96
116
|
}
|
|
97
117
|
getUploadBoundary(request) {
|
|
@@ -12,4 +12,4 @@ export declare const parseFromReadableStream: (readStream: Readable, boundary: a
|
|
|
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[];
|
|
14
14
|
export declare const parseHead: (headBuf: Buffer) => {};
|
|
15
|
-
//# sourceMappingURL=
|
|
15
|
+
//# sourceMappingURL=parse.d.ts.map
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const autoRemoveUploadTmpFile: (tmpDir: string, cleanTimeout: number) => Promise<void>;
|
|
2
|
+
export declare const stopAutoRemoveUploadTmpFile: () => Promise<void>;
|
|
3
|
+
export declare const checkExists: (path: string) => Promise<boolean>;
|
|
4
|
+
export declare const ensureDir: (dirPath: string) => Promise<boolean>;
|
|
5
|
+
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureDir = exports.checkExists = exports.stopAutoRemoveUploadTmpFile = exports.autoRemoveUploadTmpFile = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const { readdir, access, stat, unlink, mkdir } = fs_1.promises;
|
|
7
|
+
let autoRemoveUploadTmpFileTimeoutHandler;
|
|
8
|
+
let autoRemoveUploadTmpFilePromise;
|
|
9
|
+
const autoRemoveUploadTmpFile = async (tmpDir, cleanTimeout) => {
|
|
10
|
+
clearTimeout(autoRemoveUploadTmpFileTimeoutHandler);
|
|
11
|
+
let waitTime = cleanTimeout / 3;
|
|
12
|
+
if (waitTime < 1000) {
|
|
13
|
+
waitTime = 1000;
|
|
14
|
+
}
|
|
15
|
+
if (autoRemoveUploadTmpFilePromise) {
|
|
16
|
+
const exists = await (0, exports.checkExists)(tmpDir);
|
|
17
|
+
if (exists) {
|
|
18
|
+
const paths = await readdir(tmpDir);
|
|
19
|
+
const now = Date.now();
|
|
20
|
+
await Promise.all(paths.map(async (path) => {
|
|
21
|
+
const filePath = (0, path_1.join)(tmpDir, path);
|
|
22
|
+
try {
|
|
23
|
+
const statInfo = await stat(filePath);
|
|
24
|
+
if (statInfo.isFile() && now - statInfo.ctimeMs > cleanTimeout) {
|
|
25
|
+
await unlink(filePath);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch (_a) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
autoRemoveUploadTmpFileTimeoutHandler = setTimeout(() => {
|
|
35
|
+
autoRemoveUploadTmpFilePromise = (0, exports.autoRemoveUploadTmpFile)(tmpDir, cleanTimeout);
|
|
36
|
+
}, waitTime);
|
|
37
|
+
};
|
|
38
|
+
exports.autoRemoveUploadTmpFile = autoRemoveUploadTmpFile;
|
|
39
|
+
const stopAutoRemoveUploadTmpFile = async () => {
|
|
40
|
+
if (autoRemoveUploadTmpFilePromise) {
|
|
41
|
+
await autoRemoveUploadTmpFilePromise;
|
|
42
|
+
autoRemoveUploadTmpFilePromise = null;
|
|
43
|
+
}
|
|
44
|
+
clearTimeout(autoRemoveUploadTmpFileTimeoutHandler);
|
|
45
|
+
};
|
|
46
|
+
exports.stopAutoRemoveUploadTmpFile = stopAutoRemoveUploadTmpFile;
|
|
47
|
+
const checkExists = async (path) => {
|
|
48
|
+
try {
|
|
49
|
+
await access(path, fs_1.constants.W_OK | fs_1.constants.R_OK);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
catch (_a) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.checkExists = checkExists;
|
|
57
|
+
const ensureDir = async (dirPath) => {
|
|
58
|
+
const isExists = await (0, exports.checkExists)(dirPath);
|
|
59
|
+
if (isExists) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
await mkdir(dirPath, { recursive: true });
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
catch (_a) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
exports.ensureDir = ensureDir;
|
|
71
|
+
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/upload",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.14",
|
|
4
4
|
"description": "Midway Component for upload",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -21,18 +21,17 @@
|
|
|
21
21
|
},
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"
|
|
25
|
-
"raw-body": "^2.4.1"
|
|
24
|
+
"raw-body": "2.4.2"
|
|
26
25
|
},
|
|
27
26
|
"devDependencies": {
|
|
28
|
-
"@midwayjs/core": "^3.0.0-beta.
|
|
29
|
-
"@midwayjs/decorator": "^3.0.0-beta.
|
|
30
|
-
"@midwayjs/express": "^3.0.0-beta.
|
|
31
|
-
"@midwayjs/faas": "^3.0.0-beta.
|
|
32
|
-
"@midwayjs/koa": "^3.0.0-beta.
|
|
33
|
-
"@midwayjs/mock": "^3.0.0-beta.
|
|
34
|
-
"@midwayjs/serverless-app": "^3.0.0-beta.
|
|
35
|
-
"@midwayjs/web": "^3.0.0-beta.
|
|
27
|
+
"@midwayjs/core": "^3.0.0-beta.14",
|
|
28
|
+
"@midwayjs/decorator": "^3.0.0-beta.14",
|
|
29
|
+
"@midwayjs/express": "^3.0.0-beta.14",
|
|
30
|
+
"@midwayjs/faas": "^3.0.0-beta.14",
|
|
31
|
+
"@midwayjs/koa": "^3.0.0-beta.14",
|
|
32
|
+
"@midwayjs/mock": "^3.0.0-beta.14",
|
|
33
|
+
"@midwayjs/serverless-app": "^3.0.0-beta.14",
|
|
34
|
+
"@midwayjs/web": "^3.0.0-beta.14"
|
|
36
35
|
},
|
|
37
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "95695ada36190cf376e93a1a8866b3fb6dde06bc"
|
|
38
37
|
}
|