51cf732a966cbb7b70e3941f7cf2eee00c2a18ff
[fanfix.git] / src / be / nikiroo / fanfix / output / Cbz.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 be.nikiroo.fanfix.data.MetaData;
8 import be.nikiroo.fanfix.data.Paragraph;
9 import be.nikiroo.fanfix.data.Story;
10 import be.nikiroo.utils.IOUtils;
11
12 class Cbz extends BasicOutput {
13 private File dir;
14
15 @Override
16 public File process(Story story, File targetDir, String targetName)
17 throws IOException {
18 String targetNameOrig = targetName;
19 targetName += getDefaultExtension();
20
21 File target = new File(targetDir, targetName);
22
23 dir = File.createTempFile("fanfic-reader-cbz-dir", ".wip");
24 dir.delete();
25 dir.mkdir();
26
27 // will also save the images!
28 new InfoText().process(story, dir, targetNameOrig);
29 IOUtils.writeSmallFile(dir, "version", "3.0");
30
31 try {
32 super.process(story, targetDir, targetNameOrig);
33 } finally {
34 }
35
36 IOUtils.zip(dir, target, true);
37 IOUtils.deltree(dir);
38
39 return target;
40 }
41
42 @Override
43 protected String getDefaultExtension() {
44 return ".cbz";
45 }
46
47 @Override
48 protected void writeStoryHeader(Story story) throws IOException {
49 MetaData meta = story.getMeta();
50
51 StringBuilder builder = new StringBuilder();
52 if (meta != null && meta.getResume() != null) {
53 for (Paragraph para : story.getMeta().getResume()) {
54 builder.append(para.getContent());
55 builder.append("\n");
56 }
57 }
58
59 FileWriter writer = new FileWriter(new File(dir, "URL"));
60 try {
61 if (meta != null) {
62 writer.write(meta.getUuid());
63 }
64 writer.write("\n\n");
65 writer.write(builder.toString());
66 } finally {
67 writer.close();
68 }
69
70 writer = new FileWriter(new File(dir, "SUMMARY"));
71 try {
72 String title = "";
73 if (meta != null && meta.getTitle() != null) {
74 title = meta.getTitle();
75 }
76
77 writer.write(title);
78 if (meta != null && meta.getAuthor() != null) {
79 writer.write("\n©");
80 writer.write(meta.getAuthor());
81 }
82 writer.write("\n\n");
83 writer.write(builder.toString());
84 } finally {
85 writer.close();
86 }
87 }
88 }