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().error(new IOException(
46 "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(
59 "Cannot reset the Epub Text stream", e
));
69 protected boolean supports(URL url
) {
70 return url
.getPath().toLowerCase().endsWith(".epub");
74 protected MetaData
getMeta() throws IOException
{
79 protected Document
loadDocument(URL source
) throws IOException
{
80 super.loadDocument(source
); // prepares super.getSourceFile() and
83 InputStream in
= super.getInput();
84 ZipInputStream zipIn
= null;
86 zipIn
= new ZipInputStream(in
);
87 tmpDir
= Instance
.getInstance().getTempFiles()
88 .createTempDir("fanfic-reader-parser");
89 File tmp
= new File(tmpDir
, "file.txt");
90 File tmpInfo
= new File(tmpDir
, "file.info");
92 fakeSource
= tmp
.toURI().toURL();
97 url
= getSource().toURI().toURL().toString();
98 } catch (URISyntaxException e1
) {
99 url
= getSource().toString();
102 String author
= null;
104 for (ZipEntry entry
= zipIn
105 .getNextEntry(); entry
!= null; entry
= zipIn
107 if (!entry
.isDirectory()
108 && entry
.getName().startsWith(getDataPrefix())) {
109 String entryLName
= entry
.getName().toLowerCase();
111 boolean imageEntry
= false;
112 for (String ext
: bsImages
.getImageExt(false)) {
113 if (entryLName
.endsWith(ext
)) {
118 if (entry
.getName().equals(getDataPrefix() + "version")) {
119 // Nothing to do for now ("first"
121 } else if (entryLName
.endsWith(".info")) {
123 IOUtils
.write(zipIn
, tmpInfo
);
124 } else if (imageEntry
) {
128 cover
= new Image(zipIn
);
129 } catch (Exception e
) {
130 Instance
.getInstance().getTraceHandler()
134 } else if (entry
.getName()
135 .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().endsWith(".desc")) {
144 // if (this.desc != null) {
145 // this.desc = IOUtils.readSmallStream(zipIn).trim();
147 } else if (entry
.getName()
148 .equals(getDataPrefix() + "SUMMARY")) {
149 String
[] descArray
= StringUtils
150 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
153 if (descArray
.length
> 1) {
154 title
= descArray
[0].trim();
156 if (descArray
.length
> 2
157 && descArray
[1].startsWith("©")) {
158 author
= descArray
[1].substring(1).trim();
163 // for (int i = skip; i < descArray.length; i++) {
164 // this.desc += descArray[i].trim() + "\n";
167 // this.desc = this.desc.trim();
169 // Hopefully the data file
170 IOUtils
.write(zipIn
, tmp
);
175 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
176 throw new IOException(
177 "file not supported (maybe not created with this program or corrupt)");
181 this.fakeIn
= new MarkableFileInputStream(tmp
);
184 if (tmpInfo
.exists()) {
185 meta
= InfoReader
.readMeta(tmpInfo
, true);
188 if (title
== null || title
.isEmpty()) {
189 title
= getSourceFileOriginal().getName();
190 if (title
.toLowerCase().endsWith(".cbz")) {
191 title
= title
.substring(0, title
.length() - 4);
193 title
= URLDecoder
.decode(title
, "UTF-8").trim();
196 meta
= new MetaData();
198 meta
.setTags(new ArrayList
<String
>());
199 meta
.setSource(getType().getSourceName());
202 meta
.setTitle(title
);
203 meta
.setAuthor(author
);
204 meta
.setImageDocument(isImagesDocumentByDefault());
207 if (meta
.getCover() == null) {
209 meta
.setCover(cover
);
211 meta
.setCover(InfoReader
.getCoverByName(
212 getSourceFileOriginal().toURI().toURL()));
228 protected void close() {
229 if (tmpDir
!= null) {
230 IOUtils
.deltree(tmpDir
);
238 protected String
getDataPrefix() {
242 protected boolean requireInfo() {
246 protected boolean getCover() {
250 protected boolean isImagesDocumentByDefault() {