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 Progress pgMeta
= new Progress();
68 pg
.addProgress(pgMeta
, 10);
69 Story story
= processMeta(true, pgMeta
);
70 MetaData meta
= story
.getMeta();
74 File tmpDir
= Instance
.getInstance().getTempFiles().createTempDir("info-text");
75 String basename
= null;
77 Map
<String
, Image
> images
= new HashMap
<String
, Image
>();
78 InputStream cbzIn
= null;
79 ZipInputStream zipIn
= null;
81 cbzIn
= new MarkableFileInputStream(getSourceFileOriginal());
82 zipIn
= new ZipInputStream(cbzIn
);
83 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
85 if (!entry
.isDirectory()
86 && entry
.getName().startsWith(getDataPrefix())) {
87 String entryLName
= entry
.getName().toLowerCase();
88 boolean imageEntry
= false;
89 for (String ext
: bsImages
.getImageExt(false)) {
90 if (entryLName
.endsWith(ext
)) {
96 String uuid
= meta
.getUuid() + "_" + entry
.getName();
98 images
.put(uuid
, new Image(zipIn
));
99 } catch (Exception e
) {
100 Instance
.getInstance().getTraceHandler().error(e
);
103 if (pg
.getProgress() < 85) {
106 } else if (entryLName
.endsWith(".info")) {
107 basename
= entryLName
.substring(0, entryLName
.length()
109 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
110 } else if (entryLName
.endsWith(".txt")) {
111 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
117 + Instance
.getInstance().getConfig().getString(Config
.FILE_FORMAT_IMAGE_FORMAT_COVER
).toLowerCase();
118 String coverName
= meta
.getUuid() + "_" + basename
+ ext
;
119 Image cover
= images
.get(coverName
);
120 images
.remove(coverName
);
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/cover 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 if (story
.getMeta().getCover() == null) {
139 story
.getMeta().setCover(cover
);
141 story
.setChapters(new ArrayList
<Chapter
>());
143 // Check if we can find non-images chapters, for hybrid-cbz support
144 if (origStory
!= null) {
145 for (Chapter chap
: origStory
) {
146 Boolean isImages
= null;
147 for (Paragraph para
: chap
) {
148 ParagraphType t
= para
.getType();
149 if (isImages
== null && !t
.isText(true)) {
152 if (t
.isText(false)) {
153 String line
= para
.getContent();
154 // Images are saved in text mode as "[image-link]"
155 if (!(line
.startsWith("[") && line
.endsWith("]"))) {
161 if (isImages
!= null && !isImages
) {
162 story
.getChapters().add(chap
);
163 chap
.setNumber(story
.getChapters().size());
168 if (!imagesList
.isEmpty()) {
169 Chapter chap
= new Chapter(story
.getChapters().size() + 1, null);
170 story
.getChapters().add(chap
);
172 for (String uuid
: imagesList
) {
174 chap
.getParagraphs().add(
175 new Paragraph(images
.get(uuid
)));
176 } catch (Exception e
) {
177 Instance
.getInstance().getTraceHandler().error(e
);
182 if (meta
.getCover() == null && !images
.isEmpty()) {
183 meta
.setCover(images
.get(imagesList
.get(0)));
184 meta
.setFakeCover(true);
187 IOUtils
.deltree(tmpDir
);
200 private Story
getStoryFromTxt(File tmpDir
, String basename
) {
201 Story origStory
= null;
203 File txt
= new File(tmpDir
, basename
+ ".txt");
207 if (basename
!= null) {
209 BasicSupport support
= BasicSupport
.getSupport(txt
.toURI()
211 origStory
= support
.process(null);
212 } catch (Exception e
) {