Skip to content

Commit

Permalink
feat: 이미지 업로드 최대사이즈 제한 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
Daolove0323 committed Nov 8, 2024
1 parent 9772c6f commit 17b8d02
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 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();
}
}
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 @@ -48,6 +48,7 @@
@WebMvcTest(CurationController.class)
class CurationControllerUnitTest {


private static ObjectMapper objectMapper;

@MockBean
Expand All @@ -66,7 +67,6 @@ class CurationControllerUnitTest {

@BeforeAll
public static void init() {
objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
}

Expand Down

0 comments on commit 17b8d02

Please sign in to comment.