diff --git a/src/main/java/com/ddf/vodsystem/VodSystemApplication.java b/src/main/java/com/ddf/vodsystem/VodSystemApplication.java index f8c3ef9..17f4d61 100644 --- a/src/main/java/com/ddf/vodsystem/VodSystemApplication.java +++ b/src/main/java/com/ddf/vodsystem/VodSystemApplication.java @@ -2,8 +2,10 @@ package com.ddf.vodsystem; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class VodSystemApplication { public static void main(String[] args) { diff --git a/src/main/java/com/ddf/vodsystem/services/DirectoryService.java b/src/main/java/com/ddf/vodsystem/services/DirectoryService.java index efc8fc4..698b2e2 100644 --- a/src/main/java/com/ddf/vodsystem/services/DirectoryService.java +++ b/src/main/java/com/ddf/vodsystem/services/DirectoryService.java @@ -2,6 +2,7 @@ package com.ddf.vodsystem.services; import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -28,6 +29,8 @@ public class DirectoryService { @Value("${storage.temp.outputs}") private String tempOutputsDir; + private static final long TEMP_DIR_TIMELIMIT = 3 * 60 * 60 * (long) 1000; // 3 hours + public File getTempInputFile(String id, String extension) { String dir = tempInputsDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension); return new File(dir); @@ -75,7 +78,7 @@ public class DirectoryService { return fileName.substring(dotIndex + 1); } - public void createDirectory(String dir) throws IOException { + private void createDirectory(String dir) throws IOException { // Create the directory if it doesn't exist Path outputPath = Paths.get(dir); if (Files.notExists(outputPath)) { @@ -84,10 +87,33 @@ public class DirectoryService { } } + private void cleanUpDirectory(String dir) throws IOException { + File file = new File(dir); + File[] files = file.listFiles(); + + if (files == null) { + logger.warn("No files found in directory: {}", dir); + return; + } + + for (File f : files){ + if (f.isFile() && f.lastModified() < (System.currentTimeMillis() - TEMP_DIR_TIMELIMIT)) { + Files.delete(f.toPath()); + } + } + + } + @PostConstruct public void createDirectoriesIfNotExist() throws IOException { createDirectory(tempInputsDir); createDirectory(tempOutputsDir); createDirectory(outputDir); } + + @Scheduled(fixedRate = (30 * 60 * 1000)) + public void cleanTempDirectories() throws IOException { + cleanUpDirectory(tempInputsDir); + cleanUpDirectory(tempOutputsDir); + } } \ No newline at end of file