Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / output / InfoCover.java
1 package be.nikiroo.fanfix.output;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8
9 import be.nikiroo.fanfix.Instance;
10 import be.nikiroo.fanfix.bundles.Config;
11 import be.nikiroo.fanfix.data.MetaData;
12
13 public class InfoCover {
14 public static void writeInfo(File targetDir, String targetName,
15 MetaData meta) throws IOException {
16 File info = new File(targetDir, targetName + ".info");
17
18 BufferedWriter infoWriter = null;
19 try {
20 infoWriter = new BufferedWriter(new OutputStreamWriter(
21 new FileOutputStream(info), "UTF-8"));
22
23 if (meta != null) {
24 String tags = "";
25 if (meta.getTags() != null) {
26 for (String tag : meta.getTags()) {
27 if (!tags.isEmpty()) {
28 tags += ", ";
29 }
30 tags += tag;
31 }
32 }
33
34 writeMeta(infoWriter, "TITLE", meta.getTitle());
35 writeMeta(infoWriter, "AUTHOR", meta.getAuthor());
36 writeMeta(infoWriter, "DATE", meta.getDate());
37 writeMeta(infoWriter, "SUBJECT", meta.getSubject());
38 writeMeta(infoWriter, "SOURCE", meta.getSource());
39 writeMeta(infoWriter, "URL", meta.getUrl());
40 writeMeta(infoWriter, "TAGS", tags);
41 writeMeta(infoWriter, "UUID", meta.getUuid());
42 writeMeta(infoWriter, "LUID", meta.getLuid());
43 writeMeta(infoWriter, "LANG", meta.getLang() == null ? ""
44 : meta.getLang().toLowerCase());
45 writeMeta(infoWriter, "IMAGES_DOCUMENT",
46 meta.isImageDocument() ? "true" : "false");
47 writeMeta(infoWriter, "TYPE", meta.getType());
48 if (meta.getCover() != null) {
49 String format = Instance.getInstance().getConfig()
50 .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
51 writeMeta(infoWriter, "COVER", targetName + "." + format);
52 } else {
53 writeMeta(infoWriter, "COVER", "");
54 }
55 writeMeta(infoWriter, "EPUBCREATOR", BasicOutput.EPUB_CREATOR);
56 writeMeta(infoWriter, "PUBLISHER", meta.getPublisher());
57 writeMeta(infoWriter, "WORDCOUNT",
58 Long.toString(meta.getWords()));
59 writeMeta(infoWriter, "CREATION_DATE", meta.getCreationDate());
60 writeMeta(infoWriter, "FAKE_COVER",
61 Boolean.toString(meta.isFakeCover()));
62 }
63 } finally {
64 if (infoWriter != null) {
65 infoWriter.close();
66 }
67 }
68 }
69
70 public static void writeCover(File targetDir, String targetName,
71 MetaData meta) {
72 if (meta != null && meta.getCover() != null) {
73 try {
74 Instance.getInstance().getCache().saveAsImage(meta.getCover(), new File(targetDir, targetName), true);
75 } catch (IOException e) {
76 // Allow to continue without cover
77 Instance.getInstance().getTraceHandler().error(new IOException("Failed to save the cover image", e));
78 }
79 }
80 }
81
82 private static void writeMeta(BufferedWriter writer, String key,
83 String value) throws IOException {
84 if (value == null) {
85 value = "";
86 }
87
88 writer.write(String.format("%s=\"%s\"\n", key, value.replace("\"", "'")));
89 }
90 }