1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.URISyntaxException
;
8 import java
.net
.URLDecoder
;
9 import java
.util
.ArrayList
;
10 import java
.util
.zip
.ZipEntry
;
11 import java
.util
.zip
.ZipInputStream
;
13 import org
.jsoup
.nodes
.Document
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.data
.MetaData
;
17 import be
.nikiroo
.utils
.IOUtils
;
18 import be
.nikiroo
.utils
.Image
;
19 import be
.nikiroo
.utils
.StringUtils
;
20 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
23 * Support class for EPUB files created with this program (as we need some
24 * metadata available in those we create).
28 class Epub
extends InfoText
{
29 private MetaData meta
;
33 private URL fakeSource
;
34 private InputStream fakeIn
;
36 public File
getSourceFileOriginal() {
37 return super.getSourceFile();
41 protected File
getSourceFile() {
43 return new File(fakeSource
.toURI());
44 } catch (URISyntaxException e
) {
45 Instance
.getInstance().getTraceHandler()
46 .error(new IOException("Cannot get the source file from the info-text URL", e
));
53 protected InputStream
getInput() {
57 } catch (IOException e
) {
58 Instance
.getInstance().getTraceHandler().error(new IOException("Cannot reset the Epub Text stream", e
));
68 protected boolean supports(URL url
) {
69 return url
.getPath().toLowerCase().endsWith(".epub");
73 protected MetaData
getMeta() throws IOException
{
78 protected Document
loadDocument(URL source
) throws IOException
{
79 super.loadDocument(source
); // prepares super.getSourceFile() and
82 InputStream in
= super.getInput();
83 ZipInputStream zipIn
= null;
85 zipIn
= new ZipInputStream(in
);
86 tmpDir
= Instance
.getInstance().getTempFiles().createTempDir("fanfic-reader-parser");
87 File tmp
= new File(tmpDir
, "file.txt");
88 File tmpInfo
= new File(tmpDir
, "file.info");
90 fakeSource
= tmp
.toURI().toURL();
95 url
= getSource().toURI().toURL().toString();
96 } catch (URISyntaxException e1
) {
97 url
= getSource().toString();
100 String author
= null;
102 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
104 if (!entry
.isDirectory()
105 && entry
.getName().startsWith(getDataPrefix())) {
106 String entryLName
= entry
.getName().toLowerCase();
108 boolean imageEntry
= false;
109 for (String ext
: bsImages
.getImageExt(false)) {
110 if (entryLName
.endsWith(ext
)) {
115 if (entry
.getName().equals(getDataPrefix() + "version")) {
116 // Nothing to do for now ("first"
118 } else if (entryLName
.endsWith(".info")) {
120 IOUtils
.write(zipIn
, tmpInfo
);
121 } else if (imageEntry
) {
125 cover
= new Image(zipIn
);
126 } catch (Exception e
) {
127 Instance
.getInstance().getTraceHandler().error(e
);
130 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
131 String
[] descArray
= StringUtils
132 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
134 if (descArray
.length
> 0) {
135 url
= descArray
[0].trim();
137 } else if (entry
.getName().equals(
138 getDataPrefix() + "SUMMARY")) {
139 String
[] descArray
= StringUtils
140 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
143 if (descArray
.length
> 1) {
144 title
= descArray
[0].trim();
146 if (descArray
.length
> 2
147 && descArray
[1].startsWith("©")) {
148 author
= descArray
[1].substring(1).trim();
153 for (int i
= skip
; i
< descArray
.length
; i
++) {
154 this.desc
+= descArray
[i
].trim() + "\n";
157 this.desc
= this.desc
.trim();
159 // Hopefully the data file
160 IOUtils
.write(zipIn
, tmp
);
165 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
166 throw new IOException(
167 "file not supported (maybe not created with this program or corrupt)");
171 this.fakeIn
= new MarkableFileInputStream(tmp
);
174 if (tmpInfo
.exists()) {
175 meta
= InfoReader
.readMeta(tmpInfo
, true);
178 if (title
== null || title
.isEmpty()) {
179 title
= getSourceFileOriginal().getName();
180 if (title
.toLowerCase().endsWith(".cbz")) {
181 title
= title
.substring(0, title
.length() - 4);
183 title
= URLDecoder
.decode(title
, "UTF-8").trim();
186 meta
= new MetaData();
188 meta
.setTags(new ArrayList
<String
>());
189 meta
.setSource(getType().getSourceName());
192 meta
.setTitle(title
);
193 meta
.setAuthor(author
);
194 meta
.setImageDocument(isImagesDocumentByDefault());
197 if (meta
.getCover() == null) {
199 meta
.setCover(cover
);
201 meta
.setCover(InfoReader
202 .getCoverByName(getSourceFileOriginal().toURI()
219 protected void close() {
220 if (tmpDir
!= null) {
221 IOUtils
.deltree(tmpDir
);
229 protected String
getDataPrefix() {
233 protected boolean requireInfo() {
237 protected boolean getCover() {
241 protected boolean isImagesDocumentByDefault() {