|
| 1 | +import { HttpStatus, Injectable } from '@nestjs/common'; |
| 2 | +import { AwsClient } from 'aws4fetch'; |
| 3 | +import { ConfigService } from '@nestjs/config'; |
| 4 | +import { Repository } from 'typeorm'; |
| 5 | +import { File } from './entities/file.entity'; |
| 6 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 7 | +import { AppException } from 'omniboxd/common/exceptions/app.exception'; |
| 8 | +import { I18nService } from 'nestjs-i18n'; |
| 9 | + |
| 10 | +@Injectable() |
| 11 | +export class FilesService { |
| 12 | + private readonly awsClient: AwsClient; |
| 13 | + private readonly s3Url: URL; |
| 14 | + private readonly s3InternalUrl: URL; |
| 15 | + |
| 16 | + constructor( |
| 17 | + configService: ConfigService, |
| 18 | + |
| 19 | + @InjectRepository(File) |
| 20 | + private readonly fileRepo: Repository<File>, |
| 21 | + private readonly i18n: I18nService, |
| 22 | + ) { |
| 23 | + const accessKeyId = configService.get<string>('OBB_S3_ACCESS_KEY_ID'); |
| 24 | + const secretAccessKey = configService.get<string>( |
| 25 | + 'OBB_S3_SECRET_ACCESS_KEY', |
| 26 | + ); |
| 27 | + if (!accessKeyId || !secretAccessKey) { |
| 28 | + throw new Error('S3 credentials not set'); |
| 29 | + } |
| 30 | + |
| 31 | + let s3Url = configService.get<string>('OBB_S3_URL'); |
| 32 | + if (!s3Url) { |
| 33 | + throw new Error('S3 URL not set'); |
| 34 | + } |
| 35 | + if (!s3Url.endsWith('/')) { |
| 36 | + s3Url += '/'; |
| 37 | + } |
| 38 | + |
| 39 | + let s3InternalUrl = configService.get<string>('OBB_S3_INTERNAL_URL'); |
| 40 | + if (!s3InternalUrl) { |
| 41 | + s3InternalUrl = s3Url; |
| 42 | + } else if (!s3InternalUrl.endsWith('/')) { |
| 43 | + s3InternalUrl += '/'; |
| 44 | + } |
| 45 | + |
| 46 | + this.awsClient = new AwsClient({ accessKeyId, secretAccessKey }); |
| 47 | + this.s3Url = new URL(s3Url); |
| 48 | + this.s3InternalUrl = new URL(s3InternalUrl); |
| 49 | + } |
| 50 | + |
| 51 | + async createFile( |
| 52 | + userId: string, |
| 53 | + namespaceId: string, |
| 54 | + filename: string, |
| 55 | + mimetype: string, |
| 56 | + ): Promise<File> { |
| 57 | + return await this.fileRepo.save( |
| 58 | + this.fileRepo.create({ |
| 59 | + namespaceId, |
| 60 | + userId, |
| 61 | + name: filename, |
| 62 | + mimetype, |
| 63 | + }), |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + async getFile(namespaceId: string, fileId: string): Promise<File | null> { |
| 68 | + return await this.fileRepo.findOne({ where: { namespaceId, id: fileId } }); |
| 69 | + } |
| 70 | + |
| 71 | + async generateUploadUrl(fileId: string): Promise<string> { |
| 72 | + const fileUrl = new URL(fileId, this.s3Url); |
| 73 | + fileUrl.searchParams.set('X-Amz-Expires', '900'); // 900 seconds |
| 74 | + const signedReq = await this.awsClient.sign(fileUrl.toString(), { |
| 75 | + method: 'PUT', |
| 76 | + aws: { |
| 77 | + service: 's3', |
| 78 | + signQuery: true, |
| 79 | + }, |
| 80 | + }); |
| 81 | + return signedReq.url; |
| 82 | + } |
| 83 | + |
| 84 | + private async generateDownloadUrl( |
| 85 | + namespaceId: string, |
| 86 | + fileId: string, |
| 87 | + s3Url: URL, |
| 88 | + ): Promise<string> { |
| 89 | + const file = await this.getFile(namespaceId, fileId); |
| 90 | + if (!file) { |
| 91 | + const message = this.i18n.t('resource.errors.fileNotFound'); |
| 92 | + throw new AppException(message, 'FILE_NOT_FOUND', HttpStatus.NOT_FOUND); |
| 93 | + } |
| 94 | + |
| 95 | + const fileUrl = new URL(fileId, s3Url); |
| 96 | + fileUrl.searchParams.set('X-Amz-Expires', '900'); // 900 seconds |
| 97 | + fileUrl.searchParams.set( |
| 98 | + 'response-content-disposition', |
| 99 | + `attachment; filename*=UTF-8''${encodeURIComponent(file.name)}`, |
| 100 | + ); |
| 101 | + |
| 102 | + const signedReq = await this.awsClient.sign(fileUrl.toString(), { |
| 103 | + method: 'GET', |
| 104 | + aws: { |
| 105 | + service: 's3', |
| 106 | + signQuery: true, |
| 107 | + }, |
| 108 | + }); |
| 109 | + return signedReq.url; |
| 110 | + } |
| 111 | + |
| 112 | + async generatePublicDownloadUrl( |
| 113 | + namespaceId: string, |
| 114 | + fileId: string, |
| 115 | + ): Promise<string> { |
| 116 | + return this.generateDownloadUrl(namespaceId, fileId, this.s3Url); |
| 117 | + } |
| 118 | + |
| 119 | + async generateInternalDownloadUrl( |
| 120 | + namespaceId: string, |
| 121 | + fileId: string, |
| 122 | + ): Promise<string> { |
| 123 | + return this.generateDownloadUrl(namespaceId, fileId, this.s3InternalUrl); |
| 124 | + } |
| 125 | +} |
0 commit comments