| 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.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 | } |
| 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.getCache().saveAsImage(meta.getCover(), |
| 75 | new File(targetDir, targetName), true); |
| 76 | } catch (IOException e) { |
| 77 | // Allow to continue without cover |
| 78 | Instance.getTraceHandler().error( |
| 79 | new IOException("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 | } |