From eed46cf266ac5c2a3923237d9229fcdc8e08db85 Mon Sep 17 00:00:00 2001 From: Dylan De Faoite Date: Mon, 15 Dec 2025 22:06:57 +0000 Subject: [PATCH] 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. --- .../ddf/vodsystem/services/ClipService.java | 48 ++++++++++++------- .../ddf/vodsystem/services/JobService.java | 4 +- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/ddf/vodsystem/services/ClipService.java b/src/main/java/com/ddf/vodsystem/services/ClipService.java index 5d04982..9931556 100644 --- a/src/main/java/com/ddf/vodsystem/services/ClipService.java +++ b/src/main/java/com/ddf/vodsystem/services/ClipService.java @@ -121,6 +121,33 @@ public class ClipService { 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, User user, File tempFile, @@ -141,25 +168,14 @@ public class ClipService { try { thumbnailService.createThumbnail(clipFile, thumbnailFile, 0.0f); } 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(); } - // Save clip to database - Clip clip = new Clip(); - 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); + clipMetadata.setTitle(clipOptions.getTitle()); + clipMetadata.setDescription(clipOptions.getDescription()); + Clip clip = saveClip(clipMetadata, user, clipFile.getAbsolutePath(), thumbnailFile.getAbsolutePath()); logger.info("Clip created successfully with ID: {}", clip.getId()); } @@ -171,7 +187,7 @@ public class ClipService { Files.deleteIfExists(clipFile.toPath()); Files.deleteIfExists(thumbnailFile.toPath()); } catch (IOException e) { - throw new RuntimeException(e); + logger.error("Could not delete clip files for clip ID: {}", clip.getId()); } } } diff --git a/src/main/java/com/ddf/vodsystem/services/JobService.java b/src/main/java/com/ddf/vodsystem/services/JobService.java index af64889..0849862 100644 --- a/src/main/java/com/ddf/vodsystem/services/JobService.java +++ b/src/main/java/com/ddf/vodsystem/services/JobService.java @@ -119,10 +119,10 @@ public class JobService { try { Optional user = userService.getLoggedInUser(); compressionService.compress(job.getInputFile(), job.getOutputFile(), job.getOutputClipOptions(), job.getStatus().getProcess()) - .thenRun(() -> user.ifPresent(value -> + .thenRun(() -> user.ifPresent(userVal -> clipService.persistClip( job.getOutputClipOptions(), - value, + userVal, job.getOutputFile(), job.getInputFile().getName() )