76b66aba7c75fa24317eb4daca6ff75854658326
[nikiroo-utils.git] / supported / Cbz.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipInputStream;
14
15 import be.nikiroo.fanfix.Instance;
16 import be.nikiroo.fanfix.bundles.Config;
17 import be.nikiroo.fanfix.data.Chapter;
18 import be.nikiroo.fanfix.data.MetaData;
19 import be.nikiroo.fanfix.data.Paragraph;
20 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
21 import be.nikiroo.fanfix.data.Story;
22 import be.nikiroo.utils.IOUtils;
23 import be.nikiroo.utils.Image;
24 import be.nikiroo.utils.Progress;
25 import be.nikiroo.utils.streams.MarkableFileInputStream;
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 */
33 class Cbz extends Epub {
34 @Override
35 protected boolean supports(URL url) {
36 return url.toString().toLowerCase().endsWith(".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 isImagesDocumentByDefault() {
51 return true;
52 }
53
54 @Override
55 protected boolean getCover() {
56 return false;
57 }
58
59 @Override
60 public Story doProcess(Progress pg) throws IOException {
61 if (pg == null) {
62 pg = new Progress();
63 } else {
64 pg.setMinMax(0, 100);
65 }
66
67 Progress pgMeta = new Progress();
68 pg.addProgress(pgMeta, 10);
69 Story story = processMeta(true, pgMeta);
70 MetaData meta = story.getMeta();
71
72 pgMeta.done(); // 10%
73
74 File tmpDir = Instance.getInstance().getTempFiles().createTempDir("info-text");
75 String basename = null;
76
77 Map<String, Image> images = new HashMap<String, Image>();
78 InputStream cbzIn = null;
79 ZipInputStream zipIn = null;
80 try {
81 cbzIn = new MarkableFileInputStream(getSourceFileOriginal());
82 zipIn = new ZipInputStream(cbzIn);
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;
89 for (String ext : bsImages.getImageExt(false)) {
90 if (entryLName.endsWith(ext)) {
91 imageEntry = true;
92 }
93 }
94
95 if (imageEntry) {
96 String uuid = meta.getUuid() + "_" + entry.getName();
97 try {
98 images.put(uuid, new Image(zipIn));
99 } catch (Exception e) {
100 Instance.getInstance().getTraceHandler().error(e);
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));
112 }
113 }
114 }
115
116 String ext = "."
117 + Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
118 String coverName = meta.getUuid() + "_" + basename + ext;
119 Image cover = images.get(coverName);
120 images.remove(coverName);
121
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
130 // only the description/cover is kept
131 Story origStory = getStoryFromTxt(tmpDir, basename);
132 if (origStory != null) {
133 if (origStory.getMeta().getCover() == null) {
134 origStory.getMeta().setCover(story.getMeta().getCover());
135 }
136 story.setMeta(origStory.getMeta());
137 }
138 if (story.getMeta().getCover() == null) {
139 story.getMeta().setCover(cover);
140 }
141 story.setChapters(new ArrayList<Chapter>());
142
143 // Check if we can find non-images chapters, for hybrid-cbz support
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 }
158 }
159 }
160
161 if (isImages != null && !isImages) {
162 story.getChapters().add(chap);
163 chap.setNumber(story.getChapters().size());
164 }
165 }
166 }
167
168 if (!imagesList.isEmpty()) {
169 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
170 story.getChapters().add(chap);
171
172 for (String uuid : imagesList) {
173 try {
174 chap.getParagraphs().add(
175 new Paragraph(images.get(uuid)));
176 } catch (Exception e) {
177 Instance.getInstance().getTraceHandler().error(e);
178 }
179 }
180 }
181
182 if (meta.getCover() == null && !images.isEmpty()) {
183 meta.setCover(images.get(imagesList.get(0)));
184 meta.setFakeCover(true);
185 }
186 } finally {
187 IOUtils.deltree(tmpDir);
188 if (zipIn != null) {
189 zipIn.close();
190 }
191 if (cbzIn != null) {
192 cbzIn.close();
193 }
194 }
195
196 pg.setProgress(100);
197 return story;
198 }
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 }
220 }