From: Niki Roo Date: Thu, 16 Feb 2017 18:31:56 +0000 (+0100) Subject: Fix some perf/space problems, add a cover for e621 X-Git-Tag: fanfix-0.9.4~2 X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=commitdiff_plain;h=595dfa7a6a1dc8041b3a5a4fe7ee2fae89029a69;hp=7d0d2be6b0e9bd4e03ac8a7d749f49d6d1436242 Fix some perf/space problems, add a cover for e621 --- diff --git a/libs/nikiroo-utils-0.9.6-sources.jar b/libs/nikiroo-utils-0.9.7-sources.jar similarity index 71% rename from libs/nikiroo-utils-0.9.6-sources.jar rename to libs/nikiroo-utils-0.9.7-sources.jar index 42d2774..dbccbca 100644 Binary files a/libs/nikiroo-utils-0.9.6-sources.jar and b/libs/nikiroo-utils-0.9.7-sources.jar differ diff --git a/src/be/nikiroo/fanfix/Cache.java b/src/be/nikiroo/fanfix/Cache.java index 0166a37..b290756 100644 --- a/src/be/nikiroo/fanfix/Cache.java +++ b/src/be/nikiroo/fanfix/Cache.java @@ -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 */ diff --git a/src/be/nikiroo/fanfix/supported/BasicSupport.java b/src/be/nikiroo/fanfix/supported/BasicSupport.java index 61a4500..b58f8fa 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupport.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupport.java @@ -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) { diff --git a/src/be/nikiroo/fanfix/supported/E621.java b/src/be/nikiroo/fanfix/supported/E621.java index 45c110f..476e88b 100644 --- a/src/be/nikiroo/fanfix/supported/E621.java +++ b/src/be/nikiroo/fanfix/supported/E621.java @@ -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) { diff --git a/src/be/nikiroo/fanfix/supported/Epub.java b/src/be/nikiroo/fanfix/supported/Epub.java index f6a0b41..437197a 100644 --- a/src/be/nikiroo/fanfix/supported/Epub.java +++ b/src/be/nikiroo/fanfix/supported/Epub.java @@ -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); } diff --git a/src/be/nikiroo/fanfix/supported/MangaFox.java b/src/be/nikiroo/fanfix/supported/MangaFox.java index 5455537..776c29e 100644 --- a/src/be/nikiroo/fanfix/supported/MangaFox.java +++ b/src/be/nikiroo/fanfix/supported/MangaFox.java @@ -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(); }