doc
[nikiroo-utils.git] / 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 javax.imageio.ImageIO;
10
11 import be.nikiroo.fanfix.Instance;
12 import be.nikiroo.fanfix.bundles.Config;
13 import be.nikiroo.fanfix.data.MetaData;
14
15 public class InfoCover {
16 public static void writeInfo(File targetDir, String targetName,
17 MetaData meta) throws IOException {
18 File info = new File(targetDir, targetName + ".info");
19 BufferedWriter infoWriter = new BufferedWriter(new OutputStreamWriter(
20 new FileOutputStream(info), "UTF-8"));
21
22 if (meta != null) {
23 try {
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.getConfig()
50 .getString(Config.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 } finally {
63 infoWriter.close();
64 }
65 }
66 }
67
68 public static void writeCover(File targetDir, String targetName,
69 MetaData meta) {
70 if (meta != null && meta.getCover() != null) {
71 try {
72 String format = Instance.getConfig()
73 .getString(Config.IMAGE_FORMAT_COVER).toLowerCase();
74 ImageIO.write(meta.getCover(), format, new File(targetDir,
75 targetName + "." + format));
76 } catch (IOException e) {
77 // Allow to continue without cover
78 Instance.syserr(new IOException(
79 "Failed to save the cover image", e));
80 }
81 }
82 }
83
84 private static void writeMeta(BufferedWriter writer, String key,
85 String value) throws IOException {
86 if (value == null) {
87 value = "";
88 }
89
90 writer.write(String.format("%s=\"%s\"\n", key, value.replace("\"", "'")));
91 }
92 }