use latest svjatoslavcommons
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / FilesystemIndexer.java
index d9726dd..d13306c 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Meviz - Various tools collection to work with multimedia.
  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License
  * as published by the Free Software Foundation.
@@ -12,7 +12,6 @@ package eu.svjatoslav.meviz.htmlindexer;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -20,13 +19,10 @@ import java.util.List;
 import org.apache.log4j.Logger;
 
 import eu.svjatoslav.commons.file.IOHelper;
-import eu.svjatoslav.commons.network.UrlParamEncoder;
 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
 import eu.svjatoslav.meviz.htmlindexer.metadata.MetadadaHelper;
 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;
 
 /**
@@ -42,8 +38,6 @@ public class FilesystemIndexer extends AbstractIndexer {
 
        private final File directoryToIndex;
 
-       private String thumbnailsPath;
-
        private File metadataFile;
 
        private DirectoryMetadata metadata;
@@ -56,8 +50,7 @@ public class FilesystemIndexer extends AbstractIndexer {
 
        public FilesystemIndexer(final File directoryToIndex,
                        final CommandlineHandler commandlineHandler,
-                       final List<String> pathElements) throws IOException,
-                       ImageFormatError {
+                       final List<String> pathElements) throws Exception {
 
                this.directoryToIndex = directoryToIndex;
                this.commandlineHandler = commandlineHandler;
@@ -96,14 +89,6 @@ public class FilesystemIndexer extends AbstractIndexer {
                return Utils.isMevizGeneratedIndexFile(indexFile);
        }
 
-       public void cleanupUnusedMetadataFiles() {
-               final File thumbnailsDirectory = new File(thumbnailsPath);
-
-               for (final File file : thumbnailsDirectory.listFiles())
-                       if (!validMetadataFiles.contains(file.getName()))
-                               file.delete();
-       }
-
        public void generateHtmlFromMetadata(final HashSet<Layout> layouts) {
                // Generate HTML from metadata
                for (final Layout layout : layouts)
@@ -113,10 +98,10 @@ public class FilesystemIndexer extends AbstractIndexer {
 
                                if (canWriteIndexFile(indexFile)) {
 
-                                       indexForLayout(layout);
+                                       compileHtml(layout, metadata);
 
                                        IOHelper.overwriteFileIfContentDiffers(indexFile, layout
-                                                       .getHtml(true, true).getBytes());
+                                                       .getHtml(true, true, metadata).getBytes());
                                }
                        } catch (final Exception e) {
                                logger.error("Error writing index file. ", e);
@@ -156,7 +141,9 @@ public class FilesystemIndexer extends AbstractIndexer {
                                .getRelativeThumbnailFileName(desiredDimension);
 
                validMetadataFiles.add(thumbnailFileName);
-               final File thumbnailFile = new File(thumbnailsPath + thumbnailFileName);
+               final File thumbnailFile = new File(
+                               Utils.getThumbnailsDirectoryPath(directoryToIndex)
+                                               + thumbnailFileName);
 
                if (!thumbnailFile.exists()) {
 
@@ -168,25 +155,7 @@ public class FilesystemIndexer extends AbstractIndexer {
                                        desiredDimension.getAwtDimension());
                }
                return Constants.THUMBNAILS_DIRECTORY_NAME + "/"
-                               + UrlParamEncoder.encode(thumbnailFileName);
-       }
-
-       public void indexForLayout(final Layout layout)
-                       throws UnsupportedEncodingException {
-
-               for (final AbstractFile abstractFile : metadata.getFiles())
-                       if (abstractFile instanceof GeneralFile) {
-
-                               if (isOgv(abstractFile.getFileExtension()))
-                                       layout.enlistOgv(abstractFile);
-                               else
-                                       layout.enlistFile(abstractFile);
-
-                       } else if (abstractFile instanceof Picture)
-                               layout.enlistImage(abstractFile);
-                       else if (abstractFile instanceof DirectoryFile)
-                               layout.enlistDirectory(abstractFile);
-
+                               + Utils.urlEncode(thumbnailFileName);
        }
 
        public void initializeLayouts() {
@@ -209,26 +178,26 @@ public class FilesystemIndexer extends AbstractIndexer {
        }
 
        public void loadOrCreateMetadata() {
-               metadataFile = new File(thumbnailsPath + Constants.METADATA_FILE_NAME);
+
+               metadataFile = new File(
+                               Utils.getThumbnailsDirectoryPath(directoryToIndex)
+                                               + Constants.METADATA_FILE_NAME);
 
                metadata = MetadadaHelper.initDirectoryMetadata(metadataFile);
        }
 
        private void removeUnusedThumbnailFiles() {
 
-               final File thumbnailsDirectory = new File(thumbnailsPath);
-
-               for (final File file : thumbnailsDirectory.listFiles())
+               for (final File file : Utils.getThumbnailsDirectory(directoryToIndex)
+                               .listFiles())
                        if (!validMetadataFiles.contains(file.getName()))
                                file.delete();
 
        }
 
-       public void updateMetadata() throws IOException, ImageFormatError {
-
-               final File[] directoryContent = directoryToIndex.listFiles();
+       public void updateMetadata() throws Exception {
 
-               for (final File file : directoryContent)
+               for (final File file : directoryToIndex.listFiles())
                        if (shallFileBeIndexed(file)) {
 
                                metadata.ensureFileMetainfoIsUpToDate(directoryToIndex, file);