From: Svjatoslav Agejenko Date: Sat, 2 Nov 2013 22:03:42 +0000 (+0200) Subject: possibility to remove artifacts generated by meviz index X-Git-Tag: meviz-1.0~73 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=5e2fffbc3363880e2da9c9d282537a8e2653fa8d;p=meviz.git possibility to remove artifacts generated by meviz index --- diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java index eb32317..95aacf0 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java @@ -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 index 0000000..ba455ef --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexRemover.java @@ -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(); + } + } + + } + +} diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java index 0edea71..d9726dd 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java @@ -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 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 getChildPath(final File file) { @@ -219,8 +190,8 @@ public class FilesystemIndexer extends AbstractIndexer { } public void initializeLayouts() { - layouts = new HashSet(); - 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(); } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java index ae6c4c6..5c469d8 100644 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Main.java @@ -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()); + if (commandlineHandler.parseCommandlineArguments(args)) + if (commandlineHandler.removeIndex.isParameterSpecified()) + new FilesystemIndexRemover(commandlineHandler); + else + new FilesystemIndexer(commandlineHandler.getWorkingDirectory(), + commandlineHandler, new ArrayList()); } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java index b21f8e9..f8c82fa 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Utils.java @@ -10,58 +10,112 @@ 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 getLayouts() { + final HashSet layouts = new HashSet(); + 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; + } }