do not allow empty cover images
[fanfix.git] / src / be / nikiroo / fanfix / supported / BasicSupportImages.java
index 85b79c754ec692f6b0a58c8039051c98c243ddb9..f56b50c2b67a6dd9f5a4b1e0de572767d0a42dde 100644 (file)
@@ -9,21 +9,43 @@ import java.net.URL;
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.utils.Image;
 
+/**
+ * Helper class for {@link BasicSupport}, mostly dedicated to images for
+ * the classes that implement {@link BasicSupport}.
+ * 
+ * @author niki
+ */
 public class BasicSupportImages {
        /**
         * Check if the given resource can be a local image or a remote image, then
         * refresh the cache with it if it is.
         * 
+        * @param support
+        *            the support to use to download the resource (can be NULL)
         * @param dir
         *            the local directory to search, if any
         * @param line
         *            the resource to check
         * 
         * @return the image if found, or NULL
-        * 
         */
-       static Image getImage(BasicSupport support, File dir, String line) {
+       public Image getImage(BasicSupport support, File dir, String line) {
                URL url = getImageUrl(support, dir, line);
+               return getImage(support,url);
+       }
+       
+       /**
+        * Check if the given resource can be a local image or a remote image, then
+        * refresh the cache with it if it is.
+        * 
+        * @param support
+        *            the support to use to download the resource (can be NULL)
+        * @param url
+        *            the actual URL to check (file or remote, can be NULL)
+        * 
+        * @return the image if found, or NULL
+        */
+       public Image getImage(BasicSupport support, URL url) {
                if (url != null) {
                        if ("file".equals(url.getProtocol())) {
                                if (new File(url.getPath()).isDirectory()) {
@@ -32,8 +54,14 @@ public class BasicSupportImages {
                        }
                        InputStream in = null;
                        try {
-                               in = Instance.getCache().open(url, support, true);
-                               return new Image(in);
+                               in = Instance.getInstance().getCache().open(url, support, true);
+                               Image img = new Image(in);
+                               if (img.getSize() == 0) {
+                                       img.close();
+                                       throw new IOException(
+                                                       "Empty image not accepted");
+                               }
+                               return img;
                        } catch (IOException e) {
                        } finally {
                                if (in != null) {
@@ -52,6 +80,8 @@ public class BasicSupportImages {
         * Check if the given resource can be a local image or a remote image, then
         * refresh the cache with it if it is.
         * 
+        * @param support
+        *            the support to use to download the resource (can be NULL)
         * @param dir
         *            the local directory to search, if any
         * @param line
@@ -60,7 +90,7 @@ public class BasicSupportImages {
         * @return the image URL if found, or NULL
         * 
         */
-       static URL getImageUrl(BasicSupport support, File dir, String line) {
+       public URL getImageUrl(BasicSupport support, File dir, String line) {
                URL url = null;
 
                if (line != null) {
@@ -102,7 +132,7 @@ public class BasicSupportImages {
                                // try for URLs
                                try {
                                        for (String ext : getImageExt(true)) {
-                                               if (Instance.getCache()
+                                               if (Instance.getInstance().getCache()
                                                                .check(new URL(line + ext), true)) {
                                                        url = new URL(line + ext);
                                                        break;
@@ -114,7 +144,7 @@ public class BasicSupportImages {
                                                for (String ext : getImageExt(true)) {
                                                        try {
                                                                url = new URL(line + ext);
-                                                               Instance.getCache().refresh(url, support, true);
+                                                               Instance.getInstance().getCache().refresh(url, support, true);
                                                                break;
                                                        } catch (IOException e) {
                                                                // no image with this ext
@@ -130,7 +160,7 @@ public class BasicSupportImages {
                        // refresh the cached file
                        if (url != null) {
                                try {
-                                       Instance.getCache().refresh(url, support, true);
+                                       Instance.getInstance().getCache().refresh(url, support, true);
                                } catch (IOException e) {
                                        // woops, broken image
                                        url = null;
@@ -151,7 +181,7 @@ public class BasicSupportImages {
         * 
         * @return the extensions
         */
-       static String[] getImageExt(boolean emptyAllowed) {
+       public String[] getImageExt(boolean emptyAllowed) {
                if (emptyAllowed) {
                        return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
                }