Update nikiroo-utils, bugfixes:
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / LocalLibrary.java
1 package be.nikiroo.fanfix.library;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.FileFilter;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import javax.imageio.ImageIO;
15
16 import be.nikiroo.fanfix.Instance;
17 import be.nikiroo.fanfix.bundles.Config;
18 import be.nikiroo.fanfix.data.MetaData;
19 import be.nikiroo.fanfix.data.Story;
20 import be.nikiroo.fanfix.output.BasicOutput;
21 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
22 import be.nikiroo.fanfix.output.InfoCover;
23 import be.nikiroo.fanfix.supported.InfoReader;
24 import be.nikiroo.utils.IOUtils;
25 import be.nikiroo.utils.ImageUtils;
26 import be.nikiroo.utils.MarkableFileInputStream;
27 import be.nikiroo.utils.Progress;
28
29 /**
30 * This {@link BasicLibrary} will store the stories locally on disk.
31 *
32 * @author niki
33 */
34 public class LocalLibrary extends BasicLibrary {
35 private int lastId;
36 private Map<MetaData, File[]> stories; // Files: [ infoFile, TargetFile ]
37 private Map<String, BufferedImage> sourceCovers;
38
39 private File baseDir;
40 private OutputType text;
41 private OutputType image;
42
43 /**
44 * Create a new {@link LocalLibrary} with the given back-end directory.
45 *
46 * @param baseDir
47 * the directory where to find the {@link Story} objects
48 */
49 public LocalLibrary(File baseDir) {
50 this(baseDir, Instance.getConfig().getString(
51 Config.NON_IMAGES_DOCUMENT_TYPE), Instance.getConfig()
52 .getString(Config.IMAGES_DOCUMENT_TYPE), false);
53 }
54
55 /**
56 * Create a new {@link LocalLibrary} with the given back-end directory.
57 *
58 * @param baseDir
59 * the directory where to find the {@link Story} objects
60 * @param text
61 * the {@link OutputType} to use for non-image documents
62 * @param image
63 * the {@link OutputType} to use for image documents
64 * @param defaultIsHtml
65 * if the given text or image is invalid, use HTML by default (if
66 * not, it will be INFO_TEXT/CBZ by default)
67 */
68 public LocalLibrary(File baseDir, String text, String image,
69 boolean defaultIsHtml) {
70 this(baseDir, OutputType.valueOfAllOkUC(text,
71 defaultIsHtml ? OutputType.HTML : OutputType.INFO_TEXT),
72 OutputType.valueOfAllOkUC(image,
73 defaultIsHtml ? OutputType.HTML : OutputType.CBZ));
74 }
75
76 /**
77 * Create a new {@link LocalLibrary} with the given back-end directory.
78 *
79 * @param baseDir
80 * the directory where to find the {@link Story} objects
81 * @param text
82 * the {@link OutputType} to use for non-image documents
83 * @param image
84 * the {@link OutputType} to use for image documents
85 */
86 public LocalLibrary(File baseDir, OutputType text, OutputType image) {
87 this.baseDir = baseDir;
88 this.text = text;
89 this.image = image;
90
91 this.lastId = 0;
92 this.stories = null;
93 this.sourceCovers = new HashMap<String, BufferedImage>();
94
95 baseDir.mkdirs();
96 }
97
98 @Override
99 protected List<MetaData> getMetas(Progress pg) {
100 return new ArrayList<MetaData>(getStories(pg).keySet());
101 }
102
103 @Override
104 public File getFile(String luid, Progress pg) {
105 File[] files = getStories(pg).get(getInfo(luid));
106 if (files != null) {
107 return files[1];
108 }
109
110 return null;
111 }
112
113 @Override
114 public BufferedImage getCover(String luid) {
115 MetaData meta = getInfo(luid);
116 if (meta != null) {
117 File[] files = getStories(null).get(meta);
118 if (files != null) {
119 File infoFile = files[0];
120
121 try {
122 meta = InfoReader.readMeta(infoFile, true);
123 return meta.getCover();
124 } catch (IOException e) {
125 Instance.getTraceHandler().error(e);
126 }
127 }
128 }
129
130 return null;
131 }
132
133 @Override
134 protected void clearCache() {
135 stories = null;
136 sourceCovers = new HashMap<String, BufferedImage>();
137 }
138
139 @Override
140 protected synchronized int getNextId() {
141 getStories(null); // make sure lastId is set
142 return ++lastId;
143 }
144
145 @Override
146 protected void doDelete(String luid) throws IOException {
147 for (File file : getRelatedFiles(luid)) {
148 // TODO: throw an IOException if we cannot delete the files?
149 IOUtils.deltree(file);
150 }
151 }
152
153 @Override
154 protected Story doSave(Story story, Progress pg) throws IOException {
155 MetaData meta = story.getMeta();
156
157 File expectedTarget = getExpectedFile(meta);
158 expectedTarget.getParentFile().mkdirs();
159
160 BasicOutput it = BasicOutput.getOutput(getOutputType(meta), true);
161 it.process(story, expectedTarget.getPath(), pg);
162
163 return story;
164 }
165
166 @Override
167 protected synchronized void saveMeta(MetaData meta, Progress pg)
168 throws IOException {
169 File newDir = getExpectedDir(meta.getSource());
170 if (!newDir.exists()) {
171 newDir.mkdir();
172 }
173
174 List<File> relatedFiles = getRelatedFiles(meta.getLuid());
175 for (File relatedFile : relatedFiles) {
176 // TODO: this is not safe at all.
177 // We should copy all the files THEN delete them
178 // Maybe also adding some rollback cleanup if possible
179 if (relatedFile.getName().endsWith(".info")) {
180 try {
181 String name = relatedFile.getName().replaceFirst(
182 "\\.info$", "");
183 InfoCover.writeInfo(newDir, name, meta);
184 relatedFile.delete();
185 } catch (IOException e) {
186 Instance.getTraceHandler().error(e);
187 }
188 } else {
189 relatedFile.renameTo(new File(newDir, relatedFile.getName()));
190 }
191 }
192
193 clearCache();
194 }
195
196 @Override
197 public BufferedImage getSourceCover(String source) {
198 if (!sourceCovers.containsKey(source)) {
199 sourceCovers.put(source, super.getSourceCover(source));
200 }
201
202 return sourceCovers.get(source);
203 }
204
205 @Override
206 public void setSourceCover(String source, String luid) {
207 sourceCovers.put(source, getCover(luid));
208 File cover = new File(getExpectedDir(source), ".cover.png");
209 try {
210 ImageIO.write(sourceCovers.get(source), "png", cover);
211 } catch (IOException e) {
212 Instance.getTraceHandler().error(e);
213 sourceCovers.remove(source);
214 }
215 }
216
217 @Override
218 public void imprt(BasicLibrary other, String luid, Progress pg)
219 throws IOException {
220 if (pg == null) {
221 pg = new Progress();
222 }
223
224 // Check if we can simply copy the files instead of the whole process
225 if (other instanceof LocalLibrary) {
226 LocalLibrary otherLocalLibrary = (LocalLibrary) other;
227
228 MetaData meta = otherLocalLibrary.getInfo(luid);
229 String expectedType = ""
230 + (meta != null && meta.isImageDocument() ? image : text);
231 if (meta != null && meta.getType().equals(expectedType)) {
232 File from = otherLocalLibrary.getExpectedDir(meta.getSource());
233 File to = this.getExpectedDir(meta.getSource());
234 List<File> sources = otherLocalLibrary.getRelatedFiles(luid);
235 if (!sources.isEmpty()) {
236 pg.setMinMax(0, sources.size());
237 }
238
239 for (File source : sources) {
240 File target = new File(source.getAbsolutePath().replace(
241 from.getAbsolutePath(), to.getAbsolutePath()));
242 if (!source.equals(target)) {
243 target.getParentFile().mkdirs();
244 InputStream in = null;
245 try {
246 in = new FileInputStream(source);
247 IOUtils.write(in, target);
248 } catch (IOException e) {
249 if (in != null) {
250 try {
251 in.close();
252 } catch (Exception ee) {
253 }
254 }
255
256 pg.done();
257 throw e;
258 }
259 }
260
261 pg.add(1);
262 }
263
264 clearCache();
265 pg.done();
266 return;
267 }
268 }
269
270 super.imprt(other, luid, pg);
271
272 clearCache();
273 }
274
275 /**
276 * Return the {@link OutputType} for this {@link Story}.
277 *
278 * @param meta
279 * the {@link Story} {@link MetaData}
280 *
281 * @return the type
282 */
283 private OutputType getOutputType(MetaData meta) {
284 if (meta != null && meta.isImageDocument()) {
285 return image;
286 }
287
288 return text;
289 }
290
291 /**
292 * Get the target {@link File} related to the given <tt>.info</tt>
293 * {@link File} and {@link MetaData}.
294 *
295 * @param meta
296 * the meta
297 * @param infoFile
298 * the <tt>.info</tt> {@link File}
299 *
300 * @return the target {@link File}
301 */
302 private File getTargetFile(MetaData meta, File infoFile) {
303 // Replace .info with whatever is needed:
304 String path = infoFile.getPath();
305 path = path.substring(0, path.length() - ".info".length());
306 String newExt = getOutputType(meta).getDefaultExtension(true);
307
308 return new File(path + newExt);
309 }
310
311 /**
312 * The target (full path) where the {@link Story} related to this
313 * {@link MetaData} should be located on disk for a new {@link Story}.
314 *
315 * @param key
316 * the {@link Story} {@link MetaData}
317 *
318 * @return the target
319 */
320 private File getExpectedFile(MetaData key) {
321 String title = key.getTitle();
322 if (title == null) {
323 title = "";
324 }
325 title = title.replaceAll("[^a-zA-Z0-9._+-]", "_");
326 return new File(getExpectedDir(key.getSource()), key.getLuid() + "_"
327 + title);
328 }
329
330 /**
331 * The directory (full path) where the new {@link Story} related to this
332 * {@link MetaData} should be located on disk.
333 *
334 * @param source
335 * the type (source)
336 *
337 * @return the target directory
338 */
339 private File getExpectedDir(String source) {
340 String sanitizedSource = source.replaceAll("[^a-zA-Z0-9._+-]", "_");
341 return new File(baseDir, sanitizedSource);
342 }
343
344 /**
345 * Return the list of files/directories on disk for this {@link Story}.
346 * <p>
347 * If the {@link Story} is not found, and empty list is returned.
348 *
349 * @param luid
350 * the {@link Story} LUID
351 *
352 * @return the list of {@link File}s
353 *
354 * @throws IOException
355 * if the {@link Story} was not found
356 */
357 private List<File> getRelatedFiles(String luid) throws IOException {
358 List<File> files = new ArrayList<File>();
359
360 MetaData meta = getInfo(luid);
361 if (meta == null) {
362 throw new IOException("Story not found: " + luid);
363 }
364
365 File infoFile = getStories(null).get(meta)[0];
366 File targetFile = getStories(null).get(meta)[1];
367
368 files.add(infoFile);
369 files.add(targetFile);
370
371 String readerExt = getOutputType(meta).getDefaultExtension(true);
372 String fileExt = getOutputType(meta).getDefaultExtension(false);
373
374 String path = targetFile.getAbsolutePath();
375 if (readerExt != null && !readerExt.equals(fileExt)) {
376 path = path.substring(0, path.length() - readerExt.length())
377 + fileExt;
378 File relatedFile = new File(path);
379
380 if (relatedFile.exists()) {
381 files.add(relatedFile);
382 }
383 }
384
385 String coverExt = "."
386 + Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER)
387 .toLowerCase();
388 File coverFile = new File(path + coverExt);
389 if (!coverFile.exists()) {
390 coverFile = new File(path.substring(0,
391 path.length() - fileExt.length())
392 + coverExt);
393 }
394
395 if (coverFile.exists()) {
396 files.add(coverFile);
397 }
398
399 return files;
400 }
401
402 /**
403 * Fill the list of stories by reading the content of the local directory
404 * {@link LocalLibrary#baseDir}.
405 * <p>
406 * Will use a cached list when possible (see
407 * {@link BasicLibrary#clearCache()}).
408 *
409 * @param pg
410 * the optional {@link Progress}
411 *
412 * @return the list of stories
413 */
414 private synchronized Map<MetaData, File[]> getStories(Progress pg) {
415 if (pg == null) {
416 pg = new Progress();
417 } else {
418 pg.setMinMax(0, 100);
419 }
420
421 if (stories == null) {
422 stories = new HashMap<MetaData, File[]>();
423
424 lastId = 0;
425
426 File[] dirs = baseDir.listFiles(new FileFilter() {
427 @Override
428 public boolean accept(File file) {
429 return file != null && file.isDirectory();
430 }
431 });
432
433 Progress pgDirs = new Progress(0, 100 * dirs.length);
434 pg.addProgress(pgDirs, 100);
435
436 for (File dir : dirs) {
437 File[] infoFiles = dir.listFiles(new FileFilter() {
438 @Override
439 public boolean accept(File file) {
440 return file != null
441 && file.getPath().toLowerCase()
442 .endsWith(".info");
443 }
444 });
445
446 Progress pgFiles = new Progress(0, infoFiles.length);
447 pgDirs.addProgress(pgFiles, 100);
448 pgDirs.setName("Loading from: " + dir.getName());
449
450 String source = null;
451 for (File infoFile : infoFiles) {
452 pgFiles.setName(infoFile.getName());
453 try {
454 MetaData meta = InfoReader.readMeta(infoFile, false);
455 source = meta.getSource();
456 try {
457 int id = Integer.parseInt(meta.getLuid());
458 if (id > lastId) {
459 lastId = id;
460 }
461
462 stories.put(meta, new File[] { infoFile,
463 getTargetFile(meta, infoFile) });
464 } catch (Exception e) {
465 // not normal!!
466 throw new IOException(
467 "Cannot understand the LUID of "
468 + infoFile
469 + ": "
470 + (meta == null ? "[meta is NULL]"
471 : meta.getLuid()), e);
472 }
473 } catch (IOException e) {
474 // We should not have not-supported files in the
475 // library
476 Instance.getTraceHandler().error(
477 new IOException(
478 "Cannot load file from library: "
479 + infoFile, e));
480 }
481 pgFiles.add(1);
482 }
483
484 File cover = new File(dir, ".cover.png");
485 if (cover.exists()) {
486 try {
487 InputStream in = new MarkableFileInputStream(
488 new FileInputStream(cover));
489 try {
490 sourceCovers.put(source, ImageUtils.fromStream(in));
491 } finally {
492 in.close();
493 }
494 } catch (IOException e) {
495 Instance.getTraceHandler().error(e);
496 }
497 }
498
499 pgFiles.setName(null);
500 }
501
502 pgDirs.setName("Loading directories");
503 }
504
505 return stories;
506 }
507
508 /**
509 * Fix the source cover to the given story cover.
510 *
511 * @param source
512 * the source to change
513 * @param coverImage
514 * the cover image
515 */
516 void setSourceCover(String source, BufferedImage coverImage) {
517 sourceCovers.put(source, coverImage);
518 File cover = new File(getExpectedDir(source), ".cover.png");
519 try {
520 ImageIO.write(sourceCovers.get(source), "png", cover);
521 } catch (IOException e) {
522 Instance.getTraceHandler().error(e);
523 sourceCovers.remove(source);
524 }
525 }
526 }