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 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
.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(new FileInputStream(
82 getSourceFileOriginal()));
83 zipIn
= new ZipInputStream(cbzIn
);
84 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
86 if (!entry
.isDirectory()
87 && entry
.getName().startsWith(getDataPrefix())) {
88 String entryLName
= entry
.getName().toLowerCase();
89 boolean imageEntry
= false;
90 for (String ext
: BasicSupportImages
.getImageExt(false)) {
91 if (entryLName
.endsWith(ext
)) {
97 String uuid
= meta
.getUuid() + "_" + entry
.getName();
99 images
.put(uuid
, new Image(zipIn
));
100 } catch (Exception e
) {
101 Instance
.getTraceHandler().error(e
);
104 if (pg
.getProgress() < 85) {
107 } else if (entryLName
.endsWith(".info")) {
108 basename
= entryLName
.substring(0, entryLName
.length()
110 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
111 } else if (entryLName
.endsWith(".txt")) {
112 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
119 // ZIP order is not correct for us
120 List
<String
> imagesList
= new ArrayList
<String
>(images
.keySet());
121 Collections
.sort(imagesList
);
125 // only the description is kept
126 Story origStory
= getStoryFromTxt(tmpDir
, basename
);
127 if (origStory
!= null) {
128 if (origStory
.getMeta().getCover() == null) {
129 origStory
.getMeta().setCover(story
.getMeta().getCover());
131 story
.setMeta(origStory
.getMeta());
133 story
.setChapters(new ArrayList
<Chapter
>());
135 // Check if we can find non-images chapters, for hybrid-cbz support
136 for (Chapter chap
: origStory
) {
137 Boolean isImages
= null;
138 for (Paragraph para
: chap
) {
139 ParagraphType t
= para
.getType();
140 if (isImages
== null && !t
.isText(true)) {
143 if (t
.isText(false)) {
144 String line
= para
.getContent();
145 // Images are saved in text mode as "[image-link]"
146 if (!(line
.startsWith("[") && line
.endsWith("]"))) {
152 if (isImages
!= null && !isImages
) {
153 story
.getChapters().add(chap
);
154 chap
.setNumber(story
.getChapters().size());
158 if (!imagesList
.isEmpty()) {
159 Chapter chap
= new Chapter(story
.getChapters().size() + 1, null);
160 story
.getChapters().add(chap
);
162 for (String uuid
: imagesList
) {
164 chap
.getParagraphs().add(
165 new Paragraph(images
.get(uuid
)));
166 } catch (Exception e
) {
167 Instance
.getTraceHandler().error(e
);
172 if (meta
.getCover() == null && !images
.isEmpty()) {
173 meta
.setCover(images
.get(imagesList
.get(0)));
174 meta
.setFakeCover(true);
177 IOUtils
.deltree(tmpDir
);
190 private Story
getStoryFromTxt(File tmpDir
, String basename
) {
191 Story origStory
= null;
193 File txt
= new File(tmpDir
, basename
+ ".txt");
197 if (basename
!= null) {
199 BasicSupport support
= BasicSupport
.getSupport(txt
.toURI()
201 origStory
= support
.process(null);
202 } catch (Exception e
) {