|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Get, |
| 5 | + HttpStatus, |
| 6 | + Inject, |
| 7 | + Injectable, |
| 8 | + Post, |
| 9 | + Query, |
| 10 | + UseGuards, |
| 11 | + UseInterceptors, |
| 12 | +} from '@nestjs/common'; |
| 13 | +import { HttpException } from '@nestjs/common/exceptions/http.exception.js'; |
| 14 | +import { ApiBearerAuth, ApiBody, ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger'; |
| 15 | +import { UseCaseType } from '../../common/data-injection.tokens.js'; |
| 16 | +import { MasterPassword, QueryTableName, SlugUuid, UserId } from '../../decorators/index.js'; |
| 17 | +import { InTransactionEnum } from '../../enums/index.js'; |
| 18 | +import { Messages } from '../../exceptions/text/messages.js'; |
| 19 | +import { ConnectionEditGuard, ConnectionReadGuard } from '../../guards/index.js'; |
| 20 | +import { SentryInterceptor } from '../../interceptors/index.js'; |
| 21 | +import { S3FileUrlResponseDs, S3UploadUrlResponseDs } from './application/data-structures/s3-operation.ds.js'; |
| 22 | +import { IGetS3FileUrl, IGetS3UploadUrl } from './use-cases/s3-use-cases.interface.js'; |
| 23 | + |
| 24 | +@UseInterceptors(SentryInterceptor) |
| 25 | +@Controller() |
| 26 | +@ApiBearerAuth() |
| 27 | +@ApiTags('S3 Widget') |
| 28 | +@Injectable() |
| 29 | +export class S3WidgetController { |
| 30 | + constructor( |
| 31 | + @Inject(UseCaseType.GET_S3_FILE_URL) |
| 32 | + private readonly getS3FileUrlUseCase: IGetS3FileUrl, |
| 33 | + @Inject(UseCaseType.GET_S3_UPLOAD_URL) |
| 34 | + private readonly getS3UploadUrlUseCase: IGetS3UploadUrl, |
| 35 | + ) {} |
| 36 | + |
| 37 | + @UseGuards(ConnectionReadGuard) |
| 38 | + @ApiOperation({ summary: 'Get pre-signed URL for S3 file download' }) |
| 39 | + @ApiResponse({ |
| 40 | + status: 200, |
| 41 | + description: 'Pre-signed URL generated successfully.', |
| 42 | + }) |
| 43 | + @ApiQuery({ name: 'tableName', required: true }) |
| 44 | + @ApiQuery({ name: 'fieldName', required: true }) |
| 45 | + @ApiQuery({ name: 'fileKey', required: true }) |
| 46 | + @Get('/s3/file/:connectionId') |
| 47 | + async getFileUrl( |
| 48 | + @SlugUuid('connectionId') connectionId: string, |
| 49 | + @UserId() userId: string, |
| 50 | + @MasterPassword() masterPwd: string, |
| 51 | + @QueryTableName() tableName: string, |
| 52 | + @Query('fieldName') fieldName: string, |
| 53 | + @Query('fileKey') fileKey: string, |
| 54 | + ): Promise<S3FileUrlResponseDs> { |
| 55 | + if (!connectionId) { |
| 56 | + throw new HttpException({ message: Messages.CONNECTION_ID_MISSING }, HttpStatus.BAD_REQUEST); |
| 57 | + } |
| 58 | + if (!fieldName) { |
| 59 | + throw new HttpException({ message: 'Field name is required' }, HttpStatus.BAD_REQUEST); |
| 60 | + } |
| 61 | + if (!fileKey) { |
| 62 | + throw new HttpException({ message: 'File key is required' }, HttpStatus.BAD_REQUEST); |
| 63 | + } |
| 64 | + |
| 65 | + return await this.getS3FileUrlUseCase.execute( |
| 66 | + { |
| 67 | + connectionId, |
| 68 | + tableName, |
| 69 | + fieldName, |
| 70 | + fileKey, |
| 71 | + userId, |
| 72 | + masterPwd, |
| 73 | + }, |
| 74 | + InTransactionEnum.OFF, |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + @UseGuards(ConnectionEditGuard) |
| 79 | + @ApiOperation({ summary: 'Get pre-signed URL for S3 file upload' }) |
| 80 | + @ApiResponse({ |
| 81 | + status: 201, |
| 82 | + description: 'Pre-signed upload URL generated successfully.', |
| 83 | + }) |
| 84 | + @ApiQuery({ name: 'tableName', required: true }) |
| 85 | + @ApiQuery({ name: 'fieldName', required: true }) |
| 86 | + @ApiBody({ |
| 87 | + schema: { |
| 88 | + type: 'object', |
| 89 | + properties: { |
| 90 | + filename: { type: 'string', description: 'Name of the file to upload' }, |
| 91 | + contentType: { type: 'string', description: 'MIME type of the file' }, |
| 92 | + }, |
| 93 | + required: ['filename', 'contentType'], |
| 94 | + }, |
| 95 | + }) |
| 96 | + @Post('/s3/upload-url/:connectionId') |
| 97 | + async getUploadUrl( |
| 98 | + @SlugUuid('connectionId') connectionId: string, |
| 99 | + @UserId() userId: string, |
| 100 | + @MasterPassword() masterPwd: string, |
| 101 | + @QueryTableName() tableName: string, |
| 102 | + @Query('fieldName') fieldName: string, |
| 103 | + @Body() body: { filename: string; contentType: string }, |
| 104 | + ): Promise<S3UploadUrlResponseDs> { |
| 105 | + if (!connectionId) { |
| 106 | + throw new HttpException({ message: Messages.CONNECTION_ID_MISSING }, HttpStatus.BAD_REQUEST); |
| 107 | + } |
| 108 | + if (!fieldName) { |
| 109 | + throw new HttpException({ message: 'Field name is required' }, HttpStatus.BAD_REQUEST); |
| 110 | + } |
| 111 | + if (!body.filename) { |
| 112 | + throw new HttpException({ message: 'Filename is required' }, HttpStatus.BAD_REQUEST); |
| 113 | + } |
| 114 | + if (!body.contentType) { |
| 115 | + throw new HttpException({ message: 'Content type is required' }, HttpStatus.BAD_REQUEST); |
| 116 | + } |
| 117 | + |
| 118 | + return await this.getS3UploadUrlUseCase.execute( |
| 119 | + { |
| 120 | + connectionId, |
| 121 | + tableName, |
| 122 | + fieldName, |
| 123 | + userId, |
| 124 | + masterPwd, |
| 125 | + filename: body.filename, |
| 126 | + contentType: body.contentType, |
| 127 | + }, |
| 128 | + InTransactionEnum.OFF, |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments