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

[#88] imageUrls null 에러 해결 #89

Merged
merged 1 commit into from
May 4, 2024
Merged
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 @@ -17,4 +17,8 @@ public interface ImageRepository extends JpaRepository<Image, Long> {
@Modifying
@Query("delete from Image i where i.path in :paths")
void deletePaths(@Param("paths") List<String> paths);

@Modifying
@Query("delete from Image i where i in :images")
void deleteImages(@Param("images") List<Image> images);
}
10 changes: 10 additions & 0 deletions src/main/java/dynamicquad/agilehub/issue/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public void saveImages(Issue issue, List<MultipartFile> files, String workingDir

public void cleanupMismatchedImages(Issue issue, List<String> imageUrls, String workingDirectory) {
List<Image> 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<String> deleteImagePath = images.stream()
.map(Image::getPath)
.filter(path -> !imageUrls.contains(path))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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)
Expand Down
Loading