ADD scheduled cleanup for temporary directories in DirectoryService
This commit is contained in:
@@ -2,8 +2,10 @@ package com.ddf.vodsystem;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableScheduling
|
||||||
public class VodSystemApplication {
|
public class VodSystemApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.ddf.vodsystem.services;
|
|||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@@ -28,6 +29,8 @@ public class DirectoryService {
|
|||||||
@Value("${storage.temp.outputs}")
|
@Value("${storage.temp.outputs}")
|
||||||
private String tempOutputsDir;
|
private String tempOutputsDir;
|
||||||
|
|
||||||
|
private static final long TEMP_DIR_TIMELIMIT = 3 * 60 * 60 * (long) 1000; // 3 hours
|
||||||
|
|
||||||
public File getTempInputFile(String id, String extension) {
|
public File getTempInputFile(String id, String extension) {
|
||||||
String dir = tempInputsDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension);
|
String dir = tempInputsDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension);
|
||||||
return new File(dir);
|
return new File(dir);
|
||||||
@@ -75,7 +78,7 @@ public class DirectoryService {
|
|||||||
return fileName.substring(dotIndex + 1);
|
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
|
// Create the directory if it doesn't exist
|
||||||
Path outputPath = Paths.get(dir);
|
Path outputPath = Paths.get(dir);
|
||||||
if (Files.notExists(outputPath)) {
|
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
|
@PostConstruct
|
||||||
public void createDirectoriesIfNotExist() throws IOException {
|
public void createDirectoriesIfNotExist() throws IOException {
|
||||||
createDirectory(tempInputsDir);
|
createDirectory(tempInputsDir);
|
||||||
createDirectory(tempOutputsDir);
|
createDirectory(tempOutputsDir);
|
||||||
createDirectory(outputDir);
|
createDirectory(outputDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedRate = (30 * 60 * 1000))
|
||||||
|
public void cleanTempDirectories() throws IOException {
|
||||||
|
cleanUpDirectory(tempInputsDir);
|
||||||
|
cleanUpDirectory(tempOutputsDir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user