Fix most irregularities found by conversion tests
[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.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.MetaData;
17 import be.nikiroo.fanfix.data.Paragraph;
18 import be.nikiroo.fanfix.data.Story;
19 import be.nikiroo.utils.IOUtils;
20 import be.nikiroo.utils.Image;
21 import be.nikiroo.utils.Progress;
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 */
29 class 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 }
49
50 @Override
51 protected boolean isImagesDocumentByDefault() {
52 return true;
53 }
54
55 @Override
56 protected boolean getCover() {
57 return false;
58 }
59
60 @Override
61 public Story doProcess(Progress pg) throws IOException {
62 if (pg == null) {
63 pg = new Progress();
64 } else {
65 pg.setMinMax(0, 100);
66 }
67
68 Progress pgMeta = new Progress();
69 pg.addProgress(pgMeta, 10);
70 Story story = processMeta(true, pgMeta);
71 MetaData meta = story.getMeta();
72
73 pgMeta.done(); // 10%
74
75 File tmpDir = Instance.getTempFiles().createTempDir("info-text");
76 String basename = null;
77
78 Map<String, Image> images = new HashMap<String, Image>();
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;
87 for (String ext : BasicSupportImages.getImageExt(false)) {
88 if (entryLName.endsWith(ext)) {
89 imageEntry = true;
90 }
91 }
92
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));
110 }
111 }
112 }
113
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;
135 }
136 }
137
138 if (basename == null) {
139 story.setChapters(new ArrayList<Chapter>());
140 }
141
142 if (!imagesList.isEmpty()) {
143 Chapter chap = new Chapter(story.getChapters().size() + 1, null);
144 story.getChapters().add(chap);
145
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 }
155
156 if (meta.getCover() == null && !images.isEmpty()) {
157 meta.setCover(images.get(imagesList.get(0)));
158 meta.setFakeCover(true);
159 }
160
161 } finally {
162 IOUtils.deltree(tmpDir);
163 }
164
165 pg.setProgress(100);
166 return story;
167 }
168 }