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);