REFactor storage configuration and introduce DirectoryService for file management

This commit is contained in:
2025-07-05 10:37:27 +02:00
parent ea1508d1cf
commit 12eafcdd3e
3 changed files with 81 additions and 54 deletions

View File

@@ -0,0 +1,70 @@
package com.ddf.vodsystem.services;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
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.nio.file.StandardCopyOption;
import org.slf4j.Logger;
@Service
public class DirectoryService {
private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DirectoryService.class);
@Value("${storage.outputs}")
private String outputDir;
@Value("${storage.temp.inputs}")
private String tempInputsDir;
@Value("${storage.temp.outputs}")
private String tempOutputsDir;
public File getTempInputFile(String id) {
String dir = tempInputsDir + File.separator + id;
return new File(dir);
}
public File getTempOutputFile(String id) {
String dir = tempOutputsDir + File.separator + id;
return new File(dir);
}
public File getOutputFile(String id) {
String dir = outputDir + File.separator + id;
return new File(dir);
}
public void saveData(File file, MultipartFile multipartFile) {
try {
Path filePath = Paths.get(file.getAbsolutePath());
Files.copy(multipartFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
logger.error(e.getMessage());
}
}
public 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);
}
}
@PostConstruct
public void createDirectoriesIfNotExist() throws IOException {
createDirectory(tempInputsDir);
createDirectory(tempOutputsDir);
createDirectory(outputDir);
}
}

View File

@@ -2,19 +2,14 @@ package com.ddf.vodsystem.services;
import com.ddf.vodsystem.entities.Job;
import com.ddf.vodsystem.entities.VideoMetadata;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Base64;
import java.util.UUID;
@@ -25,18 +20,17 @@ import org.slf4j.LoggerFactory;
public class UploadService {
private static final Logger logger = LoggerFactory.getLogger(UploadService.class);
@Value("${temp.vod.storage}")
private String inputDir;
@Value("${temp.vod.output}")
private String outputDir;
private final JobService jobService;
private final MetadataService metadataService;
private final DirectoryService directoryService;
@Autowired
public UploadService(JobService jobService, MetadataService metadataService) {
public UploadService(JobService jobService,
MetadataService metadataService,
DirectoryService directoryService) {
this.jobService = jobService;
this.metadataService = metadataService;
this.directoryService = directoryService;
}
public String upload(MultipartFile file) {
@@ -45,13 +39,9 @@ public class UploadService {
String extension = getFileExtension(file.getOriginalFilename());
String filename = uuid + (extension.isEmpty() ? "" : "." + extension);
Path inputPath = Paths.get(inputDir, filename);
File inputFile = inputPath.toFile();
Path outputPath = Paths.get(outputDir, filename);
File outputFile = outputPath.toFile();
moveToFile(file, inputFile);
File inputFile = directoryService.getTempInputFile(filename);
File outputFile = directoryService.getTempOutputFile(filename);
directoryService.saveData(inputFile, file);
// add job
VideoMetadata videoMetadata = metadataService.getVideoMetadata(inputFile);
@@ -61,15 +51,6 @@ public class UploadService {
return uuid;
}
private void moveToFile(MultipartFile inputFile, File outputFile) {
try {
Path filePath = Paths.get(outputFile.getAbsolutePath());
Files.copy(inputFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
logger.error(e.getMessage());
}
}
private static String generateShortUUID() {
UUID uuid = UUID.randomUUID();
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
@@ -88,29 +69,4 @@ public class UploadService {
}
return fileName.substring(dotIndex + 1);
}
private void createDirectories() throws IOException {
// Create INPUT_DIR if it doesn't exist
Path inputDirPath = Paths.get(inputDir);
if (Files.notExists(inputDirPath)) {
Files.createDirectories(inputDirPath);
logger.info("Created directory: {}", inputDir);
}
// Create OUTPUT_DIR if it doesn't exist
Path outputDirPath = Paths.get(outputDir);
if (Files.notExists(outputDirPath)) {
Files.createDirectories(outputDirPath);
logger.info("Created directory: {}", outputDir);
}
}
@PostConstruct
public void init() {
try {
createDirectories();
} catch (IOException e) {
logger.error("Failed to create directories: " + e.getMessage(), e);
}
}
}