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
.Arrays
;
11 import java
.util
.Collections
;
12 import java
.util
.zip
.ZipEntry
;
13 import java
.util
.zip
.ZipInputStream
;
15 import org
.jsoup
.nodes
.Document
;
17 import be
.nikiroo
.fanfix
.Instance
;
18 import be
.nikiroo
.fanfix
.data
.MetaData
;
19 import be
.nikiroo
.utils
.IOUtils
;
20 import be
.nikiroo
.utils
.Image
;
21 import be
.nikiroo
.utils
.StringUtils
;
22 import be
.nikiroo
.utils
.streams
.MarkableFileInputStream
;
25 * Support class for EPUB files created with this program (as we need some
26 * metadata available in those we create).
30 class Epub
extends InfoText
{
31 private MetaData meta
;
35 private URL fakeSource
;
36 private InputStream fakeIn
;
38 public File
getSourceFileOriginal() {
39 return super.getSourceFile();
43 protected File
getSourceFile() {
45 return new File(fakeSource
.toURI());
46 } catch (URISyntaxException e
) {
47 Instance
.getInstance().getTraceHandler().error(new IOException(
48 "Cannot get the source file from the info-text URL", e
));
55 protected InputStream
getInput() {
59 } catch (IOException e
) {
60 Instance
.getInstance().getTraceHandler().error(new IOException(
61 "Cannot reset the Epub Text stream", e
));
71 protected boolean supports(URL url
) {
72 return url
.getPath().toLowerCase().endsWith(".epub");
76 protected MetaData
getMeta() throws IOException
{
81 protected Document
loadDocument(URL source
) throws IOException
{
82 super.loadDocument(source
); // prepares super.getSourceFile() and
85 InputStream in
= super.getInput();
86 ZipInputStream zipIn
= null;
88 zipIn
= new ZipInputStream(in
);
89 tmpDir
= Instance
.getInstance().getTempFiles()
90 .createTempDir("fanfic-reader-parser");
91 File tmp
= new File(tmpDir
, "file.txt");
92 File tmpInfo
= new File(tmpDir
, "file.info");
94 fakeSource
= tmp
.toURI().toURL();
99 url
= getSource().toURI().toURL().toString();
100 } catch (URISyntaxException e1
) {
101 url
= getSource().toString();
104 String author
= null;
106 for (ZipEntry entry
= zipIn
107 .getNextEntry(); entry
!= null; entry
= zipIn
109 if (!entry
.isDirectory()
110 && entry
.getName().startsWith(getDataPrefix())) {
111 String entryLName
= entry
.getName().toLowerCase();
112 entryLName
= entryLName
.substring(getDataPrefix().length());
114 boolean imageEntry
= false;
115 for (String ext
: bsImages
.getImageExt(false)) {
116 if (entryLName
.endsWith(ext
)) {
121 if (entryLName
.equals("version")) {
122 // Nothing to do for now ("first"
124 } else if (entryLName
.endsWith(".info")) {
126 IOUtils
.write(zipIn
, tmpInfo
);
127 } else if (imageEntry
) {
129 if (getCover() && cover
== null) {
131 Image img
= new Image(zipIn
);
132 if (img
.getSize() == 0) {
134 throw new IOException(
135 "Empty image not accepted");
138 } catch (Exception e
) {
139 Instance
.getInstance().getTraceHandler()
143 } else if (entryLName
.equals("url")) {
144 String
[] descArray
= StringUtils
145 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
147 if (descArray
.length
> 0) {
148 url
= descArray
[0].trim();
150 } else if (entryLName
.endsWith(".desc")) {
152 // if (this.desc != null) {
153 // this.desc = IOUtils.readSmallStream(zipIn).trim();
155 } else if (entryLName
.equals("summary")) {
156 String
[] descArray
= StringUtils
157 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
160 if (descArray
.length
> 1) {
161 title
= descArray
[0].trim();
163 if (descArray
.length
> 2
164 && descArray
[1].startsWith("©")) {
165 author
= descArray
[1].substring(1).trim();
170 // for (int i = skip; i < descArray.length; i++) {
171 // this.desc += descArray[i].trim() + "\n";
174 // this.desc = this.desc.trim();
176 // Hopefully the data file
177 IOUtils
.write(zipIn
, tmp
);
182 if (requireInfo() && !tmp
.exists()) {
183 throw new IOException(
184 "file not supported (maybe not created with this program or corrupt)");
188 this.fakeIn
= new MarkableFileInputStream(tmp
);
191 if (tmpInfo
.exists()) {
192 meta
= InfoReader
.readMeta(tmpInfo
, true);
195 if (title
== null || title
.isEmpty()) {
196 title
= getSourceFileOriginal().getName();
197 String exts
[] = new String
[] {".epub", ".cbz"};
198 for (String ext
: exts
) {
199 if (title
.toLowerCase().endsWith(ext
)) {
200 title
= title
.substring(0,
201 title
.length() - ext
.length());
204 title
= URLDecoder
.decode(title
, "UTF-8").trim();
207 meta
= new MetaData();
209 meta
.setTags(Arrays
.asList("[no_info]"));
210 meta
.setSource(getType().getSourceName());
213 meta
.setTitle(title
);
214 meta
.setAuthor(author
);
215 meta
.setImageDocument(isImagesDocumentByDefault());
217 InfoReader
.completeMeta(tmp
, meta
);
220 if (meta
.getCover() == null) {
222 meta
.setCover(cover
);
224 meta
.setCover(InfoReader
.getCoverByName(
225 getSourceFileOriginal().toURI().toURL()));
241 protected void close() {
242 if (tmpDir
!= null) {
243 IOUtils
.deltree(tmpDir
);
251 protected String
getDataPrefix() {
255 protected boolean requireInfo() {
259 protected boolean getCover() {
263 protected boolean isImagesDocumentByDefault() {