From a9eb3f46dd83115e7a549e96e800b932162b68ad Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 4 Mar 2017 21:26:12 +0100 Subject: [PATCH] Fix CBZ: do not include the first page twice - The first page was included twice in CBZ: once for the cover, once for the first page; now, the cover is tagged as 'fake' when it is the first page, and is not inserted into the CBZ --- src/be/nikiroo/fanfix/data/MetaData.java | 22 +++++++++++++++++ src/be/nikiroo/fanfix/output/Cbz.java | 5 +++- src/be/nikiroo/fanfix/output/InfoCover.java | 2 ++ src/be/nikiroo/fanfix/supported/Cbz.java | 24 +++++++++++++++---- src/be/nikiroo/fanfix/supported/E621.java | 1 + .../nikiroo/fanfix/supported/InfoReader.java | 1 + 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/be/nikiroo/fanfix/data/MetaData.java b/src/be/nikiroo/fanfix/data/MetaData.java index 06cf625..189277d 100644 --- a/src/be/nikiroo/fanfix/data/MetaData.java +++ b/src/be/nikiroo/fanfix/data/MetaData.java @@ -27,6 +27,7 @@ public class MetaData implements Cloneable, Comparable { private boolean imageDocument; private long words; private String creationDate; + private boolean fakeCover; /** * The title of the story. @@ -355,6 +356,27 @@ public class MetaData implements Cloneable, Comparable { this.creationDate = creationDate; } + /** + * The cover in this {@link MetaData} object is "fake", in the sens that it + * comes from the actual content images. + * + * @return TRUE for a fake cover + */ + public boolean isFakeCover() { + return fakeCover; + } + + /** + * The cover in this {@link MetaData} object is "fake", in the sens that it + * comes from the actual content images + * + * @param fakeCover + * TRUE for a fake cover + */ + public void setFakeCover(boolean fakeCover) { + this.fakeCover = fakeCover; + } + public int compareTo(MetaData o) { String oUuid = o == null ? null : o.getUuid(); return getUuid().compareTo(oUuid); diff --git a/src/be/nikiroo/fanfix/output/Cbz.java b/src/be/nikiroo/fanfix/output/Cbz.java index c1d5955..8d59ae8 100644 --- a/src/be/nikiroo/fanfix/output/Cbz.java +++ b/src/be/nikiroo/fanfix/output/Cbz.java @@ -28,8 +28,11 @@ class Cbz extends BasicOutput { // will also save the images! new InfoText().process(story, dir, targetNameOrig); + InfoCover.writeInfo(dir, targetNameOrig, story.getMeta()); - InfoCover.writeCover(dir, targetNameOrig, story.getMeta()); + if (story.getMeta() != null && !story.getMeta().isFakeCover()) { + InfoCover.writeCover(dir, targetNameOrig, story.getMeta()); + } IOUtils.writeSmallFile(dir, "version", "3.0"); diff --git a/src/be/nikiroo/fanfix/output/InfoCover.java b/src/be/nikiroo/fanfix/output/InfoCover.java index 7db2a1c..4be150c 100644 --- a/src/be/nikiroo/fanfix/output/InfoCover.java +++ b/src/be/nikiroo/fanfix/output/InfoCover.java @@ -57,6 +57,8 @@ class InfoCover { writeMeta(infoWriter, "WORDCOUNT", Long.toString(meta.getWords())); writeMeta(infoWriter, "CREATION_DATE", meta.getCreationDate()); + writeMeta(infoWriter, "FAKE_COVER", + Boolean.toString(meta.isFakeCover())); } finally { infoWriter.close(); } diff --git a/src/be/nikiroo/fanfix/supported/Cbz.java b/src/be/nikiroo/fanfix/supported/Cbz.java index b63ec5d..7113248 100644 --- a/src/be/nikiroo/fanfix/supported/Cbz.java +++ b/src/be/nikiroo/fanfix/supported/Cbz.java @@ -1,5 +1,6 @@ package be.nikiroo.fanfix.supported; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -10,6 +11,8 @@ import java.util.List; 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; @@ -85,9 +88,9 @@ class Cbz extends Epub { if (imageEntry) { String uuid = meta.getUuid() + "_" + entry.getName(); - images.add(uuid); try { - Instance.getCache().addToCache(zipIn, uuid); + File tmp = Instance.getCache().addToCache(zipIn, uuid); + images.add(tmp.toURI().toURL().toString()); } catch (Exception e) { Instance.syserr(e); } @@ -97,19 +100,30 @@ class Cbz extends Epub { pg.setProgress(80); - // ZIP order is not sure + // ZIP order is not correct for us Collections.sort(images); pg.setProgress(90); for (String uuid : images) { try { - chap.getParagraphs().add( - new Paragraph(new File(uuid).toURI().toURL())); + chap.getParagraphs().add(new Paragraph(new URL(uuid))); } catch (Exception e) { Instance.syserr(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(); + } + } + pg.setProgress(100); return story; } diff --git a/src/be/nikiroo/fanfix/supported/E621.java b/src/be/nikiroo/fanfix/supported/E621.java index 4014765..9315716 100644 --- a/src/be/nikiroo/fanfix/supported/E621.java +++ b/src/be/nikiroo/fanfix/supported/E621.java @@ -50,6 +50,7 @@ class E621 extends BasicSupport { meta.setType(getType().toString()); meta.setImageDocument(true); meta.setCover(getCover(source)); + meta.setFakeCover(true); return meta; } diff --git a/src/be/nikiroo/fanfix/supported/InfoReader.java b/src/be/nikiroo/fanfix/supported/InfoReader.java index 9372edd..6f1a7a5 100644 --- a/src/be/nikiroo/fanfix/supported/InfoReader.java +++ b/src/be/nikiroo/fanfix/supported/InfoReader.java @@ -60,6 +60,7 @@ public class InfoReader { meta.setWords(0); } meta.setCreationDate(getInfoTag(in, "CREATION_DATE")); + meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER"))); if (meta.getCover() == null) { meta.setCover(BasicSupport.getDefaultCover(meta.getSubject())); -- 2.27.0