-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathattachments.controller.ts
99 lines (90 loc) Β· 2.58 KB
/
attachments.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import path from 'path';
import {
Body,
Controller,
Get,
HttpCode,
Param,
Post,
Query,
Request,
Response,
UploadedFiles,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { GlobalGuard } from '../guards/global/global.guard';
import { UploadAllowedInterceptor } from '../interceptors/is-upload-allowed/is-upload-allowed.interceptor';
import { AttachmentsService } from '../services/attachments.service';
@Controller()
export class AttachmentsController {
constructor(private readonly attachmentsService: AttachmentsService) {}
@UseGuards(GlobalGuard)
@Post('/api/v1/db/storage/upload')
@HttpCode(200)
@UseInterceptors(UploadAllowedInterceptor, AnyFilesInterceptor())
async upload(
@UploadedFiles() files: Array<any>,
@Body() body: any,
@Request() req: any,
) {
const attachments = await this.attachmentsService.upload({
files: files,
path: req.query?.path as string,
});
return attachments;
}
@Post('/api/v1/db/storage/upload-by-url')
@HttpCode(200)
@UseInterceptors(UploadAllowedInterceptor)
@UseGuards(GlobalGuard)
async uploadViaURL(@Body() body: any, @Query('path') path: string) {
const attachments = await this.attachmentsService.uploadViaURL({
urls: body,
path,
});
return attachments;
}
// @Get(/^\/download\/(.+)$/)
// , getCacheMiddleware(), catchError(fileRead));
@Get('/download/:filename(*)')
// This route will match any URL that starts with
async fileRead(@Param('filename') filename: string, @Response() res) {
try {
const { img, type } = await this.attachmentsService.fileRead({
path: path.join('nc', 'uploads', filename),
});
res.writeHead(200, { 'Content-Type': type });
res.end(img, 'binary');
} catch (e) {
console.log(e);
res.status(404).send('Not found');
}
}
// @Get(/^\/dl\/([^/]+)\/([^/]+)\/(.+)$/)
@Get('/dl/:param1([a-zA-Z0-9_-]+)/:param2([a-zA-Z0-9_-]+)/:filename(*)')
// getCacheMiddleware(),
async fileReadv2(
@Param('param1') param1: string,
@Param('param2') param2: string,
@Param('filename') filename: string,
@Response() res,
) {
try {
const { img, type } = await this.attachmentsService.fileRead({
path: path.join(
'nc',
param1,
param2,
'uploads',
...filename.split('/'),
),
});
res.writeHead(200, { 'Content-Type': type });
res.end(img, 'binary');
} catch (e) {
res.status(404).send('Not found');
}
}
}