Merge branch 'master' into subtree
[fanfix.git] / supported / Cbz.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
2aac79c7 3import java.io.File;
08fe2e33 4import java.io.IOException;
298d405a 5import java.io.InputStream;
08fe2e33
NR
6import java.net.URL;
7import java.util.ArrayList;
d3c84ac3 8import java.util.Collections;
2a25f781 9import java.util.HashMap;
d3c84ac3 10import java.util.List;
2a25f781 11import java.util.Map;
08fe2e33
NR
12import java.util.zip.ZipEntry;
13import java.util.zip.ZipInputStream;
14
15import be.nikiroo.fanfix.Instance;
45994ba5 16import be.nikiroo.fanfix.bundles.Config;
08fe2e33 17import be.nikiroo.fanfix.data.Chapter;
7445f856 18import be.nikiroo.fanfix.data.MetaData;
08fe2e33 19import be.nikiroo.fanfix.data.Paragraph;
e5a2f1b3 20import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
08fe2e33 21import be.nikiroo.fanfix.data.Story;
2aac79c7 22import be.nikiroo.utils.IOUtils;
16a81ef7 23import be.nikiroo.utils.Image;
3b2b638f 24import be.nikiroo.utils.Progress;
c289a297 25import be.nikiroo.utils.streams.MarkableFileInputStream;
08fe2e33
NR
26
27/**
28 * Support class for CBZ files (works better with CBZ created with this program,
29 * as they have some metadata available).
30 *
31 * @author niki
32 */
33class Cbz extends Epub {
34 @Override
35 protected boolean supports(URL url) {
36 return url.toString().toLowerCase().endsWith(".cbz");
37 }
38
08fe2e33
NR
39 @Override
40 protected String getDataPrefix() {
41 return "";
42 }
43
44 @Override
45 protected boolean requireInfo() {
46 return false;
47 }
08fe2e33
NR
48
49 @Override
e4fa48a0
NR
50 protected boolean isImagesDocumentByDefault() {
51 return true;
08fe2e33
NR
52 }
53
68686a37 54 @Override
e4fa48a0
NR
55 protected boolean getCover() {
56 return false;
68686a37
NR
57 }
58
08fe2e33 59 @Override
9005532f 60 public Story doProcess(Progress pg) throws IOException {
92fb0719
NR
61 if (pg == null) {
62 pg = new Progress();
63 } else {
64 pg.setMinMax(0, 100);
65 }
66
ed08c171
NR
67 Progress pgMeta = new Progress();
68 pg.addProgress(pgMeta, 10);
7445f856
NR
69 Story story = processMeta(true, pgMeta);
70 MetaData meta = story.getMeta();
091397cb 71
2a25f781 72 pgMeta.done(); // 10%
ed08c171 73
d66deb8d 74 File tmpDir = Instance.getInstance().getTempFiles().createTempDir("info-text");
2aac79c7 75 String basename = null;
08fe2e33 76
16a81ef7 77 Map<String, Image> images = new HashMap<String, Image>();
298d405a
NR
78 InputStream cbzIn = null;
79 ZipInputStream zipIn = null;
2aac79c7 80 try {
67837328 81 cbzIn = new MarkableFileInputStream(getSourceFileOriginal());
298d405a 82 zipIn = new ZipInputStream(cbzIn);
2aac79c7
NR
83 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
84 .getNextEntry()) {
85 if (!entry.isDirectory()
86 && entry.getName().startsWith(getDataPrefix())) {
87 String entryLName = entry.getName().toLowerCase();
88 boolean imageEntry = false;
8d59ce07 89 for (String ext : bsImages.getImageExt(false)) {
2aac79c7
NR
90 if (entryLName.endsWith(ext)) {
91 imageEntry = true;
92 }
08fe2e33 93 }
45994ba5 94
2aac79c7
NR
95 if (imageEntry) {
96 String uuid = meta.getUuid() + "_" + entry.getName();
97 try {
98 images.put(uuid, new Image(zipIn));
99 } catch (Exception e) {
d66deb8d 100 Instance.getInstance().getTraceHandler().error(e);
2aac79c7
NR
101 }
102
103 if (pg.getProgress() < 85) {
104 pg.add(1);
105 }
106 } else if (entryLName.endsWith(".info")) {
107 basename = entryLName.substring(0, entryLName.length()
108 - ".info".length());
109 IOUtils.write(zipIn, new File(tmpDir, entryLName));
110 } else if (entryLName.endsWith(".txt")) {
111 IOUtils.write(zipIn, new File(tmpDir, entryLName));
08fe2e33 112 }
2aac79c7
NR
113 }
114 }
45994ba5
NR
115
116 String ext = "."
d66deb8d 117 + Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
45994ba5
NR
118 String coverName = meta.getUuid() + "_" + basename + ext;
119 Image cover = images.get(coverName);
120 images.remove(coverName);
2a25f781 121
2aac79c7
NR
122 pg.setProgress(85);
123
124 // ZIP order is not correct for us
125 List<String> imagesList = new ArrayList<String>(images.keySet());
126 Collections.sort(imagesList);
127
128 pg.setProgress(90);
129
45994ba5 130 // only the description/cover is kept
298d405a
NR
131 Story origStory = getStoryFromTxt(tmpDir, basename);
132 if (origStory != null) {
27694a13
NR
133 if (origStory.getMeta().getCover() == null) {
134 origStory.getMeta().setCover(story.getMeta().getCover());
135 }
298d405a 136 story.setMeta(origStory.getMeta());
2aac79c7 137 }
45994ba5
NR
138 if (story.getMeta().getCover() == null) {
139 story.getMeta().setCover(cover);
140 }
091397cb 141 story.setChapters(new ArrayList<Chapter>());
92fb0719 142
e5a2f1b3 143 // Check if we can find non-images chapters, for hybrid-cbz support
654e27bb
NR
144 if (origStory != null) {
145 for (Chapter chap : origStory) {
146 Boolean isImages = null;
147 for (Paragraph para : chap) {
148 ParagraphType t = para.getType();
149 if (isImages == null && !t.isText(true)) {
150 isImages = true;
151 }
152 if (t.isText(false)) {
153 String line = para.getContent();
154 // Images are saved in text mode as "[image-link]"
155 if (!(line.startsWith("[") && line.endsWith("]"))) {
156 isImages = false;
157 }
e5a2f1b3
NR
158 }
159 }
e5a2f1b3 160
654e27bb
NR
161 if (isImages != null && !isImages) {
162 story.getChapters().add(chap);
163 chap.setNumber(story.getChapters().size());
164 }
e5a2f1b3
NR
165 }
166 }
167
2aac79c7
NR
168 if (!imagesList.isEmpty()) {
169 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
170 story.getChapters().add(chap);
2a25f781 171
2aac79c7
NR
172 for (String uuid : imagesList) {
173 try {
174 chap.getParagraphs().add(
175 new Paragraph(images.get(uuid)));
176 } catch (Exception e) {
d66deb8d 177 Instance.getInstance().getTraceHandler().error(e);
2aac79c7
NR
178 }
179 }
180 }
d3c84ac3 181
2aac79c7
NR
182 if (meta.getCover() == null && !images.isEmpty()) {
183 meta.setCover(images.get(imagesList.get(0)));
184 meta.setFakeCover(true);
d3c84ac3 185 }
2aac79c7
NR
186 } finally {
187 IOUtils.deltree(tmpDir);
298d405a
NR
188 if (zipIn != null) {
189 zipIn.close();
190 }
191 if (cbzIn != null) {
192 cbzIn.close();
193 }
a9eb3f46
NR
194 }
195
92fb0719 196 pg.setProgress(100);
08fe2e33
NR
197 return story;
198 }
298d405a
NR
199
200 private Story getStoryFromTxt(File tmpDir, String basename) {
201 Story origStory = null;
202
203 File txt = new File(tmpDir, basename + ".txt");
204 if (!txt.exists()) {
205 basename = null;
206 }
207 if (basename != null) {
208 try {
209 BasicSupport support = BasicSupport.getSupport(txt.toURI()
210 .toURL());
211 origStory = support.process(null);
212 } catch (Exception e) {
213 basename = null;
214 }
215 }
216
217 return origStory;
218
219 }
08fe2e33 220}