fix typo in Enum i18n + fix Import
[fanfix.git] / src / be / nikiroo / fanfix / supported / Cbz.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.FileInputStream;
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.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.zip.ZipEntry;
14 import java.util.zip.ZipInputStream;
15
16 import be.nikiroo.fanfix.Instance;
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.streams.MarkableFileInputStream;
25 import be.nikiroo.utils.Progress;
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.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 : BasicSupportImages.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.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 pg.setProgress(85);
117
118 // ZIP order is not correct for us
119 List<String> imagesList = new ArrayList<String>(images.keySet());
120 Collections.sort(imagesList);
121
122 pg.setProgress(90);
123
124 // only the description is kept
125 Story origStory = getStoryFromTxt(tmpDir, basename);
126 if (origStory != null) {
127 if (origStory.getMeta().getCover() == null) {
128 origStory.getMeta().setCover(story.getMeta().getCover());
129 }
130 story.setMeta(origStory.getMeta());
131 }
132 story.setChapters(new ArrayList<Chapter>());
133
134 // Check if we can find non-images chapters, for hybrid-cbz support
135 for (Chapter chap : origStory) {
136 Boolean isImages = null;
137 for (Paragraph para : chap) {
138 ParagraphType t = para.getType();
139 if (isImages == null && !t.isText(true)) {
140 isImages = true;
141 }
142 if (t.isText(false)) {
143 String line = para.getContent();
144 // Images are saved in text mode as "[image-link]"
145 if (!(line.startsWith("[") && line.endsWith("]"))) {
146 isImages = false;
147 }
148 }
149 }
150
151 if (isImages != null && !isImages) {
152 story.getChapters().add(chap);
153 chap.setNumber(story.getChapters().size());
154 }
155 }
156
157 if (!imagesList.isEmpty()) {
158 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
159 story.getChapters().add(chap);
160
161 for (String uuid : imagesList) {
162 try {
163 chap.getParagraphs().add(
164 new Paragraph(images.get(uuid)));
165 } catch (Exception e) {
166 Instance.getTraceHandler().error(e);
167 }
168 }
169 }
170
171 if (meta.getCover() == null && !images.isEmpty()) {
172 meta.setCover(images.get(imagesList.get(0)));
173 meta.setFakeCover(true);
174 }
175 } finally {
176 IOUtils.deltree(tmpDir);
177 if (zipIn != null) {
178 zipIn.close();
179 }
180 if (cbzIn != null) {
181 cbzIn.close();
182 }
183 }
184
185 pg.setProgress(100);
186 return story;
187 }
188
189 private Story getStoryFromTxt(File tmpDir, String basename) {
190 Story origStory = null;
191
192 File txt = new File(tmpDir, basename + ".txt");
193 if (!txt.exists()) {
194 basename = null;
195 }
196 if (basename != null) {
197 try {
198 BasicSupport support = BasicSupport.getSupport(txt.toURI()
199 .toURL());
200 origStory = support.process(null);
201 } catch (Exception e) {
202 basename = null;
203 }
204 }
205
206 return origStory;
207
208 }
209 }