Skip to content

Commit

Permalink
[Weekly/11/Test/Image] 이미지 업로드 최대사이즈 제한 설정 (#96)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and lja3723 committed Nov 15, 2024
1 parent d891624 commit 327c4f0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,27 +36,28 @@ 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());
}
} 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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();
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -48,7 +46,8 @@
@WebMvcTest(CurationController.class)
class CurationControllerUnitTest {

private static ObjectMapper objectMapper;
@Autowired
private ObjectMapper objectMapper;

@MockBean
private CurationService curationService;
Expand All @@ -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())
Expand Down

0 comments on commit 327c4f0

Please sign in to comment.