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<byte[]> 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<ApiResponseBody<AdvertisementResponse>> getAdvertisementBy public ResponseEntity<ApiResponseBody<AdvertisementResponse>> 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<ApiResponseBody<AdvertisementResponse>> 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<ApiResponseBody<Void>> 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())