Skip to content

Commit

Permalink
[refactor] 이미지 DB연동 명확하게 구현 - #212
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarlgnszx committed Jul 30, 2024
1 parent 9a1ff6a commit 8a4d9ba
Showing 1 changed file with 5 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.dateroad.image.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,22 +19,15 @@ public class ImageCleanupService {

@Scheduled(cron = "0 0 4 * * ?")
@Transactional
public void findUnusedImages() throws IOException {
public void cleanupUnusedImages() throws IOException {
String prefix = "course/";
List<String> s3Keys = s3Service.getAllImageKeys(prefix);
List<Image> dbImages = imageRepository.findAll();
List<Image> imagesToDelete = new ArrayList<>();
List<Image> imagesToDeleteFromDb = dbImages.stream()
.filter(image -> !s3Keys.contains(image.getImageUrl()))
.toList();
imageRepository.deleteAllInBatch(imagesToDeleteFromDb);

for (Image dbImage : dbImages) {
if (!s3Keys.contains(dbImage.getImageUrl())) {
imagesToDelete.add(dbImage);
}
}

// DB에서 한 번에 삭제
if (!imagesToDelete.isEmpty()) {
imageRepository.deleteAllInBatch(imagesToDelete);
}

// DB에 없는 S3 키를 찾아 S3에서 삭제
for (String s3Key : s3Keys) {
Expand Down

0 comments on commit 8a4d9ba

Please sign in to comment.