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