1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
9 import be
.nikiroo
.fanfix
.bundles
.Config
;
10 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
11 import be
.nikiroo
.utils
.Cache
;
12 import be
.nikiroo
.utils
.CacheMemory
;
13 import be
.nikiroo
.utils
.Downloader
;
14 import be
.nikiroo
.utils
.Image
;
15 import be
.nikiroo
.utils
.ImageUtils
;
16 import be
.nikiroo
.utils
.TraceHandler
;
19 * This cache will manage Internet (and local) downloads, as well as put the
20 * downloaded files into a cache.
22 * As long the cached resource is not too old, it will use it instead of
23 * retrieving the file again.
27 public class DataLoader
{
28 private Downloader downloader
;
29 private Downloader downloaderNoCache
;
31 private boolean offline
;
34 * Create a new {@link DataLoader} object.
37 * the directory to use as cache
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
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
50 * in case of I/O error
52 public DataLoader(File dir
, String UA
, int hoursChanging
, int hoursStable
)
54 downloader
= new Downloader(UA
, new Cache(dir
, hoursChanging
,
56 downloaderNoCache
= new Downloader(UA
);
58 cache
= downloader
.getCache();
62 * Create a new {@link DataLoader} object without disk cache (will keep a
63 * memory cache for manual cache operations).
66 * the User-Agent to use to download the resources
68 public DataLoader(String UA
) {
69 downloader
= new Downloader(UA
);
70 downloaderNoCache
= downloader
;
71 cache
= new CacheMemory();
75 * This {@link Downloader} is forbidden to try and connect to the network.
77 * If TRUE, it will only check the cache (even in no-cache mode!).
81 * @return TRUE if offline
83 public boolean isOffline() {
88 * This {@link Downloader} is forbidden to try and connect to the network.
90 * If TRUE, it will only check the cache (even in no-cache mode!).
94 * @param offline TRUE for offline, FALSE for online
96 public void setOffline(boolean offline
) {
97 this.offline
= offline
;
98 downloader
.setOffline(offline
);
99 downloaderNoCache
.setOffline(offline
);
101 // If we don't, we cannot support no-cache using code in OFFLINE mode
103 downloaderNoCache
.setCache(cache
);
105 downloaderNoCache
.setCache(null);
110 * The traces handler for this {@link Cache}.
113 * the new traces handler
115 public void setTraceHandler(TraceHandler tracer
) {
116 downloader
.setTraceHandler(tracer
);
117 downloaderNoCache
.setTraceHandler(tracer
);
118 cache
.setTraceHandler(tracer
);
119 if (downloader
.getCache() != null) {
120 downloader
.getCache().setTraceHandler(tracer
);
126 * Open a resource (will load it from the cache if possible, or save it into
127 * the cache after downloading if not).
129 * The cached resource will be assimilated to the given original {@link URL}
132 * the resource to open
134 * the support to use to download the resource (can be NULL)
136 * TRUE for more stable resources, FALSE when they often change
138 * @return the opened resource, NOT NULL
140 * @throws IOException
141 * in case of I/O error
143 public InputStream
open(URL url
, BasicSupport support
, boolean stable
)
145 return open(url
, url
, support
, stable
, null, null, null);
149 * Open a resource (will load it from the cache if possible, or save it into
150 * the cache after downloading if not).
152 * The cached resource will be assimilated to the given original {@link URL}
155 * the resource to open
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)
161 * the support to use to download the resource
163 * TRUE for more stable resources, FALSE when they often change
165 * @return the opened resource, NOT NULL
167 * @throws IOException
168 * in case of I/O error
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);
176 * Open a resource (will load it from the cache if possible, or save it into
177 * the cache after downloading if not).
179 * The cached resource will be assimilated to the given original {@link URL}
182 * the resource to open
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)
188 * the support to use to download the resource (can be NULL)
190 * TRUE for more stable resources, FALSE when they often change
192 * the POST parameters
194 * the GET parameters (priority over POST)
196 * OAuth authorization (aka, "bearer XXXXXXX")
198 * @return the opened resource, NOT NULL
200 * @throws IOException
201 * in case of I/O error
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
{
207 Map
<String
, String
> cookiesValues
= null;
208 URL currentReferer
= url
;
210 if (support
!= null) {
211 cookiesValues
= support
.getCookies();
212 currentReferer
= support
.getCurrentReferer();
213 // priority: arguments
215 oauth
= support
.getOAuth();
219 return downloader
.open(url
, originalUrl
, currentReferer
, cookiesValues
,
220 postParams
, getParams
, oauth
, stable
);
224 * Open the given {@link URL} without using the cache, but still using and
225 * updating the cookies.
228 * the {@link URL} to open
230 * the {@link BasicSupport} used for the cookies
232 * the POST parameters
234 * the GET parameters (priority over POST)
236 * OAuth authorization (aka, "bearer XXXXXXX")
238 * @return the {@link InputStream} of the opened page
240 * @throws IOException
241 * in case of I/O error
243 public InputStream
openNoCache(URL url
, BasicSupport support
,
244 Map
<String
, String
> postParams
, Map
<String
, String
> getParams
,
245 String oauth
) throws IOException
{
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
254 oauth
= support
.getOAuth();
258 return downloaderNoCache
.open(url
, currentReferer
, cookiesValues
,
259 postParams
, getParams
, oauth
);
263 * Refresh the resource into cache if needed.
266 * the resource to open
268 * the support to use to download the resource (can be NULL)
270 * TRUE for more stable resources, FALSE when they often change
272 * @throws IOException
273 * in case of I/O error
275 public void refresh(URL url
, BasicSupport support
, boolean stable
)
277 if (!check(url
, stable
)) {
278 open(url
, url
, support
, stable
, null, null, null).close();
283 * Check the resource to see if it is in the cache.
286 * the resource to check
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
291 * @return TRUE if it is
294 public boolean check(URL url
, boolean stable
) {
295 return downloader
.getCache() != null
296 && downloader
.getCache().check(url
, false, stable
);
300 * Save the given resource as an image on disk using the default image
301 * format for content or cover -- will automatically add the extension, too.
306 * the target file without extension
308 * use the cover image format instead of the content image format
310 * @throws IOException
311 * in case of I/O error
313 public void saveAsImage(Image img
, File target
, boolean cover
)
317 format
= Instance
.getConfig().getString(Config
.FILE_FORMAT_IMAGE_FORMAT_COVER
)
320 format
= Instance
.getConfig()
321 .getString(Config
.FILE_FORMAT_IMAGE_FORMAT_CONTENT
).toLowerCase();
323 saveAsImage(img
, new File(target
.toString() + "." + format
), format
);
327 * Save the given resource as an image on disk using the given image format
328 * for content, or with "png" format if it fails.
335 * the file format ("png", "jpeg", "bmp"...)
337 * @throws IOException
338 * in case of I/O error
340 public void saveAsImage(Image img
, File target
, String format
)
342 ImageUtils
.getInstance().saveAsImage(img
, target
, format
);
346 * Manually add this item to the cache.
351 * a unique ID for this resource
354 * @throws IOException
355 * in case of I/O error
357 public void addToCache(InputStream in
, String uniqueID
) throws IOException
{
358 cache
.save(in
, uniqueID
);
362 * Return the {@link InputStream} corresponding to the given unique ID, or
363 * NULL if none found.
368 * @return the content or NULL
370 public InputStream
getFromCache(String uniqueID
) {
371 return cache
.load(uniqueID
, true, true);
375 * Remove the given resource from the cache.
378 * a unique ID used to locate the cached resource
380 * @return TRUE if it was removed
382 public boolean removeFromCache(String uniqueID
) {
383 return cache
.remove(uniqueID
);
387 * Clean the cache (delete the cached items).
390 * only clean the files that are considered too old
392 * @return the number of cleaned items
394 public int cleanCache(boolean onlyOld
) {
395 return cache
.clean(onlyOld
);