Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#193 [feat] 회원 탈퇴 시 이미지 삭제 로직 추가 #196

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines +78 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5 :
이전 코드에서는 imagekey(?)값으로 이미지를 가져오지 않았는데 어떤 이유로 사용되었는지 알 수 있을까요..?!?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hellozo0
이미지 url이 https://~~~/bucket/path 이런 식일 때
이미지를 삭제할 때 url 전체를 넣어서 삭제하는 것이 아닌, 앞에 있는 https://~~~ 를 제거해서 bucket/path 값을 넣어야 합니다...!
그래서 substring 로직을 추가했습니다!

}

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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5 :
이친구는 이전에는 주석이 되었다가 살아났는데 designer 를 해당 함수에서 사용하지 않는 것으로 보이는데 어디서 사용하는 것일까요.>>>???!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hellozo0
해당 서비스 로직은 디자이너가 사용하는 서비스 로직이라서 비즈니스 로직 시작 전에 해당 메서드를 사용하는 사람이 디자이너인지 아닌지 구분하는 용도로만 사용합니다...!

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);
}
}
Loading