|
1 | 1 | import { HttpStatus, Inject, Injectable } from '@nestjs/common'; |
2 | 2 | import { HttpException } from '@nestjs/common/exceptions/http.exception.js'; |
| 3 | +import JSON5 from 'json5'; |
3 | 4 | import AbstractUseCase from '../../../common/abstract-use.case.js'; |
4 | 5 | import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js'; |
5 | 6 | import { BaseType } from '../../../common/data-injection.tokens.js'; |
| 7 | +import { WidgetTypeEnum } from '../../../enums/index.js'; |
6 | 8 | import { Messages } from '../../../exceptions/text/messages.js'; |
7 | 9 | import { Encryptor } from '../../../helpers/encryption/encryptor.js'; |
8 | 10 | import { S3GetUploadUrlDs, S3UploadUrlResponseDs } from '../application/data-structures/s3-operation.ds.js'; |
9 | 11 | import { S3WidgetParams } from '../application/data-structures/s3-widget-params.ds.js'; |
10 | 12 | import { S3HelperService } from '../s3-helper.service.js'; |
11 | 13 | import { IGetS3UploadUrl } from './s3-use-cases.interface.js'; |
12 | | -import { WidgetTypeEnum } from '../../../enums/index.js'; |
13 | | -import JSON5 from 'json5'; |
14 | 14 |
|
15 | 15 | @Injectable() |
16 | 16 | export class GetS3UploadUrlUseCase |
17 | | - extends AbstractUseCase<S3GetUploadUrlDs, S3UploadUrlResponseDs> |
18 | | - implements IGetS3UploadUrl |
| 17 | + extends AbstractUseCase<S3GetUploadUrlDs, S3UploadUrlResponseDs> |
| 18 | + implements IGetS3UploadUrl |
19 | 19 | { |
20 | | - constructor( |
21 | | - @Inject(BaseType.GLOBAL_DB_CONTEXT) |
22 | | - protected _dbContext: IGlobalDatabaseContext, |
23 | | - private readonly s3Helper: S3HelperService, |
24 | | - ) { |
25 | | - super(); |
26 | | - } |
| 20 | + constructor( |
| 21 | + @Inject(BaseType.GLOBAL_DB_CONTEXT) |
| 22 | + protected _dbContext: IGlobalDatabaseContext, |
| 23 | + private readonly s3Helper: S3HelperService, |
| 24 | + ) { |
| 25 | + super(); |
| 26 | + } |
27 | 27 |
|
28 | | - protected async implementation(inputData: S3GetUploadUrlDs): Promise<S3UploadUrlResponseDs> { |
29 | | - const { connectionId, tableName, fieldName, userId, masterPwd, filename, contentType } = inputData; |
| 28 | + protected async implementation(inputData: S3GetUploadUrlDs): Promise<S3UploadUrlResponseDs> { |
| 29 | + const { connectionId, tableName, fieldName, userId, masterPwd, filename, contentType } = inputData; |
30 | 30 |
|
31 | | - const user = await this._dbContext.userRepository.findOneUserByIdWithCompany(userId); |
32 | | - if (!user || !user.company) { |
33 | | - throw new HttpException({ message: Messages.USER_NOT_FOUND_OR_NOT_IN_COMPANY }, HttpStatus.NOT_FOUND); |
34 | | - } |
| 31 | + const user = await this._dbContext.userRepository.findOneUserByIdWithCompany(userId); |
| 32 | + if (!user || !user.company) { |
| 33 | + throw new HttpException({ message: Messages.USER_NOT_FOUND_OR_NOT_IN_COMPANY }, HttpStatus.NOT_FOUND); |
| 34 | + } |
35 | 35 |
|
36 | | - const foundTableWidgets = await this._dbContext.tableWidgetsRepository.findTableWidgets(connectionId, tableName); |
37 | | - const widget = foundTableWidgets.find((w) => w.field_name === fieldName); |
| 36 | + const foundTableWidgets = await this._dbContext.tableWidgetsRepository.findTableWidgets(connectionId, tableName); |
| 37 | + const widget = foundTableWidgets.find((w) => w.field_name === fieldName); |
38 | 38 |
|
39 | | - if (!widget || widget.widget_type !== WidgetTypeEnum.S3) { |
40 | | - throw new HttpException({ message: 'S3 widget not configured for this field' }, HttpStatus.BAD_REQUEST); |
41 | | - } |
| 39 | + if (!widget || widget.widget_type !== WidgetTypeEnum.S3) { |
| 40 | + throw new HttpException({ message: 'S3 widget not configured for this field' }, HttpStatus.BAD_REQUEST); |
| 41 | + } |
42 | 42 |
|
43 | | - const params: S3WidgetParams = |
44 | | - typeof widget.widget_params === 'string' ? JSON5.parse(widget.widget_params) : widget.widget_params; |
| 43 | + const params: S3WidgetParams = |
| 44 | + typeof widget.widget_params === 'string' ? JSON5.parse(widget.widget_params) : widget.widget_params; |
45 | 45 |
|
46 | | - const accessKeySecret = await this._dbContext.userSecretRepository.findSecretBySlugAndCompanyId( |
47 | | - params.aws_access_key_id_secret_name, |
48 | | - user.company.id, |
49 | | - ); |
| 46 | + const accessKeySecret = await this._dbContext.userSecretRepository.findSecretBySlugAndCompanyId( |
| 47 | + params.aws_access_key_id_secret_name, |
| 48 | + user.company.id, |
| 49 | + ); |
50 | 50 |
|
51 | | - const secretKeySecret = await this._dbContext.userSecretRepository.findSecretBySlugAndCompanyId( |
52 | | - params.aws_secret_access_key_secret_name, |
53 | | - user.company.id, |
54 | | - ); |
| 51 | + const secretKeySecret = await this._dbContext.userSecretRepository.findSecretBySlugAndCompanyId( |
| 52 | + params.aws_secret_access_key_secret_name, |
| 53 | + user.company.id, |
| 54 | + ); |
55 | 55 |
|
56 | | - if (!accessKeySecret || !secretKeySecret) { |
57 | | - throw new HttpException({ message: 'AWS credentials secrets not found' }, HttpStatus.NOT_FOUND); |
58 | | - } |
| 56 | + if (!accessKeySecret || !secretKeySecret) { |
| 57 | + throw new HttpException({ message: 'AWS credentials secrets not found' }, HttpStatus.NOT_FOUND); |
| 58 | + } |
59 | 59 |
|
60 | | - let accessKeyId = Encryptor.decryptData(accessKeySecret.encryptedValue); |
61 | | - let secretAccessKey = Encryptor.decryptData(secretKeySecret.encryptedValue); |
| 60 | + let accessKeyId = Encryptor.decryptData(accessKeySecret.encryptedValue); |
| 61 | + let secretAccessKey = Encryptor.decryptData(secretKeySecret.encryptedValue); |
62 | 62 |
|
63 | | - if (accessKeySecret.masterEncryption && masterPwd) { |
64 | | - accessKeyId = Encryptor.decryptDataMasterPwd(accessKeyId, masterPwd); |
65 | | - } |
66 | | - if (secretKeySecret.masterEncryption && masterPwd) { |
67 | | - secretAccessKey = Encryptor.decryptDataMasterPwd(secretAccessKey, masterPwd); |
68 | | - } |
| 63 | + if (accessKeySecret.masterEncryption && masterPwd) { |
| 64 | + accessKeyId = Encryptor.decryptDataMasterPwd(accessKeyId, masterPwd); |
| 65 | + } |
| 66 | + if (secretKeySecret.masterEncryption && masterPwd) { |
| 67 | + secretAccessKey = Encryptor.decryptDataMasterPwd(secretAccessKey, masterPwd); |
| 68 | + } |
69 | 69 |
|
70 | | - const client = this.s3Helper.createS3Client(accessKeyId, secretAccessKey, params.region || 'us-east-1'); |
| 70 | + const client = this.s3Helper.createS3Client(accessKeyId, secretAccessKey, params.region || 'us-east-1'); |
71 | 71 |
|
72 | | - const key = this.s3Helper.generateFileKey(params.prefix, filename); |
73 | | - const expiresIn = 3600; |
74 | | - const uploadUrl = await this.s3Helper.getSignedPutUrl(client, params.bucket, key, contentType, expiresIn); |
| 72 | + const key = this.s3Helper.generateFileKey(params.prefix, filename); |
| 73 | + const expiresIn = 3600; |
| 74 | + const uploadUrl = await this.s3Helper.getSignedPutUrl(client, params.bucket, key, contentType, expiresIn); |
| 75 | + const previewUrl = await this.s3Helper.getSignedGetUrl(client, params.bucket, key, expiresIn); |
75 | 76 |
|
76 | | - return { uploadUrl, key, expiresIn }; |
77 | | - } |
| 77 | + return { uploadUrl, key, expiresIn, previewUrl }; |
| 78 | + } |
78 | 79 | } |
0 commit comments