-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestController.ts
59 lines (53 loc) · 1.58 KB
/
TestController.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
import { Response, Request, NextFunction } from 'express';
import BaseController from '.';
import Helper from '../../helpers';
import { uploadFileHandler } from '../../decorators/FileHandler';
import { Controller, Get, Post } from '../../decorators';
import logger from '../../utils/logger';
import FileUploadService from '../../services/FileUploadService';
import AWSStorageProvider from '../../services/FileUploadService/AwsStorageProvider';
@Controller('/api/test')
class TestController extends BaseController {
constructor() {
super();
}
@Get('/')
async getIndex(req: any, res: any) {
res.send('Hello, Express with Decorators! kknd');
}
@Post('/file')
@uploadFileHandler({
validationFunction: Helper.requestFileValidation([
'image/jpeg',
'image/png',
'application/pdf',
]),
limit: Helper.convertToBytes(2),
})
async doFile(req: Request, res: Response, next: NextFunction) {
try {
if (Array.isArray(req.files) && req.files.length > 0) {
const file = req.files;
let file_key = `uploads/${Date.now().toString()}-${file[0]?.originalname?.replace(
' ',
'-',
)}`;
let upload = await new FileUploadService().uploadFile(
file[0],
'image',
file_key,
);
return super.sendSuccessResponse(res, { upload, file_key }, '', 200);
}
return super.sendSuccessResponse(
res,
{ ...req.files },
'testing files',
200,
);
} catch (error) {
return next(error);
}
}
}
export default TestController;