Fix CBZ: do not include the first page twice
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Cbz.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipInputStream;
13
14 import javax.imageio.ImageIO;
15
16 import be.nikiroo.fanfix.Instance;
17 import be.nikiroo.fanfix.data.Chapter;
18 import be.nikiroo.fanfix.data.Paragraph;
19 import be.nikiroo.fanfix.data.Story;
20 import be.nikiroo.utils.Progress;
21
22 /**
23 * Support class for CBZ files (works better with CBZ created with this program,
24 * as they have some metadata available).
25 *
26 * @author niki
27 */
28 class Cbz extends Epub {
29 @Override
30 protected boolean supports(URL url) {
31 return url.toString().toLowerCase().endsWith(".cbz");
32 }
33
34 @Override
35 public String getSourceName() {
36 return "cbz";
37 }
38
39 @Override
40 protected String getDataPrefix() {
41 return "";
42 }
43
44 @Override
45 protected boolean requireInfo() {
46 return false;
47 }
48
49 @Override
50 protected boolean getCover() {
51 return false;
52 }
53
54 @Override
55 protected void preprocess(URL source, InputStream in) throws IOException {
56 super.preprocess(source, in);
57 meta.setImageDocument(true);
58 }
59
60 @Override
61 public Story process(URL url, Progress pg) throws IOException {
62 if (pg == null) {
63 pg = new Progress();
64 } else {
65 pg.setMinMax(0, 100);
66 }
67
68 Story story = processMeta(url, false, true);
69 story.setChapters(new ArrayList<Chapter>());
70 Chapter chap = new Chapter(1, null);
71 story.getChapters().add(chap);
72
73 ZipInputStream zipIn = new ZipInputStream(getInput());
74
75 pg.setProgress(10);
76 List<String> images = new ArrayList<String>();
77 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
78 .getNextEntry()) {
79 if (!entry.isDirectory()
80 && entry.getName().startsWith(getDataPrefix())) {
81 String entryLName = entry.getName().toLowerCase();
82 boolean imageEntry = false;
83 for (String ext : getImageExt(false)) {
84 if (entryLName.endsWith(ext)) {
85 imageEntry = true;
86 }
87 }
88
89 if (imageEntry) {
90 String uuid = meta.getUuid() + "_" + entry.getName();
91 try {
92 File tmp = Instance.getCache().addToCache(zipIn, uuid);
93 images.add(tmp.toURI().toURL().toString());
94 } catch (Exception e) {
95 Instance.syserr(e);
96 }
97 }
98 }
99 }
100
101 pg.setProgress(80);
102
103 // ZIP order is not correct for us
104 Collections.sort(images);
105 pg.setProgress(90);
106
107 for (String uuid : images) {
108 try {
109 chap.getParagraphs().add(new Paragraph(new URL(uuid)));
110 } catch (Exception e) {
111 Instance.syserr(e);
112 }
113 }
114
115 if (meta.getCover() == null && !images.isEmpty()) {
116 InputStream in = Instance.getCache().open(new URL(images.get(0)),
117 this, true);
118 try {
119 BufferedImage fcover = ImageIO.read(in);
120 meta.setCover(fcover);
121 meta.setFakeCover(true);
122 } finally {
123 in.close();
124 }
125 }
126
127 pg.setProgress(100);
128 return story;
129 }
130 }