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