localhost-back/src/main/java/io/company/localhost/controller/common/ImageUploadController.java
2025-02-20 11:16:48 +09:00

72 lines
2.4 KiB
Java

/************************************************************
*
* @packageName : io.company.localhost.controller.common
* @fileName : ImageUploadController.java
* @author : 공현지
* @date : 25.01.16
* @description :
*
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 25.01.16 공현지 최초 생성
*
*************************************************************/
package io.company.localhost.controller.common;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import io.company.localhost.common.annotation.ParameterCheck;
import io.company.localhost.common.annotation.ReqMap;
import io.company.localhost.common.dto.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/api/quilleditor")
@RequiredArgsConstructor
public class ImageUploadController {
@Value("${filePath.boardfile}")
private String boardFilePath;
/**
* quilleditor 안에서 삽입된 이미지를 서버에 저장하는 메소드
* @form-data 서버에 저장된 이미지 경로와 이름
* @return
*/
@ParameterCheck
@PostMapping("/upload")
public ApiResponse<String> uploadImage(@ReqMap MultipartFile file) throws IOException {
if (file.isEmpty()) {
return ApiResponse.error(HttpStatus.BAD_REQUEST, "File is empty");
}
String originalFileName = file.getOriginalFilename();
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
String fileName = UUID.randomUUID().toString() + fileExtension;
Path filePath = Paths.get(boardFilePath, fileName);
Files.createDirectories(filePath.getParent());
Files.write(filePath, file.getBytes());
String fileUrl = "upload/img/board/" + fileName;
return ApiResponse.ok(fileUrl);
}
}