woops in nikiroo-utils version, manual package done
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Cbz.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
2aac79c7 3import java.io.File;
298d405a 4import java.io.FileInputStream;
08fe2e33 5import java.io.IOException;
298d405a 6import java.io.InputStream;
08fe2e33
NR
7import java.net.URL;
8import java.util.ArrayList;
d3c84ac3 9import java.util.Collections;
2a25f781 10import java.util.HashMap;
d3c84ac3 11import java.util.List;
2a25f781 12import java.util.Map;
08fe2e33
NR
13import java.util.zip.ZipEntry;
14import java.util.zip.ZipInputStream;
15
16import be.nikiroo.fanfix.Instance;
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;
298d405a 24import be.nikiroo.utils.MarkableFileInputStream;
3b2b638f 25import be.nikiroo.utils.Progress;
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
2aac79c7
NR
74 File tmpDir = Instance.getTempFiles().createTempDir("info-text");
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 {
298d405a
NR
81 cbzIn = new MarkableFileInputStream(new FileInputStream(
82 getSourceFileOriginal()));
83 zipIn = new ZipInputStream(cbzIn);
2aac79c7
NR
84 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
85 .getNextEntry()) {
86 if (!entry.isDirectory()
87 && entry.getName().startsWith(getDataPrefix())) {
88 String entryLName = entry.getName().toLowerCase();
89 boolean imageEntry = false;
7445f856 90 for (String ext : BasicSupportImages.getImageExt(false)) {
2aac79c7
NR
91 if (entryLName.endsWith(ext)) {
92 imageEntry = true;
93 }
08fe2e33 94 }
08fe2e33 95
2aac79c7
NR
96 if (imageEntry) {
97 String uuid = meta.getUuid() + "_" + entry.getName();
98 try {
99 images.put(uuid, new Image(zipIn));
100 } catch (Exception e) {
101 Instance.getTraceHandler().error(e);
102 }
103
104 if (pg.getProgress() < 85) {
105 pg.add(1);
106 }
107 } else if (entryLName.endsWith(".info")) {
108 basename = entryLName.substring(0, entryLName.length()
109 - ".info".length());
110 IOUtils.write(zipIn, new File(tmpDir, entryLName));
111 } else if (entryLName.endsWith(".txt")) {
112 IOUtils.write(zipIn, new File(tmpDir, entryLName));
08fe2e33 113 }
2aac79c7
NR
114 }
115 }
2a25f781 116
2aac79c7
NR
117 pg.setProgress(85);
118
119 // ZIP order is not correct for us
120 List<String> imagesList = new ArrayList<String>(images.keySet());
121 Collections.sort(imagesList);
122
123 pg.setProgress(90);
124
e5a2f1b3 125 // only the description is kept
298d405a
NR
126 Story origStory = getStoryFromTxt(tmpDir, basename);
127 if (origStory != null) {
27694a13
NR
128 if (origStory.getMeta().getCover() == null) {
129 origStory.getMeta().setCover(story.getMeta().getCover());
130 }
298d405a 131 story.setMeta(origStory.getMeta());
2aac79c7 132 }
091397cb 133 story.setChapters(new ArrayList<Chapter>());
92fb0719 134
e5a2f1b3
NR
135 // Check if we can find non-images chapters, for hybrid-cbz support
136 for (Chapter chap : origStory) {
137 Boolean isImages = null;
138 for (Paragraph para : chap) {
139 ParagraphType t = para.getType();
140 if (isImages == null && !t.isText(true)) {
141 isImages = true;
142 }
143 if (t.isText(false)) {
144 String line = para.getContent();
145 // Images are saved in text mode as "[image-link]"
146 if (!(line.startsWith("[") && line.endsWith("]"))) {
147 isImages = false;
148 }
149 }
150 }
151
152 if (isImages != null && !isImages) {
153 story.getChapters().add(chap);
154 chap.setNumber(story.getChapters().size());
155 }
156 }
157
2aac79c7
NR
158 if (!imagesList.isEmpty()) {
159 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
160 story.getChapters().add(chap);
2a25f781 161
2aac79c7
NR
162 for (String uuid : imagesList) {
163 try {
164 chap.getParagraphs().add(
165 new Paragraph(images.get(uuid)));
166 } catch (Exception e) {
167 Instance.getTraceHandler().error(e);
168 }
169 }
170 }
d3c84ac3 171
2aac79c7
NR
172 if (meta.getCover() == null && !images.isEmpty()) {
173 meta.setCover(images.get(imagesList.get(0)));
174 meta.setFakeCover(true);
d3c84ac3 175 }
2aac79c7
NR
176 } finally {
177 IOUtils.deltree(tmpDir);
298d405a
NR
178 if (zipIn != null) {
179 zipIn.close();
180 }
181 if (cbzIn != null) {
182 cbzIn.close();
183 }
a9eb3f46
NR
184 }
185
92fb0719 186 pg.setProgress(100);
08fe2e33
NR
187 return story;
188 }
298d405a
NR
189
190 private Story getStoryFromTxt(File tmpDir, String basename) {
191 Story origStory = null;
192
193 File txt = new File(tmpDir, basename + ".txt");
194 if (!txt.exists()) {
195 basename = null;
196 }
197 if (basename != null) {
198 try {
199 BasicSupport support = BasicSupport.getSupport(txt.toURI()
200 .toURL());
201 origStory = support.process(null);
202 } catch (Exception e) {
203 basename = null;
204 }
205 }
206
207 return origStory;
208
209 }
08fe2e33 210}