From 7bc70c8b34cb3a15d2d663df6c013b75e59455f8 Mon Sep 17 00:00:00 2001 From: Johyunik Date: Mon, 4 Dec 2023 12:50:48 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/service/BoardService.java | 20 ++++++++++--------- .../board/service/ImageService.java | 6 ++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/weatherfit/board/service/BoardService.java b/src/main/java/com/weatherfit/board/service/BoardService.java index 44c8d04..1828463 100644 --- a/src/main/java/com/weatherfit/board/service/BoardService.java +++ b/src/main/java/com/weatherfit/board/service/BoardService.java @@ -170,6 +170,17 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St throw new RuntimeException(e); } + + if (boardUpdateDTO.getDeletedImages() != null) { + for (Integer imageId : boardUpdateDTO.getDeletedImages()) { + Optional optionalImageEntity = imageRepository.findById(imageId); + ImageEntity imageEntity = optionalImageEntity.orElseThrow(() -> new IllegalArgumentException("해당 이미지가 존재하지 않습니다. id=" + imageId)); + + imageRepository.deleteById(imageId); + imageService.deleteImage(imageEntity.getImageUrl()); + } + } + for (MultipartFile image : images) { String imageUrl = imageService.saveImage(image); @@ -186,15 +197,6 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St } - if (boardUpdateDTO.getDeletedImages() != null) { - for (Integer imageId : boardUpdateDTO.getDeletedImages()) { - Optional optionalImageEntity = imageRepository.findById(imageId); - ImageEntity imageEntity = optionalImageEntity.orElseThrow(() -> new IllegalArgumentException("해당 이미지가 존재하지 않습니다. id=" + imageId)); - - imageRepository.deleteById(imageId); - imageService.deleteImage(imageEntity.getImageUrl()); - } - } BoardEntity boardEntity = BoardEntity.builder() .boardId(boardId) diff --git a/src/main/java/com/weatherfit/board/service/ImageService.java b/src/main/java/com/weatherfit/board/service/ImageService.java index 87a3ff9..fdec4b8 100644 --- a/src/main/java/com/weatherfit/board/service/ImageService.java +++ b/src/main/java/com/weatherfit/board/service/ImageService.java @@ -32,8 +32,9 @@ public String saveImage(MultipartFile file) { String originalFilename = file.getOriginalFilename(); String fileExtension = originalFilename.substring(originalFilename.lastIndexOf(".")); - // 파일 이름에 원래 이름을 사용합니다. - String fileName = originalFilename; + // UUID를 생성하고, 이를 파일 이름에 포함합니다. + String uuid = UUID.randomUUID().toString(); + String fileName = uuid + "_" + originalFilename; String fileUrl = "https://" + bucketName + ".s3.amazonaws.com/" + fileName; if (!amazonS3Client.doesObjectExist(bucketName, fileName)) { @@ -49,6 +50,7 @@ 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);