Preliminary hash and thumb generation

This commit is contained in:
itdominator 2023-09-21 16:56:59 -05:00
parent 43a626eef9
commit dddf1d7370
3 changed files with 79 additions and 0 deletions

View File

@ -83,6 +83,13 @@
</dependency> </dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

View File

@ -1,8 +1,17 @@
package com.itdominator.api.controller; 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.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.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -35,6 +44,9 @@ public class ThumbnailerApiController {
private final ThumbnailerService thumbnailerService; private final ThumbnailerService thumbnailerService;
@Value("${videos.filter}")
private final String[] videoFilter;
@PreAuthorize("hasRole('User')") @PreAuthorize("hasRole('User')")
@PostMapping("/get-thumbnail/id/{id}") @PostMapping("/get-thumbnail/id/{id}")
@ -69,4 +81,62 @@ public class ThumbnailerApiController {
return thumbnailerService.getAllThumbnails(); return thumbnailerService.getAllThumbnails();
} }
@PreAuthorize("hasRole('User')")
@GetMapping("/temp-videos-list-test")
public Set<String> getAllVideoFiles() {
Set<Path> paths = collectVideoFilePaths("/home/abaddon/Downloads");
Set<String> files = new HashSet<>();
generateThumbnails(paths);
for (Path path : paths) {
files.add( path.getFileName().toString() );
}
return files;
}
public Set<Path> collectVideoFilePaths(final String dir) {
Set<Path> paths = new HashSet<>();
try (DirectoryStream<Path> 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<Path> 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[]
}
}
} }

View File

@ -16,4 +16,6 @@ hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
videos.filter=.mkv,.mp4,.webm,.avi,.mov,.m4v,.mpg,.mpeg,.wmv,.flv