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
;
33 * Create a new {@link DataLoader} object.
36 * the directory to use as cache
38 * the User-Agent to use to download the resources
39 * @param hoursChanging
40 * the number of hours after which a cached file that is thought
41 * to change ~often is considered too old (or -1 for
44 * the number of hours after which a LARGE cached file that is
45 * thought to change rarely is considered too old (or -1 for
49 * in case of I/O error
51 public DataLoader(File dir
, String UA
, int hoursChanging
, int hoursStable
)
53 downloader
= new Downloader(UA
, new Cache(dir
, hoursChanging
,
55 downloaderNoCache
= new Downloader(UA
);
57 cache
= downloader
.getCache();
61 * Create a new {@link DataLoader} object without disk cache (will keep a
62 * memory cache for manual cache operations).
65 * the User-Agent to use to download the resources
67 public DataLoader(String UA
) {
68 downloader
= new Downloader(UA
);
69 downloaderNoCache
= downloader
;
70 cache
= new CacheMemory();
74 * The traces handler for this {@link Cache}.
77 * the new traces handler
79 public void setTraceHandler(TraceHandler tracer
) {
80 downloader
.setTraceHandler(tracer
);
81 downloaderNoCache
.setTraceHandler(tracer
);
82 cache
.setTraceHandler(tracer
);
83 if (downloader
.getCache() != null) {
84 downloader
.getCache().setTraceHandler(tracer
);
90 * Open a resource (will load it from the cache if possible, or save it into
91 * the cache after downloading if not).
93 * The cached resource will be assimilated to the given original {@link URL}
96 * the resource to open
98 * the support to use to download the resource (can be NULL)
100 * TRUE for more stable resources, FALSE when they often change
102 * @return the opened resource, NOT NULL
104 * @throws IOException
105 * in case of I/O error
107 public InputStream
open(URL url
, BasicSupport support
, boolean stable
)
109 return open(url
, url
, support
, stable
, null, null, null);
113 * Open a resource (will load it from the cache if possible, or save it into
114 * the cache after downloading if not).
116 * The cached resource will be assimilated to the given original {@link URL}
119 * the resource to open
121 * the original {@link URL} before any redirection occurs, which
122 * is also used for the cache ID if needed (so we can retrieve
123 * the content with this URL if needed)
125 * the support to use to download the resource
127 * TRUE for more stable resources, FALSE when they often change
129 * @return the opened resource, NOT NULL
131 * @throws IOException
132 * in case of I/O error
134 public InputStream
open(URL url
, URL originalUrl
, BasicSupport support
,
135 boolean stable
) throws IOException
{
136 return open(url
, originalUrl
, support
, stable
, null, null, null);
140 * Open a resource (will load it from the cache if possible, or save it into
141 * the cache after downloading if not).
143 * The cached resource will be assimilated to the given original {@link URL}
146 * the resource to open
148 * the original {@link URL} before any redirection occurs, which
149 * is also used for the cache ID if needed (so we can retrieve
150 * the content with this URL if needed)
152 * the support to use to download the resource (can be NULL)
154 * TRUE for more stable resources, FALSE when they often change
156 * the POST parameters
158 * the GET parameters (priority over POST)
160 * OAuth authorization (aka, "bearer XXXXXXX")
162 * @return the opened resource, NOT NULL
164 * @throws IOException
165 * in case of I/O error
167 public InputStream
open(URL url
, URL originalUrl
, BasicSupport support
,
168 boolean stable
, Map
<String
, String
> postParams
,
169 Map
<String
, String
> getParams
, String oauth
) throws IOException
{
171 Map
<String
, String
> cookiesValues
= null;
172 URL currentReferer
= url
;
174 if (support
!= null) {
175 cookiesValues
= support
.getCookies();
176 currentReferer
= support
.getCurrentReferer();
177 // priority: arguments
179 oauth
= support
.getOAuth();
183 return downloader
.open(url
, originalUrl
, currentReferer
, cookiesValues
,
184 postParams
, getParams
, oauth
, stable
);
188 * Open the given {@link URL} without using the cache, but still using and
189 * updating the cookies.
192 * the {@link URL} to open
194 * the {@link BasicSupport} used for the cookies
196 * the POST parameters
198 * the GET parameters (priority over POST)
200 * OAuth authorization (aka, "bearer XXXXXXX")
202 * @return the {@link InputStream} of the opened page
204 * @throws IOException
205 * in case of I/O error
207 public InputStream
openNoCache(URL url
, BasicSupport support
,
208 Map
<String
, String
> postParams
, Map
<String
, String
> getParams
,
209 String oauth
) throws IOException
{
211 Map
<String
, String
> cookiesValues
= null;
212 URL currentReferer
= url
;
213 if (support
!= null) {
214 cookiesValues
= support
.getCookies();
215 currentReferer
= support
.getCurrentReferer();
216 // priority: arguments
218 oauth
= support
.getOAuth();
222 return downloaderNoCache
.open(url
, currentReferer
, cookiesValues
,
223 postParams
, getParams
, oauth
);
227 * Refresh the resource into cache if needed.
230 * the resource to open
232 * the support to use to download the resource (can be NULL)
234 * TRUE for more stable resources, FALSE when they often change
236 * @throws IOException
237 * in case of I/O error
239 public void refresh(URL url
, BasicSupport support
, boolean stable
)
241 if (!check(url
, stable
)) {
242 open(url
, url
, support
, stable
, null, null, null).close();
247 * Check the resource to see if it is in the cache.
250 * the resource to check
252 * a stable file (that dones't change too often) -- parameter
253 * used to check if the file is too old to keep or not
255 * @return TRUE if it is
258 public boolean check(URL url
, boolean stable
) {
259 return downloader
.getCache() != null
260 && downloader
.getCache().check(url
, false, stable
);
264 * Save the given resource as an image on disk using the default image
265 * format for content or cover -- will automatically add the extension, too.
270 * the target file without extension
272 * use the cover image format instead of the content image format
274 * @throws IOException
275 * in case of I/O error
277 public void saveAsImage(Image img
, File target
, boolean cover
)
281 format
= Instance
.getConfig().getString(Config
.FILE_FORMAT_IMAGE_FORMAT_COVER
)
284 format
= Instance
.getConfig()
285 .getString(Config
.FILE_FORMAT_IMAGE_FORMAT_CONTENT
).toLowerCase();
287 saveAsImage(img
, new File(target
.toString() + "." + format
), format
);
291 * Save the given resource as an image on disk using the given image format
292 * for content, or with "png" format if it fails.
299 * the file format ("png", "jpeg", "bmp"...)
301 * @throws IOException
302 * in case of I/O error
304 public void saveAsImage(Image img
, File target
, String format
)
306 ImageUtils
.getInstance().saveAsImage(img
, target
, format
);
310 * Manually add this item to the cache.
315 * a unique ID for this resource
318 * @throws IOException
319 * in case of I/O error
321 public void addToCache(InputStream in
, String uniqueID
) throws IOException
{
322 cache
.save(in
, uniqueID
);
326 * Return the {@link InputStream} corresponding to the given unique ID, or
327 * NULL if none found.
332 * @return the content or NULL
334 public InputStream
getFromCache(String uniqueID
) {
335 return cache
.load(uniqueID
, true, true);
339 * Remove the given resource from the cache.
342 * a unique ID used to locate the cached resource
344 * @return TRUE if it was removed
346 public boolean removeFromCache(String uniqueID
) {
347 return cache
.remove(uniqueID
);
351 * Clean the cache (delete the cached items).
354 * only clean the files that are considered too old
356 * @return the number of cleaned items
358 public int cleanCache(boolean onlyOld
) {
359 return cache
.clean(onlyOld
);