Update nikiroo-utils, update Library
[fanfix.git] / src / be / nikiroo / fanfix / supported / Cbz.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipInputStream;
13
14 import javax.imageio.ImageIO;
15
16 import be.nikiroo.fanfix.Instance;
17 import be.nikiroo.fanfix.data.Chapter;
18 import be.nikiroo.fanfix.data.Paragraph;
19 import be.nikiroo.fanfix.data.Story;
20 import 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 */
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 }
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 if (!pgMeta.isDone()) {
72 pgMeta.setProgress(pgMeta.getMax()); // 10%
73 }
74
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
81 List<String> images = new ArrayList<String>();
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) {
95 String uuid = meta.getUuid() + "_" + entry.getName();
96 try {
97 File tmp = Instance.getCache().addToCache(zipIn, uuid);
98 images.add(tmp.toURI().toURL().toString());
99 } catch (Exception e) {
100 Instance.syserr(e);
101 }
102 }
103 }
104 }
105
106 pg.setProgress(80);
107
108 // ZIP order is not correct for us
109 Collections.sort(images);
110 pg.setProgress(90);
111
112 for (String uuid : images) {
113 try {
114 chap.getParagraphs().add(new Paragraph(new URL(uuid)));
115 } catch (Exception e) {
116 Instance.syserr(e);
117 }
118 }
119
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
132 pg.setProgress(100);
133 return story;
134 }
135 }