/
multipartFile.ts
88 lines (81 loc) · 2.61 KB
/
multipartFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {DecoratorParameters, Metadata, Store, StoreMerge, StoreSet, useDecorators, useMethodDecorators} from "@tsed/core";
import {Consumes, Returns} from "@tsed/schema";
import {PlatformMulterFile} from "../../../config/interfaces/PlatformMulterSettings";
import {Req, Use, UseParamType} from "../../../mvc/decorators";
import {ParamTypes} from "../../../mvc/models/ParamTypes";
import {MulterInputOptions, PlatformMulterMiddleware} from "../../middlewares/PlatformMulterMiddleware";
function mapOptions(name: string, maxCount: number | undefined): MulterInputOptions {
return {
fields: [
{
name,
maxCount
}
]
};
}
/**
* Define a parameter as Multipart file.
*
* ```typescript
* import {Controller, Post} from "@tsed/common";
* import {MulterOptions, MultipartFile} from "@tsed/common";
* import {Multer} from "@types/multer";
*
* type MulterFile = Express.Multer.File;
*
* @Controller('/')
* class MyCtrl {
* @Post('/file')
* private uploadFile(@MultipartFile("file1") file: MulterFile) {
*
* }
*
* @Post('/file')
* @MulterOptions({dest: "/other-dir"})
* private uploadFile(@MultipartFile("file1") file: MulterFile) {
*
* }
*
* @Post('/file2')
* @MulterOptions({dest: "/other-dir"})
* private uploadFile(@MultipartFile("file1") file: MulterFile, @MultipartFile("file2") file2: MulterFile) {
*
* }
*
* @Post('/files')
* private uploadFile(@MultipartFile("file1") files: MulterFile[]) {
*
* }
* }
* ```
*
* > See the tutorial on the [multer configuration](/tutorials/multer.md).
*
* @param name
* @param maxCount
* @returns Function
* @decorator
* @input
*/
export function MultipartFile(name: string, maxCount?: number): ParameterDecorator {
return (...args: DecoratorParameters): void => {
const [target, propertyKey, index] = args;
const multiple = Metadata.getParamTypes(target, propertyKey)[index as number] === Array;
name = (typeof name === "object" ? undefined : name)!;
const expression = [name, !multiple && "0"].filter(Boolean).join(".");
const decorators = useDecorators(
useMethodDecorators(
Returns(400).Description(
`<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field> [fieldName] Example: File too long file1`
),
Consumes("multipart/form-data"),
StoreMerge(PlatformMulterMiddleware, mapOptions(name, maxCount))
),
Req({expression, useValidation: true}),
UseParamType(ParamTypes.FILES)
);
decorators(...args);
};
}
export type MultipartFile = PlatformMulterFile;