1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Collections
;
9 import java
.util
.HashMap
;
10 import java
.util
.List
;
12 import java
.util
.zip
.ZipEntry
;
13 import java
.util
.zip
.ZipInputStream
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.bundles
.Config
;
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
.Progress
;
25 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
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 protected String
getDataPrefix() {
45 protected boolean requireInfo() {
50 protected boolean isImagesDocumentByDefault() {
55 protected boolean getCover() {
60 public Story
doProcess(Progress pg
) throws IOException
{
67 pg
.setName("Initialising");
69 Progress pgMeta
= new Progress();
70 pg
.addProgress(pgMeta
, 10);
71 Story story
= processMeta(true, pgMeta
);
72 MetaData meta
= story
.getMeta();
76 File tmpDir
= Instance
.getInstance().getTempFiles().createTempDir("info-text");
77 String basename
= null;
79 Map
<String
, Image
> images
= new HashMap
<String
, Image
>();
80 InputStream cbzIn
= null;
81 ZipInputStream zipIn
= null;
83 cbzIn
= new MarkableFileInputStream(getSourceFileOriginal());
84 zipIn
= new ZipInputStream(cbzIn
);
85 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
87 if (!entry
.isDirectory()
88 && entry
.getName().startsWith(getDataPrefix())) {
89 String entryLName
= entry
.getName().toLowerCase();
90 boolean imageEntry
= false;
91 for (String ext
: bsImages
.getImageExt(false)) {
92 if (entryLName
.endsWith(ext
)) {
98 String uuid
= meta
.getUuid() + "_" + entry
.getName();
100 images
.put(uuid
, new Image(zipIn
));
101 } catch (Exception e
) {
102 Instance
.getInstance().getTraceHandler().error(e
);
105 if (pg
.getProgress() < 85) {
108 } else if (entryLName
.endsWith(".info")) {
109 basename
= entryLName
.substring(0, entryLName
.length()
111 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
112 } else if (entryLName
.endsWith(".txt")) {
113 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
119 + Instance
.getInstance().getConfig().getString(Config
.FILE_FORMAT_IMAGE_FORMAT_COVER
).toLowerCase();
120 String coverName
= meta
.getUuid() + "_" + basename
+ ext
;
121 Image cover
= images
.get(coverName
);
122 images
.remove(coverName
);
126 // ZIP order is not correct for us
127 List
<String
> imagesList
= new ArrayList
<String
>(images
.keySet());
128 Collections
.sort(imagesList
);
132 // only the description/cover is kept
133 Story origStory
= getStoryFromTxt(tmpDir
, basename
);
134 if (origStory
!= null) {
135 if (origStory
.getMeta().getCover() == null) {
136 origStory
.getMeta().setCover(story
.getMeta().getCover());
138 story
.setMeta(origStory
.getMeta());
140 if (story
.getMeta().getCover() == null) {
141 story
.getMeta().setCover(cover
);
143 story
.setChapters(new ArrayList
<Chapter
>());
145 // Check if we can find non-images chapters, for hybrid-cbz support
146 if (origStory
!= null) {
147 for (Chapter chap
: origStory
) {
148 Boolean isImages
= null;
149 for (Paragraph para
: chap
) {
150 ParagraphType t
= para
.getType();
151 if (isImages
== null && !t
.isText(true)) {
154 if (t
.isText(false)) {
155 String line
= para
.getContent();
156 // Images are saved in text mode as "[image-link]"
157 if (!(line
.startsWith("[") && line
.endsWith("]"))) {
163 if (isImages
!= null && !isImages
) {
164 story
.getChapters().add(chap
);
165 chap
.setNumber(story
.getChapters().size());
170 if (!imagesList
.isEmpty()) {
171 Chapter chap
= new Chapter(story
.getChapters().size() + 1, "");
172 story
.getChapters().add(chap
);
174 for (String uuid
: imagesList
) {
176 chap
.getParagraphs().add(
177 new Paragraph(images
.get(uuid
)));
178 } catch (Exception e
) {
179 Instance
.getInstance().getTraceHandler().error(e
);
184 if (meta
.getCover() == null && !images
.isEmpty()) {
185 meta
.setCover(images
.get(imagesList
.get(0)));
186 meta
.setFakeCover(true);
189 IOUtils
.deltree(tmpDir
);
202 private Story
getStoryFromTxt(File tmpDir
, String basename
) {
203 Story origStory
= null;
205 File txt
= new File(tmpDir
, basename
+ ".txt");
209 if (basename
!= null) {
211 BasicSupport support
= BasicSupport
.getSupport(txt
.toURI()
213 origStory
= support
.process(null);
214 } catch (Exception e
) {