fix changelog headers
[fanfix.git] / Cbz.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.supported;
2
3import java.awt.image.BufferedImage;
4import java.io.IOException;
5import java.io.InputStream;
6import java.net.URL;
7import java.util.ArrayList;
8import java.util.Collections;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12import java.util.zip.ZipEntry;
13import java.util.zip.ZipInputStream;
14
15import be.nikiroo.fanfix.Instance;
16import be.nikiroo.fanfix.data.Chapter;
17import be.nikiroo.fanfix.data.Paragraph;
18import be.nikiroo.fanfix.data.Story;
19import be.nikiroo.utils.ImageUtils;
20import be.nikiroo.utils.Progress;
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 */
28class 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 }
48
49 @Override
50 protected boolean getCover() {
51 return false;
52 }
53
54 @Override
55 protected void preprocess(URL source, InputStream in) throws IOException {
56 super.preprocess(source, in);
57 meta.setImageDocument(true);
58 }
59
60 @Override
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
68 Progress pgMeta = new Progress();
69 pg.addProgress(pgMeta, 10);
70 Story story = processMeta(url, false, true, pgMeta);
71 pgMeta.done(); // 10%
72
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
79 Map<String, BufferedImage> images = new HashMap<String, BufferedImage>();
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) {
93 String uuid = meta.getUuid() + "_" + entry.getName();
94 try {
95 images.put(uuid, ImageUtils.fromStream(zipIn));
96 } catch (Exception e) {
97 Instance.getTraceHandler().error(e);
98 }
99
100 if (pg.getProgress() < 85) {
101 pg.add(1);
102 }
103 }
104 }
105 }
106
107 pg.setProgress(85);
108
109 // ZIP order is not correct for us
110 List<String> imagesList = new ArrayList<String>(images.keySet());
111 Collections.sort(imagesList);
112
113 pg.setProgress(90);
114
115 for (String uuid : imagesList) {
116 try {
117 chap.getParagraphs().add(new Paragraph(images.get(uuid)));
118 } catch (Exception e) {
119 Instance.getTraceHandler().error(e);
120 }
121 }
122
123 if (meta.getCover() == null && !images.isEmpty()) {
124 meta.setCover(images.get(imagesList.get(0)));
125 }
126
127 pg.setProgress(100);
128 return story;
129 }
130}