Skip to content

Commit

Permalink
Merge pull request #648 from woowacourse-teams/feature/#645
Browse files Browse the repository at this point in the history
프로필 사진 변경시 기존 이미지가 있다면 s3에서 삭제한다.
  • Loading branch information
hoyeonyy authored Oct 16, 2024
2 parents 8198788 + 3ffc384 commit 02aff69
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public void updateMyInfo(
if (file != null) { // 새로 추가된 파일이 있는 경우
String url = s3Client.uploadFile(file); // S3 Upload
String newProfileUrl = imageParser.parse(url); // 새로 저장할 profile url
if (darakbangMember.hasImage()) { // 기존 이미지가 있다면 s3에서 삭제
s3Client.deleteFile(darakbangMember.getProfile());
}
darakbangMemberWriter.updateMyInfo(darakbangMember, nickname, description, newProfileUrl);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public boolean isNotSameMemberWith(DarakbangMember other) {
return !isSameMemberWith(other);
}

public boolean hasImage() {
return profile != null;
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum DarakbangMemberErrorMessage {
MEMBER_ALREADY_EXIST("이미 가입한 멤버입니다."),
MEMBER_NOT_EXIST("존재하지 않는 다락방 멤버입니다."),
NOT_ALLOWED_TO_READ("조회 권한이 없습니다."),
INVALID_DELETE_FILE("기존 이미지를 삭제할 수 없습니다."),
INVALID_FILE("잘못된 이미지 파일입니다.");

private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ public String uploadFile(MultipartFile file) {
throw new DarakbangMemberException(HttpStatus.BAD_REQUEST, DarakbangMemberErrorMessage.INVALID_FILE);
}
}

public void deleteFile(String fileUrl) {
try {
amazonS3.deleteObject(bucket, fileUrl);
} catch (Exception e) {
throw new DarakbangMemberException(HttpStatus.BAD_REQUEST, DarakbangMemberErrorMessage.INVALID_DELETE_FILE);
}
}
}

0 comments on commit 02aff69

Please sign in to comment.