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
.getTraceHandler()
46 .error(new IOException(
47 "Cannot get the source file from the info-text URL",
55 protected InputStream
getInput() {
59 } catch (IOException e
) {
60 Instance
.getTraceHandler()
61 .error(new IOException(
62 "Cannot reset the Epub Text stream", e
));
72 protected boolean supports(URL url
) {
73 return url
.getPath().toLowerCase().endsWith(".epub");
77 protected MetaData
getMeta() throws IOException
{
82 protected Document
loadDocument(URL source
) throws IOException
{
83 super.loadDocument(source
); // prepares super.getSourceFile() and
86 InputStream in
= super.getInput();
87 ZipInputStream zipIn
= null;
89 zipIn
= new ZipInputStream(in
);
90 tmpDir
= Instance
.getTempFiles().createTempDir(
91 "fanfic-reader-parser");
92 File tmp
= new File(tmpDir
, "file.txt");
93 File tmpInfo
= new File(tmpDir
, "file.info");
95 fakeSource
= tmp
.toURI().toURL();
100 url
= getSource().toURI().toURL().toString();
101 } catch (URISyntaxException e1
) {
102 url
= getSource().toString();
105 String author
= null;
107 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
109 if (!entry
.isDirectory()
110 && entry
.getName().startsWith(getDataPrefix())) {
111 String entryLName
= entry
.getName().toLowerCase();
113 boolean imageEntry
= false;
114 for (String ext
: bsImages
.getImageExt(false)) {
115 if (entryLName
.endsWith(ext
)) {
120 if (entry
.getName().equals(getDataPrefix() + "version")) {
121 // Nothing to do for now ("first"
123 } else if (entryLName
.endsWith(".info")) {
125 IOUtils
.write(zipIn
, tmpInfo
);
126 } else if (imageEntry
) {
130 cover
= new Image(zipIn
);
131 } catch (Exception e
) {
132 Instance
.getTraceHandler().error(e
);
135 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
136 String
[] descArray
= StringUtils
137 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
139 if (descArray
.length
> 0) {
140 url
= descArray
[0].trim();
142 } else if (entry
.getName().equals(
143 getDataPrefix() + "SUMMARY")) {
144 String
[] descArray
= StringUtils
145 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
148 if (descArray
.length
> 1) {
149 title
= descArray
[0].trim();
151 if (descArray
.length
> 2
152 && descArray
[1].startsWith("©")) {
153 author
= descArray
[1].substring(1).trim();
158 for (int i
= skip
; i
< descArray
.length
; i
++) {
159 this.desc
+= descArray
[i
].trim() + "\n";
162 this.desc
= this.desc
.trim();
164 // Hopefully the data file
165 IOUtils
.write(zipIn
, tmp
);
170 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
171 throw new IOException(
172 "file not supported (maybe not created with this program or corrupt)");
176 this.fakeIn
= new MarkableFileInputStream(tmp
);
179 if (tmpInfo
.exists()) {
180 meta
= InfoReader
.readMeta(tmpInfo
, true);
183 if (title
== null || title
.isEmpty()) {
184 title
= getSourceFileOriginal().getName();
185 if (title
.toLowerCase().endsWith(".cbz")) {
186 title
= title
.substring(0, title
.length() - 4);
188 title
= URLDecoder
.decode(title
, "UTF-8").trim();
191 meta
= new MetaData();
193 meta
.setTags(new ArrayList
<String
>());
194 meta
.setSource(getType().getSourceName());
197 meta
.setTitle(title
);
198 meta
.setAuthor(author
);
199 meta
.setImageDocument(isImagesDocumentByDefault());
202 if (meta
.getCover() == null) {
204 meta
.setCover(cover
);
206 meta
.setCover(InfoReader
207 .getCoverByName(getSourceFileOriginal().toURI()
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() {