Skip to content

Commit

Permalink
fix: check if image exists before setting it (#636)
Browse files Browse the repository at this point in the history
Co-authored-by: orig <[email protected]>
  • Loading branch information
origranot and orig authored Dec 24, 2023
1 parent 78a8bad commit 72e1083
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion apps/backend/src/auth/providers/providers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export class ProvidersController {
}

if (req.user.picture && this.configService.getConfig().storage.enable) {
await this.storageService.uploadImageFromUrl(req.user.picture, `${PROFILE_PICTURE_PREFIX}/${user.id}`);
const imagePath = `${PROFILE_PICTURE_PREFIX}/${user.id}`;
if (!this.storageService.exists(imagePath)) {
await this.storageService.uploadImageFromUrl(req.user.picture, imagePath);
}
}

const domain = this.configService.getConfig().front.domain;
Expand Down
14 changes: 14 additions & 0 deletions apps/backend/src/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ export class StorageService {
const data = await this.s3Client.deleteObject(params);
return data;
}

async exists(name: string) {
const params: PutObjectCommandInput = {
Bucket: this.config.getConfig().storage.bucket,
Key: name,
};

try {
await this.s3Client.headObject(params);
return true;
} catch (error) {
return false;
}
}
}

export const PROFILE_PICTURE_PREFIX = 'profile-pictures';

0 comments on commit 72e1083

Please sign in to comment.