diff --git a/backend/src/main/java/mouda/backend/darakbangmember/business/DarakbangMemberService.java b/backend/src/main/java/mouda/backend/darakbangmember/business/DarakbangMemberService.java index 0406f94c3..5e8d0dd7e 100644 --- a/backend/src/main/java/mouda/backend/darakbangmember/business/DarakbangMemberService.java +++ b/backend/src/main/java/mouda/backend/darakbangmember/business/DarakbangMemberService.java @@ -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; } diff --git a/backend/src/main/java/mouda/backend/darakbangmember/domain/DarakbangMember.java b/backend/src/main/java/mouda/backend/darakbangmember/domain/DarakbangMember.java index 89f0de670..a8334020c 100644 --- a/backend/src/main/java/mouda/backend/darakbangmember/domain/DarakbangMember.java +++ b/backend/src/main/java/mouda/backend/darakbangmember/domain/DarakbangMember.java @@ -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) diff --git a/backend/src/main/java/mouda/backend/darakbangmember/exception/DarakbangMemberErrorMessage.java b/backend/src/main/java/mouda/backend/darakbangmember/exception/DarakbangMemberErrorMessage.java index 49c0e65f9..50b47487b 100644 --- a/backend/src/main/java/mouda/backend/darakbangmember/exception/DarakbangMemberErrorMessage.java +++ b/backend/src/main/java/mouda/backend/darakbangmember/exception/DarakbangMemberErrorMessage.java @@ -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; diff --git a/backend/src/main/java/mouda/backend/darakbangmember/implement/S3Client.java b/backend/src/main/java/mouda/backend/darakbangmember/implement/S3Client.java index ccda683c0..8abc0317f 100644 --- a/backend/src/main/java/mouda/backend/darakbangmember/implement/S3Client.java +++ b/backend/src/main/java/mouda/backend/darakbangmember/implement/S3Client.java @@ -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); + } + } }