Skip to content

Commit

Permalink
[REFACTORING] - {IMAGE} 원본 이미지 전송 리팩토링, AI 이미지 저장 & 불러오기 API 구현 (#35)
Browse files Browse the repository at this point in the history
* [#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
fakerdeft authored Sep 21, 2023
1 parent 4ca14f8 commit 7299a7d
Show file tree
Hide file tree
Showing 26 changed files with 371 additions and 337 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/loveloveshot/configuration/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(resourceHandler)
.addResourceLocations(resourceLocation);
}

// @Override
// public void addCorsMappings(CorsRegistry registry) {
// // 모든 uri에 대해 http://localhost:18080, http://localhost:8180 도메인은 접근을 허용한다.
// registry.addMapping("/**")
// .allowedOrigins("http://localhost:3000", "https://loveloveshot.com", "http://192.168.0.12:3000")
// .allowedMethods("GET", "POST")
// .allowCredentials(true);
// }
}
14 changes: 14 additions & 0 deletions src/main/java/com/loveloveshot/configuration/WebFluxConfig.java
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
}
}
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;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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;
}
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;
}
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;
}
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;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.loveloveshot.image.command.application.dto;
package com.loveloveshot.image.command.application.dto.response;

import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,8 +10,6 @@
@Getter
@RequiredArgsConstructor
@NoArgsConstructor(force = true)
public class AiImageResponse implements Serializable {

public class SaveResponse implements Serializable {
private final MultipartFile aiImage;

}
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;
}
Loading

0 comments on commit 7299a7d

Please sign in to comment.