IMPROVE file path handling, handles multiple outputs

This commit is contained in:
2025-05-11 14:34:42 +02:00
parent 4d59456292
commit 3ec9f26264
3 changed files with 62 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
package com.ddf.vodsystem.services;
import com.ddf.vodsystem.tools.Job;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -24,7 +25,10 @@ public class UploadService {
private static final Logger logger = LoggerFactory.getLogger(UploadService.class);
@Value("${temp.vod.storage}")
private String UPLOAD_DIR;
private String INPUT_DIR;
@Value("${temp.vod.output}")
private String OUTPUT_DIR;
private final JobService jobService;
@Autowired
@@ -33,13 +37,21 @@ public class UploadService {
}
public String upload(MultipartFile file) {
// generate uuid, file
// generate uuid, filename
String uuid = generateShortUUID();
File uploadDir = new File(UPLOAD_DIR + uuid + ".mp4");
moveToFile(file, uploadDir);
String extension = getFileExtension(file.getOriginalFilename());
String filename = uuid + (extension.isEmpty() ? "" : "." + extension);
Path inputPath = Paths.get(INPUT_DIR, filename);
File inputFile = inputPath.toFile();
Path outputPath = Paths.get(OUTPUT_DIR, filename);
File outputFile = outputPath.toFile();
moveToFile(file, inputFile);
// add job
Job job = new Job(uuid, uploadDir);
Job job = new Job(uuid, inputFile, outputFile);
jobService.add(job);
return uuid;
@@ -61,4 +73,40 @@ public class UploadService {
bb.putLong(uuid.getLeastSignificantBits());
return Base64.getUrlEncoder().withoutPadding().encodeToString(bb.array());
}
private static String getFileExtension(String filePath) {
Path path = Paths.get(filePath);
String fileName = path.getFileName().toString();
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex == -1) {
return ""; // No extension
}
return fileName.substring(dotIndex + 1);
}
private void createDirectories() throws IOException {
// Create INPUT_DIR if it doesn't exist
Path inputDirPath = Paths.get(INPUT_DIR);
if (Files.notExists(inputDirPath)) {
Files.createDirectories(inputDirPath);
System.out.println("Created directory: " + INPUT_DIR);
}
// Create OUTPUT_DIR if it doesn't exist
Path outputDirPath = Paths.get(OUTPUT_DIR);
if (Files.notExists(outputDirPath)) {
Files.createDirectories(outputDirPath);
System.out.println("Created directory: " + OUTPUT_DIR);
}
}
@PostConstruct
public void init() {
try {
createDirectories();
} catch (IOException e) {
logger.error("Failed to create directories: " + e.getMessage(), e);
}
}
}

View File

@@ -15,7 +15,8 @@ public class Job implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(Job.class);
private String uuid;
private File file;
private File inputFile;
private File outputFile;
// configs
private ClipConfig clipConfig;
@@ -24,9 +25,10 @@ public class Job implements Runnable {
private JobStatus status = JobStatus.PENDING;
private Float progress = 0.0f;
public Job(String uuid, File file) {
public Job(String uuid, File inputFile, File outputFile) {
this.uuid = uuid;
this.file = file;
this.inputFile = inputFile;
this.outputFile = outputFile;
}
@Override
@@ -34,7 +36,7 @@ public class Job implements Runnable {
logger.info("Job {} started", uuid);
this.status = JobStatus.RUNNING;
CompressionService f = new CompressionService(file, new File("output.mp4"), clipConfig);
CompressionService f = new CompressionService(inputFile, outputFile, clipConfig);
try {
f.run();
@@ -44,7 +46,7 @@ public class Job implements Runnable {
this.status = JobStatus.FINISHED;
file.delete();
inputFile.delete();
logger.info("Job {} finished", uuid);
}
}

View File

@@ -4,7 +4,8 @@ spring.application.name=vodSystem
# VODs
spring.servlet.multipart.max-file-size=2GB
spring.servlet.multipart.max-request-size=2GB
temp.vod.storage=videos/
temp.vod.storage=videos/inputs/
temp.vod.output=videos/outputs/
# Database
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb