Use Apache Tika to handle videos.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 15 Sep 2018 22:13:25 +0000 (01:13 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 15 Sep 2018 22:13:25 +0000 (01:13 +0300)
14 files changed:
pom.xml
src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/Constants.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Video.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index b1cbf6d..02b8fda 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <artifactId>svjatoslavcommons</artifactId>
             <version>1.7</version>
         </dependency>
-
         <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.16</version>
         </dependency>
-
+        <dependency>
+            <groupId>org.apache.tika</groupId>
+            <artifactId>tika-core</artifactId>
+            <version>1.18</version>
+        </dependency>
     </dependencies>
 
     <build>
index 4e3b6eb..22ec825 100644 (file)
@@ -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;
-    }
 }
index 4459416..8c481cd 100755 (executable)
@@ -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";
 
index 48ed951..3df671e 100755 (executable)
@@ -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,
index c1e6d23..8d839b5 100755 (executable)
@@ -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)
index 41c2eed..a966f7f 100755 (executable)
@@ -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) {
index 7963582..fffa865 100755 (executable)
@@ -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("<div style=\"margin: 30px; clear: both;\">\n");
 
         // watchable video
         filesHtml.append("  <video width=\"640\" controls>\n");
-        filesHtml.append("    <source src=\"" + getFileUrl(file) + "\" type=\""
-                + getVideoType(file.getFileExtension()) + "\"/>\n");
+        filesHtml.append("    <source src=\"" + getFileUrl(video) + "\" type=\"" + video.mimeType + "\"/>\n");
         filesHtml.append("  </video>\n");
 
         filesHtml.append("  <br/>\n");
 
         // video title
         filesHtml.append("  Video: <b>"
-                        + getFileNameWithoutExtension(file.fileName)
+                        + getFileNameWithoutExtension(video.fileName)
                         + "<b/>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
 
         // video download link
-        filesHtml.append("  <a href=\"" + getFileUrl(file) + "\">download</a>\n");
+        filesHtml.append("  <a href=\"" + getFileUrl(video) + "\">download</a>\n");
 
         filesHtml.append("</div>\n");
 
index d6e65fc..4877a73 100755 (executable)
@@ -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;
         });
index 7662093..0a89b40 100644 (file)
@@ -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 <code>true</code> 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;
 
 }
index 5eff260..d54073d 100644 (file)
@@ -13,7 +13,7 @@ public class DirectoryFile extends AbstractFile {
     }
 
     @Override
-    protected void updateFileMetainfo(final File parentDirectory)
+    protected void updateFileMetadata(final File parentDirectory)
             throws IOException {
     }
 
index e449ff0..012817b 100644 (file)
@@ -13,7 +13,7 @@ public class GeneralFile extends AbstractFile {
     }
 
     @Override
-    protected void updateFileMetainfo(final File parentDirectory)
+    protected void updateFileMetadata(final File parentDirectory)
             throws IOException {
     }
 
index 5973d9d..d13c923 100644 (file)
@@ -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
index dbdf7f5..9a005af 100755 (executable)
@@ -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 (file)
index 0000000..2ee0462
--- /dev/null
@@ -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);
+    }
+}