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