diff --git a/.gitignore b/.gitignore index f4f1f4ac..c7e998cd 100644 --- a/.gitignore +++ b/.gitignore @@ -37,7 +37,7 @@ out/ .vscode/ ### static images -/src/main/resources/staticimages/ +/src/main/resources/static/images/ ### env file .env \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/api/dto/ImageResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/api/dto/ImageResponse.java index 3ecdd1dc..347af557 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/api/dto/ImageResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/api/dto/ImageResponse.java @@ -1,10 +1,10 @@ package org.ktc2.cokaen.wouldyouin.Image.api.dto; -import java.nio.file.Paths; import java.time.LocalDateTime; import lombok.Builder; import lombok.Getter; import org.ktc2.cokaen.wouldyouin.Image.persist.Image; +import org.ktc2.cokaen.wouldyouin._common.util.UriUtil; @Getter @Builder @@ -16,10 +16,10 @@ public class ImageResponse { private String extension; private LocalDateTime createdDate; - public static ImageResponse from(Image image, String apiUrlHeader) { + public static ImageResponse from(Image image, String path) { return ImageResponse.builder() .id(image.getId()) - .url(Paths.get(apiUrlHeader, image.getUrl()).toString()) + .url(UriUtil.assembleFullUrl(path, image.getName())) .size(image.getSize()) .extension(image.getExtension()) .createdDate(image.getCreatedDate()) diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/AdvertisementImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/AdvertisementImageService.java index 413d75e4..ad8308a1 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/AdvertisementImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/AdvertisementImageService.java @@ -41,6 +41,7 @@ protected AdvertisementImage toEntity(ImageRequest imageRequest) { return AdvertisementImage.builder() .name(imageRequest.getUrl()) .size(imageRequest.getSize()) + .extension(imageRequest.getExtension()) .build(); } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/CurationImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/CurationImageService.java index 2af86340..5493073d 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/CurationImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/CurationImageService.java @@ -39,6 +39,7 @@ protected CurationImage toEntity(ImageRequest imageRequest) { return CurationImage.builder() .url(imageRequest.getUrl()) .size(imageRequest.getSize()) + .extension(imageRequest.getExtension()) .build(); } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/EventImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/EventImageService.java index 821c433e..a2a020a4 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/EventImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/EventImageService.java @@ -39,6 +39,7 @@ protected EventImage toEntity(ImageRequest imageRequest) { return EventImage.builder() .url(imageRequest.getUrl()) .size(imageRequest.getSize()) + .extension(imageRequest.getExtension()) .build(); } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageService.java index 435d156c..a056d22d 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageService.java @@ -8,6 +8,7 @@ import org.ktc2.cokaen.wouldyouin.Image.persist.Image; import org.ktc2.cokaen.wouldyouin.Image.persist.ImageRepository; import org.ktc2.cokaen.wouldyouin._common.exception.EntityNotFoundException; +import org.ktc2.cokaen.wouldyouin._common.util.UriUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -32,20 +33,6 @@ public abstract class ImageService { protected abstract T toEntity(ImageRequest imageRequest); - public T getById(Long id) { - return getImageRepository().findById(id) - .orElseThrow(() -> new EntityNotFoundException(getImageDomain().name() + " 이미지를 찾을 수 없습니다.")); - } - - protected ImageResponse create(ImageRequest imageRequest) { - return ImageResponse.from(getImageRepository().save(toEntity(imageRequest)), apiUrl); - } - - protected void delete(Long id) { - getById(id); - getImageRepository().deleteById(id); - } - @Transactional public List saveImages(List images) { return images.stream() @@ -57,6 +44,20 @@ public List saveImages(List images) { public void deleteImage(Long id) { T image = getById(id); delete(id); - imageStorageService.delete(image.getUrl()); + imageStorageService.delete(getChildPath(), image.getName()); + } + + public T getById(Long id) { + return getImageRepository().findById(id) + .orElseThrow(() -> new EntityNotFoundException(getImageDomain().name() + " 이미지를 찾을 수 없습니다.")); + } + + protected ImageResponse create(ImageRequest imageRequest) { + return ImageResponse.from(getImageRepository().save(toEntity(imageRequest)), UriUtil.assembleFullUrl(apiUrl, getChildPath())); + } + + protected void delete(Long id) { + getById(id); + getImageRepository().deleteById(id); } } \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorageService.java index 80b59bbb..0e685398 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorageService.java @@ -21,34 +21,32 @@ public class ImageStorageService { private String parentPath; private final RestClientUtil client; - public byte[] readFromDirectory(Path path) { - return FileUtil.readFile(Paths.get(parentPath).resolve(path)); + public byte[] readFromDirectory(Path childPath) { + return FileUtil.readFile(Paths.get(parentPath).resolve(childPath)); } - public ImageRequest saveToDirectory(MultipartFile image, String subPath) { + public ImageRequest saveToDirectory(MultipartFile image, String childPath) { String extension = FileUtil.getExtension(image); - String fileName = FileUtil.generateUuidName() + "." + extension; - String relativeFilePath = Paths.get(subPath, fileName).toString(); - Path absoluteFilePath = Paths.get(parentPath, relativeFilePath); - FileUtil.saveFile(image, absoluteFilePath); - return ImageRequest.of(relativeFilePath, image.getSize(), FileUtil.getExtension(image)); + String fileName = FileUtil.createRandomFileName(extension); + FileUtil.saveFile(image, Path.of(parentPath, childPath, fileName)); + return ImageRequest.of(fileName, image.getSize(), extension); } - public ImageRequest saveToDirectory(String imageUrl, String subPath) { + public ImageRequest saveToDirectory(String imageUrl, String childPath) { byte[] response = client.get(byte[].class, imageUrl, new HttpHeaders(), (req, rsp) -> { throw new FailedToUploadImageException("이미지 URL에 대한 요청을 실패하였습니다."); } ); Optional.ofNullable(response).orElseThrow( - () -> new FailedToUploadImageException("응답 본문이 비어있어 이미지를 가져올 수 없습니다.")); + () -> new FailedToUploadImageException("응답 본문이 비어있어 이미지를 가져올 수 없습니다.") + ); String extension = FileUtil.getExtension(imageUrl); - String fileName = FileUtil.generateUuidName() + "." + extension; - String relativeFilePath = Paths.get(subPath, fileName).toString(); - Path absoluteFilePath = Paths.get(parentPath, relativeFilePath); - FileUtil.saveFile(response, absoluteFilePath); - return ImageRequest.of(relativeFilePath, (long) response.length, extension); + String fileName = FileUtil.createRandomFileName(extension); + Path path = Path.of(parentPath, childPath, fileName); + FileUtil.saveFile(response, path); + return ImageRequest.of(fileName, (long) response.length, extension); } - public void delete(String imagePath) { - FileUtil.deleteFile(Paths.get(imagePath)); + public void delete(String childPath, String fileName) { + FileUtil.deleteFile(Path.of(parentPath, childPath, fileName)); } } \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java index c791b08e..ddc40e53 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/MemberImageService.java @@ -39,6 +39,7 @@ protected MemberImage toEntity(ImageRequest imageRequest) { return MemberImage.builder() .url(imageRequest.getUrl()) .size(imageRequest.getSize()) + .extension(imageRequest.getExtension()) .build(); } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/persist/Image.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/persist/Image.java index 436c436d..ccdcbe2e 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/persist/Image.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/persist/Image.java @@ -29,8 +29,8 @@ public abstract class Image { protected Long id; @NotNull - @Column(name = "url") - protected String url; + @Column(name = "name") + protected String name; @Column(name = "size") private Long size; @@ -42,8 +42,8 @@ public abstract class Image { @Column(name = "created_date") private LocalDateTime createdDate; - protected Image(String url, Long size, String extension) { - this.url = url; + protected Image(String name, Long size, String extension) { + this.name = name; this.size = size; this.extension = extension; } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/util/FileUtil.java b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/util/FileUtil.java index d4ff27db..3e14780e 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/util/FileUtil.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/util/FileUtil.java @@ -45,8 +45,8 @@ public static void deleteFile(Path path) { } } - public static String generateUuidName() { - return UUID.randomUUID().toString().replace("-", ""); + public static String createRandomFileName(String extension) { + return UUID.randomUUID().toString().replace("-", "") + "." + extension; } public static String getExtension(MultipartFile file) { diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/util/UriUtil.java b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/util/UriUtil.java new file mode 100644 index 00000000..75fbd2ab --- /dev/null +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/util/UriUtil.java @@ -0,0 +1,34 @@ +package org.ktc2.cokaen.wouldyouin._common.util; + +import org.springframework.util.MultiValueMap; +import org.springframework.web.util.UriComponentsBuilder; + +public class UriUtil { + + public static String buildUrl(String scheme, String host, String path, MultiValueMap params) { + return UriComponentsBuilder.newInstance() + .scheme(scheme) + .host(host) + .path(path) + .queryParams(params) + .build() + .toString(); + } + + public static String buildUrl(String scheme, String host, String path) { + return UriComponentsBuilder.newInstance() + .scheme(scheme) + .host(host) + .path(path) + .build() + .toString(); + } + + public static String assembleFullUrl(String baseUrl, String... paths) { + String joinedPath = String.join("/", paths); + return UriComponentsBuilder.fromHttpUrl(baseUrl) + .path("/" + joinedPath) + .build() + .toString(); + } +} \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/dto/AdvertisementResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/dto/AdvertisementResponse.java index daf6a21c..4920d9f1 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/dto/AdvertisementResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/dto/AdvertisementResponse.java @@ -17,7 +17,7 @@ public static AdvertisementResponse from(Advertisement advertisement) { return AdvertisementResponse.builder() .id(advertisement.getId()) .title(advertisement.getTitle()) - .imageUrl(advertisement.getAdvertisementImage().getUrl()) + .imageUrl(advertisement.getAdvertisementImage().getName()) .startTime(advertisement.getStartTime()) .endTime(advertisement.getEndTime()) .build(); diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/auth/application/oauth/GoogleRequestService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/auth/application/oauth/GoogleRequestService.java index f70095d2..1b378ab2 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/auth/application/oauth/GoogleRequestService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/auth/application/oauth/GoogleRequestService.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming; import java.util.Objects; import org.ktc2.cokaen.wouldyouin._common.util.RestClientUtil; +import org.ktc2.cokaen.wouldyouin._common.util.UriUtil; import org.ktc2.cokaen.wouldyouin.auth.application.oauth.dto.AccessTokenResponse; import org.ktc2.cokaen.wouldyouin.auth.application.oauth.dto.OauthRequest; import org.ktc2.cokaen.wouldyouin.auth.application.oauth.dto.OauthResourcesResponse; @@ -12,7 +13,6 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; -import org.springframework.web.util.UriComponentsBuilder; @Service public class GoogleRequestService extends OauthRequestService { @@ -44,22 +44,10 @@ public class GoogleRequestService extends OauthRequestService { private final HttpHeaders loginRequestHeaders; public GoogleRequestService(RestClientUtil restClientUtil) { - this.client = restClientUtil; - loginRequestUri = UriComponentsBuilder.newInstance() - .scheme("https") - .host(loginRequestHost) - .path(loginRequestPath) - .build(true) - .toString(); - - accessRequestUri = UriComponentsBuilder.newInstance() - .scheme("https") - .host(accessRequestHost) - .path(accessRequestPath) - .build(true) - .toString(); + loginRequestUri = UriUtil.buildUrl("https", loginRequestHost, loginRequestPath); + accessRequestUri = UriUtil.buildUrl("https", accessRequestHost, accessRequestPath); loginRequestHeaders = new HttpHeaders(); loginRequestHeaders.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/auth/application/oauth/KakaoRequestService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/auth/application/oauth/KakaoRequestService.java index 57ae9585..22defe71 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/auth/application/oauth/KakaoRequestService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/auth/application/oauth/KakaoRequestService.java @@ -6,6 +6,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import org.ktc2.cokaen.wouldyouin._common.util.RestClientUtil; +import org.ktc2.cokaen.wouldyouin._common.util.UriUtil; import org.ktc2.cokaen.wouldyouin.auth.application.oauth.dto.AccessTokenResponse; import org.ktc2.cokaen.wouldyouin.auth.application.oauth.dto.OauthRequest; import org.ktc2.cokaen.wouldyouin.auth.application.oauth.dto.OauthResourcesResponse; @@ -14,7 +15,8 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; -import org.springframework.web.util.UriComponentsBuilder; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; @Service public class KakaoRequestService extends OauthRequestService { @@ -53,29 +55,23 @@ protected AccountType getAccountType() { public KakaoRequestService(RestClientUtil restClientUtil) { this.client = restClientUtil; - OauthRequest request = getOauthRequestBase(); - loginRequestUri = UriComponentsBuilder.newInstance() - .scheme("https") - .host(loginRequestHost) - .path(loginRequestPath) - .queryParam("grant_type", request.getGrantType()) - .queryParam("client_id", request.getClientId()) - .queryParam("client_secret", request.getClientSecret()) - .queryParam("code", request.getCode()) - .build(true) - .toString(); - - accessRequestUri = UriComponentsBuilder.newInstance() - .scheme("https") - .host(accessRequestHost) - .path(accessRequestPath) - .build(true) - .toString(); + loginRequestUri = UriUtil.buildUrl("https", loginRequestHost, loginRequestPath, getLoginRequestQueryParams()); + accessRequestUri = UriUtil.buildUrl("https", accessRequestHost, accessRequestPath); loginRequestHeaders = new HttpHeaders(); loginRequestHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE); } + protected MultiValueMap getLoginRequestQueryParams() { + OauthRequest request = getOauthRequestBase(); + var queries = new LinkedMultiValueMap(); + queries.add("grant_type", request.getGrantType()); + queries.add("client_id", request.getClientId()); + queries.add("client_secret", request.getClientSecret()); + queries.add("code", request.getCode()); + return queries; + } + protected HttpHeaders getAccessRequestHeaders(AccessTokenResponse authenticationResponse) { HttpHeaders accessRequestHeaders = new HttpHeaders(); accessRequestHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE); diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationCardResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationCardResponse.java index 5dc27bb8..60048c89 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationCardResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/curation/api/dto/CurationCardResponse.java @@ -18,7 +18,7 @@ public static CurationCardResponse from(CurationCard curationCard) { .content(curationCard.getContent()) .imageUrls( curationCard.getCurationImages().stream() - .map(CurationImage::getUrl) + .map(CurationImage::getName) .toList() ) .build(); diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/member/api/dto/MemberResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/member/api/dto/MemberResponse.java index 4fe093db..201653e2 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/member/api/dto/MemberResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/member/api/dto/MemberResponse.java @@ -34,7 +34,7 @@ private static MemberResponseBuilder responseBase(BaseMember baseMember) { .memberId(baseMember.getId()) .nickname(baseMember.getNickname()) .phoneNumber(baseMember.getPhone()) - .profileUrl(baseMember.getProfileImage().getUrl()); + .profileUrl(baseMember.getProfileImage().getName()); } // TODO: normal member임에도 불구, curator 형식이 호출되는 현상 수정필요 diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/member/api/dto/relation/EventHostResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/member/api/dto/relation/EventHostResponse.java index fb2d26a8..f3e9d42b 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/member/api/dto/relation/EventHostResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/member/api/dto/relation/EventHostResponse.java @@ -22,7 +22,7 @@ public static EventHostResponse from(Host host) { .nickname(host.getNickname()) .email(host.getEmail()) .phone(host.getPhone()) - .profileImageUrl(host.getProfileImage().getUrl()) + .profileImageUrl(host.getProfileImage().getName()) .intro(host.getIntro()) .likes(host.getLikes()) .hashtags(host.getHashTagList()) diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/member/persist/BaseMember.java b/src/main/java/org/ktc2/cokaen/wouldyouin/member/persist/BaseMember.java index e2a80e47..9f6d513f 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/member/persist/BaseMember.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/member/persist/BaseMember.java @@ -64,6 +64,6 @@ protected BaseMember(AccountType accountType, MemberType memberType, String emai } public String getProfileImageUrl() { - return profileImage.getUrl(); + return profileImage.getName(); } }