Commit | Line | Data |
---|---|---|
0ffa4754 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | import java.net.MalformedURLException; | |
7 | import java.net.URL; | |
8 | ||
9 | import be.nikiroo.fanfix.Instance; | |
10 | import be.nikiroo.fanfix.bundles.Config; | |
11 | import be.nikiroo.utils.Image; | |
12 | ||
13 | /** | |
14 | * Helper class for {@link BasicSupport}, mostly dedicated to text formating for | |
15 | * the classes that implement {@link BasicSupport}. | |
16 | * | |
17 | * @author niki | |
18 | */ | |
8d59ce07 | 19 | public class BasicSupportHelper { |
0ffa4754 NR |
20 | /** |
21 | * Get the default cover related to this subject (see <tt>.info</tt> files). | |
22 | * | |
23 | * @param subject | |
24 | * the subject | |
25 | * | |
26 | * @return the cover if any, or NULL | |
27 | */ | |
8d59ce07 | 28 | public Image getDefaultCover(String subject) { |
0ffa4754 NR |
29 | if (subject != null && !subject.isEmpty() |
30 | && Instance.getCoverDir() != null) { | |
31 | try { | |
32 | File fileCover = new File(Instance.getCoverDir(), subject); | |
33 | return getImage(null, fileCover.toURI().toURL(), subject); | |
34 | } catch (MalformedURLException e) { | |
35 | } | |
36 | } | |
37 | ||
38 | return null; | |
39 | } | |
40 | ||
41 | /** | |
42 | * Return the list of supported image extensions. | |
43 | * | |
44 | * @param emptyAllowed | |
45 | * TRUE to allow an empty extension on first place, which can be | |
46 | * used when you may already have an extension in your input but | |
47 | * are not sure about it | |
48 | * | |
49 | * @return the extensions | |
50 | */ | |
8d59ce07 | 51 | public String[] getImageExt(boolean emptyAllowed) { |
0ffa4754 NR |
52 | if (emptyAllowed) { |
53 | return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; | |
54 | } | |
55 | ||
56 | return new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; | |
57 | } | |
58 | ||
59 | /** | |
60 | * Check if the given resource can be a local image or a remote image, then | |
61 | * refresh the cache with it if it is. | |
62 | * | |
63 | * @param support | |
8d59ce07 | 64 | * the linked {@link BasicSupport} (can be NULL) |
0ffa4754 | 65 | * @param source |
4642806a NR |
66 | * the source of the story (for image lookup in the same path if |
67 | * the source is a file, can be NULL) | |
0ffa4754 NR |
68 | * @param line |
69 | * the resource to check | |
70 | * | |
71 | * @return the image if found, or NULL | |
72 | * | |
73 | */ | |
8d59ce07 | 74 | public Image getImage(BasicSupport support, URL source, String line) { |
0ffa4754 NR |
75 | URL url = getImageUrl(support, source, line); |
76 | if (url != null) { | |
77 | if ("file".equals(url.getProtocol())) { | |
78 | if (new File(url.getPath()).isDirectory()) { | |
79 | return null; | |
80 | } | |
81 | } | |
82 | InputStream in = null; | |
83 | try { | |
41c3bba7 | 84 | in = Instance.getCache().open(url, support, true); |
0ffa4754 NR |
85 | return new Image(in); |
86 | } catch (IOException e) { | |
87 | } finally { | |
88 | if (in != null) { | |
89 | try { | |
90 | in.close(); | |
91 | } catch (IOException e) { | |
92 | } | |
93 | } | |
94 | } | |
95 | } | |
96 | ||
97 | return null; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Check if the given resource can be a local image or a remote image, then | |
102 | * refresh the cache with it if it is. | |
103 | * | |
104 | * @param support | |
8d59ce07 | 105 | * the linked {@link BasicSupport} (can be NULL) |
0ffa4754 | 106 | * @param source |
4642806a NR |
107 | * the source of the story (for image lookup in the same path if |
108 | * the source is a file, can be NULL) | |
0ffa4754 NR |
109 | * @param line |
110 | * the resource to check | |
111 | * | |
112 | * @return the image URL if found, or NULL | |
113 | * | |
114 | */ | |
8d59ce07 | 115 | public URL getImageUrl(BasicSupport support, URL source, String line) { |
0ffa4754 NR |
116 | URL url = null; |
117 | ||
118 | if (line != null) { | |
119 | // try for files | |
120 | if (source != null) { | |
121 | try { | |
122 | ||
123 | String relPath = null; | |
124 | String absPath = null; | |
125 | try { | |
126 | String path = new File(source.getFile()).getParent(); | |
127 | relPath = new File(new File(path), line.trim()) | |
128 | .getAbsolutePath(); | |
129 | } catch (Exception e) { | |
130 | // Cannot be converted to path (one possibility to take | |
131 | // into account: absolute path on Windows) | |
132 | } | |
133 | try { | |
134 | absPath = new File(line.trim()).getAbsolutePath(); | |
135 | } catch (Exception e) { | |
136 | // Cannot be converted to path (at all) | |
137 | } | |
138 | ||
139 | for (String ext : getImageExt(true)) { | |
140 | File absFile = new File(absPath + ext); | |
141 | File relFile = new File(relPath + ext); | |
142 | if (absPath != null && absFile.exists() | |
143 | && absFile.isFile()) { | |
144 | url = absFile.toURI().toURL(); | |
145 | } else if (relPath != null && relFile.exists() | |
146 | && relFile.isFile()) { | |
147 | url = relFile.toURI().toURL(); | |
148 | } | |
149 | } | |
150 | } catch (Exception e) { | |
151 | // Should not happen since we control the correct arguments | |
152 | } | |
153 | } | |
154 | ||
155 | if (url == null) { | |
156 | // try for URLs | |
157 | try { | |
158 | for (String ext : getImageExt(true)) { | |
159 | if (Instance.getCache() | |
160 | .check(new URL(line + ext), true)) { | |
161 | url = new URL(line + ext); | |
162 | break; | |
163 | } | |
164 | } | |
165 | ||
166 | // try out of cache | |
167 | if (url == null) { | |
168 | for (String ext : getImageExt(true)) { | |
169 | try { | |
170 | url = new URL(line + ext); | |
171 | Instance.getCache().refresh(url, support, true); | |
172 | break; | |
173 | } catch (IOException e) { | |
174 | // no image with this ext | |
175 | url = null; | |
176 | } | |
177 | } | |
178 | } | |
179 | } catch (MalformedURLException e) { | |
180 | // Not an url | |
181 | } | |
182 | } | |
183 | ||
184 | // refresh the cached file | |
185 | if (url != null) { | |
186 | try { | |
187 | Instance.getCache().refresh(url, support, true); | |
188 | } catch (IOException e) { | |
189 | // woops, broken image | |
190 | url = null; | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | return url; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Fix the author name if it is prefixed with some "by" {@link String}. | |
200 | * | |
201 | * @param author | |
202 | * the author with a possible prefix | |
203 | * | |
204 | * @return the author without prefixes | |
205 | */ | |
8d59ce07 | 206 | public String fixAuthor(String author) { |
0ffa4754 NR |
207 | if (author != null) { |
208 | for (String suffix : new String[] { " ", ":" }) { | |
13fdb89a | 209 | for (String byString : Instance.getConfig().getList(Config.CONF_BYS)) { |
0ffa4754 NR |
210 | byString += suffix; |
211 | if (author.toUpperCase().startsWith(byString.toUpperCase())) { | |
212 | author = author.substring(byString.length()).trim(); | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | // Special case (without suffix): | |
218 | if (author.startsWith("©")) { | |
219 | author = author.substring(1); | |
220 | } | |
221 | } | |
222 | ||
223 | return author; | |
224 | } | |
225 | } |