X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FDataLoader.java;h=901e8da4c13d8a338cccf2945be17a52adef5abd;hp=395f68314cc7caf131295d899bb179167033c052;hb=5a6481dc6e87db089f93ef04bd03686916d42a88;hpb=b9ce9cad4a94e7c88770d5f7ac6cf4f200e336de diff --git a/src/be/nikiroo/fanfix/DataLoader.java b/src/be/nikiroo/fanfix/DataLoader.java index 395f683..901e8da 100644 --- a/src/be/nikiroo/fanfix/DataLoader.java +++ b/src/be/nikiroo/fanfix/DataLoader.java @@ -1,19 +1,19 @@ package be.nikiroo.fanfix; -import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Map; -import javax.imageio.ImageIO; - import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.utils.Cache; +import be.nikiroo.utils.CacheMemory; import be.nikiroo.utils.Downloader; +import be.nikiroo.utils.Image; import be.nikiroo.utils.ImageUtils; +import be.nikiroo.utils.TraceHandler; /** * This cache will manage Internet (and local) downloads, as well as put the @@ -25,8 +25,10 @@ import be.nikiroo.utils.ImageUtils; * @author niki */ public class DataLoader { - private Cache cache; private Downloader downloader; + private Downloader downloaderNoCache; + private Cache cache; + private boolean offline; /** * Create a new {@link DataLoader} object. @@ -49,18 +51,87 @@ public class DataLoader { */ public DataLoader(File dir, String UA, int hoursChanging, int hoursStable) throws IOException { - cache = new Cache(dir, hoursChanging, hoursStable); + downloader = new Downloader(UA, new Cache(dir, hoursChanging, + hoursStable)); + downloaderNoCache = new Downloader(UA); + + cache = downloader.getCache(); + } + + /** + * Create a new {@link DataLoader} object without disk cache (will keep a + * memory cache for manual cache operations). + * + * @param UA + * the User-Agent to use to download the resources + */ + public DataLoader(String UA) { downloader = new Downloader(UA); + downloaderNoCache = downloader; + cache = new CacheMemory(); + } + + /** + * This {@link Downloader} is forbidden to try and connect to the network. + *

+ * If TRUE, it will only check the cache (even in no-cache mode!). + *

+ * Default is FALSE. + * + * @return TRUE if offline + */ + public boolean isOffline() { + return offline; + } + + /** + * This {@link Downloader} is forbidden to try and connect to the network. + *

+ * If TRUE, it will only check the cache (even in no-cache mode!). + *

+ * Default is FALSE. + * + * @param offline TRUE for offline, FALSE for online + */ + public void setOffline(boolean offline) { + this.offline = offline; + downloader.setOffline(offline); + downloaderNoCache.setOffline(offline); + + // If we don't, we cannot support no-cache using code in OFFLINE mode + if (offline) { + downloaderNoCache.setCache(cache); + } else { + downloaderNoCache.setCache(null); + } + } + + /** + * The traces handler for this {@link Cache}. + * + * @param tracer + * the new traces handler + */ + public void setTraceHandler(TraceHandler tracer) { + downloader.setTraceHandler(tracer); + downloaderNoCache.setTraceHandler(tracer); + cache.setTraceHandler(tracer); + if (downloader.getCache() != null) { + downloader.getCache().setTraceHandler(tracer); + } + } /** * Open a resource (will load it from the cache if possible, or save it into * the cache after downloading if not). + *

+ * The cached resource will be assimilated to the given original {@link URL} * * @param url * the resource to open * @param support - * the support to use to download the resource + * the support to use to download the resource (can be NULL) * @param stable * TRUE for more stable resources, FALSE when they often change * @@ -71,8 +142,7 @@ public class DataLoader { */ public InputStream open(URL url, BasicSupport support, boolean stable) throws IOException { - // MUST NOT return null - return open(url, support, stable, url); + return open(url, url, support, stable, null, null, null); } /** @@ -83,60 +153,71 @@ public class DataLoader { * * @param url * the resource to open + * @param originalUrl + * the original {@link URL} before any redirection occurs, which + * is also used for the cache ID if needed (so we can retrieve + * the content with this URL if needed) * @param support * the support to use to download the resource * @param stable * TRUE for more stable resources, FALSE when they often change - * @param originalUrl - * the original {@link URL} used to locate the cached resource * * @return the opened resource, NOT NULL * * @throws IOException * in case of I/O error */ - public InputStream open(URL url, BasicSupport support, boolean stable, - URL originalUrl) throws IOException { - // MUST NOT return null - try { - InputStream in = cache.load(originalUrl, false, stable); - Instance.getTraceHandler().trace( - "Cache " + (in != null ? "hit" : "miss") + ": " + url); - - if (in == null) { - try { - in = openNoCache(url, support, null, null, null); - cache.save(in, originalUrl); - // ..But we want a resetable stream - in.close(); - in = cache.load(originalUrl, false, stable); - } catch (IOException e) { - throw new IOException("Cannot save the url: " - + (url == null ? "null" : url.toString()), e); - } - } - - return in; - } catch (IOException e) { - throw new IOException("Cannot open the url: " - + (url == null ? "null" : url.toString()), e); - } + public InputStream open(URL url, URL originalUrl, BasicSupport support, + boolean stable) throws IOException { + return open(url, originalUrl, support, stable, null, null, null); } /** - * Open the given {@link URL} without using the cache, but still update the - * cookies. + * Open a resource (will load it from the cache if possible, or save it into + * the cache after downloading if not). + *

+ * The cached resource will be assimilated to the given original {@link URL} * * @param url - * the {@link URL} to open + * the resource to open + * @param originalUrl + * the original {@link URL} before any redirection occurs, which + * is also used for the cache ID if needed (so we can retrieve + * the content with this URL if needed) + * @param support + * the support to use to download the resource (can be NULL) + * @param stable + * TRUE for more stable resources, FALSE when they often change + * @param postParams + * the POST parameters + * @param getParams + * the GET parameters (priority over POST) + * @param oauth + * OAuth authorization (aka, "bearer XXXXXXX") * - * @return the {@link InputStream} of the opened page + * @return the opened resource, NOT NULL * * @throws IOException * in case of I/O error */ - public InputStream openNoCache(URL url) throws IOException { - return downloader.open(url); + public InputStream open(URL url, URL originalUrl, BasicSupport support, + boolean stable, Map postParams, + Map getParams, String oauth) throws IOException { + + Map cookiesValues = null; + URL currentReferer = url; + + if (support != null) { + cookiesValues = support.getCookies(); + currentReferer = support.getCurrentReferer(); + // priority: arguments + if (oauth == null) { + oauth = support.getOAuth(); + } + } + + return downloader.open(url, originalUrl, currentReferer, cookiesValues, + postParams, getParams, oauth, stable); } /** @@ -174,8 +255,8 @@ public class DataLoader { } } - return downloader.open(url, currentReferer, cookiesValues, postParams, - getParams, oauth); + return downloaderNoCache.open(url, currentReferer, cookiesValues, + postParams, getParams, oauth); } /** @@ -184,7 +265,7 @@ public class DataLoader { * @param url * the resource to open * @param support - * the support to use to download the resource + * the support to use to download the resource (can be NULL) * @param stable * TRUE for more stable resources, FALSE when they often change * @@ -193,8 +274,8 @@ public class DataLoader { */ public void refresh(URL url, BasicSupport support, boolean stable) throws IOException { - if (!cache.check(url, false, stable)) { - open(url, support, stable).close(); + if (!check(url, stable)) { + open(url, url, support, stable, null, null, null).close(); } } @@ -211,69 +292,53 @@ public class DataLoader { * */ public boolean check(URL url, boolean stable) { - return cache.check(url, false, stable); + return downloader.getCache() != null + && downloader.getCache().check(url, false, stable); } /** * Save the given resource as an image on disk using the default image - * format for content. + * format for content or cover -- will automatically add the extension, too. * - * @param url + * @param img * the resource * @param target - * the target file + * the target file without extension + * @param cover + * use the cover image format instead of the content image format * * @throws IOException * in case of I/O error */ - public void saveAsImage(URL url, File target) throws IOException { - InputStream in = open(url, null, true); - try { - saveAsImage(ImageUtils.fromStream(in), target); - } finally { - in.close(); + public void saveAsImage(Image img, File target, boolean cover) + throws IOException { + String format; + if (cover) { + format = Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase(); + } else { + format = Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_CONTENT) + .toLowerCase(); } + saveAsImage(img, new File(target.toString() + "." + format), format); } /** - * Save the given resource as an image on disk using the default image - * format for content. + * Save the given resource as an image on disk using the given image format + * for content, or with "png" format if it fails. * - * @param image + * @param img * the resource * @param target * the target file + * @param format + * the file format ("png", "jpeg", "bmp"...) * * @throws IOException * in case of I/O error */ - public void saveAsImage(BufferedImage image, File target) + public void saveAsImage(Image img, File target, String format) throws IOException { - try { - String format = Instance.getConfig() - .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(); - - boolean ok = false; - try { - ok = ImageIO.write(image, format, target); - } catch (IOException e) { - ok = false; - } - - // Some formats are not reliable - // Second change: PNG - if (!ok && !format.equals("png")) { - ok = ImageIO.write(image, "png", target); - } - - if (!ok) { - throw new IOException( - "Cannot find a writer for this image and format: " - + format); - } - } catch (IOException e) { - throw new IOException("Cannot write image to " + target, e); - } + ImageUtils.getInstance().saveAsImage(img, target, format); } /** @@ -284,13 +349,12 @@ public class DataLoader { * @param uniqueID * a unique ID for this resource * - * @return the resulting {@link File} * * @throws IOException * in case of I/O error */ - public File addToCache(InputStream in, String uniqueID) throws IOException { - return cache.save(in, uniqueID); + public void addToCache(InputStream in, String uniqueID) throws IOException { + cache.save(in, uniqueID); } /**