Fix CBZ: do not include the first page twice
authorNiki Roo <niki@nikiroo.be>
Sat, 4 Mar 2017 20:26:12 +0000 (21:26 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 4 Mar 2017 20:26:12 +0000 (21:26 +0100)
- 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
src/be/nikiroo/fanfix/output/Cbz.java
src/be/nikiroo/fanfix/output/InfoCover.java
src/be/nikiroo/fanfix/supported/Cbz.java
src/be/nikiroo/fanfix/supported/E621.java
src/be/nikiroo/fanfix/supported/InfoReader.java

index 06cf625d3a66e59056d124e0172ab14c7f482e0a..189277d9480fb8ef4abf3f5b6b8646a5d74d303e 100644 (file)
@@ -27,6 +27,7 @@ public class MetaData implements Cloneable, Comparable<MetaData> {
        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<MetaData> {
                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);
index c1d5955d9260013dc94e25c2abefc2ce682c9536..8d59ae883a7586d966440cb6aff738ce83e6739a 100644 (file)
@@ -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");
 
index 7db2a1cf5f5f9d2c5aba9b0bfb8696d11190d9f2..4be150c268843285d952c5672db8fc9c65615118 100644 (file)
@@ -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();
                        }
index b63ec5d45395c8a8529e1e20bcdfef8f267626e4..7113248ba485fb4b91ad06f5b614cd49f1b7a3c7 100644 (file)
@@ -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;
        }
index 40147652932dcc798e29b0aeebe2b35924260979..9315716715b5dbfd6fad8a54c4f1a57bc92fced2 100644 (file)
@@ -50,6 +50,7 @@ class E621 extends BasicSupport {
                meta.setType(getType().toString());
                meta.setImageDocument(true);
                meta.setCover(getCover(source));
+               meta.setFakeCover(true);
 
                return meta;
        }
index 9372eddfebb44c85a336c97fef578bbf972b0e5f..6f1a7a50002281710613293125731f35ed0f75e0 100644 (file)
@@ -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()));