This repository was archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers-files.controller.ts
More file actions
102 lines (93 loc) · 4.62 KB
/
Copy pathusers-files.controller.ts
File metadata and controls
102 lines (93 loc) · 4.62 KB
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
100
101
102
import { Controller, Delete, Get, Param, Post, Req, UploadedFile, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { GuardPermissions } from '@modules/auth/decorators/permissions.decorator';
import { GuardSelfOrPermissions } from '@modules/auth/decorators/self-or-perms.decorator';
import { PermissionGuard } from '@modules/auth/guards/permission.guard';
import { SelfOrPermissionGuard } from '@modules/auth/guards/self-or-perms.guard';
import { ApiNotOkResponses } from '@modules/base/decorators/api-not-ok.decorator';
import { InputIdParamDTO } from '@modules/base/dto/input.dto';
import { OutputMessageDTO } from '@modules/base/dto/output.dto';
import { i18nBadRequestException } from '@modules/base/http-errors';
import { ApiDownloadFile } from '@modules/files/decorators/download.decorator';
import { ApiUploadFile } from '@modules/files/decorators/upload.decorator';
import { OutputFileDTO } from '@modules/files/dto/output.dto';
import { FilesService } from '@modules/files/files.service';
import { Request } from '../entities/user.entity';
import { UsersFilesService } from '../services/users-files.service';
@ApiTags('Users Files')
@Controller('users')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class UsersFilesController {
constructor(private readonly usersFilesService: UsersFilesService, private readonly filesService: FilesService) {}
@Post(':id/picture')
@UseGuards(SelfOrPermissionGuard)
@GuardSelfOrPermissions('id', ['CAN_EDIT_USER'])
@ApiOperation({ summary: 'Update user profile picture' })
@ApiParam({ name: 'id', description: 'The user ID' })
@ApiOkResponse({ description: 'The updated user picture', type: OutputFileDTO })
@ApiNotOkResponses({ 400: 'Invalid user ID or missing uploaded file', 404: 'User not found' })
@ApiUploadFile()
async editPicture(
@Req() req: Request,
@Param() params: InputIdParamDTO,
@UploadedFile() file: Express.Multer.File,
): Promise<OutputFileDTO> {
if (!file) throw new i18nBadRequestException('validations.file.invalid.not_provided');
return this.usersFilesService.updatePicture(req.user, params.id, file);
}
@Delete(':id/picture')
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_EDIT_USER')
@ApiOperation({ summary: 'Delete user profile picture' })
@ApiParam({ name: 'id', description: 'The user ID' })
@ApiOkResponse({ type: OutputMessageDTO })
@ApiNotOkResponses({ 400: 'Invalid ID', 404: 'User not found' })
async deletePicture(@Param() params: InputIdParamDTO): Promise<OutputMessageDTO> {
return this.usersFilesService.deletePicture(params.id);
}
@Get(':id/picture')
@ApiOperation({ summary: 'Get user profile picture' })
@ApiParam({ name: 'id', description: 'The user ID' })
@ApiNotOkResponses({ 400: 'Invalid ID', 404: 'User not found' })
@ApiDownloadFile()
async getPicture(@Req() req: Request, @Param() params: InputIdParamDTO) {
const picture = await this.usersFilesService.getPicture(params.id);
return this.filesService.getAsStreamable(picture, req.user.id);
}
@Post(':id/banner')
@UseGuards(SelfOrPermissionGuard)
@GuardSelfOrPermissions('id', ['CAN_EDIT_USER'])
@ApiOperation({ summary: 'Update user profile banner' })
@ApiParam({ name: 'id', description: 'The user ID' })
@ApiOkResponse({ description: 'The updated user banner', type: OutputFileDTO })
@ApiNotOkResponses({ 400: 'Invalid user ID or missing uploaded file', 404: 'User not found' })
@ApiUploadFile()
async editBanner(
@Param() params: InputIdParamDTO,
@UploadedFile() file: Express.Multer.File,
): Promise<OutputFileDTO> {
if (!file) throw new i18nBadRequestException('validations.file.invalid.not_provided');
return this.usersFilesService.updateBanner(params.id, file);
}
@Delete(':id/banner')
@UseGuards(SelfOrPermissionGuard)
@GuardSelfOrPermissions('id', ['CAN_EDIT_USER'])
@ApiOperation({ summary: 'Delete user profile banner' })
@ApiParam({ name: 'id', description: 'The user ID' })
@ApiOkResponse({ type: OutputMessageDTO })
@ApiNotOkResponses({ 400: 'Invalid ID', 404: 'User not found' })
async deleteBanner(@Param() params: InputIdParamDTO): Promise<OutputMessageDTO> {
return this.usersFilesService.deleteBanner(params.id);
}
@Get(':id/banner')
@ApiOperation({ summary: 'Get user profile banner' })
@ApiParam({ name: 'id', description: 'The user ID' })
@ApiNotOkResponses({ 400: 'Invalid ID', 404: 'User not found' })
@ApiDownloadFile()
async getBanner(@Req() req: Request, @Param() params: InputIdParamDTO) {
const banner = await this.usersFilesService.getBanner(params.id);
return this.filesService.getAsStreamable(banner, req.user.id);
}
}