From ac0d47700d43f425525a8aed4845f445a8381e15 Mon Sep 17 00:00:00 2001 From: LeeJungBum Date: Sun, 25 Aug 2024 01:01:03 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../back/farm/controller/FarmController.java | 37 +++++++++---------- .../back/global/image/service/S3Service.java | 14 ++----- .../global/image/service/StorageService.java | 4 +- .../product/controller/ProductController.java | 13 +++---- 4 files changed, 29 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/wefood/back/farm/controller/FarmController.java b/src/main/java/com/wefood/back/farm/controller/FarmController.java index fc1be96..33146dc 100644 --- a/src/main/java/com/wefood/back/farm/controller/FarmController.java +++ b/src/main/java/com/wefood/back/farm/controller/FarmController.java @@ -19,6 +19,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; @@ -45,33 +46,29 @@ public class FarmController { private final static String DIR_NAME = "farm"; @ResponseStatus(HttpStatus.CREATED) - @PostMapping + @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public void uploadImages( - @Valid @ModelAttribute UploadImageRequestDto requestDto, - BindingResult result) { - if (result.hasErrors()) { - throw new InvalidRequestException(result); - } - - try { - storageService.saveImages(requestDto, DIR_NAME); - } catch (IOException e) { - throw new FileUploadException("An error occurred while uploading files.", e); - } - } - - @ResponseStatus(HttpStatus.CREATED) - @PostMapping("/upload-part") - public void uploadImages( - @RequestPart("files") MultipartFile[] files, @RequestParam("id") Long id) { + @RequestParam("id") Long id, + @RequestParam("files") List files) { - UploadImageRequestDto uploadImageRequestDto = new UploadImageRequestDto(Arrays.stream(files).toList(), id); try { - storageService.saveImages(uploadImageRequestDto, DIR_NAME); + storageService.saveImages(id,files, DIR_NAME); } catch (IOException e) { throw new FileUploadException("An error occurred while uploading files.", e); } } +// @ResponseStatus(HttpStatus.CREATED) +// @PostMapping("/upload-part") +// public void uploadImages( +// @RequestPart("files") MultipartFile[] files, @RequestParam("id") Long id) { +// +// UploadImageRequestDto uploadImageRequestDto = new UploadImageRequestDto(Arrays.stream(files).toList(), id); +// try { +// storageService.saveImages(uploadImageRequestDto, DIR_NAME); +// } catch (IOException e) { +// throw new FileUploadException("An error occurred while uploading files.", e); +// } +// } @ResponseStatus(HttpStatus.CREATED) @PostMapping("/thumbnail") diff --git a/src/main/java/com/wefood/back/global/image/service/S3Service.java b/src/main/java/com/wefood/back/global/image/service/S3Service.java index c7cb4cf..f554ee3 100644 --- a/src/main/java/com/wefood/back/global/image/service/S3Service.java +++ b/src/main/java/com/wefood/back/global/image/service/S3Service.java @@ -58,19 +58,11 @@ public class S3Service implements StorageService { @Value("${cloud.aws.s3.bucketName}") private String bucket; - /** - * - * @param uploadImageRequestDto - * @param dirName - * @return - * @throws IOException - */ + @Override - public void saveImages(UploadImageRequestDto uploadImageRequestDto, String dirName) throws IOException { - List multipartFiles = uploadImageRequestDto.getFiles(); - Long id = uploadImageRequestDto.getId(); + public void saveImages(Long id, List multipartFiles, String dirName) throws IOException { List images = new ArrayList<>(); - ImageRootType rootType= dbCheckRootType(dirName, uploadImageRequestDto.getId()); + ImageRootType rootType= dbCheckRootType(dirName, id); for (MultipartFile multipartFile : multipartFiles) { File uploadFile = convert(multipartFile) // 파일 변환할 수 없으면 에러 .orElseThrow(() -> new IllegalArgumentException("error: MultipartFile -> File convert fail")); diff --git a/src/main/java/com/wefood/back/global/image/service/StorageService.java b/src/main/java/com/wefood/back/global/image/service/StorageService.java index c16c04f..55cc60b 100644 --- a/src/main/java/com/wefood/back/global/image/service/StorageService.java +++ b/src/main/java/com/wefood/back/global/image/service/StorageService.java @@ -3,6 +3,8 @@ import com.wefood.back.global.image.dto.UploadImageRequestDto; import com.wefood.back.global.image.dto.UploadThumbnailRequestDto; import java.io.IOException; +import java.util.List; +import org.springframework.web.multipart.MultipartFile; /** * class: StorageService. @@ -14,7 +16,7 @@ * @version 2024/08/11 */ public interface StorageService { - void saveImages(UploadImageRequestDto uploadImageRequestDto, String dirName) throws IOException; + void saveImages(Long id, List multipartFiles, String dirName) throws IOException; void saveThumbnail(UploadThumbnailRequestDto uploadThumbnail, String dirName) throws IOException; } diff --git a/src/main/java/com/wefood/back/product/controller/ProductController.java b/src/main/java/com/wefood/back/product/controller/ProductController.java index 74e0442..08eefab 100644 --- a/src/main/java/com/wefood/back/product/controller/ProductController.java +++ b/src/main/java/com/wefood/back/product/controller/ProductController.java @@ -13,12 +13,14 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import java.io.IOException; import java.util.List; +import org.springframework.web.multipart.MultipartFile; @RestController @RequestMapping("/api/product") @@ -89,16 +91,13 @@ public ResponseEntity> getProduct(@PathVariable(n } @ResponseStatus(HttpStatus.CREATED) - @PostMapping + @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public void uploadImages( - @Valid @ModelAttribute UploadImageRequestDto requestDto, - BindingResult result) { + @RequestParam("id") Long id, + @RequestParam("files") List files) { - if (result.hasErrors()) { - throw new InvalidRequestException(result); - } try { - storageService.saveImages(requestDto, DIR_NAME); + storageService.saveImages(id,files, DIR_NAME); } catch (IOException e) { throw new FileUploadException("An error occurred while uploading files.", e); }