|
1 | 1 | import { HttpStatus, Injectable } from '@nestjs/common'; |
2 | | -import { AwsClient } from 'aws4fetch'; |
3 | 2 | import { ConfigService } from '@nestjs/config'; |
4 | 3 | import { Repository } from 'typeorm'; |
5 | 4 | import { File } from './entities/file.entity'; |
6 | 5 | import { InjectRepository } from '@nestjs/typeorm'; |
7 | 6 | import { AppException } from 'omniboxd/common/exceptions/app.exception'; |
8 | 7 | import { I18nService } from 'nestjs-i18n'; |
| 8 | +import { S3Client } from '@aws-sdk/client-s3'; |
| 9 | +import { createPresignedPost, PresignedPost } from '@aws-sdk/s3-presigned-post'; |
| 10 | +import { AwsClient } from 'aws4fetch'; |
| 11 | +import { formatFileSize } from '../utils/format-file-size'; |
9 | 12 |
|
10 | 13 | @Injectable() |
11 | 14 | export class FilesService { |
12 | 15 | private readonly awsClient: AwsClient; |
13 | 16 | private readonly s3Url: URL; |
14 | 17 | private readonly s3InternalUrl: URL; |
| 18 | + private readonly s3Client: S3Client; |
| 19 | + private readonly s3Bucket: string; |
| 20 | + private readonly s3Prefix: string; |
| 21 | + private readonly s3MaxFileSize: number; |
15 | 22 |
|
16 | 23 | constructor( |
17 | 24 | configService: ConfigService, |
@@ -43,9 +50,39 @@ export class FilesService { |
43 | 50 | s3InternalUrl += '/'; |
44 | 51 | } |
45 | 52 |
|
| 53 | + const s3Endpoint = configService.get<string>('OBB_S3_ENDPOINT'); |
| 54 | + if (!s3Endpoint) { |
| 55 | + throw new Error('S3 endpoint not set'); |
| 56 | + } |
| 57 | + |
| 58 | + const s3Bucket = configService.get<string>('OBB_S3_BUCKET'); |
| 59 | + if (!s3Bucket) { |
| 60 | + throw new Error('S3 bucket not set'); |
| 61 | + } |
| 62 | + |
| 63 | + const s3Prefix = configService.get<string>('OBB_S3_PREFIX'); |
| 64 | + if (!s3Prefix) { |
| 65 | + throw new Error('S3 prefix not set'); |
| 66 | + } |
| 67 | + |
46 | 68 | this.awsClient = new AwsClient({ accessKeyId, secretAccessKey }); |
47 | 69 | this.s3Url = new URL(s3Url); |
48 | 70 | this.s3InternalUrl = new URL(s3InternalUrl); |
| 71 | + this.s3MaxFileSize = configService.get<number>( |
| 72 | + 'OBB_S3_MAX_FILE_SIZE', |
| 73 | + 20 * 1024 * 1024, |
| 74 | + ); |
| 75 | + const s3Region = configService.get<string>('OBB_S3_REGION', 'us-east-1'); |
| 76 | + this.s3Client = new S3Client({ |
| 77 | + region: s3Region, |
| 78 | + credentials: { |
| 79 | + accessKeyId, |
| 80 | + secretAccessKey, |
| 81 | + }, |
| 82 | + endpoint: s3Endpoint, |
| 83 | + }); |
| 84 | + this.s3Bucket = s3Bucket; |
| 85 | + this.s3Prefix = s3Prefix; |
49 | 86 | } |
50 | 87 |
|
51 | 88 | async createFile( |
@@ -81,6 +118,38 @@ export class FilesService { |
81 | 118 | return signedReq.url; |
82 | 119 | } |
83 | 120 |
|
| 121 | + async generatePostForm( |
| 122 | + fileId: string, |
| 123 | + fileSize: number | undefined, |
| 124 | + filename: string, |
| 125 | + mimetype: string, |
| 126 | + ): Promise<PresignedPost> { |
| 127 | + if (fileSize && fileSize > this.s3MaxFileSize) { |
| 128 | + const message = this.i18n.t('resource.errors.fileTooLarge', { |
| 129 | + args: { |
| 130 | + userSize: formatFileSize(fileSize), |
| 131 | + limitSize: formatFileSize(this.s3MaxFileSize), |
| 132 | + }, |
| 133 | + }); |
| 134 | + throw new AppException(message, 'FILE_TOO_LARGE', HttpStatus.BAD_REQUEST); |
| 135 | + } |
| 136 | + const disposition = `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`; |
| 137 | + return await createPresignedPost(this.s3Client, { |
| 138 | + Bucket: this.s3Bucket, |
| 139 | + Key: `${this.s3Prefix}/${fileId}`, |
| 140 | + Conditions: [ |
| 141 | + ['content-length-range', 0, this.s3MaxFileSize], |
| 142 | + { 'content-type': mimetype }, |
| 143 | + { 'content-disposition': disposition }, |
| 144 | + ], |
| 145 | + Fields: { |
| 146 | + 'content-type': mimetype, |
| 147 | + 'content-disposition': disposition, |
| 148 | + }, |
| 149 | + Expires: 900, // 900 seconds |
| 150 | + }); |
| 151 | + } |
| 152 | + |
84 | 153 | private async generateDownloadUrl( |
85 | 154 | namespaceId: string, |
86 | 155 | fileId: string, |
|
0 commit comments