222c61463c815e8000b6e491fe0234598071a05b
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
.Downloader
;
13 import be
.nikiroo
.utils
.Image
;
14 import be
.nikiroo
.utils
.ImageUtils
;
17 * This cache will manage Internet (and local) downloads, as well as put the
18 * downloaded files into a cache.
20 * As long the cached resource is not too old, it will use it instead of
21 * retrieving the file again.
25 public class DataLoader
{
27 private Downloader downloader
;
30 * Create a new {@link DataLoader} object.
33 * the directory to use as cache
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
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
46 * in case of I/O error
48 public DataLoader(File dir
, String UA
, int hoursChanging
, int hoursStable
)
50 cache
= new Cache(dir
, hoursChanging
, hoursStable
);
51 cache
.setTraceHandler(Instance
.getTraceHandler());
52 downloader
= new Downloader(UA
);
53 downloader
.setTraceHandler(Instance
.getTraceHandler());
57 * Open a resource (will load it from the cache if possible, or save it into
58 * the cache after downloading if not).
61 * the resource to open
63 * the support to use to download the resource
65 * TRUE for more stable resources, FALSE when they often change
67 * @return the opened resource, NOT NULL
70 * in case of I/O error
72 public InputStream
open(URL url
, BasicSupport support
, boolean stable
)
74 // MUST NOT return null
75 return open(url
, support
, stable
, url
);
79 * Open a resource (will load it from the cache if possible, or save it into
80 * the cache after downloading if not).
82 * The cached resource will be assimilated to the given original {@link URL}
85 * the resource to open
87 * the support to use to download the resource
89 * TRUE for more stable resources, FALSE when they often change
91 * the original {@link URL} used to locate the cached resource
93 * @return the opened resource, NOT NULL
96 * in case of I/O error
98 public InputStream
open(URL url
, BasicSupport support
, boolean stable
,
99 URL originalUrl
) throws IOException
{
100 // MUST NOT return null
102 InputStream in
= cache
.load(originalUrl
, false, stable
);
103 Instance
.getTraceHandler().trace(
104 "Cache " + (in
!= null ?
"hit" : "miss") + ": " + url
);
108 in
= openNoCache(url
, support
, null, null, null);
109 cache
.save(in
, originalUrl
);
110 // ..But we want a resetable stream
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
);
120 } catch (IOException e
) {
121 throw new IOException("Cannot open the url: "
122 + (url
== null ?
"null" : url
.toString()), e
);
127 * Open the given {@link URL} without using the cache, but still update the
131 * the {@link URL} to open
133 * @return the {@link InputStream} of the opened page
135 * @throws IOException
136 * in case of I/O error
138 public InputStream
openNoCache(URL url
) throws IOException
{
139 return downloader
.open(url
);
143 * Open the given {@link URL} without using the cache, but still using and
144 * updating the cookies.
147 * the {@link URL} to open
149 * the {@link BasicSupport} used for the cookies
151 * the POST parameters
153 * the GET parameters (priority over POST)
155 * OAuth authorization (aka, "bearer XXXXXXX")
157 * @return the {@link InputStream} of the opened page
159 * @throws IOException
160 * in case of I/O error
162 public InputStream
openNoCache(URL url
, BasicSupport support
,
163 Map
<String
, String
> postParams
, Map
<String
, String
> getParams
,
164 String oauth
) throws IOException
{
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
173 oauth
= support
.getOAuth();
177 return downloader
.open(url
, currentReferer
, cookiesValues
, postParams
,
182 * Refresh the resource into cache if needed.
185 * the resource to open
187 * the support to use to download the resource
189 * TRUE for more stable resources, FALSE when they often change
191 * @throws IOException
192 * in case of I/O error
194 public void refresh(URL url
, BasicSupport support
, boolean stable
)
196 if (!cache
.check(url
, false, stable
)) {
197 open(url
, support
, stable
).close();
202 * Check the resource to see if it is in the cache.
205 * the resource to check
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
210 * @return TRUE if it is
213 public boolean check(URL url
, boolean stable
) {
214 return cache
.check(url
, false, stable
);
218 * Save the given resource as an image on disk using the default image
219 * format for content or cover -- will automatically add the extension, too.
224 * the target file without extension
226 * use the cover image format instead of the content image format
228 * @throws IOException
229 * in case of I/O error
231 public void saveAsImage(Image img
, File target
, boolean cover
)
235 format
= Instance
.getConfig().getString(Config
.IMAGE_FORMAT_COVER
)
238 format
= Instance
.getConfig()
239 .getString(Config
.IMAGE_FORMAT_CONTENT
).toLowerCase();
241 saveAsImage(img
, new File(target
.toString() + "." + format
), format
);
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.
253 * the file format ("png", "jpeg", "bmp"...)
255 * @throws IOException
256 * in case of I/O error
258 public void saveAsImage(Image img
, File target
, String format
)
260 ImageUtils
.getInstance().saveAsImage(img
, target
, format
);
265 * Manually add this item to the cache.
270 * a unique ID for this resource
272 * @return the resulting {@link File}
274 * @throws IOException
275 * in case of I/O error
277 public File
addToCache(InputStream in
, String uniqueID
) throws IOException
{
278 return cache
.save(in
, uniqueID
);
282 * Return the {@link InputStream} corresponding to the given unique ID, or
283 * NULL if none found.
288 * @return the content or NULL
290 public InputStream
getFromCache(String uniqueID
) {
291 return cache
.load(uniqueID
, true, true);
295 * Remove the given resource from the cache.
298 * a unique ID used to locate the cached resource
300 * @return TRUE if it was removed
302 public boolean removeFromCache(String uniqueID
) {
303 return cache
.remove(uniqueID
);
307 * Clean the cache (delete the cached items).
310 * only clean the files that are considered too old
312 * @return the number of cleaned items
314 public int cleanCache(boolean onlyOld
) {
315 return cache
.clean(onlyOld
);