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;
/**
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());
}
/**
// 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 {
/**
* 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);
- } catch (IOException e) {
- throw new IOException("Cannot write image " + url, e);
- } 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);
+
}
/**
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).
*