try+1
[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.Story;
21 import be.nikiroo.utils.IOUtils;
22 import be.nikiroo.utils.Image;
23 import be.nikiroo.utils.MarkableFileInputStream;
24 import be.nikiroo.utils.Progress;
25
26 /**
27 * Support class for CBZ files (works better with CBZ created with this program,
28 * as they have some metadata available).
29 *
30 * @author niki
31 */
32 class Cbz extends Epub {
33 @Override
34 protected boolean supports(URL url) {
35 return url.toString().toLowerCase().endsWith(".cbz");
36 }
37
38 @Override
39 public String getSourceName() {
40 return "cbz";
41 }
42
43 @Override
44 protected String getDataPrefix() {
45 return "";
46 }
47
48 @Override
49 protected boolean requireInfo() {
50 return false;
51 }
52
53 @Override
54 protected boolean isImagesDocumentByDefault() {
55 return true;
56 }
57
58 @Override
59 protected boolean getCover() {
60 return false;
61 }
62
63 @Override
64 public Story doProcess(Progress pg) throws IOException {
65 if (pg == null) {
66 pg = new Progress();
67 } else {
68 pg.setMinMax(0, 100);
69 }
70
71 Progress pgMeta = new Progress();
72 pg.addProgress(pgMeta, 10);
73 Story story = processMeta(true, pgMeta);
74 MetaData meta = story.getMeta();
75
76 System.out.println("Meta from Cbz support: "+meta);
77
78 pgMeta.done(); // 10%
79
80 File tmpDir = Instance.getTempFiles().createTempDir("info-text");
81 String basename = null;
82
83 Map<String, Image> images = new HashMap<String, Image>();
84 InputStream cbzIn = null;
85 ZipInputStream zipIn = null;
86 try {
87 cbzIn = new MarkableFileInputStream(new FileInputStream(
88 getSourceFileOriginal()));
89 zipIn = new ZipInputStream(cbzIn);
90 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
91 .getNextEntry()) {
92 if (!entry.isDirectory()
93 && entry.getName().startsWith(getDataPrefix())) {
94 String entryLName = entry.getName().toLowerCase();
95 boolean imageEntry = false;
96 for (String ext : BasicSupportImages.getImageExt(false)) {
97 if (entryLName.endsWith(ext)) {
98 imageEntry = true;
99 }
100 }
101
102 if (imageEntry) {
103 String uuid = meta.getUuid() + "_" + entry.getName();
104 try {
105 images.put(uuid, new Image(zipIn));
106 } catch (Exception e) {
107 Instance.getTraceHandler().error(e);
108 }
109
110 if (pg.getProgress() < 85) {
111 pg.add(1);
112 }
113 } else if (entryLName.endsWith(".info")) {
114 basename = entryLName.substring(0, entryLName.length()
115 - ".info".length());
116 IOUtils.write(zipIn, new File(tmpDir, entryLName));
117 } else if (entryLName.endsWith(".txt")) {
118 IOUtils.write(zipIn, new File(tmpDir, entryLName));
119 }
120 }
121 }
122
123 pg.setProgress(85);
124
125 // ZIP order is not correct for us
126 List<String> imagesList = new ArrayList<String>(images.keySet());
127 Collections.sort(imagesList);
128
129 pg.setProgress(90);
130
131 // include original story
132 Story origStory = getStoryFromTxt(tmpDir, basename);
133 if (origStory != null) {
134 story.setChapters(origStory.getChapters());
135 if (origStory.getMeta().getCover() == null) {
136 origStory.getMeta().setCover(story.getMeta().getCover());
137 }
138 story.setMeta(origStory.getMeta());
139 } else {
140 story.setChapters(new ArrayList<Chapter>());
141 }
142
143 if (!imagesList.isEmpty()) {
144 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
145 story.getChapters().add(chap);
146
147 for (String uuid : imagesList) {
148 try {
149 chap.getParagraphs().add(
150 new Paragraph(images.get(uuid)));
151 } catch (Exception e) {
152 Instance.getTraceHandler().error(e);
153 }
154 }
155 }
156
157 if (meta.getCover() == null && !images.isEmpty()) {
158 meta.setCover(images.get(imagesList.get(0)));
159 meta.setFakeCover(true);
160 }
161
162 System.out.println("Meta from Cbz support at end: "+meta);
163 } finally {
164 IOUtils.deltree(tmpDir);
165 if (zipIn != null) {
166 zipIn.close();
167 }
168 if (cbzIn != null) {
169 cbzIn.close();
170 }
171 }
172
173 pg.setProgress(100);
174 return story;
175 }
176
177 private Story getStoryFromTxt(File tmpDir, String basename) {
178 Story origStory = null;
179
180 File txt = new File(tmpDir, basename + ".txt");
181 if (!txt.exists()) {
182 basename = null;
183 }
184 if (basename != null) {
185 try {
186 BasicSupport support = BasicSupport.getSupport(txt.toURI()
187 .toURL());
188 origStory = support.process(null);
189 } catch (Exception e) {
190 basename = null;
191 }
192 }
193
194 return origStory;
195
196 }
197 }