Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / output / InfoCover.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.output;
2
3d247bc3 3import java.io.BufferedWriter;
08fe2e33 4import java.io.File;
3d247bc3 5import java.io.FileOutputStream;
08fe2e33 6import java.io.IOException;
3d247bc3 7import java.io.OutputStreamWriter;
08fe2e33 8
08fe2e33
NR
9import be.nikiroo.fanfix.Instance;
10import be.nikiroo.fanfix.bundles.Config;
11import be.nikiroo.fanfix.data.MetaData;
12
70c9b112 13public class InfoCover {
08fe2e33
NR
14 public static void writeInfo(File targetDir, String targetName,
15 MetaData meta) throws IOException {
16 File info = new File(targetDir, targetName + ".info");
08fe2e33 17
9fe3f177
NR
18 BufferedWriter infoWriter = null;
19 try {
20 infoWriter = new BufferedWriter(new OutputStreamWriter(
21 new FileOutputStream(info), "UTF-8"));
22
23 if (meta != null) {
08fe2e33
NR
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
08fe2e33
NR
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());
2206ef66 39 writeMeta(infoWriter, "URL", meta.getUrl());
08fe2e33
NR
40 writeMeta(infoWriter, "TAGS", tags);
41 writeMeta(infoWriter, "UUID", meta.getUuid());
42 writeMeta(infoWriter, "LUID", meta.getLuid());
2206ef66
NR
43 writeMeta(infoWriter, "LANG", meta.getLang() == null ? ""
44 : meta.getLang().toLowerCase());
08fe2e33
NR
45 writeMeta(infoWriter, "IMAGES_DOCUMENT",
46 meta.isImageDocument() ? "true" : "false");
fe999aa4 47 writeMeta(infoWriter, "TYPE", meta.getType());
08fe2e33 48 if (meta.getCover() != null) {
d66deb8d 49 String format = Instance.getInstance().getConfig()
13fdb89a 50 .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
08fe2e33
NR
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());
793f1071
NR
57 writeMeta(infoWriter, "WORDCOUNT",
58 Long.toString(meta.getWords()));
59 writeMeta(infoWriter, "CREATION_DATE", meta.getCreationDate());
a9eb3f46
NR
60 writeMeta(infoWriter, "FAKE_COVER",
61 Boolean.toString(meta.isFakeCover()));
9fe3f177
NR
62 }
63 } finally {
64 if (infoWriter != null) {
08fe2e33
NR
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 {
d66deb8d 74 Instance.getInstance().getCache().saveAsImage(meta.getCover(), new File(targetDir, targetName), true);
08fe2e33
NR
75 } catch (IOException e) {
76 // Allow to continue without cover
d66deb8d 77 Instance.getInstance().getTraceHandler().error(new IOException("Failed to save the cover image", e));
08fe2e33
NR
78 }
79 }
80 }
81
3d247bc3
NR
82 private static void writeMeta(BufferedWriter writer, String key,
83 String value) throws IOException {
08fe2e33
NR
84 if (value == null) {
85 value = "";
86 }
87
88 writer.write(String.format("%s=\"%s\"\n", key, value.replace("\"", "'")));
89 }
90}