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;
+ ").").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())
* occurred.
*/
public boolean parseCommandlineArguments(final String[] args) {
- return parser.parse(args);
+ final boolean parsingSucceeded = parser.parse(args);
+
+ if (!parsingSucceeded)
+ parser.showHelp();
+
+ return parsingSucceeded;
}
}
--- /dev/null
+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();
+ }
+ }
+
+ }
+
+}
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;
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;
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() {
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);
.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) {
}
public void initializeLayouts() {
- layouts = new HashSet<Layout>();
- layouts.add(new MixedLayout());
+
+ layouts = Utils.getLayouts();
final String galleryTitle = commandlineHandler.getGalleryTitle();
}
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();
}
@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>());
}
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;
+ }
}