ADD DirectoryService integration for clip persistence and file management
This commit is contained in:
@@ -31,11 +31,15 @@ public class ClipService {
|
||||
|
||||
private final ClipRepository clipRepository;
|
||||
private final MetadataService metadataService;
|
||||
private final DirectoryService directoryService;
|
||||
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.metadataService = metadataService;
|
||||
this.directoryService = directoryService;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,7 +74,7 @@ public class ClipService {
|
||||
|
||||
User user = getUser();
|
||||
if (user != null) {
|
||||
createClip(job.getOutputVideoMetadata(), user);
|
||||
persistClip(job.getOutputVideoMetadata(), user, job);
|
||||
}
|
||||
|
||||
job.setStatus(JobStatus.FINISHED);
|
||||
@@ -182,10 +186,16 @@ public class ClipService {
|
||||
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.setTitle(videoMetadata.getTitle() != null ? videoMetadata.getTitle() : "Untitled Clip");
|
||||
clip.setUser(user);
|
||||
clip.setTitle(videoMetadata.getTitle() != null ? videoMetadata.getTitle() : "Untitled Clip");
|
||||
clip.setDescription(videoMetadata.getDescription());
|
||||
clip.setCreatedAt(LocalDateTime.now());
|
||||
clip.setWidth(videoMetadata.getWidth());
|
||||
@@ -193,8 +203,7 @@ public class ClipService {
|
||||
clip.setFps(videoMetadata.getFps());
|
||||
clip.setDuration(videoMetadata.getEndPoint() - videoMetadata.getStartPoint());
|
||||
clip.setFileSize(videoMetadata.getFileSize());
|
||||
clip.setVideoPath("test");
|
||||
clip.setVideoPath(outputFile.getPath());
|
||||
clipRepository.save(clip);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,18 +28,18 @@ public class DirectoryService {
|
||||
@Value("${storage.temp.outputs}")
|
||||
private String tempOutputsDir;
|
||||
|
||||
public File getTempInputFile(String id) {
|
||||
String dir = tempInputsDir + File.separator + id;
|
||||
public File getTempInputFile(String id, String extension) {
|
||||
String dir = tempInputsDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension);
|
||||
return new File(dir);
|
||||
}
|
||||
|
||||
public File getTempOutputFile(String id) {
|
||||
String dir = tempOutputsDir + File.separator + id;
|
||||
public File getTempOutputFile(String id, String extension) {
|
||||
String dir = tempOutputsDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension);
|
||||
return new File(dir);
|
||||
}
|
||||
|
||||
public File getOutputFile(String id) {
|
||||
String dir = outputDir + File.separator + id;
|
||||
public File getOutputFile(String id, String extension) {
|
||||
String dir = outputDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension);
|
||||
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 {
|
||||
// Create the directory if it doesn't exist
|
||||
Path outputPath = Paths.get(dir);
|
||||
|
||||
@@ -8,8 +8,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -36,11 +34,10 @@ public class UploadService {
|
||||
public String upload(MultipartFile file) {
|
||||
// generate uuid, filename
|
||||
String uuid = generateShortUUID();
|
||||
String extension = getFileExtension(file.getOriginalFilename());
|
||||
String filename = uuid + (extension.isEmpty() ? "" : "." + extension);
|
||||
String extension = directoryService.getFileExtension(file.getOriginalFilename());
|
||||
|
||||
File inputFile = directoryService.getTempInputFile(filename);
|
||||
File outputFile = directoryService.getTempOutputFile(filename);
|
||||
File inputFile = directoryService.getTempInputFile(uuid, extension);
|
||||
File outputFile = directoryService.getTempOutputFile(uuid, extension);
|
||||
directoryService.saveData(inputFile, file);
|
||||
|
||||
// add job
|
||||
@@ -59,14 +56,4 @@ public class UploadService {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user