Small fixes:
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Cbz.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipInputStream;
13
14 import be.nikiroo.fanfix.Instance;
15 import be.nikiroo.fanfix.data.Chapter;
16 import be.nikiroo.fanfix.data.Paragraph;
17 import be.nikiroo.fanfix.data.Story;
18 import be.nikiroo.utils.IOUtils;
19 import be.nikiroo.utils.Image;
20 import be.nikiroo.utils.Progress;
21
22 /**
23 * Support class for CBZ files (works better with CBZ created with this program,
24 * as they have some metadata available).
25 *
26 * @author niki
27 */
28 class Cbz extends Epub {
29 @Override
30 protected boolean supports(URL url) {
31 return url.toString().toLowerCase().endsWith(".cbz");
32 }
33
34 @Override
35 public String getSourceName() {
36 return "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 process(URL url, 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(url, false, true, pgMeta);
70 pgMeta.done(); // 10%
71
72 File tmpDir = Instance.getTempFiles().createTempDir("info-text");
73 String basename = null;
74
75 Map<String, Image> images = new HashMap<String, Image>();
76 try {
77 ZipInputStream zipIn = new ZipInputStream(getInput());
78 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
79 .getNextEntry()) {
80 if (!entry.isDirectory()
81 && entry.getName().startsWith(getDataPrefix())) {
82 String entryLName = entry.getName().toLowerCase();
83 boolean imageEntry = false;
84 for (String ext : getImageExt(false)) {
85 if (entryLName.endsWith(ext)) {
86 imageEntry = true;
87 }
88 }
89
90 if (imageEntry) {
91 String uuid = meta.getUuid() + "_" + entry.getName();
92 try {
93 images.put(uuid, new Image(zipIn));
94 } catch (Exception e) {
95 Instance.getTraceHandler().error(e);
96 }
97
98 if (pg.getProgress() < 85) {
99 pg.add(1);
100 }
101 } else if (entryLName.endsWith(".info")) {
102 basename = entryLName.substring(0, entryLName.length()
103 - ".info".length());
104 IOUtils.write(zipIn, new File(tmpDir, entryLName));
105 } else if (entryLName.endsWith(".txt")) {
106 IOUtils.write(zipIn, new File(tmpDir, entryLName));
107 }
108 }
109 }
110
111 pg.setProgress(85);
112
113 // ZIP order is not correct for us
114 List<String> imagesList = new ArrayList<String>(images.keySet());
115 Collections.sort(imagesList);
116
117 pg.setProgress(90);
118
119 File txt = new File(tmpDir, basename + ".txt");
120 if (!txt.exists()) {
121 basename = null;
122 }
123 if (basename != null) {
124 try {
125 BasicSupport support = BasicSupport.getSupport(txt.toURI()
126 .toURL());
127 Story origStory = support.process(null);
128 story.setChapters(origStory.getChapters());
129 story.setMeta(origStory.getMeta());
130 } catch (Exception e) {
131 basename = null;
132 }
133 }
134
135 if (basename == null) {
136 story.setChapters(new ArrayList<Chapter>());
137 }
138
139 if (!imagesList.isEmpty()) {
140 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
141 story.getChapters().add(chap);
142
143 for (String uuid : imagesList) {
144 try {
145 chap.getParagraphs().add(
146 new Paragraph(images.get(uuid)));
147 } catch (Exception e) {
148 Instance.getTraceHandler().error(e);
149 }
150 }
151 }
152
153 if (meta.getCover() == null && !images.isEmpty()) {
154 meta.setCover(images.get(imagesList.get(0)));
155 meta.setFakeCover(true);
156 }
157
158 } finally {
159 IOUtils.deltree(tmpDir);
160 }
161
162 pg.setProgress(100);
163 return story;
164 }
165 }