diff --git a/Api/src/main/java/allchive/server/api/archiving/model/dto/response/ArchivingContentsResponse.java b/Api/src/main/java/allchive/server/api/archiving/model/dto/response/ArchivingContentsResponse.java index 6e17c9bf..fa946671 100644 --- a/Api/src/main/java/allchive/server/api/archiving/model/dto/response/ArchivingContentsResponse.java +++ b/Api/src/main/java/allchive/server/api/archiving/model/dto/response/ArchivingContentsResponse.java @@ -2,6 +2,7 @@ import allchive.server.api.common.slice.SliceResponse; +import allchive.server.api.common.util.UrlUtil; import allchive.server.api.content.model.dto.response.ContentResponse; import allchive.server.core.annotation.DateFormat; import allchive.server.domain.domains.archiving.domain.Archiving; @@ -88,7 +89,7 @@ public static ArchivingContentsResponse of( .contents(contentResponseSlice) .ownerId(user.getId()) .ownerNickname(user.getNickname()) - .ownerProfileImgUrl(user.getProfileImgUrl()) + .ownerProfileImgUrl(UrlUtil.toAssetUrl(user.getProfileImgUrl())) .isMine(isMine) .isScrap(isScrap) .build(); diff --git a/Api/src/main/java/allchive/server/api/archiving/service/UpdateArchivingUseCase.java b/Api/src/main/java/allchive/server/api/archiving/service/UpdateArchivingUseCase.java index 15fe872f..fa4d01e4 100644 --- a/Api/src/main/java/allchive/server/api/archiving/service/UpdateArchivingUseCase.java +++ b/Api/src/main/java/allchive/server/api/archiving/service/UpdateArchivingUseCase.java @@ -9,6 +9,8 @@ import allchive.server.domain.domains.archiving.domain.Archiving; import allchive.server.domain.domains.archiving.service.ArchivingDomainService; import allchive.server.domain.domains.archiving.validator.ArchivingValidator; +import allchive.server.infrastructure.s3.service.S3DeleteObjectService; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.transaction.annotation.Transactional; @@ -18,11 +20,13 @@ public class UpdateArchivingUseCase { private final ArchivingDomainService archivingDomainService; private final ArchivingAdaptor archivingAdaptor; private final ArchivingValidator archivingValidator; + private final S3DeleteObjectService s3DeleteObjectService; @Transactional public void execute(Long archivingId, UpdateArchivingRequest request) { validateExecution(archivingId); Archiving archiving = archivingAdaptor.findById(archivingId); + eliminateOldImage(archivingId, request.getImageUrl()); archivingDomainService.updateArchiving( archiving, request.getTitle(), @@ -35,4 +39,12 @@ private void validateExecution(Long archivingId) { Long userId = SecurityUtil.getCurrentUserId(); archivingValidator.verifyUser(userId, archivingId); } + + private void eliminateOldImage(Long archivingId, String newUrl) { + Archiving archiving = archivingAdaptor.findById(archivingId); + if (UrlUtil.validateS3Key(archiving.getImageUrl()) + && !archiving.getImageUrl().equals(UrlUtil.convertUrlToKey(newUrl))) { + s3DeleteObjectService.deleteS3Object(List.of(archiving.getImageUrl())); + } + } } diff --git a/Api/src/main/java/allchive/server/api/common/util/UrlUtil.java b/Api/src/main/java/allchive/server/api/common/util/UrlUtil.java index 055fb4c9..2ffe0bbb 100644 --- a/Api/src/main/java/allchive/server/api/common/util/UrlUtil.java +++ b/Api/src/main/java/allchive/server/api/common/util/UrlUtil.java @@ -16,29 +16,36 @@ private UrlUtil(SpringEnvironmentHelper springEnvironmentHelper) { } public static String toAssetUrl(String key) { - // if (key.equals("")) { - // return ""; - // } - // if (springEnvironmentHelper.isProdProfile()) { - // return PROD_ASSET_URL + key; - // } - // return STAGING_ASSET_URL + key; - return key; + if (key.startsWith("http")) { + return key; + } + if (key.equals("")) { + return ""; + } + if (springEnvironmentHelper.isProdProfile()) { + return PROD_ASSET_URL + key; + } + return STAGING_ASSET_URL + key; } public static String convertUrlToKey(String url) { - // if (url.equals("")) { - // return ""; - // } - // if (validateUrl(url)) { - // return url.split("/", 4)[3]; - // } + if (url.equals("")) { + return ""; + } + if (validateS3Url(url)) { + return url.split("/", 4)[3].split("\\?", 2)[0]; + } return url; } - private static Boolean validateUrl(String url) { + public static Boolean validateS3Url(String url) { return url.contains(STAGING_ASSET_URL) || url.contains(PROD_ASSET_URL) - || url.contains(S3_ASSET_URL); + || url.contains(S3_STAGING_ASSET_URL) + || url.contains(S3_PROD_ASSET_URL); + } + + public static Boolean validateS3Key(String key) { + return key != null && !key.startsWith("https") && !key.isEmpty(); } } diff --git a/Api/src/main/java/allchive/server/api/content/service/UpdateContentUseCase.java b/Api/src/main/java/allchive/server/api/content/service/UpdateContentUseCase.java index 8a5c3ba7..c33ffaee 100644 --- a/Api/src/main/java/allchive/server/api/content/service/UpdateContentUseCase.java +++ b/Api/src/main/java/allchive/server/api/content/service/UpdateContentUseCase.java @@ -20,6 +20,7 @@ import allchive.server.domain.domains.content.service.TagDomainService; import allchive.server.domain.domains.content.validator.ContentValidator; import allchive.server.domain.domains.content.validator.TagValidator; +import allchive.server.infrastructure.s3.service.S3DeleteObjectService; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.transaction.annotation.Transactional; @@ -36,6 +37,7 @@ public class UpdateContentUseCase { private final ContentTagGroupDomainService contentTagGroupDomainService; private final ArchivingDomainService archivingDomainService; private final TagDomainService tagDomainService; + private final S3DeleteObjectService s3DeleteObjectService; @Transactional public void execute(Long contentId, UpdateContentRequest request) { @@ -43,6 +45,7 @@ public void execute(Long contentId, UpdateContentRequest request) { updateTagUsedAt(request.getTagIds()); regenerateContentTagGroup(contentId, request.getTagIds()); updateArchiving(contentId, request.getArchivingId(), request.getContentType()); + eliminateOldImage(contentId, request.getImgUrl()); contentDomainService.update( contentId, request.getArchivingId(), @@ -79,4 +82,12 @@ private void regenerateContentTagGroup(Long contentId, List tagIds) { private void updateTagUsedAt(List tagIds) { tagAdaptor.queryTagByTagIdIn(tagIds).forEach(tagDomainService::updateUsedAt); } + + private void eliminateOldImage(Long contentId, String newUrl) { + Content content = contentAdaptor.findById(contentId); + if (UrlUtil.validateS3Key(content.getImageUrl()) + && !content.getImageUrl().equals(UrlUtil.convertUrlToKey(newUrl))) { + s3DeleteObjectService.deleteS3Object(List.of(content.getImageUrl())); + } + } } diff --git a/Api/src/main/java/allchive/server/api/user/service/UpdateUserInfoUseCase.java b/Api/src/main/java/allchive/server/api/user/service/UpdateUserInfoUseCase.java index 313810c2..176cd976 100644 --- a/Api/src/main/java/allchive/server/api/user/service/UpdateUserInfoUseCase.java +++ b/Api/src/main/java/allchive/server/api/user/service/UpdateUserInfoUseCase.java @@ -5,8 +5,12 @@ import allchive.server.api.config.security.SecurityUtil; import allchive.server.api.user.model.dto.request.UpdateUserInfoRequest; import allchive.server.core.annotation.UseCase; +import allchive.server.domain.domains.user.adaptor.UserAdaptor; +import allchive.server.domain.domains.user.domain.User; import allchive.server.domain.domains.user.service.UserDomainService; import allchive.server.domain.domains.user.validator.UserValidator; +import allchive.server.infrastructure.s3.service.S3DeleteObjectService; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.transaction.annotation.Transactional; @@ -15,17 +19,31 @@ public class UpdateUserInfoUseCase { private final UserDomainService userDomainService; private final UserValidator userValidator; + private final UserAdaptor userAdaptor; + private final S3DeleteObjectService s3DeleteObjectService; @Transactional public void execute(UpdateUserInfoRequest request) { Long userId = SecurityUtil.getCurrentUserId(); validateExecution(userId); - String imgKey = UrlUtil.convertUrlToKey(request.getImgUrl()); + eliminateOldImage(userId, request.getImgUrl()); userDomainService.updateUserInfo( - userId, request.getName(), request.getEmail(), request.getNickname(), imgKey); + userId, + request.getName(), + request.getEmail(), + request.getNickname(), + UrlUtil.convertUrlToKey(request.getImgUrl())); } private void validateExecution(Long userId) { userValidator.validateUserStatusNormal(userId); } + + private void eliminateOldImage(Long userId, String newUrl) { + User user = userAdaptor.findById(userId); + if (UrlUtil.validateS3Key(user.getProfileImgUrl()) + && !user.getProfileImgUrl().equals(UrlUtil.convertUrlToKey(newUrl))) { + s3DeleteObjectService.deleteS3Object(List.of(user.getProfileImgUrl())); + } + } } diff --git a/Core/src/main/java/allchive/server/core/consts/AllchiveConst.java b/Core/src/main/java/allchive/server/core/consts/AllchiveConst.java index e5f1a45e..a9f7c47d 100644 --- a/Core/src/main/java/allchive/server/core/consts/AllchiveConst.java +++ b/Core/src/main/java/allchive/server/core/consts/AllchiveConst.java @@ -31,8 +31,10 @@ public class AllchiveConst { public static final String STAGING_ASSET_URL = "https://asset.staging.allchive.co.kr/"; public static final String PROD_ASSET_URL = "https://asset.allchive.co.kr/"; - public static final String S3_ASSET_URL = - "https://asset.staging.allchive.co.kr.s3.ap-northeast-2.amazonaws.com/"; + public static final String S3_STAGING_ASSET_URL = + "https://all-chive-dev-bucket.s3.ap-northeast-2.amazonaws.com/"; + public static final String S3_PROD_ASSET_URL = + "https://all-chive-bucket.s3.ap-northeast-2.amazonaws.com/"; public static final String[] SwaggerPatterns = { "/swagger-resources/**", "/swagger-ui/**", "/v3/api-docs/**", "/v3/api-docs", diff --git a/Infrastructure/src/main/java/allchive/server/infrastructure/s3/service/S3PresignedUrlService.java b/Infrastructure/src/main/java/allchive/server/infrastructure/s3/service/S3PresignedUrlService.java index 81bd68ac..fafc14f6 100644 --- a/Infrastructure/src/main/java/allchive/server/infrastructure/s3/service/S3PresignedUrlService.java +++ b/Infrastructure/src/main/java/allchive/server/infrastructure/s3/service/S3PresignedUrlService.java @@ -39,7 +39,7 @@ public ImageUrlDto getPreSignedUrl(Long id, PresignedType presignedType) { private String generateFileName(Long id, PresignedType presignedType) { String fileName; switch (presignedType) { - case USER -> fileName = baseUrl + "/user/"; + case USER -> fileName = baseUrl + "/user"; case CONTENT -> fileName = baseUrl + "/content/" + id.toString(); case ARCHIVING -> fileName = baseUrl + "/archiving/" + id.toString(); default -> throw InternalServerError.EXCEPTION;