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
.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
;
38 public String
getSourceName() {
42 public File
getSourceFileOriginal() {
43 return super.getSourceFile();
47 protected File
getSourceFile() {
49 return new File(fakeSource
.toURI());
50 } catch (URISyntaxException e
) {
51 Instance
.getTraceHandler()
52 .error(new IOException(
53 "Cannot get the source file from the info-text URL",
61 protected InputStream
getInput() {
65 } catch (IOException e
) {
66 Instance
.getTraceHandler()
67 .error(new IOException(
68 "Cannot reset the Epub Text stream", e
));
78 protected boolean supports(URL url
) {
79 return url
.getPath().toLowerCase().endsWith(".epub");
83 protected MetaData
getMeta() throws IOException
{
88 protected Document
loadDocument(URL source
) throws IOException
{
89 super.loadDocument(source
); // prepares super.getSourceFile() and
92 InputStream in
= super.getInput();
93 ZipInputStream zipIn
= null;
95 zipIn
= new ZipInputStream(in
);
96 tmpDir
= Instance
.getTempFiles().createTempDir(
97 "fanfic-reader-parser");
98 File tmp
= new File(tmpDir
, "file.txt");
99 File tmpInfo
= new File(tmpDir
, "file.info");
101 fakeSource
= tmp
.toURI().toURL();
106 url
= getSource().toURI().toURL().toString();
107 } catch (URISyntaxException e1
) {
108 url
= getSource().toString();
111 String author
= null;
113 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
115 if (!entry
.isDirectory()
116 && entry
.getName().startsWith(getDataPrefix())) {
117 String entryLName
= entry
.getName().toLowerCase();
119 boolean imageEntry
= false;
120 for (String ext
: BasicSupportImages
.getImageExt(false)) {
121 if (entryLName
.endsWith(ext
)) {
126 if (entry
.getName().equals(getDataPrefix() + "version")) {
127 // Nothing to do for now ("first"
129 } else if (entryLName
.endsWith(".info")) {
131 IOUtils
.write(zipIn
, tmpInfo
);
132 } else if (imageEntry
) {
136 cover
= new Image(zipIn
);
137 } catch (Exception e
) {
138 Instance
.getTraceHandler().error(e
);
141 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
142 String
[] descArray
= StringUtils
143 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
145 if (descArray
.length
> 0) {
146 url
= descArray
[0].trim();
148 } else if (entry
.getName().equals(
149 getDataPrefix() + "SUMMARY")) {
150 String
[] descArray
= StringUtils
151 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
154 if (descArray
.length
> 1) {
155 title
= descArray
[0].trim();
157 if (descArray
.length
> 2
158 && descArray
[1].startsWith("©")) {
159 author
= descArray
[1].substring(1).trim();
164 for (int i
= skip
; i
< descArray
.length
; i
++) {
165 this.desc
+= descArray
[i
].trim() + "\n";
168 this.desc
= this.desc
.trim();
170 // Hopefully the data file
171 IOUtils
.write(zipIn
, tmp
);
176 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
177 throw new IOException(
178 "file not supported (maybe not created with this program or corrupt)");
182 this.fakeIn
= new MarkableFileInputStream(new FileInputStream(
186 if (tmpInfo
.exists()) {
187 meta
= InfoReader
.readMeta(tmpInfo
, true);
189 meta
.setCover(cover
);
193 if (title
== null || title
.isEmpty()) {
194 title
= getSourceFileOriginal().getName();
195 if (title
.toLowerCase().endsWith(".cbz")) {
196 title
= title
.substring(0, title
.length() - 4);
198 title
= URLDecoder
.decode(title
, "UTF-8").trim();
201 meta
= new MetaData();
203 meta
.setTags(new ArrayList
<String
>());
204 meta
.setSource(getSourceName());
207 meta
.setTitle(title
);
208 meta
.setAuthor(author
);
209 meta
.setImageDocument(isImagesDocumentByDefault());
224 protected void close() {
225 if (tmpDir
!= null) {
226 IOUtils
.deltree(tmpDir
);
234 protected String
getDataPrefix() {
238 protected boolean requireInfo() {
242 protected boolean getCover() {
246 protected boolean isImagesDocumentByDefault() {