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); } 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/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; 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/file/controller/FileController.java b/src/main/java/server/drawwang/domain/file/controller/FileController.java new file mode 100644 index 0000000..fafb201 --- /dev/null +++ b/src/main/java/server/drawwang/domain/file/controller/FileController.java @@ -0,0 +1,26 @@ +package server.drawwang.domain.file.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +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 FileService fileService; + + @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); + } + } +} 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..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}) @@ -47,13 +45,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() ); }) 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, "파일을 처리할 수 없습니다."),