-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e4c81d
commit 9a1ff6a
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
dateroad-api/src/main/java/org/dateroad/image/service/ImageCleanupService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.dateroad.image.service; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import org.dateroad.image.domain.Image; | ||
import org.dateroad.image.repository.ImageRepository; | ||
import org.dateroad.s3.S3Service; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ImageCleanupService { | ||
private final S3Service s3Service; | ||
private final ImageRepository imageRepository; | ||
|
||
@Scheduled(cron = "0 0 4 * * ?") | ||
@Transactional | ||
public void findUnusedImages() throws IOException { | ||
String prefix = "course/"; | ||
List<String> s3Keys = s3Service.getAllImageKeys(prefix); | ||
List<Image> dbImages = imageRepository.findAll(); | ||
List<Image> imagesToDelete = new ArrayList<>(); | ||
|
||
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) { | ||
if (!dbImages.stream().map(Image::getImageUrl).toList().contains(s3Key)) { | ||
deleteImageFromS3(s3Key); | ||
} | ||
} | ||
} | ||
|
||
private void deleteImageFromS3(String key) throws IOException { | ||
s3Service.deleteImage(key); | ||
} | ||
} |