1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
8 import java
.net
.URLDecoder
;
9 import java
.util
.ArrayList
;
10 import java
.util
.List
;
11 import java
.util
.Map
.Entry
;
12 import java
.util
.zip
.ZipEntry
;
13 import java
.util
.zip
.ZipInputStream
;
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
.MarkableFileInputStream
;
20 import be
.nikiroo
.utils
.Progress
;
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 protected MetaData meta
;
35 private URL fakeSource
;
36 private InputStream fakeIn
;
39 public String
getSourceName() {
44 protected boolean supports(URL url
) {
45 if (url
.getPath().toLowerCase().endsWith(".epub")) {
53 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
58 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
65 return super.getDesc(fakeSource
, fakeIn
);
72 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
73 Progress pg
) throws IOException
{
76 return super.getChapters(fakeSource
, fakeIn
, pg
);
83 protected String
getChapterContent(URL source
, InputStream in
, int number
,
84 Progress pg
) throws IOException
{
87 return super.getChapterContent(fakeSource
, fakeIn
, number
, pg
);
94 protected void preprocess(URL source
, InputStream in
) throws IOException
{
95 // Note: do NOT close this stream, as it would also close "in"
96 ZipInputStream zipIn
= new ZipInputStream(in
);
97 tmpDir
= Instance
.getTempFiles().createTempDir("fanfic-reader-parser");
98 tmp
= new File(tmpDir
, "file.txt");
99 File tmpInfo
= new File(tmpDir
, "file.info");
100 fakeSource
= tmp
.toURI().toURL();
103 String url
= source
.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
: 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(getDataPrefix() + "SUMMARY")) {
143 String
[] descArray
= StringUtils
144 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
147 if (descArray
.length
> 1) {
148 title
= descArray
[0].trim();
150 if (descArray
.length
> 2
151 && descArray
[1].startsWith("©")) {
152 author
= descArray
[1].substring(1).trim();
157 for (int i
= skip
; i
< descArray
.length
; i
++) {
158 this.desc
+= descArray
[i
].trim() + "\n";
161 this.desc
= this.desc
.trim();
163 // Hopefully the data file
164 IOUtils
.write(zipIn
, tmp
);
169 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
170 throw new IOException(
171 "file not supported (maybe not created with this program or corrupt)");
175 this.fakeIn
= new MarkableFileInputStream(new FileInputStream(tmp
));
178 if (tmpInfo
.exists()) {
179 meta
= InfoReader
.readMeta(tmpInfo
, true);
181 meta
.setCover(cover
);
185 if (title
== null || title
.isEmpty()) {
186 title
= new File(source
.getPath()).getName();
187 if (title
.toLowerCase().endsWith(".cbz")) {
188 title
= title
.substring(0, title
.length() - 4);
190 title
= URLDecoder
.decode(title
, "UTF-8").trim();
193 meta
= new MetaData();
195 meta
.setTags(new ArrayList
<String
>());
196 meta
.setSource(getSourceName());
199 meta
.setTitle(title
);
200 meta
.setAuthor(author
);
201 meta
.setImageDocument(isImagesDocumentByDefault());
206 protected void close() {
207 if (tmpDir
!= null) {
208 IOUtils
.deltree(tmpDir
);
214 if (fakeIn
!= null) {
217 } catch (Exception e
) {
218 Instance
.getTraceHandler().error(e
);
225 protected String
getDataPrefix() {
229 protected boolean requireInfo() {
233 protected boolean getCover() {
237 protected boolean isImagesDocumentByDefault() {