diff --git a/src/main/java/solution/gdsc/PathPal/ImageNotFoundException.java b/src/main/java/solution/gdsc/PathPal/ImageNotFoundException.java new file mode 100644 index 0000000..732286e --- /dev/null +++ b/src/main/java/solution/gdsc/PathPal/ImageNotFoundException.java @@ -0,0 +1,10 @@ +package solution.gdsc.PathPal; + +import solution.gdsc.PathPal.global.error.ErrorCode; +import solution.gdsc.PathPal.global.error.baseExcaption.NotFoundException; + +public class ImageNotFoundException extends NotFoundException { + public ImageNotFoundException() { + super(ErrorCode.IMAGE_NOT_FOUND); + } +} diff --git a/src/main/java/solution/gdsc/PathPal/MainController.java b/src/main/java/solution/gdsc/PathPal/MainController.java index 9166c35..e1a57fd 100644 --- a/src/main/java/solution/gdsc/PathPal/MainController.java +++ b/src/main/java/solution/gdsc/PathPal/MainController.java @@ -1,5 +1,6 @@ package solution.gdsc.PathPal; +import io.swagger.v3.oas.annotations.Operation; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; @@ -9,33 +10,39 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Path; @RestController public class MainController { //@Value("${image.path}") private String path = "/home/hsk4991149/static/image/"; + //private String path = "/Users/hongseungtaeg/Desktop/project/GDSC-PathPal/PathPal/src/main/resources/static/"; @GetMapping + @Operation(summary = "index page", description = "index page Test") public String index() { return "hello world!"; } @PostMapping("/test") + @Operation(summary = "Post request body(json)", description = "request body(json) Test") public TestResponse test(@RequestBody TestRequest testRequest) { return TestResponse.from(testRequest.getName()); } @GetMapping("/image/{imageId}") + @Operation(summary = "이미지 조회", description = "WebSocket으로 저장된 이미지를 조회합니다.(Content-Type: image/jpeg)") public ResponseEntity viewImg(@PathVariable(name = "imageId") Integer imageId) throws IOException { final String fileFullPath = path + imageId + ".jpeg"; - System.out.println(fileFullPath); - Path path = new File(fileFullPath).toPath(); + File file = new File(fileFullPath); - FileSystemResource resource = new FileSystemResource(path); + if (!file.exists()) { + throw new ImageNotFoundException(); + } + + FileSystemResource resource = new FileSystemResource(file); return ResponseEntity.ok() - .contentType(MediaType.parseMediaType(Files.probeContentType(path))) + .contentType(MediaType.parseMediaType(Files.probeContentType(file.toPath()))) .body(resource); } } diff --git a/src/main/java/solution/gdsc/PathPal/global/config/SwaggerConfig.java b/src/main/java/solution/gdsc/PathPal/global/config/SwaggerConfig.java index 6cf3096..e19c2ba 100644 --- a/src/main/java/solution/gdsc/PathPal/global/config/SwaggerConfig.java +++ b/src/main/java/solution/gdsc/PathPal/global/config/SwaggerConfig.java @@ -13,8 +13,8 @@ @OpenAPIDefinition( info = @Info( - title = "Greenmate API 명세서", - description = "alpha-test를 위한 Greenmate API 명세서입니다.", + title = "PathPal API 명세서", + description = "GDSC Solution Challenge - PathPal API 명세서입니다.", version = "0.1" ) ) diff --git a/src/main/java/solution/gdsc/PathPal/global/config/WebSocketHandler.java b/src/main/java/solution/gdsc/PathPal/global/config/WebSocketHandler.java index a4bc9b5..a76fa47 100644 --- a/src/main/java/solution/gdsc/PathPal/global/config/WebSocketHandler.java +++ b/src/main/java/solution/gdsc/PathPal/global/config/WebSocketHandler.java @@ -18,6 +18,7 @@ public class WebSocketHandler extends BinaryWebSocketHandler { //@Value("${image.path:/home/hsk4991149/static/image/}") private String path = "/home/hsk4991149/static/image/"; + //private String path = "/Users/hongseungtaeg/Desktop/project/GDSC-PathPal/PathPal/src/main/resources/static/"; private final Set sessions = ConcurrentHashMap.newKeySet(); diff --git a/src/main/java/solution/gdsc/PathPal/global/error/ErrorCode.java b/src/main/java/solution/gdsc/PathPal/global/error/ErrorCode.java index e8e257c..9adc8a7 100644 --- a/src/main/java/solution/gdsc/PathPal/global/error/ErrorCode.java +++ b/src/main/java/solution/gdsc/PathPal/global/error/ErrorCode.java @@ -16,6 +16,9 @@ public enum ErrorCode { INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "C005", "서버 내부에서 에러가 발생하였습니다."), BAD_REQUEST(HttpStatus.BAD_REQUEST, "C006", "Bad Request"), + // image + IMAGE_NOT_FOUND(HttpStatus.BAD_REQUEST, "I001", "이미지를 찾을 수 없습니다."), + ; private final HttpStatus status;