Fix some perf/space problems, add a cover for e621
authorNiki Roo <niki@nikiroo.be>
Thu, 16 Feb 2017 18:31:56 +0000 (19:31 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 16 Feb 2017 18:31:56 +0000 (19:31 +0100)
libs/nikiroo-utils-0.9.7-sources.jar [moved from libs/nikiroo-utils-0.9.6-sources.jar with 71% similarity]
src/be/nikiroo/fanfix/Cache.java
src/be/nikiroo/fanfix/supported/BasicSupport.java
src/be/nikiroo/fanfix/supported/E621.java
src/be/nikiroo/fanfix/supported/Epub.java
src/be/nikiroo/fanfix/supported/MangaFox.java

similarity index 71%
rename from libs/nikiroo-utils-0.9.6-sources.jar
rename to libs/nikiroo-utils-0.9.7-sources.jar
index 42d2774f3505998f1b129f265c13509ee36188f7..dbccbca122182540b78a387abd863fae900e38b2 100644 (file)
Binary files a/libs/nikiroo-utils-0.9.6-sources.jar and b/libs/nikiroo-utils-0.9.7-sources.jar differ
index 0166a37d1689fa6152ec7a451bf568071809302b..b290756d1bdcb632d22cc66caa7e812cfa26bd9e 100644 (file)
@@ -25,7 +25,6 @@ import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.MarkableFileInputStream;
-import be.nikiroo.utils.StringUtils;
 
 /**
  * This cache will manage Internet (and local) downloads, as well as put the
@@ -96,13 +95,14 @@ public class Cache {
         * @param stable
         *            TRUE for more stable resources, FALSE when they often change
         * 
-        * @return the opened resource
+        * @return the opened resource, NOT NULL
         * 
         * @throws IOException
         *             in case of I/O error
         */
        public InputStream open(URL url, BasicSupport support, boolean stable)
                        throws IOException {
+               // MUST NOT return null
                return open(url, support, stable, url);
        }
 
@@ -121,13 +121,14 @@ public class Cache {
         * @param originalUrl
         *            the original {@link URL} used to locate the cached resource
         * 
-        * @return the opened resource
+        * @return the opened resource, NOT NULL
         * 
         * @throws IOException
         *             in case of I/O error
         */
        public InputStream open(URL url, BasicSupport support, boolean stable,
                        URL originalUrl) throws IOException {
+               // MUST NOT return null
                try {
                        InputStream in = load(originalUrl, false, stable);
                        if (in == null) {
@@ -138,6 +139,7 @@ public class Cache {
                                                        + (url == null ? "null" : url.toString()), e);
                                }
 
+                               // Was just saved, can load old, so, will not be null
                                in = load(originalUrl, true, stable);
                        }
 
@@ -158,8 +160,6 @@ public class Cache {
         * @param stable
         *            TRUE for more stable resources, FALSE when they often change
         * 
-        * @return TRUE if it was pre-downloaded
-        * 
         * @throws IOException
         *             in case of I/O error
         */
@@ -203,15 +203,12 @@ public class Cache {
         *             in case of I/O error
         */
        public void saveAsImage(URL url, File target) throws IOException {
-               URL cachedUrl = new URL(url.toString()
-                               + "."
-                               + Instance.getConfig().getString(Config.IMAGE_FORMAT_CONTENT)
-                                               .toLowerCase());
+               URL cachedUrl = new URL(url.toString());
                File cached = getCached(cachedUrl);
 
                if (!cached.exists() || isOld(cached, true)) {
-                       InputStream imageIn = Instance.getCache().open(url, null, true);
-                       ImageIO.write(StringUtils.toImage(imageIn), Instance.getConfig()
+                       InputStream imageIn = open(url, null, true);
+                       ImageIO.write(IOUtils.toImage(imageIn), Instance.getConfig()
                                        .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(),
                                        cached);
                }
@@ -266,7 +263,9 @@ public class Cache {
         * 
         * @param url
         *            the resource to open
-        * @return the opened resource
+        * 
+        * @return the opened resource if found, NULL i not
+        * 
         * @throws IOException
         *             in case of I/O error
         */
index 61a4500759bccb26366d23ade04f07053c169dcb..b58f8fa380042c6165bd6dbd1d2d28909853a33d 100644 (file)
@@ -14,8 +14,6 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Scanner;
 
-import javax.imageio.ImageIO;
-
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.bundles.StringId;
@@ -24,6 +22,7 @@ import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.StringUtils;
 
 /**
@@ -609,7 +608,7 @@ public abstract class BasicSupport {
                        InputStream in = null;
                        try {
                                in = Instance.getCache().open(url, getSupport(url), true);
-                               return ImageIO.read(in);
+                               return IOUtils.toImage(in);
                        } catch (IOException e) {
                        } finally {
                                if (in != null) {
index 45c110f283ebaf825d5f7c65361917333f69add9..476e88b77765e741235909c53f3d82166302399c 100644 (file)
@@ -1,5 +1,6 @@
 package be.nikiroo.fanfix.supported;
 
+import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
@@ -47,7 +48,7 @@ class E621 extends BasicSupport {
                meta.setSubject("");
                meta.setType(getType().toString());
                meta.setImageDocument(true);
-               meta.setCover(null);
+               meta.setCover(getCover(source));
 
                return meta;
        }
@@ -84,6 +85,21 @@ class E621 extends BasicSupport {
                return true;
        }
 
+       private BufferedImage getCover(URL source) throws IOException {
+               InputStream in = Instance.getCache().open(source, this, true);
+               String images = getChapterContent(new URL(source.toString() + "?page="
+                               + 1), in, 1);
+               if (!images.isEmpty()) {
+                       int pos = images.indexOf('\n');
+                       if (pos >= 0) {
+                               images = images.substring(1, pos - 1);
+                               return getImage(this, null, images);
+                       }
+               }
+
+               return null;
+       }
+
        private String getAuthor(URL source, InputStream in) throws IOException {
                String author = getLine(in, "href=\"/post/show/", 0);
                if (author != null) {
index f6a0b410338a3a1a7635601be402054fe5c761d1..437197a5a66cb12cee19a05ef192e9d35622494d 100644 (file)
@@ -12,8 +12,6 @@ import java.util.Map.Entry;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
-import javax.imageio.ImageIO;
-
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.utils.IOUtils;
@@ -115,7 +113,7 @@ class Epub extends InfoText {
                                        // Cover
                                        if (getCover()) {
                                                try {
-                                                       cover = ImageIO.read(zipIn);
+                                                       cover = IOUtils.toImage(zipIn);
                                                } catch (Exception e) {
                                                        Instance.syserr(e);
                                                }
index 5455537b340f36993e2a55915ce955c8e48aad04..776c29eea25d43985bbc4c6262e31c3903e295a6 100644 (file)
@@ -15,6 +15,7 @@ import javax.imageio.ImageIO;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.StringUtils;
 
 class MangaFox extends BasicSupport {
@@ -186,7 +187,7 @@ class MangaFox extends BasicSupport {
                        try {
                                coverIn = openEx(cover);
                                try {
-                                       return ImageIO.read(coverIn);
+                                       return IOUtils.toImage(coverIn);
                                } finally {
                                        coverIn.close();
                                }