Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
2aac79c7 | 3 | import java.io.File; |
08fe2e33 | 4 | import java.io.IOException; |
08fe2e33 NR |
5 | import java.net.URL; |
6 | import java.util.ArrayList; | |
d3c84ac3 | 7 | import java.util.Collections; |
2a25f781 | 8 | import java.util.HashMap; |
d3c84ac3 | 9 | import java.util.List; |
2a25f781 | 10 | import java.util.Map; |
08fe2e33 NR |
11 | import java.util.zip.ZipEntry; |
12 | import java.util.zip.ZipInputStream; | |
13 | ||
14 | import be.nikiroo.fanfix.Instance; | |
15 | import be.nikiroo.fanfix.data.Chapter; | |
16 | import be.nikiroo.fanfix.data.Paragraph; | |
17 | import be.nikiroo.fanfix.data.Story; | |
2aac79c7 | 18 | import be.nikiroo.utils.IOUtils; |
16a81ef7 | 19 | import be.nikiroo.utils.Image; |
3b2b638f | 20 | import be.nikiroo.utils.Progress; |
08fe2e33 NR |
21 | |
22 | /** | |
23 | * Support class for CBZ files (works better with CBZ created with this program, | |
24 | * as they have some metadata available). | |
25 | * | |
26 | * @author niki | |
27 | */ | |
28 | class Cbz extends Epub { | |
29 | @Override | |
30 | protected boolean supports(URL url) { | |
31 | return url.toString().toLowerCase().endsWith(".cbz"); | |
32 | } | |
33 | ||
34 | @Override | |
35 | public String getSourceName() { | |
36 | return "cbz"; | |
37 | } | |
38 | ||
39 | @Override | |
40 | protected String getDataPrefix() { | |
41 | return ""; | |
42 | } | |
43 | ||
44 | @Override | |
45 | protected boolean requireInfo() { | |
46 | return false; | |
47 | } | |
08fe2e33 NR |
48 | |
49 | @Override | |
e4fa48a0 NR |
50 | protected boolean isImagesDocumentByDefault() { |
51 | return true; | |
08fe2e33 NR |
52 | } |
53 | ||
68686a37 | 54 | @Override |
e4fa48a0 NR |
55 | protected boolean getCover() { |
56 | return false; | |
68686a37 NR |
57 | } |
58 | ||
08fe2e33 | 59 | @Override |
92fb0719 NR |
60 | public Story process(URL url, Progress pg) throws IOException { |
61 | if (pg == null) { | |
62 | pg = new Progress(); | |
63 | } else { | |
64 | pg.setMinMax(0, 100); | |
65 | } | |
66 | ||
ed08c171 NR |
67 | Progress pgMeta = new Progress(); |
68 | pg.addProgress(pgMeta, 10); | |
69 | Story story = processMeta(url, false, true, pgMeta); | |
2a25f781 | 70 | pgMeta.done(); // 10% |
ed08c171 | 71 | |
2aac79c7 NR |
72 | File tmpDir = Instance.getTempFiles().createTempDir("info-text"); |
73 | String basename = null; | |
08fe2e33 | 74 | |
16a81ef7 | 75 | Map<String, Image> images = new HashMap<String, Image>(); |
2aac79c7 NR |
76 | try { |
77 | ZipInputStream zipIn = new ZipInputStream(getInput()); | |
78 | for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn | |
79 | .getNextEntry()) { | |
80 | if (!entry.isDirectory() | |
81 | && entry.getName().startsWith(getDataPrefix())) { | |
82 | String entryLName = entry.getName().toLowerCase(); | |
83 | boolean imageEntry = false; | |
84 | for (String ext : getImageExt(false)) { | |
85 | if (entryLName.endsWith(ext)) { | |
86 | imageEntry = true; | |
87 | } | |
08fe2e33 | 88 | } |
08fe2e33 | 89 | |
2aac79c7 NR |
90 | if (imageEntry) { |
91 | String uuid = meta.getUuid() + "_" + entry.getName(); | |
92 | try { | |
93 | images.put(uuid, new Image(zipIn)); | |
94 | } catch (Exception e) { | |
95 | Instance.getTraceHandler().error(e); | |
96 | } | |
97 | ||
98 | if (pg.getProgress() < 85) { | |
99 | pg.add(1); | |
100 | } | |
101 | } else if (entryLName.endsWith(".info")) { | |
102 | basename = entryLName.substring(0, entryLName.length() | |
103 | - ".info".length()); | |
104 | IOUtils.write(zipIn, new File(tmpDir, entryLName)); | |
105 | } else if (entryLName.endsWith(".txt")) { | |
106 | IOUtils.write(zipIn, new File(tmpDir, entryLName)); | |
08fe2e33 | 107 | } |
2aac79c7 NR |
108 | } |
109 | } | |
2a25f781 | 110 | |
2aac79c7 NR |
111 | pg.setProgress(85); |
112 | ||
113 | // ZIP order is not correct for us | |
114 | List<String> imagesList = new ArrayList<String>(images.keySet()); | |
115 | Collections.sort(imagesList); | |
116 | ||
117 | pg.setProgress(90); | |
118 | ||
119 | File txt = new File(tmpDir, basename + ".txt"); | |
120 | if (!txt.exists()) { | |
121 | basename = null; | |
122 | } | |
123 | if (basename != null) { | |
124 | try { | |
125 | BasicSupport support = BasicSupport.getSupport(txt.toURI() | |
126 | .toURL()); | |
127 | Story origStory = support.process(null); | |
128 | story.setChapters(origStory.getChapters()); | |
129 | story.setMeta(origStory.getMeta()); | |
130 | } catch (Exception e) { | |
131 | basename = null; | |
08fe2e33 NR |
132 | } |
133 | } | |
08fe2e33 | 134 | |
2aac79c7 NR |
135 | if (basename == null) { |
136 | story.setChapters(new ArrayList<Chapter>()); | |
137 | } | |
92fb0719 | 138 | |
2aac79c7 NR |
139 | if (!imagesList.isEmpty()) { |
140 | Chapter chap = new Chapter(story.getChapters().size() + 1, null); | |
141 | story.getChapters().add(chap); | |
2a25f781 | 142 | |
2aac79c7 NR |
143 | for (String uuid : imagesList) { |
144 | try { | |
145 | chap.getParagraphs().add( | |
146 | new Paragraph(images.get(uuid))); | |
147 | } catch (Exception e) { | |
148 | Instance.getTraceHandler().error(e); | |
149 | } | |
150 | } | |
151 | } | |
d3c84ac3 | 152 | |
2aac79c7 NR |
153 | if (meta.getCover() == null && !images.isEmpty()) { |
154 | meta.setCover(images.get(imagesList.get(0))); | |
155 | meta.setFakeCover(true); | |
d3c84ac3 | 156 | } |
d3c84ac3 | 157 | |
2aac79c7 NR |
158 | } finally { |
159 | IOUtils.deltree(tmpDir); | |
a9eb3f46 NR |
160 | } |
161 | ||
92fb0719 | 162 | pg.setProgress(100); |
08fe2e33 NR |
163 | return story; |
164 | } | |
165 | } |