Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
08fe2e33 | 3 | import java.io.File; |
08fe2e33 NR |
4 | import java.io.IOException; |
5 | import java.io.InputStream; | |
7445f856 | 6 | import java.net.URISyntaxException; |
08fe2e33 | 7 | import java.net.URL; |
778d8d85 | 8 | import java.net.URLDecoder; |
68686a37 | 9 | import java.util.ArrayList; |
08fe2e33 NR |
10 | import java.util.zip.ZipEntry; |
11 | import java.util.zip.ZipInputStream; | |
12 | ||
7445f856 NR |
13 | import org.jsoup.nodes.Document; |
14 | ||
08fe2e33 | 15 | import be.nikiroo.fanfix.Instance; |
68686a37 | 16 | import be.nikiroo.fanfix.data.MetaData; |
08fe2e33 | 17 | import be.nikiroo.utils.IOUtils; |
16a81ef7 | 18 | import be.nikiroo.utils.Image; |
b7afbe42 | 19 | import be.nikiroo.utils.StringUtils; |
8d59ce07 | 20 | import be.nikiroo.utils.streams.MarkableFileInputStream; |
08fe2e33 NR |
21 | |
22 | /** | |
23 | * Support class for EPUB files created with this program (as we need some | |
24 | * metadata available in those we create). | |
25 | * | |
26 | * @author niki | |
27 | */ | |
68686a37 | 28 | class Epub extends InfoText { |
7445f856 | 29 | private MetaData meta; |
2aac79c7 | 30 | private File tmpDir; |
b7afbe42 | 31 | private String desc; |
08fe2e33 | 32 | |
68686a37 NR |
33 | private URL fakeSource; |
34 | private InputStream fakeIn; | |
08fe2e33 | 35 | |
7445f856 | 36 | public File getSourceFileOriginal() { |
298d405a | 37 | return super.getSourceFile(); |
08fe2e33 NR |
38 | } |
39 | ||
40 | @Override | |
7445f856 NR |
41 | protected File getSourceFile() { |
42 | try { | |
43 | return new File(fakeSource.toURI()); | |
44 | } catch (URISyntaxException e) { | |
45 | Instance.getTraceHandler() | |
46 | .error(new IOException( | |
47 | "Cannot get the source file from the info-text URL", | |
48 | e)); | |
08fe2e33 NR |
49 | } |
50 | ||
51 | return null; | |
52 | } | |
53 | ||
54 | @Override | |
7445f856 | 55 | protected InputStream getInput() { |
298d405a NR |
56 | if (fakeIn != null) { |
57 | try { | |
58 | fakeIn.reset(); | |
59 | } catch (IOException e) { | |
60 | Instance.getTraceHandler() | |
61 | .error(new IOException( | |
62 | "Cannot reset the Epub Text stream", e)); | |
63 | } | |
64 | ||
65 | return fakeIn; | |
66 | } | |
67 | ||
68 | return null; | |
08fe2e33 NR |
69 | } |
70 | ||
71 | @Override | |
7445f856 NR |
72 | protected boolean supports(URL url) { |
73 | return url.getPath().toLowerCase().endsWith(".epub"); | |
74 | } | |
08fe2e33 | 75 | |
7445f856 NR |
76 | @Override |
77 | protected MetaData getMeta() throws IOException { | |
78 | return meta; | |
08fe2e33 NR |
79 | } |
80 | ||
81 | @Override | |
7445f856 NR |
82 | protected Document loadDocument(URL source) throws IOException { |
83 | super.loadDocument(source); // prepares super.getSourceFile() and | |
84 | // super.getInput() | |
85 | ||
86 | InputStream in = super.getInput(); | |
87 | ZipInputStream zipIn = null; | |
88 | try { | |
89 | zipIn = new ZipInputStream(in); | |
90 | tmpDir = Instance.getTempFiles().createTempDir( | |
91 | "fanfic-reader-parser"); | |
92 | File tmp = new File(tmpDir, "file.txt"); | |
93 | File tmpInfo = new File(tmpDir, "file.info"); | |
94 | ||
95 | fakeSource = tmp.toURI().toURL(); | |
96 | Image cover = null; | |
97 | ||
98 | String url; | |
99 | try { | |
100 | url = getSource().toURI().toURL().toString(); | |
101 | } catch (URISyntaxException e1) { | |
102 | url = getSource().toString(); | |
103 | } | |
104 | String title = null; | |
105 | String author = null; | |
106 | ||
107 | for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn | |
108 | .getNextEntry()) { | |
109 | if (!entry.isDirectory() | |
110 | && entry.getName().startsWith(getDataPrefix())) { | |
111 | String entryLName = entry.getName().toLowerCase(); | |
112 | ||
113 | boolean imageEntry = false; | |
8d59ce07 | 114 | for (String ext : bsImages.getImageExt(false)) { |
7445f856 NR |
115 | if (entryLName.endsWith(ext)) { |
116 | imageEntry = true; | |
117 | } | |
08fe2e33 | 118 | } |
08fe2e33 | 119 | |
7445f856 NR |
120 | if (entry.getName().equals(getDataPrefix() + "version")) { |
121 | // Nothing to do for now ("first" | |
122 | // version is 3.0) | |
123 | } else if (entryLName.endsWith(".info")) { | |
124 | // Info file | |
125 | IOUtils.write(zipIn, tmpInfo); | |
126 | } else if (imageEntry) { | |
127 | // Cover | |
128 | if (getCover()) { | |
129 | try { | |
130 | cover = new Image(zipIn); | |
131 | } catch (Exception e) { | |
132 | Instance.getTraceHandler().error(e); | |
133 | } | |
08fe2e33 | 134 | } |
7445f856 NR |
135 | } else if (entry.getName().equals(getDataPrefix() + "URL")) { |
136 | String[] descArray = StringUtils | |
137 | .unhtml(IOUtils.readSmallStream(zipIn)).trim() | |
138 | .split("\n"); | |
139 | if (descArray.length > 0) { | |
140 | url = descArray[0].trim(); | |
141 | } | |
142 | } else if (entry.getName().equals( | |
143 | getDataPrefix() + "SUMMARY")) { | |
144 | String[] descArray = StringUtils | |
145 | .unhtml(IOUtils.readSmallStream(zipIn)).trim() | |
146 | .split("\n"); | |
147 | int skip = 0; | |
148 | if (descArray.length > 1) { | |
149 | title = descArray[0].trim(); | |
150 | skip = 1; | |
151 | if (descArray.length > 2 | |
152 | && descArray[1].startsWith("©")) { | |
153 | author = descArray[1].substring(1).trim(); | |
154 | skip = 2; | |
155 | } | |
156 | } | |
157 | this.desc = ""; | |
158 | for (int i = skip; i < descArray.length; i++) { | |
159 | this.desc += descArray[i].trim() + "\n"; | |
b7afbe42 | 160 | } |
b7afbe42 | 161 | |
7445f856 NR |
162 | this.desc = this.desc.trim(); |
163 | } else { | |
164 | // Hopefully the data file | |
165 | IOUtils.write(zipIn, tmp); | |
166 | } | |
08fe2e33 NR |
167 | } |
168 | } | |
08fe2e33 | 169 | |
7445f856 NR |
170 | if (requireInfo() && (!tmp.exists() || !tmpInfo.exists())) { |
171 | throw new IOException( | |
172 | "file not supported (maybe not created with this program or corrupt)"); | |
173 | } | |
68686a37 | 174 | |
7445f856 | 175 | if (tmp.exists()) { |
67837328 | 176 | this.fakeIn = new MarkableFileInputStream(tmp); |
68686a37 | 177 | } |
7445f856 NR |
178 | |
179 | if (tmpInfo.exists()) { | |
180 | meta = InfoReader.readMeta(tmpInfo, true); | |
7445f856 NR |
181 | tmpInfo.delete(); |
182 | } else { | |
183 | if (title == null || title.isEmpty()) { | |
184 | title = getSourceFileOriginal().getName(); | |
185 | if (title.toLowerCase().endsWith(".cbz")) { | |
186 | title = title.substring(0, title.length() - 4); | |
187 | } | |
188 | title = URLDecoder.decode(title, "UTF-8").trim(); | |
778d8d85 | 189 | } |
778d8d85 | 190 | |
7445f856 NR |
191 | meta = new MetaData(); |
192 | meta.setLang("en"); | |
193 | meta.setTags(new ArrayList<String>()); | |
727108fe | 194 | meta.setSource(getType().getSourceName()); |
7445f856 NR |
195 | meta.setUuid(url); |
196 | meta.setUrl(url); | |
197 | meta.setTitle(title); | |
198 | meta.setAuthor(author); | |
199 | meta.setImageDocument(isImagesDocumentByDefault()); | |
200 | } | |
bb7021f2 NR |
201 | |
202 | if (meta.getCover() == null) { | |
203 | if (cover != null) { | |
204 | meta.setCover(cover); | |
205 | } else { | |
bb7021f2 NR |
206 | meta.setCover(InfoReader |
207 | .getCoverByName(getSourceFileOriginal().toURI() | |
208 | .toURL())); | |
209 | } | |
210 | } | |
7445f856 NR |
211 | } finally { |
212 | if (zipIn != null) { | |
213 | zipIn.close(); | |
214 | } | |
215 | if (in != null) { | |
216 | in.close(); | |
217 | } | |
08fe2e33 | 218 | } |
7445f856 NR |
219 | |
220 | return null; | |
08fe2e33 NR |
221 | } |
222 | ||
223 | @Override | |
0ffa4754 | 224 | protected void close() { |
2aac79c7 NR |
225 | if (tmpDir != null) { |
226 | IOUtils.deltree(tmpDir); | |
08fe2e33 NR |
227 | } |
228 | ||
2aac79c7 | 229 | tmpDir = null; |
dea63313 | 230 | |
68686a37 | 231 | super.close(); |
08fe2e33 NR |
232 | } |
233 | ||
234 | protected String getDataPrefix() { | |
235 | return "DATA/"; | |
236 | } | |
237 | ||
238 | protected boolean requireInfo() { | |
239 | return true; | |
240 | } | |
241 | ||
242 | protected boolean getCover() { | |
243 | return true; | |
244 | } | |
e4fa48a0 NR |
245 | |
246 | protected boolean isImagesDocumentByDefault() { | |
247 | return false; | |
248 | } | |
08fe2e33 | 249 | } |