Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / output / Cbz.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.output;
2
3d247bc3 3import java.io.BufferedWriter;
08fe2e33 4import java.io.File;
3d247bc3 5import java.io.FileOutputStream;
08fe2e33 6import java.io.IOException;
3d247bc3 7import java.io.OutputStreamWriter;
08fe2e33 8
2aac79c7 9import be.nikiroo.fanfix.Instance;
08fe2e33
NR
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Paragraph;
12import be.nikiroo.fanfix.data.Story;
13import be.nikiroo.utils.IOUtils;
14
15class 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;
10d558d2 22 targetName += getDefaultExtension(false);
08fe2e33
NR
23
24 File target = new File(targetDir, targetName);
25
d66deb8d 26 dir = Instance.getInstance().getTempFiles().createTempDir("fanfic-reader-cbz-dir");
2a25f781 27 try {
925298fd
NR
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);
08fe2e33 34
276f95c6
NR
35 try {
36 super.process(story, targetDir, targetNameOrig);
37 } finally {
38 }
39
2a25f781
NR
40 InfoCover.writeInfo(dir, targetNameOrig, story.getMeta());
41 if (story.getMeta() != null && !story.getMeta().isFakeCover()) {
42 InfoCover.writeCover(dir, targetNameOrig, story.getMeta());
43 }
a9eb3f46 44
2a25f781 45 IOUtils.writeSmallFile(dir, "version", "3.0");
fe999aa4 46
2a25f781 47 IOUtils.zip(dir, target, true);
08fe2e33 48 } finally {
2a25f781 49 IOUtils.deltree(dir);
08fe2e33
NR
50 }
51
08fe2e33
NR
52 return target;
53 }
54
55 @Override
10d558d2 56 public String getDefaultExtension(boolean readerTarget) {
08fe2e33
NR
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
3d247bc3
NR
72 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
73 new FileOutputStream(new File(dir, "URL")), "UTF-8"));
08fe2e33
NR
74 try {
75 if (meta != null) {
9005532f 76 writer.write(meta.getUrl());
08fe2e33 77 }
08fe2e33
NR
78 } finally {
79 writer.close();
80 }
81
3d247bc3
NR
82 writer = new BufferedWriter(new OutputStreamWriter(
83 new FileOutputStream(new File(dir, "SUMMARY")), "UTF-8"));
08fe2e33
NR
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}