Improve progress names
[nikiroo-utils.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 }
3b039231
NR
66
67 pg.setName("Initialising");
92fb0719 68
ed08c171
NR
69 Progress pgMeta = new Progress();
70 pg.addProgress(pgMeta, 10);
7445f856
NR
71 Story story = processMeta(true, pgMeta);
72 MetaData meta = story.getMeta();
091397cb 73
2a25f781 74 pgMeta.done(); // 10%
3b039231
NR
75
76 pg.setName(meta.getTitle());
ed08c171 77
d66deb8d 78 File tmpDir = Instance.getInstance().getTempFiles().createTempDir("info-text");
2aac79c7 79 String basename = null;
08fe2e33 80
16a81ef7 81 Map<String, Image> images = new HashMap<String, Image>();
298d405a
NR
82 InputStream cbzIn = null;
83 ZipInputStream zipIn = null;
2aac79c7 84 try {
67837328 85 cbzIn = new MarkableFileInputStream(getSourceFileOriginal());
298d405a 86 zipIn = new ZipInputStream(cbzIn);
2aac79c7
NR
87 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
88 .getNextEntry()) {
89 if (!entry.isDirectory()
90 && entry.getName().startsWith(getDataPrefix())) {
91 String entryLName = entry.getName().toLowerCase();
92 boolean imageEntry = false;
8d59ce07 93 for (String ext : bsImages.getImageExt(false)) {
2aac79c7
NR
94 if (entryLName.endsWith(ext)) {
95 imageEntry = true;
96 }
08fe2e33 97 }
45994ba5 98
2aac79c7
NR
99 if (imageEntry) {
100 String uuid = meta.getUuid() + "_" + entry.getName();
101 try {
102 images.put(uuid, new Image(zipIn));
103 } catch (Exception e) {
d66deb8d 104 Instance.getInstance().getTraceHandler().error(e);
2aac79c7
NR
105 }
106
107 if (pg.getProgress() < 85) {
108 pg.add(1);
109 }
110 } else if (entryLName.endsWith(".info")) {
111 basename = entryLName.substring(0, entryLName.length()
112 - ".info".length());
113 IOUtils.write(zipIn, new File(tmpDir, entryLName));
114 } else if (entryLName.endsWith(".txt")) {
115 IOUtils.write(zipIn, new File(tmpDir, entryLName));
08fe2e33 116 }
2aac79c7
NR
117 }
118 }
45994ba5
NR
119
120 String ext = "."
d66deb8d 121 + Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
45994ba5
NR
122 String coverName = meta.getUuid() + "_" + basename + ext;
123 Image cover = images.get(coverName);
124 images.remove(coverName);
2a25f781 125
2aac79c7
NR
126 pg.setProgress(85);
127
128 // ZIP order is not correct for us
129 List<String> imagesList = new ArrayList<String>(images.keySet());
130 Collections.sort(imagesList);
131
132 pg.setProgress(90);
133
45994ba5 134 // only the description/cover is kept
298d405a
NR
135 Story origStory = getStoryFromTxt(tmpDir, basename);
136 if (origStory != null) {
27694a13
NR
137 if (origStory.getMeta().getCover() == null) {
138 origStory.getMeta().setCover(story.getMeta().getCover());
139 }
298d405a 140 story.setMeta(origStory.getMeta());
2aac79c7 141 }
45994ba5
NR
142 if (story.getMeta().getCover() == null) {
143 story.getMeta().setCover(cover);
144 }
091397cb 145 story.setChapters(new ArrayList<Chapter>());
92fb0719 146
e5a2f1b3 147 // Check if we can find non-images chapters, for hybrid-cbz support
654e27bb
NR
148 if (origStory != null) {
149 for (Chapter chap : origStory) {
150 Boolean isImages = null;
151 for (Paragraph para : chap) {
152 ParagraphType t = para.getType();
153 if (isImages == null && !t.isText(true)) {
154 isImages = true;
155 }
156 if (t.isText(false)) {
157 String line = para.getContent();
158 // Images are saved in text mode as "[image-link]"
159 if (!(line.startsWith("[") && line.endsWith("]"))) {
160 isImages = false;
161 }
e5a2f1b3
NR
162 }
163 }
e5a2f1b3 164
654e27bb
NR
165 if (isImages != null && !isImages) {
166 story.getChapters().add(chap);
167 chap.setNumber(story.getChapters().size());
168 }
e5a2f1b3
NR
169 }
170 }
171
2aac79c7
NR
172 if (!imagesList.isEmpty()) {
173 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
174 story.getChapters().add(chap);
2a25f781 175
2aac79c7
NR
176 for (String uuid : imagesList) {
177 try {
178 chap.getParagraphs().add(
179 new Paragraph(images.get(uuid)));
180 } catch (Exception e) {
d66deb8d 181 Instance.getInstance().getTraceHandler().error(e);
2aac79c7
NR
182 }
183 }
184 }
d3c84ac3 185
2aac79c7
NR
186 if (meta.getCover() == null && !images.isEmpty()) {
187 meta.setCover(images.get(imagesList.get(0)));
188 meta.setFakeCover(true);
d3c84ac3 189 }
2aac79c7
NR
190 } finally {
191 IOUtils.deltree(tmpDir);
298d405a
NR
192 if (zipIn != null) {
193 zipIn.close();
194 }
195 if (cbzIn != null) {
196 cbzIn.close();
197 }
a9eb3f46
NR
198 }
199
3b039231
NR
200 pg.setName(meta.getTitle());
201 pg.done();
202
08fe2e33
NR
203 return story;
204 }
298d405a
NR
205
206 private Story getStoryFromTxt(File tmpDir, String basename) {
207 Story origStory = null;
208
209 File txt = new File(tmpDir, basename + ".txt");
210 if (!txt.exists()) {
211 basename = null;
212 }
213 if (basename != null) {
214 try {
215 BasicSupport support = BasicSupport.getSupport(txt.toURI()
216 .toURL());
217 origStory = support.process(null);
218 } catch (Exception e) {
219 basename = null;
220 }
221 }
222
223 return origStory;
224
225 }
08fe2e33 226}