c7475fa775640e39f57aa8b1d218ab3cd51841b8
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
.data
.Chapter
;
17 import be
.nikiroo
.fanfix
.data
.MetaData
;
18 import be
.nikiroo
.fanfix
.data
.Paragraph
;
19 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
20 import be
.nikiroo
.fanfix
.data
.Story
;
21 import be
.nikiroo
.utils
.IOUtils
;
22 import be
.nikiroo
.utils
.Image
;
23 import be
.nikiroo
.utils
.Progress
;
24 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
27 * Support class for CBZ files (works better with CBZ created with this program,
28 * as they have some metadata available).
32 class Cbz
extends Epub
{
34 protected boolean supports(URL url
) {
35 return url
.toString().toLowerCase().endsWith(".cbz");
39 protected String
getDataPrefix() {
44 protected boolean requireInfo() {
49 protected boolean isImagesDocumentByDefault() {
54 protected boolean getCover() {
59 public Story
doProcess(Progress pg
) throws IOException
{
66 Progress pgMeta
= new Progress();
67 pg
.addProgress(pgMeta
, 10);
68 Story story
= processMeta(true, pgMeta
);
69 MetaData meta
= story
.getMeta();
73 File tmpDir
= Instance
.getTempFiles().createTempDir("info-text");
74 String basename
= null;
76 Map
<String
, Image
> images
= new HashMap
<String
, Image
>();
77 InputStream cbzIn
= null;
78 ZipInputStream zipIn
= null;
80 cbzIn
= new MarkableFileInputStream(getSourceFileOriginal());
81 zipIn
= new ZipInputStream(cbzIn
);
82 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
84 if (!entry
.isDirectory()
85 && entry
.getName().startsWith(getDataPrefix())) {
86 String entryLName
= entry
.getName().toLowerCase();
87 boolean imageEntry
= false;
88 for (String ext
: bsImages
.getImageExt(false)) {
89 if (entryLName
.endsWith(ext
)) {
95 String uuid
= meta
.getUuid() + "_" + entry
.getName();
97 images
.put(uuid
, new Image(zipIn
));
98 } catch (Exception e
) {
99 Instance
.getTraceHandler().error(e
);
102 if (pg
.getProgress() < 85) {
105 } else if (entryLName
.endsWith(".info")) {
106 basename
= entryLName
.substring(0, entryLName
.length()
108 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
109 } else if (entryLName
.endsWith(".txt")) {
110 IOUtils
.write(zipIn
, new File(tmpDir
, entryLName
));
117 // ZIP order is not correct for us
118 List
<String
> imagesList
= new ArrayList
<String
>(images
.keySet());
119 Collections
.sort(imagesList
);
123 // only the description is kept
124 Story origStory
= getStoryFromTxt(tmpDir
, basename
);
125 if (origStory
!= null) {
126 if (origStory
.getMeta().getCover() == null) {
127 origStory
.getMeta().setCover(story
.getMeta().getCover());
129 story
.setMeta(origStory
.getMeta());
131 story
.setChapters(new ArrayList
<Chapter
>());
133 // Check if we can find non-images chapters, for hybrid-cbz support
134 if (origStory
!= null) {
135 for (Chapter chap
: origStory
) {
136 Boolean isImages
= null;
137 for (Paragraph para
: chap
) {
138 ParagraphType t
= para
.getType();
139 if (isImages
== null && !t
.isText(true)) {
142 if (t
.isText(false)) {
143 String line
= para
.getContent();
144 // Images are saved in text mode as "[image-link]"
145 if (!(line
.startsWith("[") && line
.endsWith("]"))) {
151 if (isImages
!= null && !isImages
) {
152 story
.getChapters().add(chap
);
153 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
) {