From 65992f4ab071f9e08714a8626d68a8b29d729dd8 Mon Sep 17 00:00:00 2001 From: Johyunik Date: Sat, 2 Dec 2023 16:40:17 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=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 --- .../java/com/weatherfit/board/service/BoardService.java | 8 ++++++-- .../java/com/weatherfit/board/service/ImageService.java | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/weatherfit/board/service/BoardService.java b/src/main/java/com/weatherfit/board/service/BoardService.java index 18aaea7..71f7619 100644 --- a/src/main/java/com/weatherfit/board/service/BoardService.java +++ b/src/main/java/com/weatherfit/board/service/BoardService.java @@ -33,7 +33,7 @@ public class BoardService { private final ImageService imageService; @Autowired private LikeService likeService; - + private final AmazonS3Client amazonS3Client; // 게시글 전체 조회 public List findAll() { @@ -156,7 +156,11 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId)); ObjectMapper objectMapper = new ObjectMapper(); - imageRepository.deleteByBoardId(originalBoard); + + // 기존 이미지 삭제 + for (ImageEntity image : originalBoard.getImages()) { + imageService.deleteImage(image.getImage_url()); + } BoardUpdateDTO boardUpdateDTO; try { diff --git a/src/main/java/com/weatherfit/board/service/ImageService.java b/src/main/java/com/weatherfit/board/service/ImageService.java index bd0ebe3..4a69d81 100644 --- a/src/main/java/com/weatherfit/board/service/ImageService.java +++ b/src/main/java/com/weatherfit/board/service/ImageService.java @@ -24,6 +24,7 @@ public class ImageService { @Value("${cloud.aws.s3.bucket}") private String bucketName; + public String saveImage(MultipartFile file) { try { String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); @@ -48,6 +49,10 @@ public String saveImage(MultipartFile file) { throw new RuntimeException("Failed to upload image to S3", e); } } + public void deleteImage(String imageUrl) { + String imageName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); + amazonS3Client.deleteObject(bucketName, imageName); + } }