Compare commits
2 Commits
htmx-usage
...
cleanup-cl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a46cd0814 | ||
|
|
3a2ff0fc5b |
@@ -1,5 +1,6 @@
|
||||
package com.gregor_lohaus.gtransfer;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ImportRuntimeHints;
|
||||
@@ -9,11 +10,17 @@ import com.gregor_lohaus.gtransfer.config.ConfigRuntimeHints;
|
||||
import com.gregor_lohaus.gtransfer.model.ModelRuntimeHints;
|
||||
import com.gregor_lohaus.gtransfer.native_image.HibernateRuntimeHints;
|
||||
import com.gregor_lohaus.gtransfer.native_image.WebRuntimeHints;
|
||||
import com.gregor_lohaus.gtransfer.services.filecleanup.FileCleanupService;
|
||||
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
@EnableScheduling
|
||||
@ImportRuntimeHints({ConfigRuntimeHints.class, HibernateRuntimeHints.class, ModelRuntimeHints.class, WebRuntimeHints.class})
|
||||
public class GtransferApplication {
|
||||
@Autowired
|
||||
private FileCleanupService cleanupService;
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GtransferApplication.class, args);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,6 @@ public class DefaultConfig {
|
||||
uc.maxDownloadLimit = 100;
|
||||
uc.maxExpiryDays = 30;
|
||||
uc.cleanupEnabled = true;
|
||||
uc.cleanupIntervalMs = 3600000L;
|
||||
c.uploadConfig = uc;
|
||||
|
||||
config = c;
|
||||
|
||||
@@ -11,6 +11,4 @@ public class UploadConfig implements TomlSerializable {
|
||||
public Integer maxExpiryDays;
|
||||
@Property(name = "cleanupEnabled")
|
||||
public Boolean cleanupEnabled;
|
||||
@Property(name = "cleanupIntervalMs")
|
||||
public Long cleanupIntervalMs;
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.gregor_lohaus.gtransfer.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.gregor_lohaus.gtransfer.services.filewriter.AbstractStorageService;
|
||||
|
||||
@RestController
|
||||
public class Env {
|
||||
@Autowired
|
||||
private ConfigurableEnvironment env;
|
||||
@Autowired
|
||||
private AbstractStorageService storageService;
|
||||
@GetMapping("/env")
|
||||
public String env() {
|
||||
StringBuilder b = new StringBuilder();
|
||||
MutablePropertySources sources = this.env.getPropertySources();
|
||||
sources.forEach((var m) -> {
|
||||
b.append(m.toString());
|
||||
b.append("\n");
|
||||
});
|
||||
b.append(storageService.getClass().toString());
|
||||
return b.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.gregor_lohaus.gtransfer.services;
|
||||
package com.gregor_lohaus.gtransfer.services.filecleanup;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -15,10 +15,11 @@ 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 Boolean enabled;
|
||||
public FileCleanupService(Boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
private static final Logger log = LoggerFactory.getLogger(FileCleanupService.class);
|
||||
|
||||
@Autowired
|
||||
@@ -27,17 +28,24 @@ public class FileCleanupService {
|
||||
@Autowired
|
||||
private AbstractStorageService storageService;
|
||||
|
||||
@Scheduled(fixedDelayString = "${gtransfer-config.upload.cleanupIntervalMs:3600000}")
|
||||
@Scheduled(fixedDelay = 30000)
|
||||
@Transactional
|
||||
public void cleanupExpiredFiles() {
|
||||
log.info("Cleaneup started");
|
||||
if (!enabled) {
|
||||
log.info("Cleaneup skipped");
|
||||
return;
|
||||
}
|
||||
List<File> expired = fileRepository.findExpired(LocalDateTime.now());
|
||||
if (expired.isEmpty()) return;
|
||||
if (expired.isEmpty()) {
|
||||
log.info("Nothing to clean up");
|
||||
return;
|
||||
};
|
||||
|
||||
for (File file : expired) {
|
||||
storageService.delete(file.getId());
|
||||
fileRepository.delete(file);
|
||||
}
|
||||
|
||||
log.info("Cleaned up {} expired file(s)", expired.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
package com.gregor_lohaus.gtransfer.services.filecleanup;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.gregor_lohaus.gtransfer.config.types.StorageServiceType;
|
||||
|
||||
@Configuration
|
||||
public class FileCleanupServiceConfiguration {
|
||||
@Bean
|
||||
public FileCleanupService fileCleanupService(
|
||||
@Value("${gtransfer-config.upload.cleanupEnabled}")
|
||||
Boolean enabled
|
||||
) {
|
||||
return new FileCleanupService(enabled);
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,10 @@ import java.nio.file.Path;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
import com.gregor_lohaus.gtransfer.config.types.StorageServiceType;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
public class StorageServiceConfiguration {
|
||||
|
||||
//TODO S3 implementation
|
||||
|
||||
Reference in New Issue
Block a user