Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Add config for S3 profile picture
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 9, 2021
1 parent 54deaf5 commit 4d48402
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export interface Configuration {
accessKeyId: string;
secretAccessKey: string;
region: string;
profilePictureBucket?: string;
profilePictureCdnHostname?: string;
};

cloudinary: {
Expand Down
3 changes: 3 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '',
Expand Down
11 changes: 8 additions & 3 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BadRequestException,
Injectable,
InternalServerErrorException,
NotFoundException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
Expand Down Expand Up @@ -189,20 +190,24 @@ export class UsersService {
file: Files[0],
): Promise<Expose<User>> {
if (file.size > 25000000) throw new Error(FILE_TOO_LARGE);
if (!this.configService.get<string>('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<string>('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<string>(
's3.profilePictureBucket',
)}.s3.${this.configService.get<string>('s3.region')}.amazonaws.com`,
this.configService.get<string>('s3.profilePictureCdnHostname'),
),
},
});
Expand Down

0 comments on commit 4d48402

Please sign in to comment.