diff --git a/src/main/java/dynamicquad/agilehub/issue/domain/image/ImageRepository.java b/src/main/java/dynamicquad/agilehub/issue/domain/image/ImageRepository.java index ffa24b6..52e435a 100644 --- a/src/main/java/dynamicquad/agilehub/issue/domain/image/ImageRepository.java +++ b/src/main/java/dynamicquad/agilehub/issue/domain/image/ImageRepository.java @@ -17,4 +17,8 @@ public interface ImageRepository extends JpaRepository { @Modifying @Query("delete from Image i where i.path in :paths") void deletePaths(@Param("paths") List paths); + + @Modifying + @Query("delete from Image i where i in :images") + void deleteImages(@Param("images") List images); } diff --git a/src/main/java/dynamicquad/agilehub/issue/service/ImageService.java b/src/main/java/dynamicquad/agilehub/issue/service/ImageService.java index 0e703fa..a9d26fe 100644 --- a/src/main/java/dynamicquad/agilehub/issue/service/ImageService.java +++ b/src/main/java/dynamicquad/agilehub/issue/service/ImageService.java @@ -33,6 +33,16 @@ public void saveImages(Issue issue, List files, String workingDir public void cleanupMismatchedImages(Issue issue, List imageUrls, String workingDirectory) { List images = imageRepository.findByIssue(issue); + + if (images.isEmpty()) { + return; + } + if (imageUrls == null || imageUrls.isEmpty()) { + photoS3Manager.deletePhotos(images.stream().map(Image::getPath).toList(), workingDirectory); + imageRepository.deleteImages(images); + return; + } + List deleteImagePath = images.stream() .map(Image::getPath) .filter(path -> !imageUrls.contains(path)) diff --git a/src/test/java/dynamicquad/agilehub/issue/service/ImageServiceTest.java b/src/test/java/dynamicquad/agilehub/issue/service/ImageServiceTest.java index 0b31e8e..3784d7d 100644 --- a/src/test/java/dynamicquad/agilehub/issue/service/ImageServiceTest.java +++ b/src/test/java/dynamicquad/agilehub/issue/service/ImageServiceTest.java @@ -110,6 +110,51 @@ class ImageServiceTest { assertThat(em.find(Image.class, image1.getId())).isNotNull(); } + @Test + @Transactional + void 이미지링크를_안넘겼을때_모두_삭제() { + //given + Image image1 = Image.builder() + .path("https://file1.jpg") + .build(); + em.persist(image1); + + Image image2 = Image.builder() + .path("https://file2.jpg") + .build(); + em.persist(image2); + + Image image3 = Image.builder() + .path("https://file2.jpg") + .build(); + em.persist(image3); + + Epic epic = Epic.builder() + .title("이슈1") + .content("이슈1 내용") + .status(IssueStatus.DO) + .build(); + em.persist(epic); + + image1.setIssue(epic); + image2.setIssue(epic); + image3.setIssue(epic); + + List files = null; + when(photoS3Manager.deletePhotos(files, "/issue")).thenReturn(true); + + //when + imageService.cleanupMismatchedImages(epic, files, "/issue"); + + //then + em.flush(); + em.clear(); + assertThat(em.find(Image.class, image2.getId())).isNull(); + assertThat(em.find(Image.class, image1.getId())).isNull(); + assertThat(em.find(Image.class, image3.getId())).isNull(); + } + + private Project createProject(String projectName, String projectKey) { return Project.builder() .name(projectName)