X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FDataLoader.java;h=222c61463c815e8000b6e491fe0234598071a05b;hb=16a81ef7656c5c692fb831927e75edde25dd77a0;hp=ef115c9d60a9b647147fe3dff06c9bf5d95a6283;hpb=f1fb834c62f9d9a73edeeda3fed060e0dede8cef;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/DataLoader.java b/src/be/nikiroo/fanfix/DataLoader.java index ef115c9..222c614 100644 --- a/src/be/nikiroo/fanfix/DataLoader.java +++ b/src/be/nikiroo/fanfix/DataLoader.java @@ -6,12 +6,11 @@ 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.Downloader; +import be.nikiroo.utils.Image; import be.nikiroo.utils.ImageUtils; /** @@ -49,7 +48,9 @@ public class DataLoader { public DataLoader(File dir, String UA, int hoursChanging, int hoursStable) throws IOException { cache = new Cache(dir, hoursChanging, hoursStable); + cache.setTraceHandler(Instance.getTraceHandler()); downloader = new Downloader(UA); + downloader.setTraceHandler(Instance.getTraceHandler()); } /** @@ -99,8 +100,8 @@ public class DataLoader { // MUST NOT return null try { InputStream in = cache.load(originalUrl, false, stable); - Instance.trace("Cache " + (in != null ? "hit" : "miss") + ": " - + url); + Instance.getTraceHandler().trace( + "Cache " + (in != null ? "hit" : "miss") + ": " + url); if (in == null) { try { @@ -215,25 +216,49 @@ public class DataLoader { /** * 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 { - ImageIO.write(ImageUtils.fromStream(in), Instance.getConfig() - .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(), - target); - } finally { - in.close(); + public void saveAsImage(Image img, File target, boolean cover) + throws IOException { + String format; + if (cover) { + format = Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER) + .toLowerCase(); + } else { + format = Instance.getConfig() + .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(); } + saveAsImage(img, new File(target.toString() + "." + format), format); + } + + /** + * Save the given resource as an image on disk using the given image format + * for content, or with "png" format if it fails. + * + * @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(Image img, File target, String format) + throws IOException { + ImageUtils.getInstance().saveAsImage(img, target, format); + } /** @@ -266,6 +291,18 @@ public class DataLoader { return cache.load(uniqueID, true, true); } + /** + * Remove the given resource from the cache. + * + * @param uniqueID + * a unique ID used to locate the cached resource + * + * @return TRUE if it was removed + */ + public boolean removeFromCache(String uniqueID) { + return cache.remove(uniqueID); + } + /** * Clean the cache (delete the cached items). *