@midwayjs/upload 3.0.0-beta.9 → 3.0.0
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/LICENSE +21 -0
- package/{readme.md → README.md} +7 -15
- package/dist/config/config.default.js +4 -34
- 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/error.d.ts +5 -0
- package/dist/error.js +11 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/interface.d.ts +5 -8
- package/dist/interface.js +0 -7
- package/dist/middleware.d.ts +1 -0
- package/dist/middleware.js +46 -40
- package/dist/{upload.d.ts → parse.d.ts} +2 -2
- package/dist/{upload.js → parse.js} +4 -4
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +71 -0
- package/index.d.ts +39 -0
- package/package.json +17 -16
- package/CHANGELOG.md +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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.
|
package/{readme.md → README.md}
RENAMED
|
@@ -10,21 +10,12 @@ tnpm i @midwayjs/upload --save
|
|
|
10
10
|
```
|
|
11
11
|
2. 在 configuration 中引入组件,
|
|
12
12
|
```ts
|
|
13
|
-
import * as upload from '
|
|
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
|
-
|
|
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
|
-
|
|
44
|
+
fieldName: 'test2', // 表单 field 名
|
|
55
45
|
mimeType: 'application/pdf', // mime
|
|
56
46
|
},
|
|
57
47
|
// ...file 下支持同时上传多个文件
|
|
@@ -78,5 +68,7 @@ export const upload = {
|
|
|
78
68
|
whitelist: null,
|
|
79
69
|
// tmpdir: string,上传的文件临时存储路径
|
|
80
70
|
tmpdir: join(tmpdir(), 'midway-upload-files'),
|
|
71
|
+
// cleanTimeout: number,上传的文件在临时目录中多久之后自动删除,默认为 5 分钟
|
|
72
|
+
cleanTimeout: 5 * 60 * 1000,
|
|
81
73
|
}
|
|
82
|
-
```
|
|
74
|
+
```
|
|
@@ -1,44 +1,14 @@
|
|
|
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");
|
|
6
|
+
const constants_1 = require("../constants");
|
|
7
7
|
exports.upload = {
|
|
8
|
-
mode:
|
|
8
|
+
mode: 'file',
|
|
9
9
|
fileSize: '10mb',
|
|
10
|
-
whitelist:
|
|
11
|
-
// images
|
|
12
|
-
'.jpg',
|
|
13
|
-
'.jpeg',
|
|
14
|
-
'.png',
|
|
15
|
-
'.gif',
|
|
16
|
-
'.bmp',
|
|
17
|
-
'.wbmp',
|
|
18
|
-
'.webp',
|
|
19
|
-
'.tif',
|
|
20
|
-
'.psd',
|
|
21
|
-
// text
|
|
22
|
-
'.svg',
|
|
23
|
-
'.js',
|
|
24
|
-
'.jsx',
|
|
25
|
-
'.json',
|
|
26
|
-
'.css',
|
|
27
|
-
'.less',
|
|
28
|
-
'.html',
|
|
29
|
-
'.htm',
|
|
30
|
-
'.xml',
|
|
31
|
-
'.pdf',
|
|
32
|
-
// tar
|
|
33
|
-
'.zip',
|
|
34
|
-
'.gz',
|
|
35
|
-
'.tgz',
|
|
36
|
-
'.gzip',
|
|
37
|
-
// video
|
|
38
|
-
'.mp3',
|
|
39
|
-
'.mp4',
|
|
40
|
-
'.avi',
|
|
41
|
-
],
|
|
10
|
+
whitelist: constants_1.uploadWhiteList,
|
|
42
11
|
tmpdir: (0, path_1.join)((0, os_1.tmpdir)(), 'midway-upload-files'),
|
|
12
|
+
cleanTimeout: 5 * 60 * 1000,
|
|
43
13
|
};
|
|
44
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/error.d.ts
ADDED
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
package/dist/index.js
CHANGED
|
@@ -15,4 +15,6 @@ 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);
|
|
19
|
+
__exportStar(require("./constants"), exports);
|
|
18
20
|
//# sourceMappingURL=index.js.map
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Readable } from "stream";
|
|
3
|
-
export declare
|
|
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;
|
|
9
|
+
cleanTimeout?: number;
|
|
13
10
|
}
|
|
14
|
-
export interface UploadFileInfo {
|
|
11
|
+
export interface UploadFileInfo<T> {
|
|
15
12
|
filename: string;
|
|
16
|
-
|
|
13
|
+
fieldName: string;
|
|
17
14
|
mimeType: string;
|
|
18
|
-
data:
|
|
15
|
+
data: T extends string ? string : Readable;
|
|
19
16
|
}
|
|
20
17
|
//# 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
|
package/dist/middleware.d.ts
CHANGED
|
@@ -8,5 +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
|
+
static getName(): string;
|
|
11
12
|
}
|
|
12
13
|
//# sourceMappingURL=middleware.d.ts.map
|
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,19 +40,31 @@ 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
|
-
if (mode ===
|
|
45
|
-
const { fields, fileInfo } = await (0,
|
|
64
|
+
if (mode === 'stream') {
|
|
65
|
+
const { fields, fileInfo } = await (0, parse_1.parseFromReadableStream)(req, boundary);
|
|
46
66
|
if (!this.checkExt(fileInfo.filename)) {
|
|
47
|
-
|
|
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;
|
|
67
|
+
throw new _1.MultipartInvalidFilenameError(fileInfo.filename);
|
|
55
68
|
}
|
|
56
69
|
else {
|
|
57
70
|
ctx.fields = fields;
|
|
@@ -66,7 +79,7 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
66
79
|
else {
|
|
67
80
|
body = req.body;
|
|
68
81
|
}
|
|
69
|
-
const data = await (0,
|
|
82
|
+
const data = await (0, parse_1.parseMultipart)(body, boundary);
|
|
70
83
|
if (!data) {
|
|
71
84
|
return next();
|
|
72
85
|
}
|
|
@@ -79,36 +92,26 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
79
92
|
}
|
|
80
93
|
});
|
|
81
94
|
if (notCheckFile) {
|
|
82
|
-
|
|
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;
|
|
95
|
+
throw new _1.MultipartInvalidFilenameError(notCheckFile.filename);
|
|
90
96
|
}
|
|
91
|
-
ctx.files =
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
this.push(data);
|
|
106
|
-
this.push(null);
|
|
107
|
-
},
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
return file;
|
|
97
|
+
ctx.files = await Promise.all(files.map(async (file, index) => {
|
|
98
|
+
const { data, filename } = file;
|
|
99
|
+
if (mode === 'file') {
|
|
100
|
+
const ext = (0, path_1.extname)(filename);
|
|
101
|
+
const tmpFileName = (0, path_1.resolve)(tmpdir, `${requireId}.${index}${ext}`);
|
|
102
|
+
await writeFile(tmpFileName, data, 'binary');
|
|
103
|
+
file.data = tmpFileName;
|
|
104
|
+
}
|
|
105
|
+
else if (mode === 'stream') {
|
|
106
|
+
file.data = new stream_1.Readable({
|
|
107
|
+
read() {
|
|
108
|
+
this.push(data);
|
|
109
|
+
this.push(null);
|
|
110
|
+
},
|
|
111
111
|
});
|
|
112
|
+
}
|
|
113
|
+
return file;
|
|
114
|
+
}));
|
|
112
115
|
return next();
|
|
113
116
|
}
|
|
114
117
|
getUploadBoundary(request) {
|
|
@@ -148,6 +151,9 @@ let UploadMiddleware = class UploadMiddleware {
|
|
|
148
151
|
}
|
|
149
152
|
return whitelist.includes(ext);
|
|
150
153
|
}
|
|
154
|
+
static getName() {
|
|
155
|
+
return 'upload';
|
|
156
|
+
}
|
|
151
157
|
};
|
|
152
158
|
__decorate([
|
|
153
159
|
(0, decorator_1.Config)('upload'),
|
|
@@ -7,9 +7,9 @@ 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[];
|
|
14
14
|
export declare const parseHead: (headBuf: Buffer) => {};
|
|
15
|
-
//# sourceMappingURL=
|
|
15
|
+
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -25,7 +25,7 @@ const parseMultipart = async (body, boundary) => {
|
|
|
25
25
|
files.push({
|
|
26
26
|
filename: head['content-disposition'].filename,
|
|
27
27
|
data,
|
|
28
|
-
|
|
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
|
-
|
|
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.
|
|
111
|
+
fileInfo.fieldName = head['content-disposition'].name;
|
|
112
112
|
fileInfo.mimeType = head['content-type'];
|
|
113
113
|
isTransformFileData = true;
|
|
114
114
|
lastChunk = data;
|
|
@@ -186,4 +186,4 @@ const parseHead = (headBuf) => {
|
|
|
186
186
|
return head;
|
|
187
187
|
};
|
|
188
188
|
exports.parseHead = parseHead;
|
|
189
|
-
//# sourceMappingURL=
|
|
189
|
+
//# sourceMappingURL=parse.js.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/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { UploadFileInfo, UploadOptions } from './dist/index';
|
|
2
|
+
export * from './dist/index';
|
|
3
|
+
|
|
4
|
+
declare module '@midwayjs/core/dist/interface' {
|
|
5
|
+
interface MidwayConfig {
|
|
6
|
+
upload: Partial<UploadOptions>;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
declare module '@midwayjs/koa/dist/interface' {
|
|
10
|
+
interface Context {
|
|
11
|
+
files?: UploadFileInfo<any>[];
|
|
12
|
+
fields?: { [fieldName: string]: any };
|
|
13
|
+
cleanupRequestFiles?: () => Promise<Array<boolean>>;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare module '@midwayjs/web/dist/interface' {
|
|
18
|
+
interface Context {
|
|
19
|
+
files?: UploadFileInfo<any>[];
|
|
20
|
+
fields?: { [fieldName: string]: any };
|
|
21
|
+
cleanupRequestFiles?: () => Promise<Array<boolean>>;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare module '@midwayjs/faas/dist/interface' {
|
|
26
|
+
interface Context {
|
|
27
|
+
files?: UploadFileInfo<any>[];
|
|
28
|
+
fields?: { [fieldName: string]: any };
|
|
29
|
+
cleanupRequestFiles?: () => Promise<Array<boolean>>;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare module '@midwayjs/express/dist/interface' {
|
|
34
|
+
interface Context {
|
|
35
|
+
files?: UploadFileInfo<any>[];
|
|
36
|
+
fields?: { [fieldName: string]: any };
|
|
37
|
+
cleanupRequestFiles?: () => Promise<Array<boolean>>;
|
|
38
|
+
}
|
|
39
|
+
}
|
package/package.json
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/upload",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Midway Component for upload",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"typings": "
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
|
-
"test": "node --require=ts-node/register ../../node_modules/.bin/jest",
|
|
10
|
-
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit",
|
|
9
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
10
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
|
|
11
11
|
"ci": "npm run test"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [],
|
|
14
14
|
"author": "",
|
|
15
15
|
"files": [
|
|
16
16
|
"dist/**/*.js",
|
|
17
|
-
"dist/**/*.d.ts"
|
|
17
|
+
"dist/**/*.d.ts",
|
|
18
|
+
"index.d.ts"
|
|
18
19
|
],
|
|
19
20
|
"engines": {
|
|
20
21
|
"node": ">=12"
|
|
21
22
|
},
|
|
22
23
|
"license": "MIT",
|
|
23
24
|
"dependencies": {
|
|
24
|
-
"
|
|
25
|
-
"raw-body": "^2.4.1"
|
|
25
|
+
"raw-body": "2.4.2"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@midwayjs/core": "^3.0.0
|
|
29
|
-
"@midwayjs/decorator": "^3.0.0
|
|
30
|
-
"@midwayjs/
|
|
31
|
-
"@midwayjs/faas": "^3.0.0
|
|
32
|
-
"@midwayjs/
|
|
33
|
-
"@midwayjs/
|
|
34
|
-
"@midwayjs/
|
|
35
|
-
"@midwayjs/
|
|
36
|
-
}
|
|
28
|
+
"@midwayjs/core": "^3.0.0",
|
|
29
|
+
"@midwayjs/decorator": "^3.0.0",
|
|
30
|
+
"@midwayjs/express": "^3.0.0",
|
|
31
|
+
"@midwayjs/faas": "^3.0.0",
|
|
32
|
+
"@midwayjs/koa": "^3.0.0",
|
|
33
|
+
"@midwayjs/mock": "^3.0.0",
|
|
34
|
+
"@midwayjs/serverless-app": "^3.0.0",
|
|
35
|
+
"@midwayjs/web": "^3.0.0"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "55c26029bccf7bbb739fa1597e8f418dafa2caa0"
|
|
37
38
|
}
|
package/CHANGELOG.md
DELETED
|
File without changes
|