-
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.
Merge pull request #54 from 7th-UMC/feature/53
Feat : 주기적으로 s3 이미지 삭제, 데이터베이스 삭제 기능 추가
- Loading branch information
Showing
7 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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,4 @@ | ||
package hsu.umc.server.service; | ||
|
||
public interface SchedulerService { | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/hsu/umc/server/service/SchedulerServiceImpl.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,47 @@ | ||
package hsu.umc.server.service; | ||
|
||
import hsu.umc.server.aws.s3.AmazonS3Manager; | ||
import hsu.umc.server.entity.Photo; | ||
import hsu.umc.server.entity.Uuid; | ||
import hsu.umc.server.repository.PhotoRepository; | ||
import hsu.umc.server.repository.UuidRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class SchedulerServiceImpl implements SchedulerService{ | ||
private final PhotoRepository photoRepository; | ||
private final UuidRepository uuidRepository; | ||
private final AmazonS3Manager s3Manager; | ||
@Scheduled(cron = "${cloud.aws.cron}") | ||
@Transactional | ||
public void deletePhoto() { | ||
List<Photo> photoList = photoRepository.findAll(); | ||
photoList.stream() | ||
.filter(photo -> Duration.between(photo.getCreatedAt(), LocalDateTime.now()).toHours()>=2) | ||
.forEach(photo -> { | ||
s3Manager.deleteFile(photo.getPhotoUrl()); | ||
|
||
String uuidUrl = extractUuidFromPhotoUrl(photo.getPhotoUrl()); | ||
Uuid uuid = uuidRepository.findByUuid(uuidUrl); | ||
|
||
photoRepository.delete(photo); | ||
uuidRepository.delete(uuid); | ||
}); | ||
} | ||
private String extractUuidFromPhotoUrl(String photoUrl){ | ||
String[] parts = photoUrl.split("/"); | ||
String uuid = parts[parts.length-1]; | ||
System.out.println(uuid); | ||
return uuid.replace(".png",""); | ||
} | ||
|
||
} |
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
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