From: Niki Roo Date: Wed, 29 Nov 2017 20:02:19 +0000 (+0100) Subject: Update nikiroo-utils, bugfixes: X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=2a25f7814eec9854022f1c9dee188bfbdb955591;p=fanfix-jexer.git Update nikiroo-utils, bugfixes: - covers were sometimes not found (so not displayed or not deleted) - images could fail to save in JPG mode (now, we retry in PNG) - covers were not sent over the network (still need more tests) - some tmp files were not deleted in case of an import failure --- diff --git a/changelog.md b/changelog.md index d36c87f..4102812 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,9 @@ - Bug fixes - Remote server/client improvements - Better support for some CBZ files (if SUMMARY or URL files are present in it) +- Fix cover images not deleted on story delete +- Fix some images not supported because not jpeg-able (now try again in png) +- Fix some covers not found (normal and remote hopefully) ## Version 1.6.2 diff --git a/libs/nikiroo-utils-3.0.0-sources.jar b/libs/nikiroo-utils-3.1.2-sources.jar similarity index 80% rename from libs/nikiroo-utils-3.0.0-sources.jar rename to libs/nikiroo-utils-3.1.2-sources.jar index 82feb00..ec1ed6e 100644 Binary files a/libs/nikiroo-utils-3.0.0-sources.jar and b/libs/nikiroo-utils-3.1.2-sources.jar differ diff --git a/src/be/nikiroo/fanfix/DataLoader.java b/src/be/nikiroo/fanfix/DataLoader.java index 2fd59d8..2530013 100644 --- a/src/be/nikiroo/fanfix/DataLoader.java +++ b/src/be/nikiroo/fanfix/DataLoader.java @@ -1,5 +1,6 @@ package be.nikiroo.fanfix; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -228,16 +229,48 @@ public class DataLoader { public void saveAsImage(URL url, File target) throws IOException { InputStream in = open(url, null, true); try { - ImageIO.write(ImageUtils.fromStream(in), Instance.getConfig() - .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(), - target); - } catch (IOException e) { - throw new IOException("Cannot write image " + url, e); + saveAsImage(ImageUtils.fromStream(in), target); } finally { in.close(); } } + /** + * Save the given resource as an image on disk using the default image + * format for content. + * + * @param image + * the resource + * @param target + * the target file + * + * @throws IOException + * in case of I/O error + */ + public void saveAsImage(BufferedImage image, File target) + throws IOException { + try { + String format = Instance.getConfig() + .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(); + boolean ok = ImageIO.write(image, format, target); + if (!ok) { + // Some formats are not reliable + // Second change: PNG + if (!format.equals("png")) { + ok = ImageIO.write(image, "png", target); + } + + if (!ok) { + throw new IOException( + "Cannot find a writer for this image and format: " + + format); + } + } + } catch (IOException e) { + throw new IOException("Cannot write image to " + target, e); + } + } + /** * Manually add this item to the cache. * diff --git a/src/be/nikiroo/fanfix/data/Paragraph.java b/src/be/nikiroo/fanfix/data/Paragraph.java index 273aca3..a731c32 100644 --- a/src/be/nikiroo/fanfix/data/Paragraph.java +++ b/src/be/nikiroo/fanfix/data/Paragraph.java @@ -1,6 +1,6 @@ package be.nikiroo.fanfix.data; -import java.net.URL; +import java.awt.image.BufferedImage; /** * A paragraph in a chapter of the story. @@ -28,6 +28,7 @@ public class Paragraph { private ParagraphType type; private String content; + private BufferedImage contentImage; private long words; /** @@ -41,11 +42,12 @@ public class Paragraph { /** * Create a new {@link Paragraph} with the given image. * - * @param imageUrl - * the image as an URL + * @param contentImage + * the image */ - public Paragraph(URL imageUrl) { - this(ParagraphType.IMAGE, imageUrl.toString(), 1); + public Paragraph(BufferedImage contentImage) { + this(ParagraphType.IMAGE, null, 1); + this.contentImage = contentImage; } /** @@ -84,7 +86,7 @@ public class Paragraph { } /** - * The content of this {@link Paragraph}. + * The content of this {@link Paragraph} if it is not an image. * * @return the content */ @@ -102,6 +104,15 @@ public class Paragraph { this.content = content; } + /** + * The content of this {@link Paragraph} if it is an image. + * + * @return the content + */ + public BufferedImage getContentImage() { + return contentImage; + } + /** * The number of words (or images) in this {@link Paragraph}. * diff --git a/src/be/nikiroo/fanfix/library/LocalLibrary.java b/src/be/nikiroo/fanfix/library/LocalLibrary.java index 917ce09..4c45425 100644 --- a/src/be/nikiroo/fanfix/library/LocalLibrary.java +++ b/src/be/nikiroo/fanfix/library/LocalLibrary.java @@ -383,7 +383,8 @@ public class LocalLibrary extends BasicLibrary { } String coverExt = "." - + Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER); + + Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER) + .toLowerCase(); File coverFile = new File(path + coverExt); if (!coverFile.exists()) { coverFile = new File(path.substring(0, @@ -472,8 +473,10 @@ public class LocalLibrary extends BasicLibrary { } catch (IOException e) { // We should not have not-supported files in the // library - Instance.getTraceHandler().error(new IOException( - "Cannot load file from library: " + infoFile, e)); + Instance.getTraceHandler().error( + new IOException( + "Cannot load file from library: " + + infoFile, e)); } pgFiles.add(1); } diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java index 5836293..b268999 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java @@ -14,20 +14,23 @@ import be.nikiroo.utils.serial.server.ServerObject; /** * Create a new remote server that will listen for order on the given port. *

- * The available commands are given as String arrays (first item is the key, - * second is the command, the rest are the arguments): + * The available commands are given as arrays of objects (first item is the key, + * second is the command, the rest are the arguments). + *

+ * The key is always a String, the commands are also Strings; the parameters + * vary depending upon the command. *

* * @author niki diff --git a/src/be/nikiroo/fanfix/output/Cbz.java b/src/be/nikiroo/fanfix/output/Cbz.java index c350eb2..c70cc41 100644 --- a/src/be/nikiroo/fanfix/output/Cbz.java +++ b/src/be/nikiroo/fanfix/output/Cbz.java @@ -25,25 +25,27 @@ class Cbz extends BasicOutput { dir = File.createTempFile("fanfic-reader-cbz-dir", ".wip"); dir.delete(); dir.mkdir(); + try { + // will also save the images! + new InfoText().process(story, dir, targetNameOrig); - // will also save the images! - new InfoText().process(story, dir, targetNameOrig); + InfoCover.writeInfo(dir, targetNameOrig, story.getMeta()); + if (story.getMeta() != null && !story.getMeta().isFakeCover()) { + InfoCover.writeCover(dir, targetNameOrig, story.getMeta()); + } - InfoCover.writeInfo(dir, targetNameOrig, story.getMeta()); - if (story.getMeta() != null && !story.getMeta().isFakeCover()) { - InfoCover.writeCover(dir, targetNameOrig, story.getMeta()); - } + IOUtils.writeSmallFile(dir, "version", "3.0"); - IOUtils.writeSmallFile(dir, "version", "3.0"); + try { + super.process(story, targetDir, targetNameOrig); + } finally { + } - try { - super.process(story, targetDir, targetNameOrig); + IOUtils.zip(dir, target, true); } finally { + IOUtils.deltree(dir); } - IOUtils.zip(dir, target, true); - IOUtils.deltree(dir); - return target; } diff --git a/src/be/nikiroo/fanfix/output/Epub.java b/src/be/nikiroo/fanfix/output/Epub.java index 869a0bc..2c47dcb 100644 --- a/src/be/nikiroo/fanfix/output/Epub.java +++ b/src/be/nikiroo/fanfix/output/Epub.java @@ -6,7 +6,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; -import java.net.URL; import javax.imageio.ImageIO; @@ -209,7 +208,7 @@ class Epub extends BasicOutput { break; case IMAGE: File file = new File(images, getCurrentImageBestName(false)); - Instance.getCache().saveAsImage(new URL(para.getContent()), file); + Instance.getCache().saveAsImage(para.getContentImage(), file); writer.write(" "); break; diff --git a/src/be/nikiroo/fanfix/output/Text.java b/src/be/nikiroo/fanfix/output/Text.java index 28f3894..ee72385 100644 --- a/src/be/nikiroo/fanfix/output/Text.java +++ b/src/be/nikiroo/fanfix/output/Text.java @@ -5,7 +5,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; -import java.net.URL; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.StringId; @@ -105,7 +104,7 @@ class Text extends BasicOutput { protected void writeParagraphHeader(Paragraph para) throws IOException { if (para.getType() == ParagraphType.IMAGE) { File file = new File(targetDir, getCurrentImageBestName(true)); - Instance.getCache().saveAsImage(new URL(para.getContent()), file); + Instance.getCache().saveAsImage(para.getContentImage(), file); } } diff --git a/src/be/nikiroo/fanfix/supported/BasicSupport.java b/src/be/nikiroo/fanfix/supported/BasicSupport.java index ae7521e..c609c70 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupport.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupport.java @@ -757,10 +757,10 @@ public abstract class BasicSupport { * @return the {@link Paragraph} */ private Paragraph makeParagraph(URL source, String line) { - URL image = null; + BufferedImage image = null; if (line.startsWith("[") && line.endsWith("]")) { - image = getImageUrl(this, source, - line.substring(1, line.length() - 1).trim()); + image = getImage(this, source, line.substring(1, line.length() - 1) + .trim()); } if (image != null) { diff --git a/src/be/nikiroo/fanfix/supported/Cbz.java b/src/be/nikiroo/fanfix/supported/Cbz.java index 02c667c..f67f28a 100644 --- a/src/be/nikiroo/fanfix/supported/Cbz.java +++ b/src/be/nikiroo/fanfix/supported/Cbz.java @@ -1,22 +1,22 @@ package be.nikiroo.fanfix.supported; import java.awt.image.BufferedImage; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import javax.imageio.ImageIO; - import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.Paragraph; import be.nikiroo.fanfix.data.Story; +import be.nikiroo.utils.ImageUtils; import be.nikiroo.utils.Progress; /** @@ -68,9 +68,7 @@ class Cbz extends Epub { Progress pgMeta = new Progress(); pg.addProgress(pgMeta, 10); Story story = processMeta(url, false, true, pgMeta); - if (!pgMeta.isDone()) { - pgMeta.setProgress(pgMeta.getMax()); // 10% - } + pgMeta.done(); // 10% story.setChapters(new ArrayList()); Chapter chap = new Chapter(1, null); @@ -78,7 +76,7 @@ class Cbz extends Epub { ZipInputStream zipIn = new ZipInputStream(getInput()); - List images = new ArrayList(); + Map images = new HashMap(); for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn .getNextEntry()) { if (!entry.isDirectory() @@ -94,39 +92,36 @@ class Cbz extends Epub { if (imageEntry) { String uuid = meta.getUuid() + "_" + entry.getName(); try { - File tmp = Instance.getCache().addToCache(zipIn, uuid); - images.add(tmp.toURI().toURL().toString()); + images.put(uuid, ImageUtils.fromStream(zipIn)); } catch (Exception e) { Instance.getTraceHandler().error(e); } + + if (pg.getProgress() < 85) { + pg.add(1); + } } } } - pg.setProgress(80); + pg.setProgress(85); // ZIP order is not correct for us - Collections.sort(images); + List imagesList = new ArrayList(images.keySet()); + Collections.sort(imagesList); + pg.setProgress(90); - for (String uuid : images) { + for (String uuid : imagesList) { try { - chap.getParagraphs().add(new Paragraph(new URL(uuid))); + chap.getParagraphs().add(new Paragraph(images.get(uuid))); } catch (Exception e) { Instance.getTraceHandler().error(e); } } if (meta.getCover() == null && !images.isEmpty()) { - InputStream in = Instance.getCache().open(new URL(images.get(0)), - this, true); - try { - BufferedImage fcover = ImageIO.read(in); - meta.setCover(fcover); - meta.setFakeCover(true); - } finally { - in.close(); - } + meta.setCover(images.get(imagesList.get(0))); } pg.setProgress(100); diff --git a/src/be/nikiroo/fanfix/supported/InfoReader.java b/src/be/nikiroo/fanfix/supported/InfoReader.java index 466a885..ccffd7c 100644 --- a/src/be/nikiroo/fanfix/supported/InfoReader.java +++ b/src/be/nikiroo/fanfix/supported/InfoReader.java @@ -64,8 +64,9 @@ public class InfoReader { if (info.endsWith(".info")) { info = info.substring(0, info.length() - ".info".length()); String ext = "." - + Instance.getConfig().getString( - Config.IMAGE_FORMAT_COVER); + + Instance.getConfig() + .getString(Config.IMAGE_FORMAT_COVER) + .toLowerCase(); meta.setCover(BasicSupport.getImage(null, sourceInfoFile, info + ext)); } diff --git a/src/be/nikiroo/fanfix/test/BasicSupportTest.java b/src/be/nikiroo/fanfix/test/BasicSupportTest.java index af33f9d..dba0ef0 100644 --- a/src/be/nikiroo/fanfix/test/BasicSupportTest.java +++ b/src/be/nikiroo/fanfix/test/BasicSupportTest.java @@ -21,7 +21,7 @@ import be.nikiroo.utils.Progress; import be.nikiroo.utils.test.TestCase; import be.nikiroo.utils.test.TestLauncher; -public class BasicSupportTest extends TestLauncher { +class BasicSupportTest extends TestLauncher { // quote chars private char openQuote = Instance.getTrans().getCharacter( StringId.OPEN_SINGLE_QUOTE); diff --git a/src/be/nikiroo/fanfix/test/LibraryTest.java b/src/be/nikiroo/fanfix/test/LibraryTest.java index 43ca087..fc61e7d 100644 --- a/src/be/nikiroo/fanfix/test/LibraryTest.java +++ b/src/be/nikiroo/fanfix/test/LibraryTest.java @@ -15,7 +15,7 @@ import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.test.TestCase; import be.nikiroo.utils.test.TestLauncher; -public class LibraryTest extends TestLauncher { +class LibraryTest extends TestLauncher { private BasicLibrary lib; private File tmp; diff --git a/src/be/nikiroo/fanfix/test/Test.java b/src/be/nikiroo/fanfix/test/Test.java index 0dc544b..3ff9a7a 100644 --- a/src/be/nikiroo/fanfix/test/Test.java +++ b/src/be/nikiroo/fanfix/test/Test.java @@ -17,6 +17,13 @@ import be.nikiroo.utils.test.TestLauncher; * @author niki */ public class Test extends TestLauncher { + /** + * Create the Fanfix {@link TestLauncher}. + * + * @param args + * the arguments to configure the number of columns and the ok/ko + * {@link String}s + */ public Test(String[] args) { super("Fanfix", args); Instance.setTraceHandler(null); @@ -30,6 +37,7 @@ public class Test extends TestLauncher { * @param args * the arguments passed to the {@link TestLauncher}s. * @throws IOException + * in case of I/O error */ static public void main(String[] args) throws IOException { File tmpConfig = File.createTempFile("fanfix-config_", ".test");