1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.net
.URISyntaxException
;
9 import java
.net
.URLDecoder
;
10 import java
.util
.ArrayList
;
11 import java
.util
.zip
.ZipEntry
;
12 import java
.util
.zip
.ZipInputStream
;
14 import org
.jsoup
.nodes
.Document
;
16 import be
.nikiroo
.fanfix
.Instance
;
17 import be
.nikiroo
.fanfix
.data
.MetaData
;
18 import be
.nikiroo
.utils
.IOUtils
;
19 import be
.nikiroo
.utils
.Image
;
20 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
21 import be
.nikiroo
.utils
.StringUtils
;
24 * Support class for EPUB files created with this program (as we need some
25 * metadata available in those we create).
29 class Epub
extends InfoText
{
30 private MetaData meta
;
34 private URL fakeSource
;
35 private InputStream fakeIn
;
37 public File
getSourceFileOriginal() {
38 return super.getSourceFile();
42 protected File
getSourceFile() {
44 return new File(fakeSource
.toURI());
45 } catch (URISyntaxException e
) {
46 Instance
.getTraceHandler()
47 .error(new IOException(
48 "Cannot get the source file from the info-text URL",
56 protected InputStream
getInput() {
60 } catch (IOException e
) {
61 Instance
.getTraceHandler()
62 .error(new IOException(
63 "Cannot reset the Epub Text stream", e
));
73 protected boolean supports(URL url
) {
74 return url
.getPath().toLowerCase().endsWith(".epub");
78 protected MetaData
getMeta() throws IOException
{
83 protected Document
loadDocument(URL source
) throws IOException
{
84 super.loadDocument(source
); // prepares super.getSourceFile() and
87 InputStream in
= super.getInput();
88 ZipInputStream zipIn
= null;
90 zipIn
= new ZipInputStream(in
);
91 tmpDir
= Instance
.getTempFiles().createTempDir(
92 "fanfic-reader-parser");
93 File tmp
= new File(tmpDir
, "file.txt");
94 File tmpInfo
= new File(tmpDir
, "file.info");
96 fakeSource
= tmp
.toURI().toURL();
101 url
= getSource().toURI().toURL().toString();
102 } catch (URISyntaxException e1
) {
103 url
= getSource().toString();
106 String author
= null;
108 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
110 if (!entry
.isDirectory()
111 && entry
.getName().startsWith(getDataPrefix())) {
112 String entryLName
= entry
.getName().toLowerCase();
114 boolean imageEntry
= false;
115 for (String ext
: BasicSupportImages
.getImageExt(false)) {
116 if (entryLName
.endsWith(ext
)) {
121 if (entry
.getName().equals(getDataPrefix() + "version")) {
122 // Nothing to do for now ("first"
124 } else if (entryLName
.endsWith(".info")) {
126 IOUtils
.write(zipIn
, tmpInfo
);
127 } else if (imageEntry
) {
131 cover
= new Image(zipIn
);
132 } catch (Exception e
) {
133 Instance
.getTraceHandler().error(e
);
136 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
137 String
[] descArray
= StringUtils
138 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
140 if (descArray
.length
> 0) {
141 url
= descArray
[0].trim();
143 } else if (entry
.getName().equals(
144 getDataPrefix() + "SUMMARY")) {
145 String
[] descArray
= StringUtils
146 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
149 if (descArray
.length
> 1) {
150 title
= descArray
[0].trim();
152 if (descArray
.length
> 2
153 && descArray
[1].startsWith("©")) {
154 author
= descArray
[1].substring(1).trim();
159 for (int i
= skip
; i
< descArray
.length
; i
++) {
160 this.desc
+= descArray
[i
].trim() + "\n";
163 this.desc
= this.desc
.trim();
165 // Hopefully the data file
166 IOUtils
.write(zipIn
, tmp
);
171 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
172 throw new IOException(
173 "file not supported (maybe not created with this program or corrupt)");
177 this.fakeIn
= new MarkableFileInputStream(tmp
);
180 if (tmpInfo
.exists()) {
181 meta
= InfoReader
.readMeta(tmpInfo
, true);
184 if (title
== null || title
.isEmpty()) {
185 title
= getSourceFileOriginal().getName();
186 if (title
.toLowerCase().endsWith(".cbz")) {
187 title
= title
.substring(0, title
.length() - 4);
189 title
= URLDecoder
.decode(title
, "UTF-8").trim();
192 meta
= new MetaData();
194 meta
.setTags(new ArrayList
<String
>());
195 meta
.setSource(getType().getSourceName());
198 meta
.setTitle(title
);
199 meta
.setAuthor(author
);
200 meta
.setImageDocument(isImagesDocumentByDefault());
203 if (meta
.getCover() == null) {
205 meta
.setCover(cover
);
207 meta
.setCover(InfoReader
208 .getCoverByName(getSourceFileOriginal().toURI()
225 protected void close() {
226 if (tmpDir
!= null) {
227 IOUtils
.deltree(tmpDir
);
235 protected String
getDataPrefix() {
239 protected boolean requireInfo() {
243 protected boolean getCover() {
247 protected boolean isImagesDocumentByDefault() {