Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image logic 개발 #19

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ResponseEntity<BoardListResponse> listBoard() {
}

@PatchMapping("/{boardId}/like")
public ResponseEntity<Void> boaardLikes(@PathVariable Long boardId) {
public ResponseEntity<Void> boardLikes(@PathVariable Long boardId) {
boardService.boardLike(boardId);
return new ResponseEntity<>(HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public List<ToBoardResponse> listBoard() {
boardEntity.getId(),
boardEntity.getUserName(),
boardEntity.getThread().getId(),
fileStore.getPartialImagesPath(boardEntity.getImageId()),
boardEntity.getImageId(),
boardEntity.getLikes(),
boardEntity.getReports()))
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/server/drawwang/domain/file/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FileListResponse> downloadImage(@RequestParam("boardId") long[] paramBoardId) {
List<ToFileResponse> responses = fileService.listImage(paramBoardId);
return new ResponseEntity<>(new FileListResponse(responses), HttpStatus.OK);
}
}


Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<ToFileResponse> imageBytes;
}
Original file line number Diff line number Diff line change
@@ -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<ToFileResponse> listImage(long[] paramBoardId);
}
Original file line number Diff line number Diff line change
@@ -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<ToFileResponse> listImage(long[] paramBoardId) {
try {
List<ToFileResponse> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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})
Expand All @@ -47,13 +45,12 @@ public List<ToThreadResponse> 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()
);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "파일을 처리할 수 없습니다."),
Expand Down