From 976bc804ee1b7a66c9804ac717c53ae728d2c82f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 7 Mar 2024 19:38:12 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=EC=98=A4=ED=83=80=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../drawwang/domain/board/controller/BoardController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/server/drawwang/domain/board/controller/BoardController.java b/src/main/java/server/drawwang/domain/board/controller/BoardController.java index 70ade44..8c60faa 100644 --- a/src/main/java/server/drawwang/domain/board/controller/BoardController.java +++ b/src/main/java/server/drawwang/domain/board/controller/BoardController.java @@ -33,7 +33,7 @@ public ResponseEntity listBoard() { } @PatchMapping("/{boardId}/like") - public ResponseEntity boaardLikes(@PathVariable Long boardId) { + public ResponseEntity boardLikes(@PathVariable Long boardId) { boardService.boardLike(boardId); return new ResponseEntity<>(HttpStatus.OK); } From 932fe70393e91cde2d181e7a5af8823d59bb0b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 7 Mar 2024 19:42:02 +0900 Subject: [PATCH 2/7] =?UTF-8?q?ImageId=20=EB=B0=98=ED=99=98=EA=B0=92=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/service/implementation/BoardServiceImpl.java | 2 +- src/main/java/server/drawwang/domain/file/FileStore.java | 8 -------- .../thread/service/implementation/ThreadServiceImpl.java | 3 +-- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/main/java/server/drawwang/domain/board/service/implementation/BoardServiceImpl.java b/src/main/java/server/drawwang/domain/board/service/implementation/BoardServiceImpl.java index 1945e1f..9030496 100644 --- a/src/main/java/server/drawwang/domain/board/service/implementation/BoardServiceImpl.java +++ b/src/main/java/server/drawwang/domain/board/service/implementation/BoardServiceImpl.java @@ -56,7 +56,7 @@ public List listBoard() { boardEntity.getId(), boardEntity.getUserName(), boardEntity.getThread().getId(), - fileStore.getPartialImagesPath(boardEntity.getImageId()), + boardEntity.getImageId(), boardEntity.getLikes(), boardEntity.getReports())) .toList(); diff --git a/src/main/java/server/drawwang/domain/file/FileStore.java b/src/main/java/server/drawwang/domain/file/FileStore.java index 8ccc5b2..2527329 100644 --- a/src/main/java/server/drawwang/domain/file/FileStore.java +++ b/src/main/java/server/drawwang/domain/file/FileStore.java @@ -21,14 +21,6 @@ public String getFullPath(String fileName) { return Paths.get(getFilePath(), fileName).toString(); } - public String getPartialImagesPath(String fileName) { - if(fileName.isEmpty()) { - return ""; - } - - return File.separator + Paths.get("static", "images", fileName); - } - public String storeFile(MultipartFile multipartFile) { if (multipartFile.isEmpty()) { diff --git a/src/main/java/server/drawwang/domain/thread/service/implementation/ThreadServiceImpl.java b/src/main/java/server/drawwang/domain/thread/service/implementation/ThreadServiceImpl.java index 045b7bf..7402568 100644 --- a/src/main/java/server/drawwang/domain/thread/service/implementation/ThreadServiceImpl.java +++ b/src/main/java/server/drawwang/domain/thread/service/implementation/ThreadServiceImpl.java @@ -47,13 +47,12 @@ public List getThread() { String kingImageId = Optional.ofNullable(kingBoardId) .flatMap(id -> boardRepository.findById(id).map(BoardEntity::getImageId)) .orElse(""); - String kingImageUrl = fileStore.getPartialImagesPath(kingImageId); return new ToThreadResponse( threadEntity.getId(), threadEntity.getThreadName(), kingBoardId, - kingImageUrl, + kingImageId, threadEntity.getExpirationDate() ); }) From cb5b803b633428e10226b794ef925e854cedb3a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 7 Mar 2024 20:56:29 +0900 Subject: [PATCH 3/7] =?UTF-8?q?Image=EC=9D=98=20Byte=EA=B0=92=EC=9D=84=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=ED=95=98=EB=8A=94=20API=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/controller/FileController.java | 38 +++++++++++++++++++ .../global/exception/CustomErrorCode.java | 1 + 2 files changed, 39 insertions(+) create mode 100644 src/main/java/server/drawwang/domain/file/controller/FileController.java diff --git a/src/main/java/server/drawwang/domain/file/controller/FileController.java b/src/main/java/server/drawwang/domain/file/controller/FileController.java new file mode 100644 index 0000000..254f7b4 --- /dev/null +++ b/src/main/java/server/drawwang/domain/file/controller/FileController.java @@ -0,0 +1,38 @@ +package server.drawwang.domain.file.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.core.io.Resource; +import org.springframework.core.io.UrlResource; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import server.drawwang.domain.file.FileStore; +import server.drawwang.global.exception.CustomErrorCode; +import server.drawwang.global.exception.CustomException; + +@RequiredArgsConstructor +@ResponseBody +@RestController("/api/image") +public class FileController { + private final FileStore fileStore; + + @GetMapping("/{filename}") + public ResponseEntity downloadImage(@PathVariable String filename) { + try { + Resource resource = new UrlResource("file:" + fileStore.getFullPath(filename)); + + if(resource.exists() && resource.isReadable()) { + byte[] imageBytes = resource.getInputStream().readAllBytes(); + + return new ResponseEntity<>(imageBytes, HttpStatus.OK); + } + else { + throw new CustomException(CustomErrorCode.IMAGE_NOT_FOUND_ERROR); + } + } catch (Exception e) { + throw new CustomException(CustomErrorCode.IMAGE_NOT_FOUND_ERROR); + } + } +} + + diff --git a/src/main/java/server/drawwang/global/exception/CustomErrorCode.java b/src/main/java/server/drawwang/global/exception/CustomErrorCode.java index 2656420..febd663 100644 --- a/src/main/java/server/drawwang/global/exception/CustomErrorCode.java +++ b/src/main/java/server/drawwang/global/exception/CustomErrorCode.java @@ -12,6 +12,7 @@ public enum CustomErrorCode { THREAD_KING_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "쓰레드 왕의 게시물을 찾을 수 없습니다."), THREAD_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "쓰레드를 찾을 수 없습니다."), BOARD_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "게시물을 찾을 수 없습니다."), + IMAGE_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "이미지를 찾을 수 없습니다."), //==500==// FILE_PROCESSING_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "파일을 처리할 수 없습니다."), From dd5fd4cedf5ebd094949d0a3ec5ac9ceba446bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 7 Mar 2024 20:58:53 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EB=AA=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/{Listener => listener}/BoardLikedEventListener.java | 2 +- .../{Listener => listener}/SubmittedBoardEventListener.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/server/drawwang/domain/board/service/implementation/event/{Listener => listener}/BoardLikedEventListener.java (99%) rename src/main/java/server/drawwang/domain/board/service/implementation/event/{Listener => listener}/SubmittedBoardEventListener.java (99%) diff --git a/src/main/java/server/drawwang/domain/board/service/implementation/event/Listener/BoardLikedEventListener.java b/src/main/java/server/drawwang/domain/board/service/implementation/event/listener/BoardLikedEventListener.java similarity index 99% rename from src/main/java/server/drawwang/domain/board/service/implementation/event/Listener/BoardLikedEventListener.java rename to src/main/java/server/drawwang/domain/board/service/implementation/event/listener/BoardLikedEventListener.java index 4ccb378..9207ea9 100644 --- a/src/main/java/server/drawwang/domain/board/service/implementation/event/Listener/BoardLikedEventListener.java +++ b/src/main/java/server/drawwang/domain/board/service/implementation/event/listener/BoardLikedEventListener.java @@ -1,4 +1,4 @@ -package server.drawwang.domain.board.service.implementation.event.Listener; +package server.drawwang.domain.board.service.implementation.event.listener; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; diff --git a/src/main/java/server/drawwang/domain/board/service/implementation/event/Listener/SubmittedBoardEventListener.java b/src/main/java/server/drawwang/domain/board/service/implementation/event/listener/SubmittedBoardEventListener.java similarity index 99% rename from src/main/java/server/drawwang/domain/board/service/implementation/event/Listener/SubmittedBoardEventListener.java rename to src/main/java/server/drawwang/domain/board/service/implementation/event/listener/SubmittedBoardEventListener.java index 3ffa7b1..fbac311 100644 --- a/src/main/java/server/drawwang/domain/board/service/implementation/event/Listener/SubmittedBoardEventListener.java +++ b/src/main/java/server/drawwang/domain/board/service/implementation/event/listener/SubmittedBoardEventListener.java @@ -1,4 +1,4 @@ -package server.drawwang.domain.board.service.implementation.event.Listener; +package server.drawwang.domain.board.service.implementation.event.listener; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; From ad63aba8e50ec92807487a07351a4cbc657e8da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 7 Mar 2024 21:20:11 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../drawwang/domain/file/controller/FileController.java | 4 ++-- .../thread/service/implementation/ThreadServiceImpl.java | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/server/drawwang/domain/file/controller/FileController.java b/src/main/java/server/drawwang/domain/file/controller/FileController.java index 254f7b4..1a34f7e 100644 --- a/src/main/java/server/drawwang/domain/file/controller/FileController.java +++ b/src/main/java/server/drawwang/domain/file/controller/FileController.java @@ -11,8 +11,8 @@ import server.drawwang.global.exception.CustomException; @RequiredArgsConstructor -@ResponseBody -@RestController("/api/image") +@RestController +@RequestMapping("/api/image") public class FileController { private final FileStore fileStore; diff --git a/src/main/java/server/drawwang/domain/thread/service/implementation/ThreadServiceImpl.java b/src/main/java/server/drawwang/domain/thread/service/implementation/ThreadServiceImpl.java index 7402568..342846b 100644 --- a/src/main/java/server/drawwang/domain/thread/service/implementation/ThreadServiceImpl.java +++ b/src/main/java/server/drawwang/domain/thread/service/implementation/ThreadServiceImpl.java @@ -5,7 +5,6 @@ import org.springframework.transaction.annotation.Transactional; import server.drawwang.domain.board.entity.BoardEntity; import server.drawwang.domain.board.repository.BoardRepository; -import server.drawwang.domain.file.FileStore; import server.drawwang.domain.thread.entity.ThreadEntity; import server.drawwang.domain.thread.entity.dto.request.CreateThreadRequest; import server.drawwang.domain.thread.entity.dto.response.ToThreadResponse; @@ -23,7 +22,6 @@ public class ThreadServiceImpl implements ThreadService { private final ThreadRepository threadRepository; private final BoardRepository boardRepository; - private final FileStore fileStore; @Override @Transactional(rollbackFor = {Exception.class}) From 7d7b94afbe87ab52dc628ad38b7ed510e4fc563d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Thu, 7 Mar 2024 23:32:49 +0900 Subject: [PATCH 6/7] =?UTF-8?q?ContentType=EC=9D=84=20=EB=AA=85=EC=8B=9C?= =?UTF-8?q?=ED=95=98=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../drawwang/domain/file/controller/FileController.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/server/drawwang/domain/file/controller/FileController.java b/src/main/java/server/drawwang/domain/file/controller/FileController.java index 1a34f7e..11e6836 100644 --- a/src/main/java/server/drawwang/domain/file/controller/FileController.java +++ b/src/main/java/server/drawwang/domain/file/controller/FileController.java @@ -3,7 +3,9 @@ import lombok.RequiredArgsConstructor; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import server.drawwang.domain.file.FileStore; @@ -22,9 +24,11 @@ public ResponseEntity downloadImage(@PathVariable String filename) { Resource resource = new UrlResource("file:" + fileStore.getFullPath(filename)); if(resource.exists() && resource.isReadable()) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.IMAGE_PNG); byte[] imageBytes = resource.getInputStream().readAllBytes(); - return new ResponseEntity<>(imageBytes, HttpStatus.OK); + return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK); } else { throw new CustomException(CustomErrorCode.IMAGE_NOT_FOUND_ERROR); From e361a443e1406d46bbc80a600e30439f4983aac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A4=80?= <11dlguswns@naver.com> Date: Mon, 11 Mar 2024 17:52:11 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=EB=8B=A4=EC=A4=91=20image=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/controller/FileController.java | 36 ++++--------- .../domain/file/entity/ToFileResponse.java | 13 +++++ .../entity/dto/response/FileListResponse.java | 13 +++++ .../domain/file/service/FileService.java | 9 ++++ .../implementation/FileServiceImpl.java | 52 +++++++++++++++++++ 5 files changed, 97 insertions(+), 26 deletions(-) create mode 100644 src/main/java/server/drawwang/domain/file/entity/ToFileResponse.java create mode 100644 src/main/java/server/drawwang/domain/file/entity/dto/response/FileListResponse.java create mode 100644 src/main/java/server/drawwang/domain/file/service/FileService.java create mode 100644 src/main/java/server/drawwang/domain/file/service/implementation/FileServiceImpl.java diff --git a/src/main/java/server/drawwang/domain/file/controller/FileController.java b/src/main/java/server/drawwang/domain/file/controller/FileController.java index 11e6836..fafb201 100644 --- a/src/main/java/server/drawwang/domain/file/controller/FileController.java +++ b/src/main/java/server/drawwang/domain/file/controller/FileController.java @@ -1,41 +1,25 @@ package server.drawwang.domain.file.controller; import lombok.RequiredArgsConstructor; -import org.springframework.core.io.Resource; -import org.springframework.core.io.UrlResource; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import server.drawwang.domain.file.FileStore; -import server.drawwang.global.exception.CustomErrorCode; -import server.drawwang.global.exception.CustomException; +import server.drawwang.domain.file.entity.ToFileResponse; +import server.drawwang.domain.file.entity.dto.response.FileListResponse; +import server.drawwang.domain.file.service.FileService; + +import java.util.List; @RequiredArgsConstructor @RestController @RequestMapping("/api/image") public class FileController { - private final FileStore fileStore; - - @GetMapping("/{filename}") - public ResponseEntity downloadImage(@PathVariable String filename) { - try { - Resource resource = new UrlResource("file:" + fileStore.getFullPath(filename)); - - if(resource.exists() && resource.isReadable()) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.IMAGE_PNG); - byte[] imageBytes = resource.getInputStream().readAllBytes(); + private final FileService fileService; - return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK); - } - else { - throw new CustomException(CustomErrorCode.IMAGE_NOT_FOUND_ERROR); - } - } catch (Exception e) { - throw new CustomException(CustomErrorCode.IMAGE_NOT_FOUND_ERROR); - } + @GetMapping + public ResponseEntity downloadImage(@RequestParam("boardId") long[] paramBoardId) { + List responses = fileService.listImage(paramBoardId); + return new ResponseEntity<>(new FileListResponse(responses), HttpStatus.OK); } } diff --git a/src/main/java/server/drawwang/domain/file/entity/ToFileResponse.java b/src/main/java/server/drawwang/domain/file/entity/ToFileResponse.java new file mode 100644 index 0000000..ad8406b --- /dev/null +++ b/src/main/java/server/drawwang/domain/file/entity/ToFileResponse.java @@ -0,0 +1,13 @@ +package server.drawwang.domain.file.entity; + + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ToFileResponse { + + private final long boardId; + private final byte[] imageByte; +} diff --git a/src/main/java/server/drawwang/domain/file/entity/dto/response/FileListResponse.java b/src/main/java/server/drawwang/domain/file/entity/dto/response/FileListResponse.java new file mode 100644 index 0000000..32af0fa --- /dev/null +++ b/src/main/java/server/drawwang/domain/file/entity/dto/response/FileListResponse.java @@ -0,0 +1,13 @@ +package server.drawwang.domain.file.entity.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import server.drawwang.domain.file.entity.ToFileResponse; + +import java.util.List; + +@Getter +@AllArgsConstructor +public class FileListResponse { + List imageBytes; +} diff --git a/src/main/java/server/drawwang/domain/file/service/FileService.java b/src/main/java/server/drawwang/domain/file/service/FileService.java new file mode 100644 index 0000000..c49c2ca --- /dev/null +++ b/src/main/java/server/drawwang/domain/file/service/FileService.java @@ -0,0 +1,9 @@ +package server.drawwang.domain.file.service; + +import server.drawwang.domain.file.entity.ToFileResponse; + +import java.util.List; + +public interface FileService { + List listImage(long[] paramBoardId); +} diff --git a/src/main/java/server/drawwang/domain/file/service/implementation/FileServiceImpl.java b/src/main/java/server/drawwang/domain/file/service/implementation/FileServiceImpl.java new file mode 100644 index 0000000..22d03c9 --- /dev/null +++ b/src/main/java/server/drawwang/domain/file/service/implementation/FileServiceImpl.java @@ -0,0 +1,52 @@ +package server.drawwang.domain.file.service.implementation; + +import lombok.RequiredArgsConstructor; +import org.springframework.core.io.Resource; +import org.springframework.core.io.UrlResource; +import org.springframework.stereotype.Service; +import server.drawwang.domain.board.entity.BoardEntity; +import server.drawwang.domain.board.repository.BoardRepository; +import server.drawwang.domain.file.FileStore; +import server.drawwang.domain.file.entity.ToFileResponse; +import server.drawwang.domain.file.service.FileService; +import server.drawwang.global.exception.CustomErrorCode; +import server.drawwang.global.exception.CustomException; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class FileServiceImpl implements FileService { + + private final BoardRepository boardRepository; + private final FileStore fileStore; + + @Override + public List listImage(long[] paramBoardId) { + try { + List responses = new ArrayList<>(); + for (long boardId : paramBoardId) { + BoardEntity boardEntity = boardRepository.findById(boardId) + .orElseThrow(() -> new CustomException(CustomErrorCode.BOARD_NOT_FOUND_ERROR)); + Resource resource = new UrlResource("file:" + fileStore.getFullPath(boardEntity.getImageId())); + + if (resource.exists() && resource.isReadable()) { + byte[] imageByte = resource.getInputStream().readAllBytes(); + responses.add(new ToFileResponse(boardId, imageByte)); + + } else { + throw new CustomException(CustomErrorCode.IMAGE_NOT_FOUND_ERROR); + } + } + return responses; + + } catch (MalformedURLException e) { + throw new CustomException(CustomErrorCode.IMAGE_NOT_FOUND_ERROR); + } catch (IOException e) { + throw new CustomException(CustomErrorCode.FILE_PROCESSING_ERROR); + } + } +}