| 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.data.MetaData; |
| 11 | import be.nikiroo.fanfix.data.Paragraph; |
| 12 | import be.nikiroo.fanfix.data.Story; |
| 13 | import be.nikiroo.utils.IOUtils; |
| 14 | |
| 15 | class Cbz extends BasicOutput { |
| 16 | private File dir; |
| 17 | |
| 18 | @Override |
| 19 | public File process(Story story, File targetDir, String targetName) |
| 20 | throws IOException { |
| 21 | String targetNameOrig = targetName; |
| 22 | targetName += getDefaultExtension(false); |
| 23 | |
| 24 | File target = new File(targetDir, targetName); |
| 25 | |
| 26 | dir = Instance.getInstance().getTempFiles().createTempDir("fanfic-reader-cbz-dir"); |
| 27 | try { |
| 28 | // will also save the images! (except the cover -> false) |
| 29 | BasicOutput |
| 30 | .getOutput(OutputType.TEXT, isWriteInfo(), isWriteCover()) |
| 31 | // Force cover to FALSE: |
| 32 | .setType(OutputType.TEXT, isWriteInfo(), false) |
| 33 | .process(story, dir, targetNameOrig); |
| 34 | |
| 35 | try { |
| 36 | super.process(story, targetDir, targetNameOrig); |
| 37 | } finally { |
| 38 | } |
| 39 | |
| 40 | InfoCover.writeInfo(dir, targetNameOrig, story.getMeta()); |
| 41 | if (story.getMeta() != null && !story.getMeta().isFakeCover()) { |
| 42 | InfoCover.writeCover(dir, targetNameOrig, story.getMeta()); |
| 43 | } |
| 44 | |
| 45 | IOUtils.writeSmallFile(dir, "version", "3.0"); |
| 46 | |
| 47 | IOUtils.zip(dir, target, true); |
| 48 | } finally { |
| 49 | IOUtils.deltree(dir); |
| 50 | } |
| 51 | |
| 52 | return target; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public String getDefaultExtension(boolean readerTarget) { |
| 57 | return ".cbz"; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void writeStoryHeader(Story story) throws IOException { |
| 62 | MetaData meta = story.getMeta(); |
| 63 | |
| 64 | StringBuilder builder = new StringBuilder(); |
| 65 | if (meta != null && meta.getResume() != null) { |
| 66 | for (Paragraph para : story.getMeta().getResume()) { |
| 67 | builder.append(para.getContent()); |
| 68 | builder.append("\n"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( |
| 73 | new FileOutputStream(new File(dir, "URL")), "UTF-8")); |
| 74 | try { |
| 75 | if (meta != null) { |
| 76 | writer.write(meta.getUrl()); |
| 77 | } |
| 78 | } finally { |
| 79 | writer.close(); |
| 80 | } |
| 81 | |
| 82 | writer = new BufferedWriter(new OutputStreamWriter( |
| 83 | new FileOutputStream(new File(dir, "SUMMARY")), "UTF-8")); |
| 84 | try { |
| 85 | String title = ""; |
| 86 | if (meta != null && meta.getTitle() != null) { |
| 87 | title = meta.getTitle(); |
| 88 | } |
| 89 | |
| 90 | writer.write(title); |
| 91 | if (meta != null && meta.getAuthor() != null) { |
| 92 | writer.write("\n©"); |
| 93 | writer.write(meta.getAuthor()); |
| 94 | } |
| 95 | writer.write("\n\n"); |
| 96 | writer.write(builder.toString()); |
| 97 | } finally { |
| 98 | writer.close(); |
| 99 | } |
| 100 | } |
| 101 | } |