Skip to content

Commit

Permalink
Image (#115)
Browse files Browse the repository at this point in the history
* fix: privateAlert channelService에서 하기, 소켓 끊길 때 db 업데이트 잘하기, s3 이미지 api 추가

* fix: s3 presignedurl 발급 key 수정
  • Loading branch information
tomatozil authored Apr 23, 2024
1 parent 01bcefb commit 6c8017b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,19 @@ export class UsersController {
@UseGuards(AuthGuard('access'))
@Get('/s3image')
async getPresignedUrl(@GetUser() user: User) {
const presignedUrl = await this.usersService.getPresignedUrl(
user.nickname,
);
const presignedUrl = await this.usersService.getPresignedUrl(user.id);

return presignedUrl;
}

@UseGuards(AuthGuard('access'))
@Delete('/s3image')
async deleteAndGetPresignedUrl(@GetUser() user: User) {
const nickname = user.nickname;
const userId = user.id;

await this.usersService.deleteS3Image(nickname);
await this.usersService.deleteS3Image(userId);

const presignedUrl = await this.usersService.getPresignedUrl(nickname);
const presignedUrl = await this.usersService.getPresignedUrl(userId);

return presignedUrl;
}
Expand Down
9 changes: 5 additions & 4 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,21 @@ export class UsersService {
return await this.userRepository.findRanksWithPage();
}

async getPresignedUrl(nickname: string) {
async getPresignedUrl(userId: number) {
// TODO: 수정 72시간 이내에는 에러

const command = new PutObjectCommand({
Bucket: this.s3Configure.S3_BUCKET_NAME,
Key: nickname,
Key: `images/${userId}.jpeg`,
ContentType: 'image/jpeg',
});
return await getSignedUrl(this.s3, command, { expiresIn: 3000 });
}

async deleteS3Image(nickname: string) {
async deleteS3Image(userId: number) {
const command = new DeleteObjectCommand({
Bucket: this.s3Configure.S3_BUCKET_NAME,
Key: nickname,
Key: `images/${userId}.jpeg`,
});

const response = await this.s3.send(command);
Expand Down

0 comments on commit 6c8017b

Please sign in to comment.