From 8b0fa37543a8ccd5e37d7e16f31b449f8f5440b7 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 16 Sep 2018 01:13:25 +0300 Subject: [PATCH] Use Apache Tika to handle videos. --- pom.xml | 7 ++- .../meviz/htmlindexer/AbstractIndexer.java | 48 ++++++------------- .../meviz/htmlindexer/Constants.java | 10 ++-- .../meviz/htmlindexer/FilesystemIndexer.java | 2 +- .../svjatoslav/meviz/htmlindexer/Layout.java | 6 +-- .../svjatoslav/meviz/htmlindexer/Utils.java | 7 ++- .../htmlindexer/layouts/MixedLayout.java | 11 ++--- .../metadata/DirectoryMetadata.java | 26 ++++++---- .../metadata/fileTypes/AbstractFile.java | 25 +++++----- .../metadata/fileTypes/DirectoryFile.java | 2 +- .../metadata/fileTypes/GeneralFile.java | 2 +- .../metadata/fileTypes/MevizSpecialFile.java | 2 +- .../metadata/fileTypes/Picture.java | 9 ++-- .../htmlindexer/metadata/fileTypes/Video.java | 22 +++++++++ 14 files changed, 98 insertions(+), 81 deletions(-) create mode 100644 src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Video.java diff --git a/pom.xml b/pom.xml index b1cbf6d..02b8fda 100644 --- a/pom.xml +++ b/pom.xml @@ -18,13 +18,16 @@ svjatoslavcommons 1.7 - log4j log4j 1.2.16 - + + org.apache.tika + tika-core + 1.18 + diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java index 4e3b6eb..22ec825 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java @@ -2,25 +2,28 @@ package eu.svjatoslav.meviz.htmlindexer; import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension; import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata; -import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile; -import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.DirectoryFile; -import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile; -import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture; +import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.*; import java.io.File; import java.io.UnsupportedEncodingException; +import static eu.svjatoslav.meviz.htmlindexer.Constants.SUPPORTED_IMAGE_EXTENSIONS; +import static eu.svjatoslav.meviz.htmlindexer.Constants.SUPPORTED_VIDEO_EXTENSIONS; + public abstract class AbstractIndexer { public static boolean isImage(final String fileExtension) { - for (final String ext : Constants.SUPPORTED_IMAGE_EXTENSIONS) + for (final String ext : SUPPORTED_IMAGE_EXTENSIONS) if (ext.equals(fileExtension)) return true; return false; } - public static boolean isOgv(final String fileExtension) { - return fileExtension.equalsIgnoreCase("ogv"); + public static boolean isVideo(final String fileExtension) { + for (final String ext : SUPPORTED_VIDEO_EXTENSIONS) + if (ext.equals(fileExtension)) + return true; + return false; } static boolean shallFileBeIndexed(final File file) { @@ -40,18 +43,13 @@ public abstract class AbstractIndexer { for (final AbstractFile file : directory.getFiles()) if (file instanceof GeneralFile) { - final String fileExtension = file.getFileExtension(); - - if (isVideo(fileExtension)) - layout.enlistVideo(file); - else - layout.enlistFile(file, directory); - - } else if (file instanceof Picture) + layout.enlistFile(file, directory); + } else if (file instanceof Picture){ layout.enlistImage((Picture) file, directory); - else if (file instanceof DirectoryFile) + } else if (file instanceof Video){ + layout.enlistVideo((Video) file); + } else if (file instanceof DirectoryFile) layout.enlistDirectory(file, directory); - } public abstract String getDirectoryUrl(final AbstractFile directory, @@ -62,20 +60,4 @@ public abstract class AbstractIndexer { public abstract String getThumbnailUrl(Picture picture, final Dimension desiredDimension, IndexingContext context); - public static String getVideoType(String fileExtension) { - switch (fileExtension){ - case "ogv": - return "video/ogg"; - case "webm": - return "video/webm"; - case "mp4": - return "video/mp4"; - default: - return null; - } - } - - public boolean isVideo(String fileExtension){ - return getVideoType(fileExtension) != null; - } } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Constants.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Constants.java index 4459416..8c481cd 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Constants.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Constants.java @@ -30,10 +30,14 @@ public class Constants { */ public static final String HTML_MAGIC_STRING = "DirListGen"; - public static final String HTML_MEMO = "Directory Listing Generator by Svjatoslav Agejenko. E-mail: svjatoslav@svjatoslav.eu, homepage: http://svjatoslav.eu"; + public static final String HTML_MEMO = "Directory Listing Generator by Svjatoslav Agejenko. " + + "E-mail: svjatoslav@svjatoslav.eu, homepage: http://svjatoslav.eu"; - public static final String[] SUPPORTED_IMAGE_EXTENSIONS = {"jpg", "jpeg", - "png", "gif"}; + public static final String[] SUPPORTED_IMAGE_EXTENSIONS = { + "jpg", "jpeg", "png", "gif"}; + + public static final String[] SUPPORTED_VIDEO_EXTENSIONS = { + "avi", "mp4", "mpeg", "mpg", "mkv", "flv", "ogv"}; public static final String THUMBNAIL_VERSION = "2"; diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java index 48ed951..3df671e 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java @@ -191,7 +191,7 @@ public class FilesystemIndexer extends AbstractIndexer { for (final File file : directoryToIndex.listFiles()) if (shallFileBeIndexed(file)) { - metadata.ensureFileMetainfoIsUpToDate(directoryToIndex, file); + metadata.ensureFileMetadataIsUpToDate(directoryToIndex, file); if (file.isDirectory()) new FilesystemIndexer(file, commandlineHandler, diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java index c1e6d23..8d839b5 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java @@ -12,8 +12,8 @@ package eu.svjatoslav.meviz.htmlindexer; import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata; import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile; import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture; +import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Video; -import java.io.IOException; import java.util.List; /** @@ -46,7 +46,7 @@ public interface Layout { /** * Enlist video file. */ - void enlistVideo(AbstractFile file); + void enlistVideo(Video file); /** * Return layout specific suffix that will be appended between @@ -58,8 +58,6 @@ public interface Layout { /** * After necessary files have been enlisted for particular directory, use * this method to retrieve generated HTML result. - * - * @throws IOException */ String getHtml(final boolean showTitle, final boolean doHtmlHeaderAndFooter, DirectoryMetadata metadata) diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java index 41c2eed..a966f7f 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java @@ -11,7 +11,6 @@ package eu.svjatoslav.meviz.htmlindexer; import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout; -import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; @@ -20,9 +19,9 @@ import java.io.IOException; import java.util.HashSet; import java.util.zip.CRC32; -public class Utils { +import static javax.imageio.ImageIO.read; - private static final String UTF_8 = "UTF-8"; +public class Utils { private static File lastLoadedFile; private static BufferedImage lastLoadedBufferedImage; @@ -39,7 +38,7 @@ public class Utils { return lastLoadedBufferedImage; System.out.println("Loading image: " + file.getPath()); - lastLoadedBufferedImage = ImageIO.read(file); + lastLoadedBufferedImage = read(file); lastLoadedFile = file; if (lastLoadedBufferedImage == null) { diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java index 7963582..fffa865 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java @@ -18,13 +18,13 @@ import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension; import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata; import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile; import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture; +import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Video; import org.apache.log4j.Logger; import java.io.IOException; import java.util.List; import static eu.svjatoslav.commons.file.FilePathParser.getFileNameWithoutExtension; -import static eu.svjatoslav.meviz.htmlindexer.AbstractIndexer.getVideoType; import static eu.svjatoslav.meviz.htmlindexer.Utils.urlEncode; public class MixedLayout implements Layout { @@ -242,25 +242,24 @@ public class MixedLayout implements Layout { } @Override - public void enlistVideo(AbstractFile file) { + public void enlistVideo(Video video) { filesHtml.append("
\n"); // watchable video filesHtml.append(" \n"); filesHtml.append("
\n"); // video title filesHtml.append(" Video: " - + getFileNameWithoutExtension(file.fileName) + + getFileNameWithoutExtension(video.fileName) + "            "); // video download link - filesHtml.append(" download\n"); + filesHtml.append(" download\n"); filesHtml.append("
\n"); diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java index d6e65fc..4877a73 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java @@ -10,7 +10,6 @@ package eu.svjatoslav.meviz.htmlindexer.metadata; import eu.svjatoslav.commons.file.FilePathParser; -import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer; import eu.svjatoslav.meviz.htmlindexer.FileSortComparator; import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.*; @@ -18,6 +17,9 @@ import java.io.File; import java.io.Serializable; import java.util.*; +import static eu.svjatoslav.meviz.htmlindexer.AbstractIndexer.isImage; +import static eu.svjatoslav.meviz.htmlindexer.AbstractIndexer.isVideo; + /** * Corresponds to single filesystem directory. */ @@ -40,21 +42,21 @@ public class DirectoryMetadata implements Serializable { while (fileIterator.hasNext()) { final AbstractFile file = fileIterator.next(); - if (!file.isMetaInfoVerified()) + if (!file.isMetadataVerified()) fileIterator.remove(); } } - public void ensureFileMetainfoIsUpToDate(final File parentDirectory, + public void ensureFileMetadataIsUpToDate(final File parentDirectory, final File file) throws Exception { final String fileName = file.getName(); if (fileNameToFileMap.containsKey(fileName)) { - final boolean metainfoUpdated = fileNameToFileMap.get(fileName) - .ensureFileMetainfoIsUpToDate(parentDirectory); + final boolean metadataUpdated = fileNameToFileMap.get(fileName) + .ensureFileMetadataIsUpToDate(parentDirectory); - if (metainfoUpdated) + if (metadataUpdated) changed = true; } else registerFile(parentDirectory, file); @@ -70,7 +72,7 @@ public class DirectoryMetadata implements Serializable { final String fileExtension = FilePathParser.getFileExtension(fileName) .toLowerCase(); - if (AbstractIndexer.isImage(fileExtension)) + if (isImage(fileExtension)) try { return new Picture(parentDirectory, fileName); } catch (final Exception exception) { @@ -78,6 +80,14 @@ public class DirectoryMetadata implements Serializable { return new GeneralFile(parentDirectory, fileName); } + if (isVideo(fileExtension)) + try { + return new Video(parentDirectory, fileName); + } catch (final Exception exception) { + // in case video decoding failed, handle it as general file + return new GeneralFile(parentDirectory, fileName); + } + if (fileName.endsWith(".meviz.org")) return new MevizSpecialFile(parentDirectory, fileName); @@ -132,7 +142,7 @@ public class DirectoryMetadata implements Serializable { public void removeUnusedMetadataEntries() { - new HashSet<>(fileNameToFileMap.keySet()).stream().filter(key -> !fileNameToFileMap.get(key).isMetaInfoVerified()).forEach(key -> { + new HashSet<>(fileNameToFileMap.keySet()).stream().filter(key -> !fileNameToFileMap.get(key).isMetadataVerified()).forEach(key -> { fileNameToFileMap.remove(key); changed = true; }); diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java index 7662093..0a89b40 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java @@ -19,23 +19,23 @@ public abstract class AbstractFile implements Serializable { */ private long fileLength = -1; - private transient boolean metaInfoVerified; + private transient boolean metadataVerified; AbstractFile(final File parentDirectory, final String fileName) throws Exception { this.fileName = fileName; - ensureFileMetainfoIsUpToDate(parentDirectory); + ensureFileMetadataIsUpToDate(parentDirectory); } /** * @return true if file metadata was updated. */ - public boolean ensureFileMetainfoIsUpToDate(final File parentDirectory) + public boolean ensureFileMetadataIsUpToDate(final File parentDirectory) throws Exception { - if (!isMetainfoUpToDate(parentDirectory)) { + if (!isMetadataUpToDate(parentDirectory)) { fileLength = getFile(parentDirectory).length(); - updateFileMetainfo(parentDirectory); - metaInfoVerified = true; + updateFileMetadata(parentDirectory); + metadataVerified = true; return true; } return false; @@ -57,9 +57,9 @@ public abstract class AbstractFile implements Serializable { return fileLength; } - private boolean isMetainfoUpToDate(final java.io.File parentDirectory) { + private boolean isMetadataUpToDate(final java.io.File parentDirectory) { - if (metaInfoVerified) + if (metadataVerified) return true; final File file = getFile(parentDirectory); @@ -76,15 +76,14 @@ public abstract class AbstractFile implements Serializable { if (file.length() != fileLength) return false; - metaInfoVerified = true; + metadataVerified = true; return true; } - public boolean isMetaInfoVerified() { - return metaInfoVerified; + public boolean isMetadataVerified() { + return metadataVerified; } - protected abstract void updateFileMetainfo(final File parentDirectory) - throws Exception; + protected abstract void updateFileMetadata(final File parentDirectory) throws Exception; } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java index 5eff260..d54073d 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java @@ -13,7 +13,7 @@ public class DirectoryFile extends AbstractFile { } @Override - protected void updateFileMetainfo(final File parentDirectory) + protected void updateFileMetadata(final File parentDirectory) throws IOException { } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java index e449ff0..012817b 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java @@ -13,7 +13,7 @@ public class GeneralFile extends AbstractFile { } @Override - protected void updateFileMetainfo(final File parentDirectory) + protected void updateFileMetadata(final File parentDirectory) throws IOException { } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java index 5973d9d..d13c923 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java @@ -21,7 +21,7 @@ public class MevizSpecialFile extends AbstractFile { } @Override - protected void updateFileMetainfo(final File parentDirectory) + protected void updateFileMetadata(final File parentDirectory) throws IOException { fileContent = IOHelper diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java index dbdf7f5..9a005af 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java @@ -27,6 +27,8 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.List; +import static eu.svjatoslav.meviz.htmlindexer.Utils.getBufferedImage; + public class Picture extends AbstractFile { private static final long serialVersionUID = -4156533490858298387L; @@ -105,7 +107,7 @@ public class Picture extends AbstractFile { try { - final BufferedImage inputImage = Utils.getBufferedImage(inputFile); + final BufferedImage inputImage = getBufferedImage(inputFile); final java.awt.Dimension sourceImageDimension = new java.awt.Dimension( inputImage.getWidth(), inputImage.getHeight()); @@ -179,9 +181,8 @@ public class Picture extends AbstractFile { } @Override - public void updateFileMetainfo(final File parentDirectory) throws Exception { - final BufferedImage image = Utils - .getBufferedImage(getFile(parentDirectory)); + public void updateFileMetadata(final File parentDirectory) throws Exception { + final BufferedImage image = getBufferedImage(getFile(parentDirectory)); dimensions = new Dimension(image.getWidth(), image.getHeight()); } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Video.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Video.java new file mode 100644 index 0000000..2ee0462 --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Video.java @@ -0,0 +1,22 @@ +package eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes; + +import org.apache.tika.Tika; + +import java.io.File; + +public class Video extends AbstractFile { + + public String mimeType; + + public Video(File parentDirectory, String fileName) throws Exception { + super(parentDirectory, fileName); + } + + @Override + protected void updateFileMetadata(File parentDirectory) throws Exception { + File file = getFile(parentDirectory); + Tika tika = new Tika(); + mimeType = tika.detect(file); + System.out.println("Detected video type: " + mimeType); + } +} -- 2.20.1