From dddf1d73709b6ef712ba6f3100a608f20e7a6fd6 Mon Sep 17 00:00:00 2001
From: itdominator <1itdominator@gmail.com>
Date: Thu, 21 Sep 2023 16:56:59 -0500
Subject: [PATCH] Preliminary hash and thumb generation
---
pom.xml | 7 ++
.../controller/ThumbnailerApiController.java | 70 +++++++++++++++++++
src/main/resources/application.properties | 2 +
3 files changed, 79 insertions(+)
diff --git a/pom.xml b/pom.xml
index 5705802..a03a38b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -83,6 +83,13 @@
+
+ commons-codec
+ commons-codec
+ 1.11
+
+
+
org.springframework.boot
spring-boot-starter-test
diff --git a/src/main/java/com/itdominator/api/controller/ThumbnailerApiController.java b/src/main/java/com/itdominator/api/controller/ThumbnailerApiController.java
index 31d450f..aa46ba2 100644
--- a/src/main/java/com/itdominator/api/controller/ThumbnailerApiController.java
+++ b/src/main/java/com/itdominator/api/controller/ThumbnailerApiController.java
@@ -1,8 +1,17 @@
package com.itdominator.api.controller;
+import java.io.IOException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.validation.annotation.Validated;
@@ -35,6 +44,9 @@ public class ThumbnailerApiController {
private final ThumbnailerService thumbnailerService;
+ @Value("${videos.filter}")
+ private final String[] videoFilter;
+
@PreAuthorize("hasRole('User')")
@PostMapping("/get-thumbnail/id/{id}")
@@ -69,4 +81,62 @@ public class ThumbnailerApiController {
return thumbnailerService.getAllThumbnails();
}
+ @PreAuthorize("hasRole('User')")
+ @GetMapping("/temp-videos-list-test")
+ public Set getAllVideoFiles() {
+ Set paths = collectVideoFilePaths("/home/abaddon/Downloads");
+ Set files = new HashSet<>();
+
+ generateThumbnails(paths);
+ for (Path path : paths) {
+ files.add( path.getFileName().toString() );
+ }
+
+ return files;
+ }
+
+
+ public Set collectVideoFilePaths(final String dir) {
+ Set paths = new HashSet<>();
+
+ try (DirectoryStream stream = Files.newDirectoryStream( Paths.get(dir) )) {
+ for (Path path : stream) {
+ if ( !Files.isDirectory(path) ) {
+ String target = path.getFileName().toString();
+ for (String filterItem : videoFilter) {
+ if ( target.endsWith(filterItem) ) {
+ paths.add(path);
+ }
+ }
+ }
+ }
+ } catch (IOException e) {
+ log.debug("Invalid path given to scan...");
+ }
+
+ return paths;
+ }
+
+ public void generateThumbnails(Set paths) {
+ for (Path path : paths) {
+ String fileName = path.getFileName().toString();
+ String fileHash = DigestUtils.sha256Hex(fileName);
+ generateThumbnail(path.toString(), "/home/abaddon/Downloads/tmp/" + fileHash + ".jpg","65%");
+ }
+ }
+
+
+
+ // proc = subprocess.Popen([self.FFMPG_THUMBNLR, "-t", scrub_percent, "-s", "300", "-c", "jpg", "-i", full_path, "-o", hash_img_path])
+ public void generateThumbnail(final String full_path, final String hash_img_path, final String scrub_percent) {
+ try {
+ ProcessBuilder pb = new ProcessBuilder("ffmpegthumbnailer", "-t", scrub_percent, "-s", "300", "-c", "jpg", "-i", full_path, "-o", hash_img_path);
+ Process p = pb.start();
+ } catch (Exception e) {
+ log.debug("Thumbnail couldn't be generated...");
+ // Note: Need supporting logic for failed loads. Maybe return empty byte[]
+ }
+
+ }
+
}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index b7b6611..b1be10f 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -16,4 +16,6 @@ hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=update
+videos.filter=.mkv,.mp4,.webm,.avi,.mov,.m4v,.mpg,.mpeg,.wmv,.flv
+