diff --git a/src/config/configuration.interface.ts b/src/config/configuration.interface.ts index c21ef5cc2..f2b4e7b04 100644 --- a/src/config/configuration.interface.ts +++ b/src/config/configuration.interface.ts @@ -99,6 +99,8 @@ export interface Configuration { accessKeyId: string; secretAccessKey: string; region: string; + profilePictureBucket?: string; + profilePictureCdnHostname?: string; }; cloudinary: { diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 9864e3a6a..52aa80161 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -124,6 +124,9 @@ const configuration: Configuration = { accessKeyId: process.env.AWS_S3_ACCESS_KEY ?? '', secretAccessKey: process.env.AWS_S3_SECRET_KEY ?? '', region: process.env.AWS_S3_REGION ?? '', + profilePictureBucket: process.env.AWS_S3_PROFILE_PICTURE_BUCKET ?? '', + profilePictureCdnHostname: + process.env.AWS_S3_PROFILE_PICTURE_CDN_HOST_NAME ?? '', }, cloudinary: { cloudName: process.env.CLOUDINARY_CLOUD_NAME ?? '', diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index d3f85085c..8dae05476 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -1,6 +1,7 @@ import { BadRequestException, Injectable, + InternalServerErrorException, NotFoundException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @@ -189,20 +190,24 @@ export class UsersService { file: Files[0], ): Promise> { if (file.size > 25000000) throw new Error(FILE_TOO_LARGE); + if (!this.configService.get('s3.profilePictureBucket')) + throw new InternalServerErrorException('Profile picture bucket not set'); const { Location } = await this.s3Service.upload( `picture-${id}-${this.tokensService.generateUuid()}${extname( file.originalname, )}`, file.buffer, - 'koj-user-uploads', + this.configService.get('s3.profilePictureBucket'), true, ); return this.prisma.user.update({ where: { id }, data: { profilePictureUrl: Location.replace( - 'koj-user-uploads.s3.eu-central-1.amazonaws.com', - 'd1ykbyudrr6gx9.cloudfront.net', + `${this.configService.get( + 's3.profilePictureBucket', + )}.s3.${this.configService.get('s3.region')}.amazonaws.com`, + this.configService.get('s3.profilePictureCdnHostname'), ), }, });