-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REFACTORING] - {IMAGE} 원본 이미지 전송 리팩토링, AI 이미지 저장 & 불러오기 API 구현 (#35)
* [#28]feat: WebClient 적용 * [#30]bugfix: 클라이언트에서 결과 출력 안 되는 버그 해결 * [#32]refactoring: 기본 이미지 전송 방식 리팩토링 * [#32]setting: 조회 request 세팅 * [#32]setting: 조회 response 세팅 * [#32]setting: query전용 repository 세팅 * [#32]refactoring: 로그 레벨 수정 * [#32]refactoring: ai 이미지 요청 응답 방식 리팩토링 * [#32]refactoring: 일반 이미지 업로드 리팩토링 * [#32]refactoring: 프리미엄 이미지 업로드 리팩토링 * [#32]refactoring: 일반 이미지 업로드 리팩토링 * [#32]refactoring: 일반 이미지 저장 리팩토링 * [#32]: 일반 ai 이미지 저장 구현 * [#32]: 일반 ai 이미지 저장 구현 * [#32]: 일반 ai 이미지 저장 구현 * [#32]: 일반 ai 이미지 저장 구현 * [#32]feat: 병합 수정
- Loading branch information
Showing
26 changed files
with
371 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/loveloveshot/configuration/WebFluxConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.loveloveshot.configuration; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.codec.ServerCodecConfigurer; | ||
import org.springframework.web.reactive.config.WebFluxConfigurer; | ||
|
||
@Configuration | ||
public class WebFluxConfig implements WebFluxConfigurer { | ||
|
||
@Override | ||
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) { | ||
configurer.defaultCodecs().maxInMemorySize(-1); //10MB | ||
} | ||
} |
122 changes: 85 additions & 37 deletions
122
...in/java/com/loveloveshot/image/command/application/controller/ImageCommandController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,112 @@ | ||
package com.loveloveshot.image.command.application.controller; | ||
|
||
import com.loveloveshot.common.response.ApiResponse; | ||
import com.loveloveshot.image.command.application.dto.SingleImageRequest; | ||
import com.loveloveshot.image.command.application.dto.request.ImageRequest; | ||
import com.loveloveshot.image.command.application.dto.request.SaveRequest; | ||
import com.loveloveshot.image.command.application.service.ImageCommandService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1") | ||
@RequiredArgsConstructor | ||
@CrossOrigin(origins = {"*"}) | ||
@CrossOrigin(origins = {"http://loveloveshot.com", "http://192.168.0.12:3000", "*"}) | ||
public class ImageCommandController { | ||
|
||
private final ImageCommandService imageCommandService; | ||
|
||
@PostMapping(value = "/singleImage") | ||
// 필기. RequestParam에 name값을 지정해주지 않으면 디폴트로 변수명을 key값으로 가짐. | ||
public ResponseEntity<ApiResponse> uploadSingleImage(@RequestParam MultipartFile maleSingleImage, | ||
@RequestParam MultipartFile femaleSingleImage, | ||
SingleImageRequest singleImageDTO) throws IOException { | ||
singleImageDTO.setMaleSingleImage(maleSingleImage); | ||
singleImageDTO.setFemaleSingleImage(femaleSingleImage); | ||
// 일반 이미지 업로드 | ||
@PostMapping(value = "/uploadStandardImage") | ||
public ResponseEntity<ApiResponse> uploadStandardImage(@RequestParam MultipartFile maleImage, | ||
@RequestParam MultipartFile femaleImage, | ||
ImageRequest imageRequest) { | ||
List<Resource> maleImages = new ArrayList<>(); | ||
List<Resource> femaleImages = new ArrayList<>(); | ||
|
||
if (maleSingleImage.isEmpty() || femaleSingleImage.isEmpty()) { | ||
throw new IllegalArgumentException("사진을 첨부해주세요"); | ||
try { | ||
if (maleImage.isEmpty() || femaleImage.isEmpty()) { | ||
throw new NullPointerException("사진을 첨부해 주세요"); | ||
} | ||
if (!maleImage.getContentType().startsWith("image") || | ||
!femaleImage.getContentType().startsWith("image")) { | ||
throw new IllegalArgumentException("이미지 형식의 파일을 올려주세요"); | ||
} | ||
} catch (NullPointerException | IllegalArgumentException e) { | ||
e.printStackTrace(); | ||
} | ||
maleImages.add(0, maleImage.getResource()); | ||
femaleImages.add(0, femaleImage.getResource()); | ||
imageRequest.setMaleImages(maleImages); | ||
imageRequest.setFemaleImages(femaleImages); | ||
|
||
return ResponseEntity.ok(ApiResponse.success("성공적으로 업로드 되었습니다. " | ||
, imageCommandService.uploadStandardImage(imageRequest))); | ||
} | ||
|
||
// 일반 AI 이미지 저장 | ||
@PostMapping("/saveAiImage") | ||
public ResponseEntity<ApiResponse> saveAiImages(@RequestPart(value = "file", required = false) MultipartFile aiImage, | ||
@RequestPart(value = "task_id", required = false) String taskId, | ||
SaveRequest saveRequest) throws IOException { | ||
System.out.println("aiImage = " + aiImage); | ||
System.out.println("taskId = " + taskId); | ||
List<File> files = new ArrayList<>(); | ||
|
||
// 설명. 파일 확장자 이미지 형식인지 확인 | ||
if (!maleSingleImage.getContentType().startsWith("image") || | ||
!femaleSingleImage.getContentType().startsWith("image")) { | ||
throw new IllegalArgumentException("이미지 형식의 파일을 올려주세요"); | ||
String filePath = System.getProperty("user.dir") + "/src/main/webapp/AiImages/"; // Ai 이미지 로컬 저장 경로 | ||
|
||
File dir = new File(filePath); | ||
if (!dir.exists()) { | ||
dir.mkdirs(); | ||
} | ||
|
||
Long userNo = 1L; //임의 값 | ||
String originFileName = aiImage.getOriginalFilename(); | ||
String ext = originFileName.substring(originFileName.lastIndexOf(".")); | ||
String savedName = UUID.randomUUID().toString().replaceAll("-", "") + ext; | ||
|
||
File targetFile = new File(filePath + "/" + savedName); // 저장할 파일 객체 생성 | ||
try { | ||
aiImage.transferTo(targetFile); | ||
} catch (IOException e) { | ||
new File(filePath + "/" + savedName).delete(); | ||
} | ||
files.add(0, targetFile); | ||
System.out.println("targetFile = " + targetFile); | ||
saveRequest.setAiImage(targetFile); | ||
saveRequest.setTaskId(taskId); | ||
|
||
return ResponseEntity.ok(ApiResponse.success("성공적으로 등록되었습니다." | ||
, imageCommandService.createAISingleImage(userNo, singleImageDTO))); | ||
return ResponseEntity.ok(ApiResponse.success("성공적으로 저장 되었습니다." | ||
, imageCommandService.saveStandardImage(saveRequest))); | ||
} | ||
|
||
// 프리미엄 이미지 업로드 | ||
@PostMapping("/uploadPremiumImage") | ||
public ResponseEntity<ApiResponse> uploadImageList(@RequestParam List<MultipartFile> maleImages, | ||
@RequestParam List<MultipartFile> femaleImages, | ||
ImageRequest imageRequest) { | ||
List<Resource> maleImageResources = getMultipartFile(maleImages); | ||
List<Resource> femaleImageResources = getMultipartFile(femaleImages); | ||
|
||
// @PostMapping("/imageList") | ||
// public void uploadImageList(@RequestParam List<MultipartFile> maleImageList, | ||
// @RequestParam List<MultipartFile> femaleImageList, | ||
// ImageListRequestDTO imageListDTO) { | ||
// | ||
// imageListDTO.setMaleImageList(maleImageList); | ||
// imageListDTO.setFemaleImageList(femaleImageList); | ||
// | ||
// if(maleImageList.size() < 2 || maleImageList.size() > 20) { | ||
// throw new IllegalArgumentException("2~20장의 사진을 올려주세요"); | ||
// } | ||
// | ||
// if(femaleImageList.size() < 2 || femaleImageList.size() > 20) { | ||
// throw new IllegalArgumentException("2~20장의 사진을 올려주세요"); | ||
// } | ||
// | ||
// imageCommandService.transferImageList(imageListDTO); | ||
// } | ||
imageRequest.setMaleImages(maleImageResources); | ||
imageRequest.setFemaleImages(femaleImageResources); | ||
|
||
return ResponseEntity.ok(ApiResponse.success("성공적으로 업로드 되었습니다." | ||
, imageCommandService.uploadPremiumImages(imageRequest))); | ||
} | ||
|
||
private List<Resource> getMultipartFile(List<MultipartFile> images) { | ||
List<Resource> imageResources = new ArrayList<>(); | ||
|
||
for (MultipartFile image : images) { | ||
imageResources.add(image.getResource()); | ||
} | ||
return imageResources; | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
src/main/java/com/loveloveshot/image/command/application/dto/ImageListRequest.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/main/java/com/loveloveshot/image/command/application/dto/ImageResponse.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/main/java/com/loveloveshot/image/command/application/dto/ImagesDTO.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/main/java/com/loveloveshot/image/command/application/dto/SingleImageRequest.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/java/com/loveloveshot/image/command/application/dto/request/FindRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.loveloveshot.image.command.application.dto.request; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@NoArgsConstructor(force = true) | ||
public class FindRequest { | ||
private final String taskId; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/loveloveshot/image/command/application/dto/request/ImageRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.loveloveshot.image.command.application.dto.request; | ||
|
||
import lombok.*; | ||
import org.springframework.core.io.Resource; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@ToString | ||
public class ImageRequest implements Serializable { | ||
private List<Resource> maleImages; | ||
private List<Resource> femaleImages; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/loveloveshot/image/command/application/dto/request/SaveRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.loveloveshot.image.command.application.dto.request; | ||
|
||
import lombok.*; | ||
|
||
import java.io.File; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@ToString | ||
public class SaveRequest { | ||
private File aiImage; | ||
private String taskId; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/loveloveshot/image/command/application/dto/response/FindResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.loveloveshot.image.command.application.dto.response; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@NoArgsConstructor(force = true) | ||
public class FindResponse { | ||
private final String imageName; | ||
private final String imagePath; | ||
private final String taskId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/loveloveshot/image/command/application/dto/response/UploadResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.loveloveshot.image.command.application.dto.response; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@NoArgsConstructor(force = true) | ||
public class UploadResponse { | ||
private final String status; | ||
private final String taskId; | ||
} |
Oops, something went wrong.