possibility to remove artifacts generated by meviz index
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 2 Nov 2013 22:03:42 +0000 (00:03 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 2 Nov 2013 22:03:42 +0000 (00:03 +0200)
src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexRemover.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java

index eb32317..95aacf0 100755 (executable)
@@ -13,6 +13,7 @@ import java.io.File;
 
 import eu.svjatoslav.commons.commandline.parameterparser.Parser;
 import eu.svjatoslav.commons.commandline.parameterparser.parameter.DirectoryParameter;
+import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter;
 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
 import eu.svjatoslav.meviz.encoder.EncodingOptions;
 
@@ -25,8 +26,11 @@ public class CommandlineHandler {
                                        + ").").addAliases("-t", "--gallery-title");
 
        DirectoryParameter workingDirectoryParameter = parser
-                       .createDirectoryParameter("Working directory.").addAliases("-w",
-                                       "--working-directory");
+                       .createDirectoryParameter("Working directory.")
+                       .addAliases("-w", "--working-directory").mustExist();
+
+       NullParameter removeIndex = parser.createNullParameter(
+                       "Remove generated index").addAliases("-r", "--remove");
 
        public String getGalleryTitle() {
                if (galleryNameParameter.isParameterSpecified())
@@ -47,6 +51,11 @@ public class CommandlineHandler {
         *         occurred.
         */
        public boolean parseCommandlineArguments(final String[] args) {
-               return parser.parse(args);
+               final boolean parsingSucceeded = parser.parse(args);
+
+               if (!parsingSucceeded)
+                       parser.showHelp();
+
+               return parsingSucceeded;
        }
 }
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexRemover.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexRemover.java
new file mode 100644 (file)
index 0000000..ba455ef
--- /dev/null
@@ -0,0 +1,49 @@
+package eu.svjatoslav.meviz.htmlindexer;
+
+import java.io.File;
+import java.io.IOException;
+
+import eu.svjatoslav.commons.file.IOHelper;
+
+public class FilesystemIndexRemover {
+
+       private final CommandlineHandler commandlineHandler;
+
+       public FilesystemIndexRemover(final CommandlineHandler commandlineHandler)
+                       throws IOException {
+               this.commandlineHandler = commandlineHandler;
+
+               removeIndex(commandlineHandler.getWorkingDirectory());
+       }
+
+       public void removeIndex(final File workingDirectory) throws IOException {
+
+               // remove thumbnails directory from current directory
+               final File thumbnailsDirectory = Utils
+                               .getThumbnailsDirectory(workingDirectory);
+               if (thumbnailsDirectory.exists()) {
+                       System.out.println("Deleting thumbnails directory: "
+                                       + thumbnailsDirectory);
+                       IOHelper.deleteRecursively(thumbnailsDirectory);
+               }
+
+               // recursively remove thumbnail directories from sub directories
+               for (final File subFile : workingDirectory.listFiles())
+                       if (subFile.isDirectory())
+                               removeIndex(subFile);
+
+               for (final Layout layout : Utils.getLayouts()) {
+                       final File indexFile = Utils.getLayoutIndexFile(layout,
+                                       workingDirectory);
+
+                       if (indexFile.exists())
+                               if (Utils.isMevizGeneratedIndexFile(indexFile)) {
+                                       System.out.println("Deleting generated index file: "
+                                                       + indexFile);
+                                       indexFile.delete();
+                               }
+               }
+
+       }
+
+}
index 0edea71..d9726dd 100755 (executable)
@@ -9,10 +9,8 @@
 
 package eu.svjatoslav.meviz.htmlindexer;
 
-import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
@@ -23,7 +21,6 @@ import org.apache.log4j.Logger;
 
 import eu.svjatoslav.commons.file.IOHelper;
 import eu.svjatoslav.commons.network.UrlParamEncoder;
-import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
 import eu.svjatoslav.meviz.htmlindexer.metadata.MetadadaHelper;
@@ -92,33 +89,11 @@ public class FilesystemIndexer extends AbstractIndexer {
 
        public boolean canWriteIndexFile(final File indexFile)
                        throws FileNotFoundException, IOException {
-               boolean canWriteIndexFile = false;
 
-               if (indexFile.exists()) {
-                       // if file already exists, make sure that we can overwrite
-                       // it
-                       final FileReader fileReader = new FileReader(indexFile);
-                       final BufferedReader reader = new BufferedReader(fileReader);
+               if (!indexFile.exists())
+                       return true;
 
-                       parseFile: {
-                               while (true) {
-                                       final String line = reader.readLine();
-
-                                       if (line == null)
-                                               break parseFile;
-
-                                       if (line.contains(Constants.HTML_MAGIC_STRING)) {
-                                               canWriteIndexFile = true;
-                                               break parseFile;
-                                       }
-                               }
-                       }
-
-                       reader.close();
-                       fileReader.close();
-               } else
-                       canWriteIndexFile = true;
-               return canWriteIndexFile;
+               return Utils.isMevizGeneratedIndexFile(indexFile);
        }
 
        public void cleanupUnusedMetadataFiles() {
@@ -131,14 +106,11 @@ public class FilesystemIndexer extends AbstractIndexer {
 
        public void generateHtmlFromMetadata(final HashSet<Layout> layouts) {
                // Generate HTML from metadata
-               for (final Layout layout : layouts) {
-
-                       final String indexFilePath = directoryToIndex.getAbsolutePath()
-                                       + "/index" + layout.getFileNameSuffix() + ".html";
-
+               for (final Layout layout : layouts)
                        try {
+                               final File indexFile = Utils.getLayoutIndexFile(layout,
+                                               directoryToIndex);
 
-                               final File indexFile = new File(indexFilePath);
                                if (canWriteIndexFile(indexFile)) {
 
                                        indexForLayout(layout);
@@ -147,9 +119,8 @@ public class FilesystemIndexer extends AbstractIndexer {
                                                        .getHtml(true, true).getBytes());
                                }
                        } catch (final Exception e) {
-                               logger.error("Error writing index file to:" + indexFilePath, e);
+                               logger.error("Error writing index file. ", e);
                        }
-               }
        }
 
        public List<String> getChildPath(final File file) {
@@ -219,8 +190,8 @@ public class FilesystemIndexer extends AbstractIndexer {
        }
 
        public void initializeLayouts() {
-               layouts = new HashSet<Layout>();
-               layouts.add(new MixedLayout());
+
+               layouts = Utils.getLayouts();
 
                final String galleryTitle = commandlineHandler.getGalleryTitle();
 
@@ -230,12 +201,9 @@ public class FilesystemIndexer extends AbstractIndexer {
        }
 
        public void initializeThumbnailsDirectory() {
-               // initialize thumbnails directory
-               thumbnailsPath = directoryToIndex.getAbsolutePath() + "/"
-                               + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
+               final File thumbnailsDirectory = Utils
+                               .getThumbnailsDirectory(directoryToIndex);
 
-               // ensure thumbnails directory exists
-               final File thumbnailsDirectory = new File(thumbnailsPath);
                if (!thumbnailsDirectory.exists())
                        thumbnailsDirectory.mkdirs();
        }
index ae6c4c6..5c469d8 100644 (file)
@@ -24,16 +24,12 @@ public class Main implements Module {
        @Override
        public void run(final String[] args) throws Exception {
 
-               final boolean parsingStatus = commandlineHandler
-                               .parseCommandlineArguments(args);
-
-               if (!parsingStatus) {
-                       showCommandlineHelp();
-                       return;
-               }
-
-               new FilesystemIndexer(commandlineHandler.getWorkingDirectory(),
-                               commandlineHandler, new ArrayList<String>());
+               if (commandlineHandler.parseCommandlineArguments(args))
+                       if (commandlineHandler.removeIndex.isParameterSpecified())
+                               new FilesystemIndexRemover(commandlineHandler);
+                       else
+                               new FilesystemIndexer(commandlineHandler.getWorkingDirectory(),
+                                               commandlineHandler, new ArrayList<String>());
 
        }
 
index b21f8e9..f8c82fa 100755 (executable)
 package eu.svjatoslav.meviz.htmlindexer;
 
 import java.awt.image.BufferedImage;
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
+import java.util.HashSet;
 import java.util.zip.CRC32;
 
 import javax.imageio.ImageIO;
 
+import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
+
 public class Utils {
 
-    private static File lastLoadedFile;
+       private static File lastLoadedFile;
+
+       private static BufferedImage lastLoadedBufferedImage;
+
+       /**
+        * Load image into {@link BufferedImage} and return it. Caches last loaded
+        * image to speed up subsequent loading attempts.
+        * 
+        * @throws ImageFormatError
+        * @throws IOException
+        */
+       public static BufferedImage getBufferedImage(final File file)
+                       throws ImageFormatError, IOException {
+               if (file.equals(lastLoadedFile))
+                       return lastLoadedBufferedImage;
+
+               System.out.println("Loading image: " + file.getPath());
+               lastLoadedBufferedImage = ImageIO.read(file);
+               lastLoadedFile = file;
+
+               if (lastLoadedBufferedImage == null)
+                       throw new ImageFormatError("File: " + file
+                                       + " is not a valid image.");
+
+               return lastLoadedBufferedImage;
+       }
+
+       public static File getLayoutIndexFile(final Layout layout,
+                       final File directoryToIndex) {
+
+               final String indexFilePath = directoryToIndex.getAbsolutePath()
+                               + "/index" + layout.getFileNameSuffix() + ".html";
+
+               return new File(indexFilePath);
+       }
+
+       public static HashSet<Layout> getLayouts() {
+               final HashSet<Layout> layouts = new HashSet<Layout>();
+               layouts.add(new MixedLayout());
+               return layouts;
+       }
+
+       public static String getStringCrcAsHex(final String input) {
+
+               // create a new CRC-calculating object
+               final CRC32 crc = new CRC32();
+
+               // loop, calculating CRC for each byte of the string
+               // There is no CRC16.update(byte[]) method.
+               for (final byte b : input.getBytes())
+                       crc.update(b);
 
-    private static BufferedImage lastLoadedBufferedImage;
+               // note use crc.value, not crc.getValue()
+               final String hex = Integer.toHexString((int) crc.getValue())
+                               .toUpperCase();
 
-    /**
-     * Load image into {@link BufferedImage} and return it. Caches last loaded
-     * image to speed up subsequent loading attempts.
-     * 
-     * @throws ImageFormatError
-     * @throws IOException
-     */
-    public static BufferedImage getBufferedImage(final File file) throws ImageFormatError, IOException {
-        if (file.equals(lastLoadedFile)) {
-            return lastLoadedBufferedImage;
-        }
+               // System.out.println("Input string: " + input);
+               // System.out.println("Result: " + hex);
 
-        System.out.println("Loading image: " + file.getPath());
-        lastLoadedBufferedImage = ImageIO.read(file);
-        lastLoadedFile = file;
+               return hex;
+       }
 
-        if (lastLoadedBufferedImage == null)
-            throw new ImageFormatError("File: " + file + " is not a valid image.");
+       public static File getThumbnailsDirectory(final File directoryToIndex) {
+               return new File(directoryToIndex.getAbsolutePath() + "/"
+                               + Constants.THUMBNAILS_DIRECTORY_NAME + "/");
+       }
 
-        return lastLoadedBufferedImage;
-    }
+       public static boolean isMevizGeneratedIndexFile(final File indexFile)
+                       throws FileNotFoundException, IOException {
 
-    public static String getStringCrcAsHex(final String input) {
+               boolean isMevizFile = false;
 
-        // create a new CRC-calculating object
-        final CRC32 crc = new CRC32();
+               final FileReader fileReader = new FileReader(indexFile);
+               final BufferedReader reader = new BufferedReader(fileReader);
 
-        // loop, calculating CRC for each byte of the string
-        // There is no CRC16.update(byte[]) method.
-        for (final byte b : input.getBytes()) {
-            crc.update(b);
-        }
+               parseFile: {
+                       while (true) {
+                               final String line = reader.readLine();
 
-        // note use crc.value, not crc.getValue()
-        final String hex = Integer.toHexString((int) crc.getValue()).toUpperCase();
+                               if (line == null)
+                                       break parseFile;
 
-        // System.out.println("Input string: " + input);
-        // System.out.println("Result: " + hex);
+                               if (line.contains(Constants.HTML_MAGIC_STRING)) {
+                                       isMevizFile = true;
+                                       break parseFile;
+                               }
+                       }
+               }
 
-        return hex;
-    }
+               reader.close();
+               fileReader.close();
+               return isMevizFile;
+       }
 
 }