try+1
[fanfix.git] / src / be / nikiroo / fanfix / supported / Cbz.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
2aac79c7 3import java.io.File;
298d405a 4import java.io.FileInputStream;
08fe2e33 5import java.io.IOException;
298d405a 6import java.io.InputStream;
08fe2e33
NR
7import java.net.URL;
8import java.util.ArrayList;
d3c84ac3 9import java.util.Collections;
2a25f781 10import java.util.HashMap;
d3c84ac3 11import java.util.List;
2a25f781 12import java.util.Map;
08fe2e33
NR
13import java.util.zip.ZipEntry;
14import java.util.zip.ZipInputStream;
15
16import be.nikiroo.fanfix.Instance;
17import be.nikiroo.fanfix.data.Chapter;
7445f856 18import be.nikiroo.fanfix.data.MetaData;
08fe2e33
NR
19import be.nikiroo.fanfix.data.Paragraph;
20import be.nikiroo.fanfix.data.Story;
2aac79c7 21import be.nikiroo.utils.IOUtils;
16a81ef7 22import be.nikiroo.utils.Image;
298d405a 23import be.nikiroo.utils.MarkableFileInputStream;
3b2b638f 24import be.nikiroo.utils.Progress;
08fe2e33
NR
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 */
32class 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 }
08fe2e33
NR
52
53 @Override
e4fa48a0
NR
54 protected boolean isImagesDocumentByDefault() {
55 return true;
08fe2e33
NR
56 }
57
68686a37 58 @Override
e4fa48a0
NR
59 protected boolean getCover() {
60 return false;
68686a37
NR
61 }
62
08fe2e33 63 @Override
9005532f 64 public Story doProcess(Progress pg) throws IOException {
92fb0719
NR
65 if (pg == null) {
66 pg = new Progress();
67 } else {
68 pg.setMinMax(0, 100);
69 }
70
ed08c171
NR
71 Progress pgMeta = new Progress();
72 pg.addProgress(pgMeta, 10);
7445f856
NR
73 Story story = processMeta(true, pgMeta);
74 MetaData meta = story.getMeta();
27694a13
NR
75
76System.out.println("Meta from Cbz support: "+meta);
7445f856 77
2a25f781 78 pgMeta.done(); // 10%
ed08c171 79
2aac79c7
NR
80 File tmpDir = Instance.getTempFiles().createTempDir("info-text");
81 String basename = null;
08fe2e33 82
16a81ef7 83 Map<String, Image> images = new HashMap<String, Image>();
298d405a
NR
84 InputStream cbzIn = null;
85 ZipInputStream zipIn = null;
2aac79c7 86 try {
298d405a
NR
87 cbzIn = new MarkableFileInputStream(new FileInputStream(
88 getSourceFileOriginal()));
89 zipIn = new ZipInputStream(cbzIn);
2aac79c7
NR
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;
7445f856 96 for (String ext : BasicSupportImages.getImageExt(false)) {
2aac79c7
NR
97 if (entryLName.endsWith(ext)) {
98 imageEntry = true;
99 }
08fe2e33 100 }
08fe2e33 101
2aac79c7
NR
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));
08fe2e33 119 }
2aac79c7
NR
120 }
121 }
2a25f781 122
2aac79c7
NR
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
298d405a
NR
131 // include original story
132 Story origStory = getStoryFromTxt(tmpDir, basename);
133 if (origStory != null) {
134 story.setChapters(origStory.getChapters());
27694a13
NR
135 if (origStory.getMeta().getCover() == null) {
136 origStory.getMeta().setCover(story.getMeta().getCover());
137 }
298d405a
NR
138 story.setMeta(origStory.getMeta());
139 } else {
2aac79c7
NR
140 story.setChapters(new ArrayList<Chapter>());
141 }
92fb0719 142
2aac79c7
NR
143 if (!imagesList.isEmpty()) {
144 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
145 story.getChapters().add(chap);
2a25f781 146
2aac79c7
NR
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 }
d3c84ac3 156
2aac79c7
NR
157 if (meta.getCover() == null && !images.isEmpty()) {
158 meta.setCover(images.get(imagesList.get(0)));
159 meta.setFakeCover(true);
d3c84ac3 160 }
27694a13
NR
161
162System.out.println("Meta from Cbz support at end: "+meta);
2aac79c7
NR
163 } finally {
164 IOUtils.deltree(tmpDir);
298d405a
NR
165 if (zipIn != null) {
166 zipIn.close();
167 }
168 if (cbzIn != null) {
169 cbzIn.close();
170 }
a9eb3f46
NR
171 }
172
92fb0719 173 pg.setProgress(100);
08fe2e33
NR
174 return story;
175 }
298d405a
NR
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 }
08fe2e33 197}