changelog
[nikiroo-utils.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;
ae78e517 12import be.nikiroo.utils.CacheMemory;
f1fb834c 13import be.nikiroo.utils.Downloader;
16a81ef7 14import be.nikiroo.utils.Image;
f1fb834c 15import be.nikiroo.utils.ImageUtils;
ae78e517 16import be.nikiroo.utils.TraceHandler;
f1fb834c
NR
17
18/**
19 * This cache will manage Internet (and local) downloads, as well as put the
20 * downloaded files into a cache.
21 * <p>
22 * As long the cached resource is not too old, it will use it instead of
23 * retrieving the file again.
24 *
25 * @author niki
26 */
27public class DataLoader {
f1fb834c 28 private Downloader downloader;
12443642 29 private Downloader downloaderNoCache;
ae78e517 30 private Cache cache;
f1ce03f7 31 private boolean offline;
f1fb834c
NR
32
33 /**
34 * Create a new {@link DataLoader} object.
35 *
36 * @param dir
37 * the directory to use as cache
38 * @param UA
39 * the User-Agent to use to download the resources
40 * @param hoursChanging
41 * the number of hours after which a cached file that is thought
42 * to change ~often is considered too old (or -1 for
43 * "never too old")
44 * @param hoursStable
45 * the number of hours after which a LARGE cached file that is
46 * thought to change rarely is considered too old (or -1 for
47 * "never too old")
48 *
49 * @throws IOException
50 * in case of I/O error
51 */
52 public DataLoader(File dir, String UA, int hoursChanging, int hoursStable)
53 throws IOException {
12443642
NR
54 downloader = new Downloader(UA, new Cache(dir, hoursChanging,
55 hoursStable));
56 downloaderNoCache = new Downloader(UA);
57
58 cache = downloader.getCache();
ae78e517
NR
59 }
60
61 /**
62 * Create a new {@link DataLoader} object without disk cache (will keep a
63 * memory cache for manual cache operations).
64 *
65 * @param UA
66 * the User-Agent to use to download the resources
67 */
68 public DataLoader(String UA) {
69 downloader = new Downloader(UA);
12443642 70 downloaderNoCache = downloader;
ae78e517
NR
71 cache = new CacheMemory();
72 }
f1ce03f7
NR
73
74 /**
75 * This {@link Downloader} is forbidden to try and connect to the network.
76 * <p>
77 * If TRUE, it will only check the cache (even in no-cache mode!).
78 * <p>
79 * Default is FALSE.
80 *
81 * @return TRUE if offline
82 */
83 public boolean isOffline() {
84 return offline;
85 }
86
87 /**
88 * This {@link Downloader} is forbidden to try and connect to the network.
89 * <p>
90 * If TRUE, it will only check the cache (even in no-cache mode!).
91 * <p>
92 * Default is FALSE.
93 *
94 * @param offline TRUE for offline, FALSE for online
95 */
96 public void setOffline(boolean offline) {
97 this.offline = offline;
98 downloader.setOffline(offline);
99 downloaderNoCache.setOffline(offline);
100
101 // If we don't, we cannot support no-cache using code in OFFLINE mode
102 if (offline) {
103 downloaderNoCache.setCache(cache);
104 } else {
105 downloaderNoCache.setCache(null);
106 }
107 }
ae78e517
NR
108
109 /**
110 * The traces handler for this {@link Cache}.
111 *
112 * @param tracer
113 * the new traces handler
114 */
115 public void setTraceHandler(TraceHandler tracer) {
116 downloader.setTraceHandler(tracer);
12443642 117 downloaderNoCache.setTraceHandler(tracer);
ae78e517 118 cache.setTraceHandler(tracer);
12443642
NR
119 if (downloader.getCache() != null) {
120 downloader.getCache().setTraceHandler(tracer);
ae78e517
NR
121 }
122
f1fb834c
NR
123 }
124
125 /**
126 * Open a resource (will load it from the cache if possible, or save it into
127 * the cache after downloading if not).
12443642
NR
128 * <p>
129 * The cached resource will be assimilated to the given original {@link URL}
f1fb834c
NR
130 *
131 * @param url
132 * the resource to open
133 * @param support
8d59ce07 134 * the support to use to download the resource (can be NULL)
f1fb834c
NR
135 * @param stable
136 * TRUE for more stable resources, FALSE when they often change
137 *
138 * @return the opened resource, NOT NULL
139 *
140 * @throws IOException
141 * in case of I/O error
142 */
143 public InputStream open(URL url, BasicSupport support, boolean stable)
144 throws IOException {
12443642 145 return open(url, url, support, stable, null, null, null);
f1fb834c
NR
146 }
147
148 /**
149 * Open a resource (will load it from the cache if possible, or save it into
150 * the cache after downloading if not).
151 * <p>
152 * The cached resource will be assimilated to the given original {@link URL}
153 *
154 * @param url
155 * the resource to open
12443642
NR
156 * @param originalUrl
157 * the original {@link URL} before any redirection occurs, which
158 * is also used for the cache ID if needed (so we can retrieve
159 * the content with this URL if needed)
f1fb834c
NR
160 * @param support
161 * the support to use to download the resource
162 * @param stable
163 * TRUE for more stable resources, FALSE when they often change
f1fb834c
NR
164 *
165 * @return the opened resource, NOT NULL
166 *
167 * @throws IOException
168 * in case of I/O error
169 */
12443642
NR
170 public InputStream open(URL url, URL originalUrl, BasicSupport support,
171 boolean stable) throws IOException {
172 return open(url, originalUrl, support, stable, null, null, null);
f1fb834c
NR
173 }
174
175 /**
12443642
NR
176 * Open a resource (will load it from the cache if possible, or save it into
177 * the cache after downloading if not).
178 * <p>
179 * The cached resource will be assimilated to the given original {@link URL}
f1fb834c
NR
180 *
181 * @param url
12443642
NR
182 * the resource to open
183 * @param originalUrl
184 * the original {@link URL} before any redirection occurs, which
185 * is also used for the cache ID if needed (so we can retrieve
186 * the content with this URL if needed)
187 * @param support
8d59ce07 188 * the support to use to download the resource (can be NULL)
12443642
NR
189 * @param stable
190 * TRUE for more stable resources, FALSE when they often change
191 * @param postParams
192 * the POST parameters
193 * @param getParams
194 * the GET parameters (priority over POST)
195 * @param oauth
196 * OAuth authorization (aka, "bearer XXXXXXX")
f1fb834c 197 *
12443642 198 * @return the opened resource, NOT NULL
f1fb834c
NR
199 *
200 * @throws IOException
201 * in case of I/O error
202 */
12443642
NR
203 public InputStream open(URL url, URL originalUrl, BasicSupport support,
204 boolean stable, Map<String, String> postParams,
205 Map<String, String> getParams, String oauth) throws IOException {
206
207 Map<String, String> cookiesValues = null;
208 URL currentReferer = url;
209
210 if (support != null) {
211 cookiesValues = support.getCookies();
212 currentReferer = support.getCurrentReferer();
213 // priority: arguments
214 if (oauth == null) {
215 oauth = support.getOAuth();
216 }
217 }
218
219 return downloader.open(url, originalUrl, currentReferer, cookiesValues,
220 postParams, getParams, oauth, stable);
f1fb834c
NR
221 }
222
223 /**
224 * Open the given {@link URL} without using the cache, but still using and
225 * updating the cookies.
226 *
227 * @param url
228 * the {@link URL} to open
229 * @param support
230 * the {@link BasicSupport} used for the cookies
231 * @param postParams
232 * the POST parameters
233 * @param getParams
234 * the GET parameters (priority over POST)
235 * @param oauth
236 * OAuth authorization (aka, "bearer XXXXXXX")
237 *
238 * @return the {@link InputStream} of the opened page
239 *
240 * @throws IOException
241 * in case of I/O error
242 */
243 public InputStream openNoCache(URL url, BasicSupport support,
244 Map<String, String> postParams, Map<String, String> getParams,
245 String oauth) throws IOException {
246
247 Map<String, String> cookiesValues = null;
248 URL currentReferer = url;
249 if (support != null) {
250 cookiesValues = support.getCookies();
251 currentReferer = support.getCurrentReferer();
252 // priority: arguments
253 if (oauth == null) {
254 oauth = support.getOAuth();
255 }
256 }
257
12443642
NR
258 return downloaderNoCache.open(url, currentReferer, cookiesValues,
259 postParams, getParams, oauth);
f1fb834c
NR
260 }
261
262 /**
263 * Refresh the resource into cache if needed.
264 *
265 * @param url
266 * the resource to open
267 * @param support
8d59ce07 268 * the support to use to download the resource (can be NULL)
f1fb834c
NR
269 * @param stable
270 * TRUE for more stable resources, FALSE when they often change
271 *
272 * @throws IOException
273 * in case of I/O error
274 */
275 public void refresh(URL url, BasicSupport support, boolean stable)
276 throws IOException {
c289a297 277 if (!check(url, stable)) {
12443642 278 open(url, url, support, stable, null, null, null).close();
f1fb834c
NR
279 }
280 }
281
282 /**
283 * Check the resource to see if it is in the cache.
284 *
285 * @param url
286 * the resource to check
287 * @param stable
288 * a stable file (that dones't change too often) -- parameter
289 * used to check if the file is too old to keep or not
290 *
291 * @return TRUE if it is
292 *
293 */
294 public boolean check(URL url, boolean stable) {
12443642
NR
295 return downloader.getCache() != null
296 && downloader.getCache().check(url, false, stable);
f1fb834c
NR
297 }
298
299 /**
300 * Save the given resource as an image on disk using the default image
16a81ef7 301 * format for content or cover -- will automatically add the extension, too.
f1fb834c 302 *
16a81ef7 303 * @param img
f1fb834c
NR
304 * the resource
305 * @param target
16a81ef7
NR
306 * the target file without extension
307 * @param cover
308 * use the cover image format instead of the content image format
f1fb834c
NR
309 *
310 * @throws IOException
311 * in case of I/O error
312 */
16a81ef7
NR
313 public void saveAsImage(Image img, File target, boolean cover)
314 throws IOException {
315 String format;
316 if (cover) {
d66deb8d 317 format = Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
16a81ef7 318 } else {
d66deb8d
NR
319 format = Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_CONTENT)
320 .toLowerCase();
f1fb834c 321 }
16a81ef7 322 saveAsImage(img, new File(target.toString() + "." + format), format);
f1fb834c
NR
323 }
324
2a25f781 325 /**
16a81ef7
NR
326 * Save the given resource as an image on disk using the given image format
327 * for content, or with "png" format if it fails.
2a25f781 328 *
16a81ef7 329 * @param img
2a25f781
NR
330 * the resource
331 * @param target
332 * the target file
16a81ef7
NR
333 * @param format
334 * the file format ("png", "jpeg", "bmp"...)
2a25f781
NR
335 *
336 * @throws IOException
337 * in case of I/O error
338 */
16a81ef7 339 public void saveAsImage(Image img, File target, String format)
2a25f781 340 throws IOException {
16a81ef7 341 ImageUtils.getInstance().saveAsImage(img, target, format);
2a25f781
NR
342 }
343
f1fb834c
NR
344 /**
345 * Manually add this item to the cache.
346 *
347 * @param in
348 * the input data
349 * @param uniqueID
350 * a unique ID for this resource
351 *
f1fb834c
NR
352 *
353 * @throws IOException
354 * in case of I/O error
355 */
ae78e517
NR
356 public void addToCache(InputStream in, String uniqueID) throws IOException {
357 cache.save(in, uniqueID);
f1fb834c
NR
358 }
359
360 /**
361 * Return the {@link InputStream} corresponding to the given unique ID, or
362 * NULL if none found.
363 *
364 * @param uniqueID
365 * the unique ID
366 *
367 * @return the content or NULL
368 */
369 public InputStream getFromCache(String uniqueID) {
370 return cache.load(uniqueID, true, true);
371 }
372
085a2f9a
NR
373 /**
374 * Remove the given resource from the cache.
375 *
376 * @param uniqueID
377 * a unique ID used to locate the cached resource
378 *
379 * @return TRUE if it was removed
380 */
381 public boolean removeFromCache(String uniqueID) {
382 return cache.remove(uniqueID);
383 }
384
f1fb834c
NR
385 /**
386 * Clean the cache (delete the cached items).
387 *
388 * @param onlyOld
389 * only clean the files that are considered too old
390 *
391 * @return the number of cleaned items
392 */
393 public int cleanCache(boolean onlyOld) {
394 return cache.clean(onlyOld);
395 }
396}