Skip to content

Commit

Permalink
feat: 이미지 컨트롤러 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Daolove0323 committed Nov 9, 2024
1 parent 73c69fc commit 498292e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.ktc2.cokaen.wouldyouin._common.api.ApiResponse;
import org.ktc2.cokaen.wouldyouin._common.api.ApiResponseBody;
import org.ktc2.cokaen.wouldyouin._common.exception.FailToReadImageException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -37,11 +39,13 @@ public ResponseEntity<ApiResponseBody<List<ImageResponse>>> uploadImages(
return ApiResponse.ok(imageServiceFactory.getImageServiceByImageType(imageDomain).saveAndCreateImages(images));
}

// Todo: path 수정
@GetMapping(value = "{path}", produces = {MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE})
public ResponseEntity<byte[]> getImage(@PathVariable String path) {
// Todo: path 수정, service로 로직이동
@GetMapping(value = "images/{domain}/{path}", produces = {MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE})
public ResponseEntity<byte[]> getImage(@PathVariable String domain, @PathVariable String path) {
try {
return ResponseEntity.status(HttpStatus.OK).body(Files.readAllBytes(Paths.get("src/main/resources/static", path)));
Resource resource = new ClassPathResource("static/images/" + domain + "/" + path);
System.out.println(Paths.get("static/images", domain, path));
return ResponseEntity.status(HttpStatus.OK).body(Files.readAllBytes(resource.getFile().toPath()));
} catch (IOException e) {
throw new FailToReadImageException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
public class FailToReadImageException extends BusinessException {

public FailToReadImageException() {
super(ErrorCode.ENTITY_NOT_FOUND);
super(ErrorCode.FAIL_TO_READ_IMAGE);
}
}

0 comments on commit 498292e

Please sign in to comment.