X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FCache.java;h=40ce15efec98860c559177dc0734c3b68377f797;hb=211f7ddb50f68aa8a999023ef6d63d5756bdace6;hp=d97a58a66a13a67751ea94f876e9c36c4f1b6a27;hpb=13285ff8473a4092bddeed74c953f358ba1671d6;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/Cache.java b/src/be/nikiroo/fanfix/Cache.java index d97a58a..40ce15e 100644 --- a/src/be/nikiroo/fanfix/Cache.java +++ b/src/be/nikiroo/fanfix/Cache.java @@ -3,6 +3,7 @@ package be.nikiroo.fanfix; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -13,7 +14,6 @@ import java.net.CookiePolicy; import java.net.CookieStore; import java.net.HttpCookie; import java.net.HttpURLConnection; -import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; @@ -26,6 +26,7 @@ import javax.imageio.ImageIO; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.utils.IOUtils; +import be.nikiroo.utils.ImageUtils; import be.nikiroo.utils.MarkableFileInputStream; /** @@ -302,17 +303,13 @@ public class Cache { } /** - * Open a resource (will load it from the cache if possible, or save it into - * the cache after downloading if not) as an Image, then save it where - * requested. - *

- * This version will not always work properly if the original file was not - * downloaded before. + * Save the given resource as an image on disk using the default image + * format for content. * * @param url - * the resource to open - * - * @return the opened resource image + * the resource + * @param target + * the target file * * @throws IOException * in case of I/O error @@ -323,7 +320,7 @@ public class Cache { if (!cached.exists() || isOld(cached, true)) { InputStream imageIn = open(url, null, true); - ImageIO.write(IOUtils.toImage(imageIn), Instance.getConfig() + ImageIO.write(ImageUtils.fromStream(imageIn), Instance.getConfig() .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(), cached); } @@ -345,11 +342,32 @@ public class Cache { * in case of I/O error */ public File addToCache(InputStream in, String uniqueID) throws IOException { - File file = getCached(new File(uniqueID).toURI().toURL()); + File file = getCached(uniqueID); IOUtils.write(in, file); return file; } + /** + * Return the {@link InputStream} corresponding to the given unique ID, or + * NULL if none found. + * + * @param uniqueID + * the unique ID + * + * @return the content or NULL + */ + public InputStream getFromCache(String uniqueID) { + File file = getCached(uniqueID); + if (file.exists()) { + try { + return new MarkableFileInputStream(new FileInputStream(file)); + } catch (FileNotFoundException e) { + } + } + + return null; + } + /** * Clean the cache (delete the cached items). * @@ -378,16 +396,21 @@ public class Cache { * * @param url * the resource to open + * @param allowTooOld + * allow files even if they are considered too old + * @param stable + * a stable file (that dones't change too often) -- parameter + * used to check if the file is too old to keep or not * * @return the opened resource if found, NULL i not * * @throws IOException * in case of I/O error */ - private InputStream load(URL url, boolean allowOld, boolean stable) + private InputStream load(URL url, boolean allowTooOld, boolean stable) throws IOException { File cached = getCached(url); - if (cached.exists() && !isOld(cached, stable)) { + if (cached.exists() && (allowTooOld || !isOld(cached, stable))) { return new MarkableFileInputStream(new FileInputStream(cached)); } @@ -406,7 +429,6 @@ public class Cache { * * @throws IOException * in case of I/O error - * @throws URISyntaxException */ private void save(URL url, BasicSupport support, URL originalUrl) throws IOException { @@ -496,19 +518,34 @@ public class Cache { * * @param url * the url + * * @return the cached version if present, NULL if not */ private File getCached(URL url) { String name = url.getHost(); - if (name == null || name.length() == 0) { + if (name == null || name.isEmpty()) { name = url.getFile(); } else { name = url.toString(); } - name = name.replace('/', '_').replace(':', '_'); + return getCached(name); + } + + /** + * Get the cache resource from the cache if it is present for this unique + * ID. + * + * @param uniqueID + * the id + * + * @return the cached version if present, NULL if not + */ + private File getCached(String uniqueID) { + uniqueID = uniqueID.replace('/', '_').replace(':', '_') + .replace("\\", "_"); - return new File(dir, name); + return new File(dir, uniqueID); } /**