From 99bc9002a93c8ab224934032a1a5fe0c00d5de96 Mon Sep 17 00:00:00 2001
From: Svjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Date: Sun, 7 Dec 2014 17:51:33 +0200
Subject: [PATCH] better error handling

---
 .../meviz/htmlindexer/AbstractIndexer.java    | 48 +++++++++----------
 .../meviz/htmlindexer/FilesystemIndexer.java  | 19 ++++----
 .../svjatoslav/meviz/htmlindexer/Layout.java  |  9 ++--
 .../htmlindexer/layouts/MixedLayout.java      |  6 +--
 .../metadata/DirectoryMetadata.java           | 14 ++++--
 .../metadata/fileTypes/AbstractFile.java      |  6 +--
 .../metadata/fileTypes/DirectoryFile.java     |  2 +-
 .../metadata/fileTypes/GeneralFile.java       |  2 +-
 .../metadata/fileTypes/MevizSpecialFile.java  |  2 +-
 .../metadata/fileTypes/Picture.java           | 15 ++----
 10 files changed, 58 insertions(+), 65 deletions(-)

diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java
index 762fcad..84a5868 100644
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/AbstractIndexer.java
@@ -12,9 +12,31 @@ import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
 
 public abstract class AbstractIndexer {
 
+	public static boolean isImage(final String fileExtension) {
+		for (final String ext : Constants.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 shallFileBeIndexed(final File file) {
+
+		if (file.getName().startsWith("."))
+			return false;
+		if (file.getName().startsWith("index"))
+			if (file.getName().endsWith(".html"))
+				return false;
+
+		return true;
+	}
+
 	public void compileHtml(final Layout layout,
 			final DirectoryMetadata directory)
-			throws UnsupportedEncodingException {
+					throws UnsupportedEncodingException {
 
 		for (final AbstractFile file : directory.getFiles())
 			if (file instanceof GeneralFile) {
@@ -26,7 +48,7 @@ public abstract class AbstractIndexer {
 					layout.enlistFile(file);
 
 			} else if (file instanceof Picture)
-				layout.enlistImage(file);
+				layout.enlistImage((Picture) file);
 			else if (file instanceof DirectoryFile)
 				layout.enlistDirectory(file);
 
@@ -40,26 +62,4 @@ public abstract class AbstractIndexer {
 	public abstract String getThumbnailUrl(Picture picture,
 			final Dimension desiredDimension, IndexingContext context);
 
-	public static boolean isImage(final String fileExtension) {
-		for (final String ext : Constants.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 shallFileBeIndexed(final File file) {
-
-		if (file.getName().startsWith("."))
-			return false;
-		if (file.getName().startsWith("index"))
-			if (file.getName().endsWith(".html"))
-				return false;
-
-		return true;
-	}
-
 }
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java
index 58244f0..61b9027 100755
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/FilesystemIndexer.java
@@ -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.
@@ -54,8 +54,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;
@@ -148,7 +147,7 @@ public class FilesystemIndexer extends AbstractIndexer {
 		validMetadataFiles.add(thumbnailFileName);
 		final File thumbnailFile = new File(
 				Utils.getThumbnailsDirectoryPath(directoryToIndex)
-						+ thumbnailFileName);
+				+ thumbnailFileName);
 
 		if (!thumbnailFile.exists()) {
 
@@ -160,7 +159,7 @@ public class FilesystemIndexer extends AbstractIndexer {
 					desiredDimension.getAwtDimension());
 		}
 		return Constants.THUMBNAILS_DIRECTORY_NAME + "/"
-				+ UrlParamEncoder.encode(thumbnailFileName);
+		+ UrlParamEncoder.encode(thumbnailFileName);
 	}
 
 	public void indexForLayout(final Layout layout)
@@ -175,7 +174,7 @@ public class FilesystemIndexer extends AbstractIndexer {
 					layout.enlistFile(abstractFile);
 
 			} else if (abstractFile instanceof Picture)
-				layout.enlistImage(abstractFile);
+				layout.enlistImage((Picture) abstractFile);
 			else if (abstractFile instanceof DirectoryFile)
 				layout.enlistDirectory(abstractFile);
 
@@ -204,7 +203,7 @@ public class FilesystemIndexer extends AbstractIndexer {
 
 		metadataFile = new File(
 				Utils.getThumbnailsDirectoryPath(directoryToIndex)
-						+ Constants.METADATA_FILE_NAME);
+				+ Constants.METADATA_FILE_NAME);
 
 		metadata = MetadadaHelper.initDirectoryMetadata(metadataFile);
 	}
@@ -218,11 +217,9 @@ public class FilesystemIndexer extends AbstractIndexer {
 
 	}
 
-	public void updateMetadata() throws IOException, ImageFormatError {
+	public void updateMetadata() throws Exception {
 
-		final File[] directoryContent = directoryToIndex.listFiles();
-
-		for (final File file : directoryContent)
+		for (final File file : directoryToIndex.listFiles())
 			if (shallFileBeIndexed(file)) {
 
 				metadata.ensureFileMetainfoIsUpToDate(directoryToIndex, file);
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java
index 5110b3e..8df469e 100755
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/Layout.java
@@ -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.
@@ -13,11 +13,12 @@ import java.io.UnsupportedEncodingException;
 import java.util.List;
 
 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
+import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
 
 /**
  * In order to have possibility of several different layouts per directory.
  * Universal interface is defined.
- * 
+ *
  * Layout implementation is responsible for actual HTML generation.
  */
 
@@ -36,11 +37,11 @@ public interface Layout {
 	/**
 	 * Enlist image file.
 	 */
-	public void enlistImage(AbstractFile picture);
+	public void enlistImage(Picture picture);
 
 	/**
 	 * Enlist video in OGV format.
-	 * 
+	 *
 	 * @throws UnsupportedEncodingException
 	 */
 	public void enlistOgv(AbstractFile file)
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java
index f336ad1..f7eee2c 100755
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/layouts/MixedLayout.java
@@ -69,11 +69,9 @@ public class MixedLayout implements Layout {
 	}
 
 	@Override
-	public void enlistImage(final AbstractFile abstractFile) {
+	public void enlistImage(final Picture picture) {
 		try {
 
-			final Picture picture = (Picture) abstractFile;
-
 			final Dimension imageSize = picture.getDimensions();
 
 			final List<Dimension> recommendedDimensions = Picture
@@ -122,7 +120,7 @@ public class MixedLayout implements Layout {
 							+ e.toString()
 							+ ", thumbnail generation skipped, enlisting as ordinary file instead.",
 					e);
-			enlistFile(abstractFile);
+			enlistFile(picture);
 		}
 	}
 
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java
index 110f2bd..5c560b0 100755
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/DirectoryMetadata.java
@@ -57,7 +57,7 @@ public class DirectoryMetadata implements Serializable {
 	}
 
 	public void ensureFileMetainfoIsUpToDate(final File parentDirectory,
-			final File file) throws IOException, ImageFormatError {
+			final File file) throws Exception {
 
 		final String fileName = file.getName();
 
@@ -73,8 +73,7 @@ public class DirectoryMetadata implements Serializable {
 	}
 
 	private AbstractFile getAbstractFile(final File parentDirectory,
-			final File file, final String fileName) throws IOException,
-			ImageFormatError {
+			final File file, final String fileName) throws Exception {
 
 		if (file.isDirectory())
 			return new DirectoryFile(parentDirectory, fileName);
@@ -83,7 +82,12 @@ public class DirectoryMetadata implements Serializable {
 				.toLowerCase();
 
 		if (AbstractIndexer.isImage(fileExtension))
-			return new Picture(parentDirectory, fileName);
+			try {
+				return new Picture(parentDirectory, fileName);
+			} catch (final Exception exception) {
+				// in case image decoding failed, handle image as general file
+				return new GeneralFile(parentDirectory, fileName);
+			}
 
 		if (fileExtension.equals("meviz"))
 			return new MevizSpecialFile(parentDirectory, fileName);
@@ -112,7 +116,7 @@ public class DirectoryMetadata implements Serializable {
 	}
 
 	public AbstractFile registerFile(final File parentDirectory, final File file)
-			throws IOException, ImageFormatError {
+			throws Exception {
 
 		final String fileName = file.getName();
 
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java
index fb6e0a8..566e944 100644
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/AbstractFile.java
@@ -23,7 +23,7 @@ public abstract class AbstractFile implements Serializable {
 	private transient boolean metaInfoVerified;
 
 	public AbstractFile(final File parentDirectory, final String fileName)
-			throws IOException {
+			throws Exception {
 		this.fileName = fileName;
 		ensureFileMetainfoIsUpToDate(parentDirectory);
 	}
@@ -32,7 +32,7 @@ public abstract class AbstractFile implements Serializable {
 	 * @return <code>true</code> if file metadata was updated.
 	 */
 	public boolean ensureFileMetainfoIsUpToDate(final File parentDirectory)
-			throws IOException {
+			throws Exception {
 		if (!isMetainfoUpToDate(parentDirectory)) {
 			fileLength = getFile(parentDirectory).length();
 			updateFileMetainfo(parentDirectory);
@@ -87,6 +87,6 @@ public abstract class AbstractFile implements Serializable {
 	}
 
 	protected abstract void updateFileMetainfo(final File parentDirectory)
-			throws IOException;
+			throws Exception;
 
 }
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java
index 7c53e79..c2f252a 100644
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/DirectoryFile.java
@@ -8,7 +8,7 @@ public class DirectoryFile extends AbstractFile {
 	private static final long serialVersionUID = 2387153281300820532L;
 
 	public DirectoryFile(final File parentDirectory, final String fileName)
-			throws IOException {
+			throws Exception {
 		super(parentDirectory, fileName);
 	}
 
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java
index f4d6657..43db928 100644
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/GeneralFile.java
@@ -8,7 +8,7 @@ public class GeneralFile extends AbstractFile {
 	private static final long serialVersionUID = -2373609481729401415L;
 
 	public GeneralFile(final File parentDirectory, final String fileName)
-			throws IOException {
+			throws Exception {
 		super(parentDirectory, fileName);
 	}
 
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java
index 7748cb7..f4ddc20 100644
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/MevizSpecialFile.java
@@ -8,7 +8,7 @@ public class MevizSpecialFile extends AbstractFile {
 	private static final long serialVersionUID = -8749190459653455640L;
 
 	public MevizSpecialFile(final File parentDirectory, final String fileName)
-			throws IOException {
+			throws Exception {
 		super(parentDirectory, fileName);
 	}
 
diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java
index dfc883f..9af52df 100755
--- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java
+++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java
@@ -18,7 +18,6 @@ import java.awt.image.ImageFilter;
 import java.awt.image.ImageProducer;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
@@ -28,7 +27,6 @@ import javax.swing.ImageIcon;
 
 import eu.svjatoslav.commons.file.FilePathParser;
 import eu.svjatoslav.meviz.htmlindexer.Constants;
-import eu.svjatoslav.meviz.htmlindexer.ImageFormatError;
 import eu.svjatoslav.meviz.htmlindexer.Utils;
 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 
@@ -144,7 +142,7 @@ public class Picture extends AbstractFile {
 	private Dimension dimensions;
 
 	public Picture(final File parentDirectory, final String fileName)
-			throws IOException, ImageFormatError {
+			throws Exception {
 		super(parentDirectory, fileName);
 	}
 
@@ -175,14 +173,9 @@ public class Picture extends AbstractFile {
 	}
 
 	@Override
-	public void updateFileMetainfo(final File parentDirectory)
-			throws IOException {
-		BufferedImage image;
-		try {
-			image = Utils.getBufferedImage(getFile(parentDirectory));
-		} catch (final ImageFormatError e) {
-			throw new RuntimeException(e);
-		}
+	public void updateFileMetainfo(final File parentDirectory) throws Exception {
+		final BufferedImage image = Utils
+				.getBufferedImage(getFile(parentDirectory));
 		dimensions = new Dimension(image.getWidth(), image.getHeight());
 	}
 
-- 
2.20.1