refactor(directories): switch from File to Path objs
This commit is contained in:
@@ -10,7 +10,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Stream;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -20,135 +20,108 @@ public class DirectoryService {
|
||||
|
||||
private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DirectoryService.class);
|
||||
|
||||
@Value("${storage.outputs}")
|
||||
private String outputDir;
|
||||
@Value("${media.users}")
|
||||
private String usersDir;
|
||||
|
||||
@Value("${STREAM_DATA}")
|
||||
private String streamDir;
|
||||
@Value("${media.streams}")
|
||||
private String streamsDir;
|
||||
|
||||
@Value("${storage.temp.inputs}")
|
||||
@Value("${media.temp.inputs}")
|
||||
private String tempInputsDir;
|
||||
|
||||
@Value("${storage.temp.outputs}")
|
||||
@Value("${media.temp.outputs}")
|
||||
private String tempOutputsDir;
|
||||
|
||||
private static final long TEMP_DIR_TIME_LIMIT = 3 * 60 * 60 * (long) 1000; // 3 hours
|
||||
private static final long TEMP_DIR_CLEANUP_RATE = 30 * 60 * (long) 1000; // 30 minutes
|
||||
|
||||
public File getTempInputFile(String filename) {
|
||||
String dir = tempInputsDir + File.separator + filename;
|
||||
return new File(dir);
|
||||
private static final String VODS_DIR_NAME = "vods";
|
||||
private static final String CLIPS_DIR_NAME = "clips";
|
||||
private static final String THUMBNAIL_DIR_NAME = "thumbnails";
|
||||
|
||||
public Path getTempInputDir() {
|
||||
return Path.of(tempInputsDir);
|
||||
}
|
||||
|
||||
public File getTempOutputFile(String filename) {
|
||||
String dir = tempOutputsDir + File.separator + filename;
|
||||
return new File(dir);
|
||||
public Path getTempOutputDir() {
|
||||
return Path.of(tempOutputsDir);
|
||||
}
|
||||
|
||||
public File getUserFolder(Long userId) throws IOException {
|
||||
if (userId == null) {
|
||||
throw new IllegalArgumentException("User ID cannot be null or empty");
|
||||
}
|
||||
|
||||
String dir = outputDir + File.separator + userId;
|
||||
createDirectory(dir);
|
||||
return new File(dir);
|
||||
public Path getUserDir(Long userId) throws IOException {
|
||||
Path userDir = Path.of(usersDir + File.separator + userId);
|
||||
Files.createDirectories(userDir);
|
||||
return userDir;
|
||||
}
|
||||
|
||||
public File getUserClipsFile(Long userId, String filename) throws IOException {
|
||||
if (filename == null || filename.isEmpty()) {
|
||||
throw new IllegalArgumentException("File name cannot be null or empty");
|
||||
}
|
||||
|
||||
File userDir = getUserFolder(userId);
|
||||
String fullPath = userDir.getAbsolutePath() + File.separator + "clips" + File.separator + filename;
|
||||
File file = new File(fullPath);
|
||||
|
||||
createDirectory(file.getParent());
|
||||
return file;
|
||||
public Path getVodsDir(Long userId) throws IOException {
|
||||
Path vodDir = getUserDir(userId).resolve(VODS_DIR_NAME);
|
||||
Files.createDirectories(vodDir);
|
||||
return vodDir;
|
||||
}
|
||||
|
||||
public File getVodFile(Long userId, String filename) throws IOException {
|
||||
if (filename == null || filename.isEmpty()) {
|
||||
throw new IllegalArgumentException("File name cannot be null or empty");
|
||||
}
|
||||
|
||||
String userDir = getUserFolder(userId).getAbsolutePath();
|
||||
File file = new File(userDir + File.separator + "vods" + File.separator + filename);
|
||||
createDirectory(file.getParent());
|
||||
|
||||
return file;
|
||||
public Path getClipsDir(Long userId) throws IOException {
|
||||
Path clipsDir = getUserDir(userId).resolve(CLIPS_DIR_NAME);
|
||||
Files.createDirectories(clipsDir);
|
||||
return clipsDir;
|
||||
}
|
||||
|
||||
public File getStreamDir(String streamKey) throws IOException {
|
||||
File file = new File(streamDir + File.separator + streamKey);
|
||||
createDirectory(file.getAbsolutePath());
|
||||
return file;
|
||||
public Path getThumbnailsDir(Long userId) throws IOException {
|
||||
Path thumbnailsDir = getUserDir(userId).resolve(THUMBNAIL_DIR_NAME);
|
||||
Files.createDirectories(thumbnailsDir);
|
||||
return thumbnailsDir;
|
||||
}
|
||||
|
||||
public File getUserThumbnailsFile(Long userId, String filename) throws IOException {
|
||||
if (filename == null || filename.isEmpty()) {
|
||||
throw new IllegalArgumentException("File name cannot be null or empty");
|
||||
}
|
||||
|
||||
String userDir = getUserFolder(userId).getAbsolutePath();
|
||||
File file = new File(userDir + File.separator + "thumbnails" + File.separator + filename);
|
||||
createDirectory(file.getParent());
|
||||
|
||||
return file;
|
||||
public Path getStreamDir(String streamKey) throws IOException {
|
||||
Path streamFolder = Path.of(streamsDir, streamKey);
|
||||
Files.createDirectories(streamFolder);
|
||||
return streamFolder;
|
||||
}
|
||||
|
||||
public void saveMultipartFile(File file, MultipartFile multipartFile) throws IOException {
|
||||
createDirectory(file.getParent());
|
||||
Path filePath = Paths.get(file.getAbsolutePath());
|
||||
Files.copy(multipartFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||
public void saveMultipartFile(Path path, MultipartFile multipartFile) throws IOException {
|
||||
Files.createDirectories(path.getParent());
|
||||
Files.copy(multipartFile.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
public void copyFile(File source, File target) throws IOException {
|
||||
Path sourcePath = Paths.get(source.getAbsolutePath());
|
||||
Path destPath = Paths.get(target.getAbsolutePath());
|
||||
|
||||
Files.createDirectories(destPath.getParent());
|
||||
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
|
||||
logger.info("Copied file from {} to {}", sourcePath, destPath);
|
||||
}
|
||||
|
||||
public void cutFile(File source, File target) throws IOException {
|
||||
copyFile(source, target);
|
||||
Files.deleteIfExists(source.toPath());
|
||||
logger.info("Deleted source file: {}", source.getAbsolutePath());
|
||||
}
|
||||
|
||||
private void createDirectory(String dir) throws IOException {
|
||||
// Create the directory if it doesn't exist
|
||||
Path outputPath = Paths.get(dir);
|
||||
if (Files.notExists(outputPath)) {
|
||||
Files.createDirectories(outputPath);
|
||||
logger.info("Created directory: {}", outputPath);
|
||||
}
|
||||
public void copyFile(Path source, Path target) throws IOException {
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
|
||||
logger.info("Copied file from {} to {}", source, target);
|
||||
}
|
||||
|
||||
private void cleanUpDirectory(String dir) throws IOException {
|
||||
File file = new File(dir);
|
||||
File[] files = file.listFiles();
|
||||
Path dirPath = Path.of(dir);
|
||||
|
||||
if (files == null) {
|
||||
if (Files.notExists(dirPath)) {
|
||||
logger.warn("No files found in directory: {}", dir);
|
||||
return;
|
||||
}
|
||||
|
||||
for (File f : files){
|
||||
if (f.isFile() && f.lastModified() < (System.currentTimeMillis() - TEMP_DIR_TIME_LIMIT)) {
|
||||
Files.delete(f.toPath());
|
||||
}
|
||||
try (Stream<Path> files = Files.list(dirPath)) {
|
||||
files.filter(Files::isRegularFile)
|
||||
.filter(p -> {
|
||||
try {
|
||||
return Files.getLastModifiedTime(p).toMillis()
|
||||
< System.currentTimeMillis() - TEMP_DIR_TIME_LIMIT;
|
||||
} catch (IOException e) {
|
||||
logger.warn("Could not read last modified time for {}", p);
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.forEach(p -> {
|
||||
try {
|
||||
Files.delete(p);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Could not delete file {}", p);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void createDirectoriesIfNotExist() throws IOException {
|
||||
createDirectory(tempInputsDir);
|
||||
createDirectory(tempOutputsDir);
|
||||
createDirectory(outputDir);
|
||||
Files.createDirectories(Path.of(tempInputsDir));
|
||||
Files.createDirectories(Path.of(tempOutputsDir));
|
||||
Files.createDirectories(Path.of(usersDir));
|
||||
}
|
||||
|
||||
@Scheduled(fixedRate = TEMP_DIR_CLEANUP_RATE)
|
||||
|
||||
Reference in New Issue
Block a user