ADD DirectoryService integration for clip persistence and file management

This commit is contained in:
2025-07-07 21:59:54 +02:00
parent 12eafcdd3e
commit dcb2bcff22
3 changed files with 47 additions and 28 deletions

View File

@@ -31,11 +31,15 @@ public class ClipService {
private final ClipRepository clipRepository; private final ClipRepository clipRepository;
private final MetadataService metadataService; private final MetadataService metadataService;
private final DirectoryService directoryService;
private final Pattern timePattern = Pattern.compile("out_time_ms=(\\d+)"); private final Pattern timePattern = Pattern.compile("out_time_ms=(\\d+)");
public ClipService(ClipRepository clipRepository, MetadataService metadataService) { public ClipService(ClipRepository clipRepository,
MetadataService metadataService,
DirectoryService directoryService) {
this.clipRepository = clipRepository; this.clipRepository = clipRepository;
this.metadataService = metadataService; this.metadataService = metadataService;
this.directoryService = directoryService;
} }
/** /**
@@ -70,7 +74,7 @@ public class ClipService {
User user = getUser(); User user = getUser();
if (user != null) { if (user != null) {
createClip(job.getOutputVideoMetadata(), user); persistClip(job.getOutputVideoMetadata(), user, job);
} }
job.setStatus(JobStatus.FINISHED); job.setStatus(JobStatus.FINISHED);
@@ -182,10 +186,16 @@ public class ClipService {
return new ProcessBuilder(command); return new ProcessBuilder(command);
} }
private void createClip(VideoMetadata videoMetadata, User user) { private void persistClip(VideoMetadata videoMetadata, User user, Job job) {
// Move clip from temp to output directory
String fileExtension = directoryService.getFileExtension(job.getOutputFile().getAbsolutePath());
File outputFile = directoryService.getOutputFile(job.getUuid(), fileExtension);
directoryService.copyFile(job.getOutputFile(), outputFile);
// Save clip to database
Clip clip = new Clip(); Clip clip = new Clip();
clip.setTitle(videoMetadata.getTitle() != null ? videoMetadata.getTitle() : "Untitled Clip");
clip.setUser(user); clip.setUser(user);
clip.setTitle(videoMetadata.getTitle() != null ? videoMetadata.getTitle() : "Untitled Clip");
clip.setDescription(videoMetadata.getDescription()); clip.setDescription(videoMetadata.getDescription());
clip.setCreatedAt(LocalDateTime.now()); clip.setCreatedAt(LocalDateTime.now());
clip.setWidth(videoMetadata.getWidth()); clip.setWidth(videoMetadata.getWidth());
@@ -193,8 +203,7 @@ public class ClipService {
clip.setFps(videoMetadata.getFps()); clip.setFps(videoMetadata.getFps());
clip.setDuration(videoMetadata.getEndPoint() - videoMetadata.getStartPoint()); clip.setDuration(videoMetadata.getEndPoint() - videoMetadata.getStartPoint());
clip.setFileSize(videoMetadata.getFileSize()); clip.setFileSize(videoMetadata.getFileSize());
clip.setVideoPath("test"); clip.setVideoPath(outputFile.getPath());
clipRepository.save(clip); clipRepository.save(clip);
} }
} }

View File

@@ -28,18 +28,18 @@ public class DirectoryService {
@Value("${storage.temp.outputs}") @Value("${storage.temp.outputs}")
private String tempOutputsDir; private String tempOutputsDir;
public File getTempInputFile(String id) { public File getTempInputFile(String id, String extension) {
String dir = tempInputsDir + File.separator + id; String dir = tempInputsDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension);
return new File(dir); return new File(dir);
} }
public File getTempOutputFile(String id) { public File getTempOutputFile(String id, String extension) {
String dir = tempOutputsDir + File.separator + id; String dir = tempOutputsDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension);
return new File(dir); return new File(dir);
} }
public File getOutputFile(String id) { public File getOutputFile(String id, String extension) {
String dir = outputDir + File.separator + id; String dir = outputDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension);
return new File(dir); return new File(dir);
} }
@@ -52,6 +52,29 @@ public class DirectoryService {
} }
} }
public void copyFile(File source, File target) {
Path sourcePath = Paths.get(source.getAbsolutePath());
Path destPath = Paths.get(target.getAbsolutePath());
try {
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
logger.info("Copied file from {} to {}", sourcePath, destPath);
} catch (IOException e) {
logger.error(e.getMessage());
}
}
public 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);
}
public void createDirectory(String dir) throws IOException { public 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);

View File

@@ -8,8 +8,6 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64; import java.util.Base64;
import java.util.UUID; import java.util.UUID;
@@ -36,11 +34,10 @@ public class UploadService {
public String upload(MultipartFile file) { public String upload(MultipartFile file) {
// generate uuid, filename // generate uuid, filename
String uuid = generateShortUUID(); String uuid = generateShortUUID();
String extension = getFileExtension(file.getOriginalFilename()); String extension = directoryService.getFileExtension(file.getOriginalFilename());
String filename = uuid + (extension.isEmpty() ? "" : "." + extension);
File inputFile = directoryService.getTempInputFile(filename); File inputFile = directoryService.getTempInputFile(uuid, extension);
File outputFile = directoryService.getTempOutputFile(filename); File outputFile = directoryService.getTempOutputFile(uuid, extension);
directoryService.saveData(inputFile, file); directoryService.saveData(inputFile, file);
// add job // add job
@@ -59,14 +56,4 @@ public class UploadService {
return Base64.getUrlEncoder().withoutPadding().encodeToString(bb.array()); 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);
}
} }