scheduled cleanup

This commit is contained in:
Gregor Lohaus
2026-02-25 15:10:34 +01:00
parent 84d8024604
commit 857a691e2b
6 changed files with 60 additions and 0 deletions

View File

@@ -2,3 +2,4 @@
.gradle .gradle
bin bin
build build
.devenv

View File

@@ -47,6 +47,8 @@ public class DefaultConfig {
UploadConfig uc = new UploadConfig(); UploadConfig uc = new UploadConfig();
uc.maxDownloadLimit = 100; uc.maxDownloadLimit = 100;
uc.maxExpiryDays = 30; uc.maxExpiryDays = 30;
uc.cleanupEnabled = true;
uc.cleanupIntervalMs = 3600000L;
c.uploadConfig = uc; c.uploadConfig = uc;
config = c; config = c;

View File

@@ -9,4 +9,8 @@ public class UploadConfig implements TomlSerializable {
public Integer maxDownloadLimit; public Integer maxDownloadLimit;
@Property(name = "maxExpiryDays") @Property(name = "maxExpiryDays")
public Integer maxExpiryDays; public Integer maxExpiryDays;
@Property(name = "cleanupEnabled")
public Boolean cleanupEnabled;
@Property(name = "cleanupIntervalMs")
public Long cleanupIntervalMs;
} }

View File

@@ -1,8 +1,16 @@
package com.gregor_lohaus.gtransfer.model; package com.gregor_lohaus.gtransfer.model;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface FileRepository extends JpaRepository<File, String> { public interface FileRepository extends JpaRepository<File, String> {
@Query("SELECT f FROM File f WHERE f.expireyDateTime IS NOT NULL AND f.expireyDateTime < :now")
List<File> findExpired(@Param("now") LocalDateTime now);
} }

View File

@@ -0,0 +1,43 @@
package com.gregor_lohaus.gtransfer.services;
import java.time.LocalDateTime;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.gregor_lohaus.gtransfer.model.File;
import com.gregor_lohaus.gtransfer.model.FileRepository;
import com.gregor_lohaus.gtransfer.services.filewriter.AbstractStorageService;
@Component
@ConditionalOnProperty(name = "gtransfer-config.upload.cleanupEnabled", havingValue = "true", matchIfMissing = true)
public class FileCleanupService {
private static final Logger log = LoggerFactory.getLogger(FileCleanupService.class);
@Autowired
private FileRepository fileRepository;
@Autowired
private AbstractStorageService storageService;
@Scheduled(fixedDelayString = "${gtransfer-config.upload.cleanupIntervalMs:3600000}")
@Transactional
public void cleanupExpiredFiles() {
List<File> expired = fileRepository.findExpired(LocalDateTime.now());
if (expired.isEmpty()) return;
for (File file : expired) {
storageService.delete(file.getId());
fileRepository.delete(file);
}
log.info("Cleaned up {} expired file(s)", expired.size());
}
}

View File

@@ -5,10 +5,12 @@ import java.nio.file.Path;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import com.gregor_lohaus.gtransfer.config.types.StorageServiceType; import com.gregor_lohaus.gtransfer.config.types.StorageServiceType;
@Configuration @Configuration
@EnableScheduling
public class StorageServiceConfiguration { public class StorageServiceConfiguration {
//TODO S3 implementation //TODO S3 implementation