개인 포트폴리오/술렁술렁(전통주 플렛폼)

술렁술렁 프로젝트 - 개인 백엔드 작업, 이미지 호출 API

roalwh 2023. 10. 12. 20:10

술렁술렁 프로젝트 - 전통주 컨트롤러

  • 처음에는 프론트에서 불어오나 했었는데 디비에 경로를 등록하고 백단에서 처리 후 이미지를 리턴하는 방식으로 확인하였음
  • 실제 타 팀 프로젝트 중 프론트에 이미지 쌓고 있다가 제대로 동작을 못하는 경우가 생겼었다.
  • 이미지 호출의 경우 이미지 컨트롤러에서 로컬 데이터를 찾아서 넘겨주는 것이기 때문에 컨트롤러단에서 처리가 끝난다.
  • 도메인의 경우 DB에 저장한 도메인값이 들어오도록 설정한다. 프론트 단하고 맞춰놔야 이미지 불러오기가 수월해지기 때문

1. 컨트롤러 설정

@RequiredArgsConstructor
@RestController
public class ImgController {

  @Autowired
  ImgService imgService;
  • 컨트롤러에서 요청에 참조할 서비스 등록

2. 이미지 호출 API

2-1. 리뷰이미지 호출 API

// 리뷰 이미지 가져오기
  @GetMapping(value = "/rimg/{drinkid}/{rid}/{iname}")
  public ResponseEntity<?> returnImage(@PathVariable Long drinkid, @PathVariable Long rid, @PathVariable String iname) {
    String path1 = System.getProperty("user.dir");

    // 윈도우
    // String path2 = "\\src\\main\\resources\\img\\drink" + drinkid + "\\rimg" +
    // rid + "\\";

    // 리눅스
    String path2 = "/src/main/resources/img/drink" + drinkid + "/rimg" + rid + "/";

    System.out.println(path1 + path2 + iname);

    File file = new File("");
    File file1 = new File(path1 + path2 + iname);
    // 윈도우
    // File file2 = new File(path1 + "\\src\\main\\resources\\mainLogo.png");
    // 리눅스
    File file2 = new File(path1 + "/src/main/resources/mainLogo.png");

    if (file1.exists()) {
      file = file1;
    } else {
      file = file2;
    }

    // 저장된 이미지파일의 이진데이터 형식을 구함
    byte[] result = null;
    ResponseEntity<byte[]> entity = null;
    try {
      result = FileCopyUtils.copyToByteArray(file);

      // 2. header
      HttpHeaders header = new HttpHeaders();
      header.add("Content-type", Files.probeContentType(file.toPath())); // 파일의 컨텐츠타입을 직접 구해서 header에 저장

      // 3. 응답본문
      entity = new ResponseEntity<>(result, header, HttpStatus.OK);// 데이터, 헤더, 상태값
    } catch (IOException e) {
      e.printStackTrace();
    }

    return entity;
  }

2-2. 술 상세 보기 이미지 API

// 술 정보 이미지 가져오기
  @GetMapping(value = "/dimg/{drinkid}/{iname}")
  public ResponseEntity<?> returnImage(@PathVariable String drinkid, @PathVariable String iname) {
    String path1 = System.getProperty("user.dir");
    //윈도우
    // String path2 = "\\src\\main\\resources\\img\\drink" + drinkid + "\\dimg\\"; // 이미지가 저장된 위치

    //리눅스
    String path2 = "/src/main/resources/img/drink" + drinkid + "/dimg/"; // 이미지가 저장된 위치

    File file = new File("");
    File file1 = new File(path1 + path2 + iname);
    // 윈도우
    // File file2 = new File(path1 + "\\src\\main\\resources\\mainLogo.png");
    //리눅스
    File file2 = new File(path1 + "/src/main/resources/mainLogo.png");

    if (file1.exists()) {
      file = file1;
    } else {
      file = file2;
    }

    byte[] result = null;
    ResponseEntity<byte[]> entity = null;
    try {
      result = FileCopyUtils.copyToByteArray(file);

      // 2. header
      HttpHeaders header = new HttpHeaders();
      header.add("Content-type", Files.probeContentType(file.toPath())); // 파일의 컨텐츠타입을 직접 구해서 header에 저장

      // 3. 응답본문
      entity = new ResponseEntity<>(result, header, HttpStatus.OK);// 데이터, 헤더, 상태값
    } catch (IOException e) {
      e.printStackTrace();
    }

    return entity;
  }