From db5ceb7aeafe260b3f27e3f5f74006b09e32bff8 Mon Sep 17 00:00:00 2001 From: RhiobeT Date: Tue, 25 May 2021 23:10:09 +0200 Subject: [PATCH] Generate thumbnails for Jellyfin shows --- .../lalafin/file/ThumbnailService.java | 60 +++++++++++++++++-- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/src/main/java/sh/rhiobet/lalafin/file/ThumbnailService.java b/src/main/java/sh/rhiobet/lalafin/file/ThumbnailService.java index 12d4d25..ba4de67 100644 --- a/src/main/java/sh/rhiobet/lalafin/file/ThumbnailService.java +++ b/src/main/java/sh/rhiobet/lalafin/file/ThumbnailService.java @@ -30,6 +30,8 @@ public class ThumbnailService { FileInfo fileInfo = (FileInfo) fileInfoBase; if (fileInfo.filename.endsWith(".zip")) { return this.generateThumbnailFromZip(fileInfo, thumbPath); + } else { + return this.generateThumbnailForFile(fileInfo, thumbPath); } } else if (fileInfoBase instanceof FolderInfo) { return this.generateThumbnailForFolder((FolderInfo) fileInfoBase, thumbPath); @@ -37,6 +39,32 @@ public class ThumbnailService { return false; } + public boolean generateThumbnailForFile(FileInfo fileInfo, Path thumbPath) { + Path filePath = Paths.get(fileApiConfiguration.directory() + + URLDecoder.decode(fileInfo.directUrl, StandardCharsets.UTF_8)); + + Path imgPath = filePath.resolveSibling( + filePath.getFileName().toString().replaceAll("\\.[^.]*$", "-thumb.jpg")); + if (Files.exists(imgPath)) { + try { + thumbPath.getParent().toFile().mkdirs(); + + ConvertCmd convert = new ConvertCmd(); + IMOperation op = new IMOperation(); + op.addImage(imgPath.toString()); + op.resize(200, null); + op.addImage(thumbPath.toString()); + convert.run(op); + + return true; + } catch (IOException | InterruptedException | IM4JavaException e) { + e.printStackTrace(); + } + } + + return false; + } + public boolean generateThumbnailFromZip(FileInfo fileInfo, Path thumbPath) { Path zipPath = Paths.get(fileApiConfiguration.directory() + URLDecoder.decode(fileInfo.directUrl, StandardCharsets.UTF_8)); @@ -77,10 +105,30 @@ public class ThumbnailService { + URLDecoder.decode(folderInfo.directUrl, StandardCharsets.UTF_8)); try { - Optional imgPath = Files.list(folderPath).sorted() - .filter(p -> !Files.isDirectory(p) - && (p.toString().endsWith(".png") || p.toString().endsWith(".jpg"))) - .findFirst(); + Optional imgPath; + + Path posterPath = folderPath.resolve("poster.jpg"); + if (Files.exists(posterPath)) { + // Check for series poster + imgPath = Optional.of(posterPath); + } else if (folderPath.getFileName().toString().startsWith("Season ") + || (folderPath.getFileName().toString().equals("Specials"))) { + // Season folder + String folderName = folderPath.getFileName().toString(); + if (folderName.startsWith("Season ")) { + String seasonNumber = + String.format("%02d", Integer.parseInt(folderName.substring(7))); + imgPath = Optional + .of(folderPath.resolveSibling("season" + seasonNumber + "-poster.jpg")); + } else { + imgPath = Optional.of(folderPath.resolveSibling("season-specials-poster.jpg")); + } + } else { + imgPath = Files.list(folderPath).sorted() + .filter(p -> !Files.isDirectory(p) + && (p.toString().endsWith(".png") || p.toString().endsWith(".jpg"))) + .findFirst(); + } if (imgPath.isPresent()) { thumbPath.getParent().toFile().mkdirs(); @@ -93,8 +141,8 @@ public class ThumbnailService { return true; } - } catch (IOException | InterruptedException | IM4JavaException ignored) { - ignored.printStackTrace(); + } catch (IOException | InterruptedException | IM4JavaException e) { + e.printStackTrace(); } return false;