Skip to content

Commit

Permalink
[feat] url 파씽 로직 추가 및 기존 이미지 삭제 로직 추가 (#134)
Browse files Browse the repository at this point in the history
* [feat] image url 추가, url 파씽 로직 추가 #133

* [chore] spotless 적용 #133

* [feat] 수정시 기존 이미지 삭제 #133

* [chore] spotless 적용 #133
  • Loading branch information
wjdtkdgns authored Aug 24, 2023
1 parent 20aac6f commit d4df855
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(),
Expand All @@ -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()));
}
}
}
39 changes: 23 additions & 16 deletions Api/src/main/java/allchive/server/api/common/util/UrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,13 +37,15 @@ 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) {
validateExecution(contentId, request);
updateTagUsedAt(request.getTagIds());
regenerateContentTagGroup(contentId, request.getTagIds());
updateArchiving(contentId, request.getArchivingId(), request.getContentType());
eliminateOldImage(contentId, request.getImgUrl());
contentDomainService.update(
contentId,
request.getArchivingId(),
Expand Down Expand Up @@ -79,4 +82,12 @@ private void regenerateContentTagGroup(Long contentId, List<Long> tagIds) {
private void updateTagUsedAt(List<Long> 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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d4df855

Please sign in to comment.