forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.controller.ts
75 lines (70 loc) 路 1.51 KB
/
app.controller.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
import {
Body,
Controller,
Get,
ParseFilePipeBuilder,
Post,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Express } from 'express';
import { AppService } from './app.service';
import { SampleDto } from './sample.dto';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
sayHello() {
return this.appService.getHello();
}
@UseInterceptors(FileInterceptor('file'))
@Post('file')
uploadFile(
@Body() body: SampleDto,
@UploadedFile() file: Express.Multer.File,
) {
return {
body,
file: file.buffer.toString(),
};
}
@UseInterceptors(FileInterceptor('file'))
@Post('file/pass-validation')
uploadFileAndPassValidation(
@Body() body: SampleDto,
@UploadedFile(
new ParseFilePipeBuilder()
.addFileTypeValidator({
fileType: 'json',
})
.build({
fileIsRequired: false,
}),
)
file?: Express.Multer.File,
) {
return {
body,
file: file?.buffer.toString(),
};
}
@UseInterceptors(FileInterceptor('file'))
@Post('file/fail-validation')
uploadFileAndFailValidation(
@Body() body: SampleDto,
@UploadedFile(
new ParseFilePipeBuilder()
.addFileTypeValidator({
fileType: 'jpg',
})
.build(),
)
file: Express.Multer.File,
) {
return {
body,
file: file.buffer.toString(),
};
}
}