remote: fix 30s timeout bug
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / BasicSupportHelper.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.InputStream;
6import java.net.MalformedURLException;
7import java.net.URL;
8
9import be.nikiroo.fanfix.Instance;
10import be.nikiroo.fanfix.bundles.Config;
11import 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 */
19class BasicSupportHelper {
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 */
28 public static Image getDefaultCover(String subject) {
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 */
51 public static String[] getImageExt(boolean emptyAllowed) {
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
64 * the linked {@link BasicSupport}
65 * @param source
66 * the story source
67 * @param line
68 * the resource to check
69 *
70 * @return the image if found, or NULL
71 *
72 */
73 public static Image getImage(BasicSupport support, URL source, String line) {
74 URL url = getImageUrl(support, source, line);
75 if (url != null) {
76 if ("file".equals(url.getProtocol())) {
77 if (new File(url.getPath()).isDirectory()) {
78 return null;
79 }
80 }
81 InputStream in = null;
82 try {
83 in = Instance.getCache().open(url, support, true);
84 return new Image(in);
85 } catch (IOException e) {
86 } finally {
87 if (in != null) {
88 try {
89 in.close();
90 } catch (IOException e) {
91 }
92 }
93 }
94 }
95
96 return null;
97 }
98
99 /**
100 * Check if the given resource can be a local image or a remote image, then
101 * refresh the cache with it if it is.
102 *
103 * @param support
104 * the linked {@link BasicSupport}
105 * @param source
106 * the story source
107 * @param line
108 * the resource to check
109 *
110 * @return the image URL if found, or NULL
111 *
112 */
113 public static URL getImageUrl(BasicSupport support, URL source, String line) {
114 URL url = null;
115
116 if (line != null) {
117 // try for files
118 if (source != null) {
119 try {
120
121 String relPath = null;
122 String absPath = null;
123 try {
124 String path = new File(source.getFile()).getParent();
125 relPath = new File(new File(path), line.trim())
126 .getAbsolutePath();
127 } catch (Exception e) {
128 // Cannot be converted to path (one possibility to take
129 // into account: absolute path on Windows)
130 }
131 try {
132 absPath = new File(line.trim()).getAbsolutePath();
133 } catch (Exception e) {
134 // Cannot be converted to path (at all)
135 }
136
137 for (String ext : getImageExt(true)) {
138 File absFile = new File(absPath + ext);
139 File relFile = new File(relPath + ext);
140 if (absPath != null && absFile.exists()
141 && absFile.isFile()) {
142 url = absFile.toURI().toURL();
143 } else if (relPath != null && relFile.exists()
144 && relFile.isFile()) {
145 url = relFile.toURI().toURL();
146 }
147 }
148 } catch (Exception e) {
149 // Should not happen since we control the correct arguments
150 }
151 }
152
153 if (url == null) {
154 // try for URLs
155 try {
156 for (String ext : getImageExt(true)) {
157 if (Instance.getCache()
158 .check(new URL(line + ext), true)) {
159 url = new URL(line + ext);
160 break;
161 }
162 }
163
164 // try out of cache
165 if (url == null) {
166 for (String ext : getImageExt(true)) {
167 try {
168 url = new URL(line + ext);
169 Instance.getCache().refresh(url, support, true);
170 break;
171 } catch (IOException e) {
172 // no image with this ext
173 url = null;
174 }
175 }
176 }
177 } catch (MalformedURLException e) {
178 // Not an url
179 }
180 }
181
182 // refresh the cached file
183 if (url != null) {
184 try {
185 Instance.getCache().refresh(url, support, true);
186 } catch (IOException e) {
187 // woops, broken image
188 url = null;
189 }
190 }
191 }
192
193 return url;
194 }
195
196 /**
197 * Fix the author name if it is prefixed with some "by" {@link String}.
198 *
199 * @param author
200 * the author with a possible prefix
201 *
202 * @return the author without prefixes
203 */
204 public static String fixAuthor(String author) {
205 if (author != null) {
206 for (String suffix : new String[] { " ", ":" }) {
207 for (String byString : Instance.getConfig()
208 .getString(Config.BYS).split(",")) {
209 byString += suffix;
210 if (author.toUpperCase().startsWith(byString.toUpperCase())) {
211 author = author.substring(byString.length()).trim();
212 }
213 }
214 }
215
216 // Special case (without suffix):
217 if (author.startsWith("©")) {
218 author = author.substring(1);
219 }
220 }
221
222 return author;
223 }
224}