Skip to content

Commit

Permalink
Merge pull request #196 from TEAM-MODDY/feat/#193
Browse files Browse the repository at this point in the history
#193 [feat] 회원 탈퇴 시 이미지 삭제 로직 추가
  • Loading branch information
KWY0218 authored Jan 26, 2024
2 parents 7a5e9c5 + 1462fe9 commit b8d802e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
26 changes: 19 additions & 7 deletions src/main/java/com/moddy/server/external/s3/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class S3Service {
private final static String APPLICATION_PATH = "APPLICATION";
private final static String MODEL_PROFILE_PATH = "HAIR_MODEL_PROFILE";
private final static String MODEL_PROFILE_IMAGE_NAME = "/model_default_profile.png";
private final static int IMAGE_URL_PREFIX_LENGTH = 54;
private final static int EXPIRED_TIME = 3;
private final AmazonS3 amazonS3;
@Value("${cloud.aws.s3.bucket}")
Expand All @@ -44,6 +45,22 @@ public String getDefaultProfileImageUrl() {
return amazonS3.getUrl(bucket, MODEL_PROFILE_PATH + MODEL_PROFILE_IMAGE_NAME).toString();
}

public String getPreSignedUrlToDownload(final String fileName) {
final String imageKey = getImageUrlToKey(fileName);

GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, imageKey)
.withMethod(GET)
.withExpiration(getExpiredTime())
.withResponseHeaders(new ResponseHeaderOverrides().withContentDisposition("attachment"));

return amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString();
}

public void deleteS3Image(final String imageUrl) {
final String imageKey = getImageUrlToKey(imageUrl);
amazonS3.deleteObject(bucket, imageKey);
}

private String uploadImage(MultipartFile multipartFile, String path) {
String fileName = createFileName(multipartFile.getOriginalFilename());
ObjectMetadata objectMetadata = new ObjectMetadata();
Expand All @@ -58,13 +75,8 @@ private String uploadImage(MultipartFile multipartFile, String path) {
}
}

public String getPreSignedUrlToDownload(final String fileName) {
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, fileName)
.withMethod(GET)
.withExpiration(getExpiredTime())
.withResponseHeaders(new ResponseHeaderOverrides().withContentDisposition("attachment"));

return amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString();
private String getImageUrlToKey(final String imageUrl) {
return imageUrl.substring(IMAGE_URL_PREFIX_LENGTH);
}

private Date getExpiredTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,8 @@ private Boolean getIsSendStatus(Long applicationId, Long userId) {
}

public DownloadUrlResponseDto getOfferImageDownloadUrl(final Long userId, final String offerImageUrl) {
//Designer designer = designerJpaRepository.findById(userId).orElseThrow(() -> new NotFoundException(DESIGNER_NOT_FOUND_EXCEPTION));
String s3Key = offerImageUrl.substring(54);
String preSignedUrl = s3Service.getPreSignedUrlToDownload(s3Key);
Designer designer = designerJpaRepository.findById(userId).orElseThrow(() -> new NotFoundException(DESIGNER_NOT_FOUND_EXCEPTION));
String preSignedUrl = s3Service.getPreSignedUrlToDownload(offerImageUrl);
return new DownloadUrlResponseDto(preSignedUrl);
}
}
23 changes: 18 additions & 5 deletions src/main/java/com/moddy/server/service/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.moddy.server.domain.prefer_region.repository.PreferRegionJpaRepository;
import com.moddy.server.domain.user.User;
import com.moddy.server.domain.user.repository.UserRepository;
import com.moddy.server.external.s3.S3Service;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -37,6 +38,7 @@ public class UserService {
private final PreferRegionJpaRepository preferRegionJpaRepository;
private final ModelJpaRepository modelJpaRepository;
private final DesignerJpaRepository designerJpaRepository;
private final S3Service s3Service;

public UserDetailResponseDto getUserDetail(Long userId) {
User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(USER_NOT_FOUND_EXCEPTION));
Expand All @@ -61,12 +63,18 @@ private void deleteModelHairServiceOfferInfos(Long userId) {
private void deleteModelApplications(Long userId) {
List<HairModelApplication> hairModelApplications = hairModelApplicationJpaRepository.findAllByModelId(userId);
hairModelApplications.forEach(hairModelApplication -> {
deleteApplicationImage(hairModelApplication);
preferHairStyleJpaRepository.deleteAllByHairModelApplication(hairModelApplication);
hairServiceRecordJpaRepository.deleteAllByHairModelApplication(hairModelApplication);
hairModelApplicationJpaRepository.deleteById(hairModelApplication.getId());
});
}

private void deleteApplicationImage(final HairModelApplication hairModelApplication) {
s3Service.deleteS3Image(hairModelApplication.getApplicationCaptureUrl());
s3Service.deleteS3Image(hairModelApplication.getModelImgUrl());
}

private void deleteModelPreferRegions(Long userId) {
preferRegionJpaRepository.deleteAllByModelId(userId);
}
Expand All @@ -76,10 +84,11 @@ private void deleteModelInfo(Long userId) {
userRepository.deleteById(userId);
}

private void deleteDesignerInfos(Long userId) {
deleteDesignerHairServiceOfferInfos(userId);
deleteDesignerDayOffs(userId);
deleteDesignerInfo(userId);
private void deleteDesignerInfos(final User user) {
deleteDesignerHairServiceOfferInfos(user.getId());
deleteDesignerDayOffs(user.getId());
deleteDesignerProfileImage(user);
deleteDesignerInfo(user.getId());
}

private void deleteDesignerHairServiceOfferInfos(Long userId) {
Expand All @@ -99,10 +108,14 @@ private void deleteDesignerInfo(Long userId) {
userRepository.deleteById(userId);
}

private void deleteDesignerProfileImage(final User user) {
s3Service.deleteS3Image(user.getProfileImgUrl());
}

@Transactional
public void withdraw(Long userId) {
User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(USER_NOT_FOUND_EXCEPTION));
if (user.getRole() == MODEL) deleteModelInfos(userId);
else deleteDesignerInfos(userId);
else deleteDesignerInfos(user);
}
}

0 comments on commit b8d802e

Please sign in to comment.