Update nikiroo-utils, remove Instance.syserr/trace
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Cbz.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
a9eb3f46 3import java.awt.image.BufferedImage;
08fe2e33
NR
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStream;
7import java.net.URL;
8import java.util.ArrayList;
d3c84ac3
NR
9import java.util.Collections;
10import java.util.List;
08fe2e33
NR
11import java.util.zip.ZipEntry;
12import java.util.zip.ZipInputStream;
13
a9eb3f46
NR
14import javax.imageio.ImageIO;
15
08fe2e33
NR
16import be.nikiroo.fanfix.Instance;
17import be.nikiroo.fanfix.data.Chapter;
18import be.nikiroo.fanfix.data.Paragraph;
19import be.nikiroo.fanfix.data.Story;
3b2b638f 20import 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 */
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 }
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);
71 if (!pgMeta.isDone()) {
72 pgMeta.setProgress(pgMeta.getMax()); // 10%
73 }
74
08fe2e33
NR
75 story.setChapters(new ArrayList<Chapter>());
76 Chapter chap = new Chapter(1, null);
77 story.getChapters().add(chap);
78
79 ZipInputStream zipIn = new ZipInputStream(getInput());
80
d3c84ac3 81 List<String> images = new ArrayList<String>();
08fe2e33
NR
82 for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
83 .getNextEntry()) {
84 if (!entry.isDirectory()
85 && entry.getName().startsWith(getDataPrefix())) {
86 String entryLName = entry.getName().toLowerCase();
87 boolean imageEntry = false;
88 for (String ext : getImageExt(false)) {
89 if (entryLName.endsWith(ext)) {
90 imageEntry = true;
91 }
92 }
93
94 if (imageEntry) {
d3c84ac3 95 String uuid = meta.getUuid() + "_" + entry.getName();
08fe2e33 96 try {
a9eb3f46
NR
97 File tmp = Instance.getCache().addToCache(zipIn, uuid);
98 images.add(tmp.toURI().toURL().toString());
08fe2e33 99 } catch (Exception e) {
62c63b07 100 Instance.getTraceHandler().error(e);
08fe2e33
NR
101 }
102 }
103 }
104 }
105
92fb0719
NR
106 pg.setProgress(80);
107
a9eb3f46 108 // ZIP order is not correct for us
d3c84ac3 109 Collections.sort(images);
92fb0719 110 pg.setProgress(90);
d3c84ac3
NR
111
112 for (String uuid : images) {
113 try {
a9eb3f46 114 chap.getParagraphs().add(new Paragraph(new URL(uuid)));
d3c84ac3 115 } catch (Exception e) {
62c63b07 116 Instance.getTraceHandler().error(e);
d3c84ac3
NR
117 }
118 }
119
a9eb3f46
NR
120 if (meta.getCover() == null && !images.isEmpty()) {
121 InputStream in = Instance.getCache().open(new URL(images.get(0)),
122 this, true);
123 try {
124 BufferedImage fcover = ImageIO.read(in);
125 meta.setCover(fcover);
126 meta.setFakeCover(true);
127 } finally {
128 in.close();
129 }
130 }
131
92fb0719 132 pg.setProgress(100);
08fe2e33
NR
133 return story;
134 }
135}