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