Extract database clip saving into saveClip()

persistClip() had grown too large and handled multiple concerns.
This change moves the database save logic into saveClip(), making
persistClip() smaller and easier to refactor further.
This commit is contained in:
2025-12-15 22:06:57 +00:00
parent 65ec8cb29a
commit eed46cf266
2 changed files with 34 additions and 18 deletions

View File

@@ -121,6 +121,33 @@ public class ClipService {
return user.get().getId().equals(clip.getUser().getId()); return user.get().getId().equals(clip.getUser().getId());
} }
/**
* Persists a clip to the database
* @param options ClipOptions object of the clip metadata to save to the database. All fields required except for title, description
* @param user User to save the clip to
* @param videoPath Path of the clip
* @param thumbnailPath Path of the thumbnail
* @return Clip object saved to the database
*/
public Clip saveClip(ClipOptions options,
User user,
String videoPath,
String thumbnailPath) {
Clip clip = new Clip();
clip.setUser(user);
clip.setTitle(options.getTitle() != null ? options.getTitle() : "Untitled Clip");
clip.setDescription(options.getDescription() != null ? options.getDescription() : "");
clip.setCreatedAt(LocalDateTime.now());
clip.setWidth(options.getWidth());
clip.setHeight(options.getHeight());
clip.setFps(options.getFps());
clip.setDuration(options.getDuration() - options.getStartPoint());
clip.setFileSize(options.getFileSize());
clip.setVideoPath(videoPath);
clip.setThumbnailPath(thumbnailPath);
return clipRepository.save(clip);
}
public void persistClip(ClipOptions clipOptions, public void persistClip(ClipOptions clipOptions,
User user, User user,
File tempFile, File tempFile,
@@ -141,25 +168,14 @@ public class ClipService {
try { try {
thumbnailService.createThumbnail(clipFile, thumbnailFile, 0.0f); thumbnailService.createThumbnail(clipFile, thumbnailFile, 0.0f);
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
logger.error("Error generating thumbnail for clip: {}", e.getMessage()); logger.error("Error generating thumbnail for user: {}, {}", user.getId(), e.getMessage());
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
// Save clip to database clipMetadata.setTitle(clipOptions.getTitle());
Clip clip = new Clip(); clipMetadata.setDescription(clipOptions.getDescription());
clip.setUser(user);
clip.setTitle(clipOptions.getTitle() != null ? clipOptions.getTitle() : "Untitled Clip");
clip.setDescription(clipOptions.getDescription() != null ? clipOptions.getDescription() : "");
clip.setCreatedAt(LocalDateTime.now());
clip.setWidth(clipMetadata.getWidth());
clip.setHeight(clipMetadata.getHeight());
clip.setFps(clipMetadata.getFps());
clip.setDuration(clipMetadata.getDuration() - clipMetadata.getStartPoint());
clip.setFileSize(clipMetadata.getFileSize());
clip.setVideoPath(clipFile.getPath());
clip.setThumbnailPath(thumbnailFile.getPath());
clipRepository.save(clip);
Clip clip = saveClip(clipMetadata, user, clipFile.getAbsolutePath(), thumbnailFile.getAbsolutePath());
logger.info("Clip created successfully with ID: {}", clip.getId()); logger.info("Clip created successfully with ID: {}", clip.getId());
} }
@@ -171,7 +187,7 @@ public class ClipService {
Files.deleteIfExists(clipFile.toPath()); Files.deleteIfExists(clipFile.toPath());
Files.deleteIfExists(thumbnailFile.toPath()); Files.deleteIfExists(thumbnailFile.toPath());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); logger.error("Could not delete clip files for clip ID: {}", clip.getId());
} }
} }
} }

View File

@@ -119,10 +119,10 @@ public class JobService {
try { try {
Optional<User> user = userService.getLoggedInUser(); Optional<User> user = userService.getLoggedInUser();
compressionService.compress(job.getInputFile(), job.getOutputFile(), job.getOutputClipOptions(), job.getStatus().getProcess()) compressionService.compress(job.getInputFile(), job.getOutputFile(), job.getOutputClipOptions(), job.getStatus().getProcess())
.thenRun(() -> user.ifPresent(value -> .thenRun(() -> user.ifPresent(userVal ->
clipService.persistClip( clipService.persistClip(
job.getOutputClipOptions(), job.getOutputClipOptions(),
value, userVal,
job.getOutputFile(), job.getOutputFile(),
job.getInputFile().getName() job.getInputFile().getName()
) )