7ac3efc78703f7fb9e4a27fc2668c04c541965ad
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 File sourceFileOriginal
;
32 private MetaData meta
;
36 private URL fakeSource
;
37 private InputStream fakeIn
;
40 public String
getSourceName() {
44 public File
getSourceFileOriginal() {
45 return sourceFileOriginal
;
49 protected File
getSourceFile() {
51 return new File(fakeSource
.toURI());
52 } catch (URISyntaxException e
) {
53 Instance
.getTraceHandler()
54 .error(new IOException(
55 "Cannot get the source file from the info-text URL",
63 protected InputStream
getInput() {
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
.getTempFiles().createTempDir(
87 "fanfic-reader-parser");
88 File tmp
= new File(tmpDir
, "file.txt");
89 File tmpInfo
= new File(tmpDir
, "file.info");
91 fakeSource
= tmp
.toURI().toURL();
96 url
= getSource().toURI().toURL().toString();
97 } catch (URISyntaxException e1
) {
98 url
= getSource().toString();
101 String author
= null;
103 for (ZipEntry entry
= zipIn
.getNextEntry(); entry
!= null; entry
= zipIn
105 if (!entry
.isDirectory()
106 && entry
.getName().startsWith(getDataPrefix())) {
107 String entryLName
= entry
.getName().toLowerCase();
109 boolean imageEntry
= false;
110 for (String ext
: BasicSupportImages
.getImageExt(false)) {
111 if (entryLName
.endsWith(ext
)) {
116 if (entry
.getName().equals(getDataPrefix() + "version")) {
117 // Nothing to do for now ("first"
119 } else if (entryLName
.endsWith(".info")) {
121 IOUtils
.write(zipIn
, tmpInfo
);
122 } else if (imageEntry
) {
126 cover
= new Image(zipIn
);
127 } catch (Exception e
) {
128 Instance
.getTraceHandler().error(e
);
131 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
132 String
[] descArray
= StringUtils
133 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
135 if (descArray
.length
> 0) {
136 url
= descArray
[0].trim();
138 } else if (entry
.getName().equals(
139 getDataPrefix() + "SUMMARY")) {
140 String
[] descArray
= StringUtils
141 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
144 if (descArray
.length
> 1) {
145 title
= descArray
[0].trim();
147 if (descArray
.length
> 2
148 && descArray
[1].startsWith("©")) {
149 author
= descArray
[1].substring(1).trim();
154 for (int i
= skip
; i
< descArray
.length
; i
++) {
155 this.desc
+= descArray
[i
].trim() + "\n";
158 this.desc
= this.desc
.trim();
160 // Hopefully the data file
161 IOUtils
.write(zipIn
, tmp
);
166 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
167 throw new IOException(
168 "file not supported (maybe not created with this program or corrupt)");
172 this.fakeIn
= new MarkableFileInputStream(new FileInputStream(
176 if (tmpInfo
.exists()) {
177 meta
= InfoReader
.readMeta(tmpInfo
, true);
179 meta
.setCover(cover
);
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(getSourceName());
197 meta
.setTitle(title
);
198 meta
.setAuthor(author
);
199 meta
.setImageDocument(isImagesDocumentByDefault());
214 protected void close() {
215 if (tmpDir
!= null) {
216 IOUtils
.deltree(tmpDir
);
224 protected String
getDataPrefix() {
228 protected boolean requireInfo() {
232 protected boolean getCover() {
236 protected boolean isImagesDocumentByDefault() {