Commit | Line | Data |
---|---|---|
7445f856 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.utils.Image; | |
11 | ||
8d59ce07 NR |
12 | /** |
13 | * Helper class for {@link BasicSupport}, mostly dedicated to images for | |
14 | * the classes that implement {@link BasicSupport}. | |
15 | * | |
16 | * @author niki | |
17 | */ | |
7445f856 NR |
18 | public class BasicSupportImages { |
19 | /** | |
20 | * Check if the given resource can be a local image or a remote image, then | |
21 | * refresh the cache with it if it is. | |
22 | * | |
8ac3d099 NR |
23 | * @param support |
24 | * the support to use to download the resource (can be NULL) | |
7445f856 NR |
25 | * @param dir |
26 | * the local directory to search, if any | |
27 | * @param line | |
28 | * the resource to check | |
29 | * | |
30 | * @return the image if found, or NULL | |
7445f856 | 31 | */ |
8d59ce07 | 32 | public Image getImage(BasicSupport support, File dir, String line) { |
7445f856 | 33 | URL url = getImageUrl(support, dir, line); |
8ac3d099 NR |
34 | return getImage(support,url); |
35 | } | |
36 | ||
37 | /** | |
38 | * Check if the given resource can be a local image or a remote image, then | |
39 | * refresh the cache with it if it is. | |
40 | * | |
41 | * @param support | |
42 | * the support to use to download the resource (can be NULL) | |
43 | * @param url | |
44 | * the actual URL to check (file or remote, can be NULL) | |
45 | * | |
46 | * @return the image if found, or NULL | |
47 | */ | |
48 | public Image getImage(BasicSupport support, URL url) { | |
7445f856 NR |
49 | if (url != null) { |
50 | if ("file".equals(url.getProtocol())) { | |
51 | if (new File(url.getPath()).isDirectory()) { | |
52 | return null; | |
53 | } | |
54 | } | |
55 | InputStream in = null; | |
56 | try { | |
d66deb8d | 57 | in = Instance.getInstance().getCache().open(url, support, true); |
002972e9 NR |
58 | Image img = new Image(in); |
59 | if (img.getSize() == 0) { | |
60 | img.close(); | |
61 | throw new IOException( | |
62 | "Empty image not accepted"); | |
63 | } | |
64 | return img; | |
7445f856 NR |
65 | } catch (IOException e) { |
66 | } finally { | |
67 | if (in != null) { | |
68 | try { | |
69 | in.close(); | |
70 | } catch (IOException e) { | |
71 | } | |
72 | } | |
73 | } | |
74 | } | |
75 | ||
76 | return null; | |
77 | } | |
78 | ||
79 | /** | |
80 | * Check if the given resource can be a local image or a remote image, then | |
81 | * refresh the cache with it if it is. | |
82 | * | |
8ac3d099 NR |
83 | * @param support |
84 | * the support to use to download the resource (can be NULL) | |
7445f856 NR |
85 | * @param dir |
86 | * the local directory to search, if any | |
87 | * @param line | |
88 | * the resource to check | |
89 | * | |
90 | * @return the image URL if found, or NULL | |
91 | * | |
92 | */ | |
8d59ce07 | 93 | public URL getImageUrl(BasicSupport support, File dir, String line) { |
7445f856 NR |
94 | URL url = null; |
95 | ||
96 | if (line != null) { | |
97 | // try for files | |
98 | if (dir != null && dir.exists() && !dir.isFile()) { | |
99 | try { | |
100 | ||
101 | String relPath = null; | |
102 | String absPath = null; | |
103 | try { | |
104 | relPath = new File(dir, line.trim()).getAbsolutePath(); | |
105 | } catch (Exception e) { | |
106 | // Cannot be converted to path (one possibility to take | |
107 | // into account: absolute path on Windows) | |
108 | } | |
109 | try { | |
110 | absPath = new File(line.trim()).getAbsolutePath(); | |
111 | } catch (Exception e) { | |
112 | // Cannot be converted to path (at all) | |
113 | } | |
114 | ||
115 | for (String ext : getImageExt(true)) { | |
116 | File absFile = new File(absPath + ext); | |
117 | File relFile = new File(relPath + ext); | |
118 | if (absPath != null && absFile.exists() | |
119 | && absFile.isFile()) { | |
120 | url = absFile.toURI().toURL(); | |
121 | } else if (relPath != null && relFile.exists() | |
122 | && relFile.isFile()) { | |
123 | url = relFile.toURI().toURL(); | |
124 | } | |
125 | } | |
126 | } catch (Exception e) { | |
127 | // Should not happen since we control the correct arguments | |
128 | } | |
129 | } | |
130 | ||
131 | if (url == null) { | |
132 | // try for URLs | |
133 | try { | |
134 | for (String ext : getImageExt(true)) { | |
d66deb8d | 135 | if (Instance.getInstance().getCache() |
7445f856 NR |
136 | .check(new URL(line + ext), true)) { |
137 | url = new URL(line + ext); | |
138 | break; | |
139 | } | |
140 | } | |
141 | ||
142 | // try out of cache | |
143 | if (url == null) { | |
144 | for (String ext : getImageExt(true)) { | |
145 | try { | |
146 | url = new URL(line + ext); | |
d66deb8d | 147 | Instance.getInstance().getCache().refresh(url, support, true); |
7445f856 NR |
148 | break; |
149 | } catch (IOException e) { | |
150 | // no image with this ext | |
151 | url = null; | |
152 | } | |
153 | } | |
154 | } | |
155 | } catch (MalformedURLException e) { | |
156 | // Not an url | |
157 | } | |
158 | } | |
159 | ||
160 | // refresh the cached file | |
161 | if (url != null) { | |
162 | try { | |
d66deb8d | 163 | Instance.getInstance().getCache().refresh(url, support, true); |
7445f856 NR |
164 | } catch (IOException e) { |
165 | // woops, broken image | |
166 | url = null; | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | return url; | |
172 | } | |
173 | ||
174 | /** | |
175 | * Return the list of supported image extensions. | |
176 | * | |
177 | * @param emptyAllowed | |
178 | * TRUE to allow an empty extension on first place, which can be | |
179 | * used when you may already have an extension in your input but | |
180 | * are not sure about it | |
181 | * | |
182 | * @return the extensions | |
183 | */ | |
8d59ce07 | 184 | public String[] getImageExt(boolean emptyAllowed) { |
7445f856 NR |
185 | if (emptyAllowed) { |
186 | return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; | |
187 | } | |
188 | ||
189 | return new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; | |
190 | } | |
191 | } |