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