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
;
34 private URL fakeSource
;
35 private InputStream fakeIn
;
38 public String
getSourceName() {
43 protected boolean supports(URL url
) {
44 if (url
.getPath().toLowerCase().endsWith(".epub")) {
52 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
57 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
64 return super.getDesc(fakeSource
, fakeIn
);
71 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
72 Progress pg
) throws IOException
{
75 return super.getChapters(fakeSource
, fakeIn
, pg
);
82 protected String
getChapterContent(URL source
, InputStream in
, int number
,
83 Progress pg
) throws IOException
{
86 return super.getChapterContent(fakeSource
, fakeIn
, number
, pg
);
93 protected void preprocess(URL source
, InputStream in
) throws IOException
{
94 // Note: do NOT close this stream, as it would also close "in"
95 ZipInputStream zipIn
= new ZipInputStream(in
);
96 tmp
= File
.createTempFile("fanfic-reader-parser_", ".tmp");
97 File tmpInfo
= new File(tmp
+ ".info");
98 fakeSource
= tmp
.toURI().toURL();
101 String url
= source
.toString();
103 String author
= null;
105 for (ZipEntry entry
= zipIn
.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
: 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
.getTraceHandler().error(e
);
133 } else if (entry
.getName().equals(getDataPrefix() + "URL")) {
134 String
[] descArray
= StringUtils
135 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
137 if (descArray
.length
> 0) {
138 url
= descArray
[0].trim();
140 } else if (entry
.getName().equals(getDataPrefix() + "SUMMARY")) {
141 String
[] descArray
= StringUtils
142 .unhtml(IOUtils
.readSmallStream(zipIn
)).trim()
145 if (descArray
.length
> 1) {
146 title
= descArray
[0].trim();
148 if (descArray
.length
> 2
149 && descArray
[1].startsWith("©")) {
150 author
= descArray
[1].substring(1).trim();
155 for (int i
= skip
; i
< descArray
.length
; i
++) {
156 this.desc
+= descArray
[i
].trim() + "\n";
159 this.desc
= this.desc
.trim();
161 // Hopefully the data file
162 IOUtils
.write(zipIn
, tmp
);
167 if (requireInfo() && (!tmp
.exists() || !tmpInfo
.exists())) {
168 throw new IOException(
169 "file not supported (maybe not created with this program or corrupt)");
173 this.fakeIn
= new MarkableFileInputStream(new FileInputStream(tmp
));
176 if (tmpInfo
.exists()) {
177 meta
= InfoReader
.readMeta(tmpInfo
, true);
179 meta
.setCover(cover
);
183 if (title
== null || title
.isEmpty()) {
184 title
= new File(source
.getPath()).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
);
203 protected void close() throws IOException
{
204 if (tmp
!= null && tmp
.exists()) {
212 if (fakeIn
!= null) {
219 protected String
getDataPrefix() {
223 protected boolean requireInfo() {
227 protected boolean getCover() {