Fix e621 getAuthor() which could error out
[fanfix.git] / src / be / nikiroo / fanfix / output / InfoCover.java
1 package be.nikiroo.fanfix.output;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6
7 import javax.imageio.ImageIO;
8
9 import be.nikiroo.fanfix.Instance;
10 import be.nikiroo.fanfix.bundles.Config;
11 import be.nikiroo.fanfix.data.MetaData;
12
13 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 FileWriter infoWriter = new FileWriter(info);
18
19 if (meta != null) {
20 try {
21 String tags = "";
22 if (meta.getTags() != null) {
23 for (String tag : meta.getTags()) {
24 if (!tags.isEmpty()) {
25 tags += ", ";
26 }
27 tags += tag;
28 }
29 }
30
31 writeMeta(infoWriter, "TITLE", meta.getTitle());
32 writeMeta(infoWriter, "AUTHOR", meta.getAuthor());
33 writeMeta(infoWriter, "DATE", meta.getDate());
34 writeMeta(infoWriter, "SUBJECT", meta.getSubject());
35 writeMeta(infoWriter, "SOURCE", meta.getSource());
36 writeMeta(infoWriter, "URL", meta.getUrl());
37 writeMeta(infoWriter, "TAGS", tags);
38 writeMeta(infoWriter, "UUID", meta.getUuid());
39 writeMeta(infoWriter, "LUID", meta.getLuid());
40 writeMeta(infoWriter, "LANG", meta.getLang() == null ? ""
41 : meta.getLang().toLowerCase());
42 writeMeta(infoWriter, "IMAGES_DOCUMENT",
43 meta.isImageDocument() ? "true" : "false");
44 writeMeta(infoWriter, "TYPE", meta.getType());
45 if (meta.getCover() != null) {
46 String format = Instance.getConfig()
47 .getString(Config.IMAGE_FORMAT_COVER).toLowerCase();
48 writeMeta(infoWriter, "COVER", targetName + "." + format);
49 } else {
50 writeMeta(infoWriter, "COVER", "");
51 }
52 writeMeta(infoWriter, "EPUBCREATOR", BasicOutput.EPUB_CREATOR);
53 writeMeta(infoWriter, "PUBLISHER", meta.getPublisher());
54 } finally {
55 infoWriter.close();
56 }
57 }
58 }
59
60 public static void writeCover(File targetDir, String targetName,
61 MetaData meta) {
62 if (meta != null && meta.getCover() != null) {
63 try {
64 String format = Instance.getConfig()
65 .getString(Config.IMAGE_FORMAT_COVER).toLowerCase();
66 ImageIO.write(meta.getCover(), format, new File(targetDir,
67 targetName + "." + format));
68 } catch (IOException e) {
69 // Allow to continue without cover
70 Instance.syserr(new IOException(
71 "Failed to save the cover image", e));
72 }
73 }
74 }
75
76 private static void writeMeta(FileWriter writer, String key, String value)
77 throws IOException {
78 if (value == null) {
79 value = "";
80 }
81
82 writer.write(String.format("%s=\"%s\"\n", key, value.replace("\"", "'")));
83 }
84 }