From a14667fc6719ad3a51609c12e674c967237225a5 Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Sat, 31 Aug 2024 18:58:13 +0900 Subject: [PATCH] =?UTF-8?q?Feat=20:=20=EC=A3=BC=EA=B8=B0=EC=A0=81=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20s3=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C,=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hsu/umc/server/ServerApplication.java | 2 + .../umc/server/aws/s3/AmazonS3Manager.java | 4 ++ .../umc/server/repository/UuidRepository.java | 1 + .../umc/server/service/SchedulerService.java | 4 ++ .../server/service/SchedulerServiceImpl.java | 47 +++++++++++++++++++ src/main/resources/application-dev.yml | 1 + src/main/resources/application-local.yml | 1 + 7 files changed, 60 insertions(+) create mode 100644 src/main/java/hsu/umc/server/service/SchedulerService.java create mode 100644 src/main/java/hsu/umc/server/service/SchedulerServiceImpl.java diff --git a/src/main/java/hsu/umc/server/ServerApplication.java b/src/main/java/hsu/umc/server/ServerApplication.java index 5251408..050b71c 100644 --- a/src/main/java/hsu/umc/server/ServerApplication.java +++ b/src/main/java/hsu/umc/server/ServerApplication.java @@ -4,12 +4,14 @@ import io.swagger.v3.oas.annotations.servers.Server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @OpenAPIDefinition( servers = { @Server(url = "/",description = "Dev Server URL") } ) +@EnableScheduling @SpringBootApplication public class ServerApplication { diff --git a/src/main/java/hsu/umc/server/aws/s3/AmazonS3Manager.java b/src/main/java/hsu/umc/server/aws/s3/AmazonS3Manager.java index 69fd8cb..9e31fb7 100644 --- a/src/main/java/hsu/umc/server/aws/s3/AmazonS3Manager.java +++ b/src/main/java/hsu/umc/server/aws/s3/AmazonS3Manager.java @@ -1,6 +1,7 @@ package hsu.umc.server.aws.s3; import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.DeleteObjectRequest; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; import hsu.umc.server.config.AmazonConfig; @@ -25,6 +26,9 @@ public String uploadFile(String keyName, MultipartFile file)throws IOException { amazonS3.putObject(new PutObjectRequest(amazonConfig.getBucket(), keyName, file.getInputStream(), metadata)); return amazonS3.getUrl(amazonConfig.getBucket(),keyName).toString(); } + public void deleteFile(String photoUrl){ + amazonS3.deleteObject(new DeleteObjectRequest(amazonConfig.getBucket(),photoUrl)); + } public String generatePhotoKeyName(Uuid uuid) { return amazonConfig.getPhotoPath() + '/' + uuid.getUuid() + ".png"; } diff --git a/src/main/java/hsu/umc/server/repository/UuidRepository.java b/src/main/java/hsu/umc/server/repository/UuidRepository.java index bdbf6bb..59f24b5 100644 --- a/src/main/java/hsu/umc/server/repository/UuidRepository.java +++ b/src/main/java/hsu/umc/server/repository/UuidRepository.java @@ -4,4 +4,5 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface UuidRepository extends JpaRepository { + Uuid findByUuid(String uuidUrl); } diff --git a/src/main/java/hsu/umc/server/service/SchedulerService.java b/src/main/java/hsu/umc/server/service/SchedulerService.java new file mode 100644 index 0000000..a700a98 --- /dev/null +++ b/src/main/java/hsu/umc/server/service/SchedulerService.java @@ -0,0 +1,4 @@ +package hsu.umc.server.service; + +public interface SchedulerService { +} diff --git a/src/main/java/hsu/umc/server/service/SchedulerServiceImpl.java b/src/main/java/hsu/umc/server/service/SchedulerServiceImpl.java new file mode 100644 index 0000000..b9174d4 --- /dev/null +++ b/src/main/java/hsu/umc/server/service/SchedulerServiceImpl.java @@ -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 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",""); + } + +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 4181a92..af4d031 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -31,6 +31,7 @@ cloud: static: ap-northeast-2 stack: auto: false + cron: "0 0 */1 * * *" credentials: accessKey: ${aws.access.key.id} secretKey: ${aws.secret.access.key} diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 2f92b7d..3dc43f3 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -32,6 +32,7 @@ cloud: static: ap-northeast-2 stack: auto: false + cron: "0 */2 * * * *" credentials: accessKey: ${aws.access.key.id} secretKey: ${aws.secret.access.key} \ No newline at end of file