1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
8 import java
.util
.ArrayList
;
9 import java
.util
.Collections
;
10 import java
.util
.HashMap
;
11 import java
.util
.List
;
13 import java
.util
.zip
.ZipEntry
;
14 import java
.util
.zip
.ZipInputStream
;
16 import be
.nikiroo
.fanfix
.Instance
;
17 import be
.nikiroo
.fanfix
.data
.Chapter
;
18 import be
.nikiroo
.fanfix
.data
.MetaData
;
19 import be
.nikiroo
.fanfix
.data
.Paragraph
;
20 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
21 import be
.nikiroo
.fanfix
.data
.Story
;
22 import be
.nikiroo
.utils
.IOUtils
;
23 import be
.nikiroo
.utils
.Image
;
24 import be
.nikiroo
.utils
.MarkableFileInputStream
;
25 import be
.nikiroo
.utils
.Progress
;
28 * Support class for CBZ files (works better with CBZ created with this program,
29 * as they have some metadata available).
33 class Cbz
extends Epub
{
35 protected boolean supports(URL url
) {
36 return url
.toString().toLowerCase().endsWith(".cbz");
40 public String
getSourceName() {
45 protected String
getDataPrefix() {
50 protected boolean requireInfo() {
55 protected boolean isImagesDocumentByDefault() {
60 protected boolean getCover() {
65 public Story
doProcess(Progress pg
) throws IOException
{
72 Progress pgMeta
= new Progress();
73 pg
.addProgress(pgMeta
, 10);
74 Story story
= processMeta(true, pgMeta
);
75 MetaData meta
= story
.getMeta();
79 File tmpDir
= Instance
.getTempFiles().createTempDir("info-text");
80 String basename
= null;
82 Map
<String
, Image
> images
= new HashMap
<String
, Image
>();
83 InputStream cbzIn
= null;
84 ZipInputStream zipIn
= null;
86 cbzIn
= new MarkableFileInputStream(new FileInputStream(
87 getSourceFileOriginal()));
88 zipIn
= new ZipInputStream(cbzIn
);
89 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
91 if (!entry
.isDirectory()
92 && entry
.getName().startsWith(getDataPrefix())) {
93 String entryLName
= entry
.getName().toLowerCase();
94 boolean imageEntry
= false;
95 for (String ext
: BasicSupportImages
.getImageExt(false)) {
96 if (entryLName
.endsWith(ext
)) {
102 String uuid
= meta
.getUuid() + "_" + entry
.getName();
104 images
.put(uuid
, new Image(zipIn
));
105 } catch (Exception e
) {
106 Instance
.getTraceHandler().error(e
);
109 if (pg
.getProgress() < 85) {
112 } else if (entryLName
.endsWith(".info")) {
113 basename
= entryLName
.substring(0, entryLName
.length()
115 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
116 } else if (entryLName
.endsWith(".txt")) {
117 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
124 // ZIP order is not correct for us
125 List
<String
> imagesList
= new ArrayList
<String
>(images
.keySet());
126 Collections
.sort(imagesList
);
130 // only the description is kept
131 Story origStory
= getStoryFromTxt(tmpDir
, basename
);
132 if (origStory
!= null) {
133 if (origStory
.getMeta().getCover() == null) {
134 origStory
.getMeta().setCover(story
.getMeta().getCover());
136 story
.setMeta(origStory
.getMeta());
138 story
.setChapters(new ArrayList
<Chapter
>());
140 // Check if we can find non-images chapters, for hybrid-cbz support
141 for (Chapter chap
: origStory
) {
142 Boolean isImages
= null;
143 for (Paragraph para
: chap
) {
144 ParagraphType t
= para
.getType();
145 if (isImages
== null && !t
.isText(true)) {
148 if (t
.isText(false)) {
149 String line
= para
.getContent();
150 // Images are saved in text mode as "[image-link]"
151 if (!(line
.startsWith("[") && line
.endsWith("]"))) {
157 if (isImages
!= null && !isImages
) {
158 story
.getChapters().add(chap
);
159 chap
.setNumber(story
.getChapters().size());
163 if (!imagesList
.isEmpty()) {
164 Chapter chap
= new Chapter(story
.getChapters().size() + 1, null);
165 story
.getChapters().add(chap
);
167 for (String uuid
: imagesList
) {
169 chap
.getParagraphs().add(
170 new Paragraph(images
.get(uuid
)));
171 } catch (Exception e
) {
172 Instance
.getTraceHandler().error(e
);
177 if (meta
.getCover() == null && !images
.isEmpty()) {
178 meta
.setCover(images
.get(imagesList
.get(0)));
179 meta
.setFakeCover(true);
182 IOUtils
.deltree(tmpDir
);
195 private Story
getStoryFromTxt(File tmpDir
, String basename
) {
196 Story origStory
= null;
198 File txt
= new File(tmpDir
, basename
+ ".txt");
202 if (basename
!= null) {
204 BasicSupport support
= BasicSupport
.getSupport(txt
.toURI()
206 origStory
= support
.process(null);
207 } catch (Exception e
) {