From 194f66767509d952b98e6b59536b055a2674a984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=ED=99=8D=EC=8B=9D?= <90020593+Daolove0323@users.noreply.github.com> Date: Sat, 9 Nov 2024 01:03:09 +0900 Subject: [PATCH] =?UTF-8?q?[Weekly/11/Test/Image]=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=20=EC=97=85=EB=A1=9C=EB=93=9C=20=EC=B5=9C=EB=8C=80?= =?UTF-8?q?=EC=82=AC=EC=9D=B4=EC=A6=88=20=EC=A0=9C=ED=95=9C=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20(#96)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 이미지 업로드 최대사이즈 제한 설정 * feat: objectMapper autowired * [Weekly/11/Test/like] Like리팩터링 및 Event Unit Test 수정 (#97) * test: Event Unit Test 리팩터링 * feat: Like 리팩터링 * feat: 이미지 업로드 최대사이즈 제한 설정 * feat: objectMapper autowired * Update AdvertisementController.java --------- Co-authored-by: ariimo --- .../wouldyouin/Image/application/ImageStorage.java | 13 +++++++------ .../Image/application/MemberImageService.java | 5 +---- .../_common/config/ObjectMapperConfig.java | 14 ++++++++++++++ .../advertisement/api/AdvertisementController.java | 6 +++--- src/main/resources/application.yml | 5 +++++ .../curation/CurationControllerUnitTest.java | 13 +++---------- 6 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java index 046aea29..569c92a2 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/Image/application/ImageStorage.java @@ -4,10 +4,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.UUID; +import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageRequest; import org.ktc2.cokaen.wouldyouin._common.exception.FailedToDeleteImageException; import org.ktc2.cokaen.wouldyouin._common.exception.FailedToUploadImageException; import org.springframework.beans.factory.annotation.Value; @@ -36,19 +36,19 @@ public String save(MultipartFile image, String subPath) { } // Todo: payment랑 이부분 restclient 유틸로 빼기 - public String save(String imageUrl, String subPath) { + public ImageRequest save(String imageUrl, String subPath) { RestClient client = RestClient.builder().build(); String fileName = ""; + Long size = 0L; try { ResponseEntity response = client.get() .uri(imageUrl) .retrieve() .toEntity(byte[].class); - if (Optional.ofNullable(response).isPresent() && response.getStatusCode().is2xxSuccessful()) { - byte[] imageBytes = response.getBody(); - + if (Optional.ofNullable(response.getBody()).isPresent() && response.getStatusCode().is2xxSuccessful()) { fileName = generateUuidName() + "." + getExtension(imageUrl); + size = (long) response.getBody().length; Path path = Paths.get(commonPath, subPath, fileName); Files.createDirectories(path.getParent()); Files.write(path, response.getBody()); @@ -56,7 +56,8 @@ public String save(String imageUrl, String subPath) { } catch (IOException ex) { throw new FailedToUploadImageException(); } - return subPath + "/" + fileName; + String url = subPath + "/" + fileName; + return ImageRequest.of(url, size, getExtension(imageUrl)); } public void delete(String imagePath) { 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 621679b3..ba009567 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 @@ -60,10 +60,7 @@ public void setBaseMember(MemberImage image, BaseMember member) { image.setBaseMember(member); } - // TODO: imageUrl을 MemberImage로 변환하는 로직 추가 필요 - // Todo: extension과 size 불러오기 public MemberImage convert(String imageUrl) { - var request = ImageRequest.of(imageStorage.save(imageUrl, subPath), 123123L, "jpg"); - return memberImageRepository.save(toEntity(request)); + return memberImageRepository.save(toEntity(imageStorage.save(imageUrl, subPath))); } } \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java new file mode 100644 index 00000000..861847a4 --- /dev/null +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/_common/config/ObjectMapperConfig.java @@ -0,0 +1,14 @@ +package org.ktc2.cokaen.wouldyouin._common.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ObjectMapperConfig { + + @Bean + ObjectMapper objectMapper() { + return new ObjectMapper(); + } +} \ No newline at end of file diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/AdvertisementController.java b/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/AdvertisementController.java index e8beda9e..89c66f07 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/AdvertisementController.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/advertisement/api/AdvertisementController.java @@ -45,7 +45,7 @@ public ResponseEntity> getAdvertisementBy public ResponseEntity> createAdvertisement( @Valid @RequestPart AdvertisementRequest advertisementRequest, @RequestPart(required = false) MultipartFile image, - @Authorize(MemberType.admin) MemberIdentifier memberIdentifier) { + @Authorize(MemberType.admin) MemberIdentifier admin) { return ApiResponse.created(advertisementService.create(advertisementRequest, image)); } @@ -54,14 +54,14 @@ public ResponseEntity> updateAdvertisemen @PathVariable Long adId, @Valid @RequestPart AdvertisementRequest advertisementRequest, @RequestPart(required = false) MultipartFile image, - @Authorize(MemberType.admin) MemberIdentifier memberIdentifier) { + @Authorize(MemberType.admin) MemberIdentifier admin) { return ApiResponse.ok(advertisementService.update(adId, advertisementRequest, image)); } @DeleteMapping("/{adId}") public ResponseEntity> deleteAdvertisement( @PathVariable Long adId, - @Authorize(MemberType.admin) MemberIdentifier memberIdentifier) { + @Authorize(MemberType.admin) MemberIdentifier admin) { advertisementService.delete(adId); return ApiResponse.noContent(); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e76ba79b..446aa5a9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,6 +4,11 @@ spring: application.name: WouldYouIn + servlet: + multipart: + max-file-size: 10MB + max-request-size: 10MB + # 임시 프로젝트 url wouldyouin-domain-name: http://52.78.71.136/ profiles: diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java index d6fcb308..7ee0bf05 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import java.util.List; import java.util.Random; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -31,7 +30,6 @@ import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockMember; import org.ktc2.cokaen.wouldyouin.auth.application.JwtAuthFilter; import org.ktc2.cokaen.wouldyouin.curation.api.CurationController; -import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCardRequest; import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCreateRequest; import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationEditRequest; import org.ktc2.cokaen.wouldyouin.curation.application.CurationService; @@ -48,7 +46,8 @@ @WebMvcTest(CurationController.class) class CurationControllerUnitTest { - private static ObjectMapper objectMapper; + @Autowired + private ObjectMapper objectMapper; @MockBean private CurationService curationService; @@ -63,15 +62,9 @@ class CurationControllerUnitTest { private WebApplicationContext context; private static final long randomId = abs(new Random().nextLong()); - - @BeforeAll - public static void init() { - objectMapper = new ObjectMapper(); - objectMapper.registerModule(new JavaTimeModule()); - } - @BeforeEach public void setup() throws Exception { + objectMapper.registerModule(new JavaTimeModule()); mockMvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity())