Remove or move java.awt dependencies
[fanfix.git] / src / be / nikiroo / fanfix / DataLoader.java
CommitLineData
f1fb834c
NR
1package be.nikiroo.fanfix;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.InputStream;
6import java.net.URL;
7import java.util.Map;
8
f1fb834c
NR
9import be.nikiroo.fanfix.bundles.Config;
10import be.nikiroo.fanfix.supported.BasicSupport;
11import be.nikiroo.utils.Cache;
12import be.nikiroo.utils.Downloader;
16a81ef7 13import be.nikiroo.utils.Image;
f1fb834c
NR
14import be.nikiroo.utils.ImageUtils;
15
16/**
17 * This cache will manage Internet (and local) downloads, as well as put the
18 * downloaded files into a cache.
19 * <p>
20 * As long the cached resource is not too old, it will use it instead of
21 * retrieving the file again.
22 *
23 * @author niki
24 */
25public class DataLoader {
26 private Cache cache;
27 private Downloader downloader;
28
29 /**
30 * Create a new {@link DataLoader} object.
31 *
32 * @param dir
33 * the directory to use as cache
34 * @param UA
35 * the User-Agent to use to download the resources
36 * @param hoursChanging
37 * the number of hours after which a cached file that is thought
38 * to change ~often is considered too old (or -1 for
39 * "never too old")
40 * @param hoursStable
41 * the number of hours after which a LARGE cached file that is
42 * thought to change rarely is considered too old (or -1 for
43 * "never too old")
44 *
45 * @throws IOException
46 * in case of I/O error
47 */
48 public DataLoader(File dir, String UA, int hoursChanging, int hoursStable)
49 throws IOException {
50 cache = new Cache(dir, hoursChanging, hoursStable);
16a81ef7 51 cache.setTraceHandler(Instance.getTraceHandler());
f1fb834c 52 downloader = new Downloader(UA);
16a81ef7 53 downloader.setTraceHandler(Instance.getTraceHandler());
f1fb834c
NR
54 }
55
56 /**
57 * Open a resource (will load it from the cache if possible, or save it into
58 * the cache after downloading if not).
59 *
60 * @param url
61 * the resource to open
62 * @param support
63 * the support to use to download the resource
64 * @param stable
65 * TRUE for more stable resources, FALSE when they often change
66 *
67 * @return the opened resource, NOT NULL
68 *
69 * @throws IOException
70 * in case of I/O error
71 */
72 public InputStream open(URL url, BasicSupport support, boolean stable)
73 throws IOException {
74 // MUST NOT return null
75 return open(url, support, stable, url);
76 }
77
78 /**
79 * Open a resource (will load it from the cache if possible, or save it into
80 * the cache after downloading if not).
81 * <p>
82 * The cached resource will be assimilated to the given original {@link URL}
83 *
84 * @param url
85 * the resource to open
86 * @param support
87 * the support to use to download the resource
88 * @param stable
89 * TRUE for more stable resources, FALSE when they often change
90 * @param originalUrl
91 * the original {@link URL} used to locate the cached resource
92 *
93 * @return the opened resource, NOT NULL
94 *
95 * @throws IOException
96 * in case of I/O error
97 */
98 public InputStream open(URL url, BasicSupport support, boolean stable,
99 URL originalUrl) throws IOException {
100 // MUST NOT return null
101 try {
102 InputStream in = cache.load(originalUrl, false, stable);
62c63b07
NR
103 Instance.getTraceHandler().trace(
104 "Cache " + (in != null ? "hit" : "miss") + ": " + url);
f1fb834c
NR
105
106 if (in == null) {
107 try {
108 in = openNoCache(url, support, null, null, null);
109 cache.save(in, originalUrl);
110 // ..But we want a resetable stream
111 in.close();
112 in = cache.load(originalUrl, false, stable);
113 } catch (IOException e) {
114 throw new IOException("Cannot save the url: "
115 + (url == null ? "null" : url.toString()), e);
116 }
117 }
118
119 return in;
120 } catch (IOException e) {
121 throw new IOException("Cannot open the url: "
122 + (url == null ? "null" : url.toString()), e);
123 }
124 }
125
126 /**
127 * Open the given {@link URL} without using the cache, but still update the
128 * cookies.
129 *
130 * @param url
131 * the {@link URL} to open
132 *
133 * @return the {@link InputStream} of the opened page
134 *
135 * @throws IOException
136 * in case of I/O error
137 */
138 public InputStream openNoCache(URL url) throws IOException {
139 return downloader.open(url);
140 }
141
142 /**
143 * Open the given {@link URL} without using the cache, but still using and
144 * updating the cookies.
145 *
146 * @param url
147 * the {@link URL} to open
148 * @param support
149 * the {@link BasicSupport} used for the cookies
150 * @param postParams
151 * the POST parameters
152 * @param getParams
153 * the GET parameters (priority over POST)
154 * @param oauth
155 * OAuth authorization (aka, "bearer XXXXXXX")
156 *
157 * @return the {@link InputStream} of the opened page
158 *
159 * @throws IOException
160 * in case of I/O error
161 */
162 public InputStream openNoCache(URL url, BasicSupport support,
163 Map<String, String> postParams, Map<String, String> getParams,
164 String oauth) throws IOException {
165
166 Map<String, String> cookiesValues = null;
167 URL currentReferer = url;
168 if (support != null) {
169 cookiesValues = support.getCookies();
170 currentReferer = support.getCurrentReferer();
171 // priority: arguments
172 if (oauth == null) {
173 oauth = support.getOAuth();
174 }
175 }
176
177 return downloader.open(url, currentReferer, cookiesValues, postParams,
178 getParams, oauth);
179 }
180
181 /**
182 * Refresh the resource into cache if needed.
183 *
184 * @param url
185 * the resource to open
186 * @param support
187 * the support to use to download the resource
188 * @param stable
189 * TRUE for more stable resources, FALSE when they often change
190 *
191 * @throws IOException
192 * in case of I/O error
193 */
194 public void refresh(URL url, BasicSupport support, boolean stable)
195 throws IOException {
196 if (!cache.check(url, false, stable)) {
197 open(url, support, stable).close();
198 }
199 }
200
201 /**
202 * Check the resource to see if it is in the cache.
203 *
204 * @param url
205 * the resource to check
206 * @param stable
207 * a stable file (that dones't change too often) -- parameter
208 * used to check if the file is too old to keep or not
209 *
210 * @return TRUE if it is
211 *
212 */
213 public boolean check(URL url, boolean stable) {
214 return cache.check(url, false, stable);
215 }
216
217 /**
218 * Save the given resource as an image on disk using the default image
16a81ef7 219 * format for content or cover -- will automatically add the extension, too.
f1fb834c 220 *
16a81ef7 221 * @param img
f1fb834c
NR
222 * the resource
223 * @param target
16a81ef7
NR
224 * the target file without extension
225 * @param cover
226 * use the cover image format instead of the content image format
f1fb834c
NR
227 *
228 * @throws IOException
229 * in case of I/O error
230 */
16a81ef7
NR
231 public void saveAsImage(Image img, File target, boolean cover)
232 throws IOException {
233 String format;
234 if (cover) {
235 format = Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER)
236 .toLowerCase();
237 } else {
238 format = Instance.getConfig()
239 .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase();
f1fb834c 240 }
16a81ef7 241 saveAsImage(img, new File(target.toString() + "." + format), format);
f1fb834c
NR
242 }
243
2a25f781 244 /**
16a81ef7
NR
245 * Save the given resource as an image on disk using the given image format
246 * for content, or with "png" format if it fails.
2a25f781 247 *
16a81ef7 248 * @param img
2a25f781
NR
249 * the resource
250 * @param target
251 * the target file
16a81ef7
NR
252 * @param format
253 * the file format ("png", "jpeg", "bmp"...)
2a25f781
NR
254 *
255 * @throws IOException
256 * in case of I/O error
257 */
16a81ef7 258 public void saveAsImage(Image img, File target, String format)
2a25f781 259 throws IOException {
16a81ef7 260 ImageUtils.getInstance().saveAsImage(img, target, format);
b9ce9cad 261
2a25f781
NR
262 }
263
f1fb834c
NR
264 /**
265 * Manually add this item to the cache.
266 *
267 * @param in
268 * the input data
269 * @param uniqueID
270 * a unique ID for this resource
271 *
272 * @return the resulting {@link File}
273 *
274 * @throws IOException
275 * in case of I/O error
276 */
277 public File addToCache(InputStream in, String uniqueID) throws IOException {
278 return cache.save(in, uniqueID);
279 }
280
281 /**
282 * Return the {@link InputStream} corresponding to the given unique ID, or
283 * NULL if none found.
284 *
285 * @param uniqueID
286 * the unique ID
287 *
288 * @return the content or NULL
289 */
290 public InputStream getFromCache(String uniqueID) {
291 return cache.load(uniqueID, true, true);
292 }
293
085a2f9a
NR
294 /**
295 * Remove the given resource from the cache.
296 *
297 * @param uniqueID
298 * a unique ID used to locate the cached resource
299 *
300 * @return TRUE if it was removed
301 */
302 public boolean removeFromCache(String uniqueID) {
303 return cache.remove(uniqueID);
304 }
305
f1fb834c
NR
306 /**
307 * Clean the cache (delete the cached items).
308 *
309 * @param onlyOld
310 * only clean the files that are considered too old
311 *
312 * @return the number of cleaned items
313 */
314 public int cleanCache(boolean onlyOld) {
315 return cache.clean(onlyOld);
316 }
317}