Merge commit '2953f98805434534094068a52bf983b71282cf5c' into wip
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Cbz.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
2aac79c7 3import java.io.File;
08fe2e33 4import java.io.IOException;
08fe2e33
NR
5import java.net.URL;
6import java.util.ArrayList;
d3c84ac3 7import java.util.Collections;
2a25f781 8import java.util.HashMap;
d3c84ac3 9import java.util.List;
2a25f781 10import java.util.Map;
08fe2e33
NR
11import java.util.zip.ZipEntry;
12import java.util.zip.ZipInputStream;
13
14import be.nikiroo.fanfix.Instance;
15import be.nikiroo.fanfix.data.Chapter;
7445f856 16import be.nikiroo.fanfix.data.MetaData;
08fe2e33
NR
17import be.nikiroo.fanfix.data.Paragraph;
18import be.nikiroo.fanfix.data.Story;
2aac79c7 19import be.nikiroo.utils.IOUtils;
16a81ef7 20import be.nikiroo.utils.Image;
3b2b638f 21import be.nikiroo.utils.Progress;
08fe2e33
NR
22
23/**
24 * Support class for CBZ files (works better with CBZ created with this program,
25 * as they have some metadata available).
26 *
27 * @author niki
28 */
29class Cbz extends Epub {
30 @Override
31 protected boolean supports(URL url) {
32 return url.toString().toLowerCase().endsWith(".cbz");
33 }
34
35 @Override
36 public String getSourceName() {
37 return "cbz";
38 }
39
40 @Override
41 protected String getDataPrefix() {
42 return "";
43 }
44
45 @Override
46 protected boolean requireInfo() {
47 return false;
48 }
08fe2e33
NR
49
50 @Override
e4fa48a0
NR
51 protected boolean isImagesDocumentByDefault() {
52 return true;
08fe2e33
NR
53 }
54
68686a37 55 @Override
e4fa48a0
NR
56 protected boolean getCover() {
57 return false;
68686a37
NR
58 }
59
08fe2e33 60 @Override
9005532f 61 public Story doProcess(Progress pg) throws IOException {
92fb0719
NR
62 if (pg == null) {
63 pg = new Progress();
64 } else {
65 pg.setMinMax(0, 100);
66 }
67
ed08c171
NR
68 Progress pgMeta = new Progress();
69 pg.addProgress(pgMeta, 10);
7445f856
NR
70 Story story = processMeta(true, pgMeta);
71 MetaData meta = story.getMeta();
72
2a25f781 73 pgMeta.done(); // 10%
ed08c171 74
2aac79c7
NR
75 File tmpDir = Instance.getTempFiles().createTempDir("info-text");
76 String basename = null;
08fe2e33 77
16a81ef7 78 Map<String, Image> images = new HashMap<String, Image>();
2aac79c7
NR
79 try {
80 ZipInputStream zipIn = new ZipInputStream(getInput());
81 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
82 .getNextEntry()) {
83 if (!entry.isDirectory()
84 && entry.getName().startsWith(getDataPrefix())) {
85 String entryLName = entry.getName().toLowerCase();
86 boolean imageEntry = false;
7445f856 87 for (String ext : BasicSupportImages.getImageExt(false)) {
2aac79c7
NR
88 if (entryLName.endsWith(ext)) {
89 imageEntry = true;
90 }
08fe2e33 91 }
08fe2e33 92
2aac79c7
NR
93 if (imageEntry) {
94 String uuid = meta.getUuid() + "_" + entry.getName();
95 try {
96 images.put(uuid, new Image(zipIn));
97 } catch (Exception e) {
98 Instance.getTraceHandler().error(e);
99 }
100
101 if (pg.getProgress() < 85) {
102 pg.add(1);
103 }
104 } else if (entryLName.endsWith(".info")) {
105 basename = entryLName.substring(0, entryLName.length()
106 - ".info".length());
107 IOUtils.write(zipIn, new File(tmpDir, entryLName));
108 } else if (entryLName.endsWith(".txt")) {
109 IOUtils.write(zipIn, new File(tmpDir, entryLName));
08fe2e33 110 }
2aac79c7
NR
111 }
112 }
2a25f781 113
2aac79c7
NR
114 pg.setProgress(85);
115
116 // ZIP order is not correct for us
117 List<String> imagesList = new ArrayList<String>(images.keySet());
118 Collections.sort(imagesList);
119
120 pg.setProgress(90);
121
122 File txt = new File(tmpDir, basename + ".txt");
123 if (!txt.exists()) {
124 basename = null;
125 }
126 if (basename != null) {
127 try {
128 BasicSupport support = BasicSupport.getSupport(txt.toURI()
129 .toURL());
130 Story origStory = support.process(null);
131 story.setChapters(origStory.getChapters());
132 story.setMeta(origStory.getMeta());
133 } catch (Exception e) {
134 basename = null;
08fe2e33
NR
135 }
136 }
08fe2e33 137
2aac79c7
NR
138 if (basename == null) {
139 story.setChapters(new ArrayList<Chapter>());
140 }
92fb0719 141
2aac79c7
NR
142 if (!imagesList.isEmpty()) {
143 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
144 story.getChapters().add(chap);
2a25f781 145
2aac79c7
NR
146 for (String uuid : imagesList) {
147 try {
148 chap.getParagraphs().add(
149 new Paragraph(images.get(uuid)));
150 } catch (Exception e) {
151 Instance.getTraceHandler().error(e);
152 }
153 }
154 }
d3c84ac3 155
2aac79c7
NR
156 if (meta.getCover() == null && !images.isEmpty()) {
157 meta.setCover(images.get(imagesList.get(0)));
158 meta.setFakeCover(true);
d3c84ac3 159 }
d3c84ac3 160
2aac79c7
NR
161 } finally {
162 IOUtils.deltree(tmpDir);
a9eb3f46
NR
163 }
164
92fb0719 165 pg.setProgress(100);
08fe2e33
NR
166 return story;
167 }
168}