Fix tests by removing uneeded error traces
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Cbz.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
08fe2e33 3import java.io.IOException;
08fe2e33
NR
4import java.net.URL;
5import java.util.ArrayList;
d3c84ac3 6import java.util.Collections;
2a25f781 7import java.util.HashMap;
d3c84ac3 8import java.util.List;
2a25f781 9import java.util.Map;
08fe2e33
NR
10import java.util.zip.ZipEntry;
11import java.util.zip.ZipInputStream;
12
13import be.nikiroo.fanfix.Instance;
14import be.nikiroo.fanfix.data.Chapter;
15import be.nikiroo.fanfix.data.Paragraph;
16import be.nikiroo.fanfix.data.Story;
16a81ef7 17import be.nikiroo.utils.Image;
3b2b638f 18import be.nikiroo.utils.Progress;
08fe2e33
NR
19
20/**
21 * Support class for CBZ files (works better with CBZ created with this program,
22 * as they have some metadata available).
23 *
24 * @author niki
25 */
26class Cbz extends Epub {
27 @Override
28 protected boolean supports(URL url) {
29 return url.toString().toLowerCase().endsWith(".cbz");
30 }
31
32 @Override
33 public String getSourceName() {
34 return "cbz";
35 }
36
37 @Override
38 protected String getDataPrefix() {
39 return "";
40 }
41
42 @Override
43 protected boolean requireInfo() {
44 return false;
45 }
08fe2e33
NR
46
47 @Override
e4fa48a0
NR
48 protected boolean isImagesDocumentByDefault() {
49 return true;
08fe2e33
NR
50 }
51
68686a37 52 @Override
e4fa48a0
NR
53 protected boolean getCover() {
54 return false;
68686a37
NR
55 }
56
08fe2e33 57 @Override
92fb0719
NR
58 public Story process(URL url, Progress pg) throws IOException {
59 if (pg == null) {
60 pg = new Progress();
61 } else {
62 pg.setMinMax(0, 100);
63 }
64
ed08c171
NR
65 Progress pgMeta = new Progress();
66 pg.addProgress(pgMeta, 10);
67 Story story = processMeta(url, false, true, pgMeta);
2a25f781 68 pgMeta.done(); // 10%
ed08c171 69
08fe2e33
NR
70 story.setChapters(new ArrayList<Chapter>());
71 Chapter chap = new Chapter(1, null);
72 story.getChapters().add(chap);
73
74 ZipInputStream zipIn = new ZipInputStream(getInput());
75
16a81ef7 76 Map<String, Image> images = new HashMap<String, Image>();
08fe2e33
NR
77 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
78 .getNextEntry()) {
79 if (!entry.isDirectory()
80 && entry.getName().startsWith(getDataPrefix())) {
81 String entryLName = entry.getName().toLowerCase();
82 boolean imageEntry = false;
83 for (String ext : getImageExt(false)) {
84 if (entryLName.endsWith(ext)) {
85 imageEntry = true;
86 }
87 }
88
89 if (imageEntry) {
d3c84ac3 90 String uuid = meta.getUuid() + "_" + entry.getName();
08fe2e33 91 try {
16a81ef7 92 images.put(uuid, new Image(zipIn));
08fe2e33 93 } catch (Exception e) {
62c63b07 94 Instance.getTraceHandler().error(e);
08fe2e33 95 }
2a25f781
NR
96
97 if (pg.getProgress() < 85) {
98 pg.add(1);
99 }
08fe2e33
NR
100 }
101 }
102 }
103
2a25f781 104 pg.setProgress(85);
92fb0719 105
a9eb3f46 106 // ZIP order is not correct for us
2a25f781
NR
107 List<String> imagesList = new ArrayList<String>(images.keySet());
108 Collections.sort(imagesList);
109
92fb0719 110 pg.setProgress(90);
d3c84ac3 111
2a25f781 112 for (String uuid : imagesList) {
d3c84ac3 113 try {
2a25f781 114 chap.getParagraphs().add(new Paragraph(images.get(uuid)));
d3c84ac3 115 } catch (Exception e) {
62c63b07 116 Instance.getTraceHandler().error(e);
d3c84ac3
NR
117 }
118 }
119
a9eb3f46 120 if (meta.getCover() == null && !images.isEmpty()) {
2a25f781 121 meta.setCover(images.get(imagesList.get(0)));
925298fd 122 meta.setFakeCover(true);
a9eb3f46
NR
123 }
124
92fb0719 125 pg.setProgress(100);
08fe2e33
NR
126 return story;
127 }
128}