If saving a thumbnail image fails, delete the resulting file.

OpenJDK throws "javax.imageio.IIOException: Invalid argument to native writeImage", because it
does not support transparency, and that results in zero-sized thumbnail files being created.
This commit is contained in:
LoneFox 2015-06-11 09:17:05 +03:00
parent 658daa65e3
commit 90486afdbb

View file

@ -27,11 +27,11 @@ import org.mage.plugins.card.utils.CardImageUtils;
* This class stores ALL card images in a cache with soft values. this means * This class stores ALL card images in a cache with soft values. this means
* that the images may be garbage collected when they are not needed any more, but will * that the images may be garbage collected when they are not needed any more, but will
* be kept as long as possible. * be kept as long as possible.
* *
* Key format: "<cardname>#<setname>#<type>#<collectorID>#<param>" * Key format: "<cardname>#<setname>#<type>#<collectorID>#<param>"
* *
* where param is: * where param is:
* *
* <ul> * <ul>
* <li>#Normal: request for unrotated image</li> * <li>#Normal: request for unrotated image</li>
* <li>#Tapped: request for rotated image</li> * <li>#Tapped: request for rotated image</li>
@ -56,7 +56,7 @@ public class ImageCache {
@Override @Override
public BufferedImage apply(String key) { public BufferedImage apply(String key) {
try { try {
boolean usesVariousArt = false; boolean usesVariousArt = false;
if (key.endsWith("#usesVariousArt")) { if (key.endsWith("#usesVariousArt")) {
usesVariousArt = true; usesVariousArt = true;
@ -111,7 +111,7 @@ public class ImageCache {
} catch(Exception ex) { } catch(Exception ex) {
exists = false; exists = false;
} }
} }
if (exists) { if (exists) {
log.debug("loading thumbnail for " + key + ", path="+thumbnailPath); log.debug("loading thumbnail for " + key + ", path="+thumbnailPath);
return loadImage(thumbnailFile); return loadImage(thumbnailFile);
@ -150,7 +150,7 @@ public class ImageCache {
return null; return null;
} }
TFile file = getTFile(path); TFile file = getTFile(path);
return loadImage(file); return loadImage(file);
} }
public static BufferedImage getManifestImage() { public static BufferedImage getManifestImage() {
@ -159,7 +159,7 @@ public class ImageCache {
String path = CardImageUtils.generateTokenImagePath(info); String path = CardImageUtils.generateTokenImagePath(info);
if (path == null) { if (path == null) {
return null; return null;
} }
TFile file = getTFile(path); TFile file = getTFile(path);
return loadImage(file); return loadImage(file);
} }
@ -219,7 +219,7 @@ public class ImageCache {
private static BufferedImage getImage(String key) { private static BufferedImage getImage(String key) {
try { try {
BufferedImage image = imageCache.get(key); BufferedImage image = imageCache.get(key);
return image; return image;
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
// unfortunately NullOutputException, thrown when apply() returns // unfortunately NullOutputException, thrown when apply() returns
// null, is not public // null, is not public
@ -261,7 +261,7 @@ public class ImageCache {
/** /**
* Load image from file * Load image from file
* *
* @param file * @param file
* file to load image from * file to load image from
* @return {@link BufferedImage} * @return {@link BufferedImage}
@ -298,6 +298,7 @@ public class ImageCache {
} }
} catch (IOException e) { } catch (IOException e) {
log.error(e,e); log.error(e,e);
imageFile.delete();
} }
return image; return image;
} }
@ -382,7 +383,7 @@ public class ImageCache {
return getFullSizeImage(original, scale); return getFullSizeImage(original, scale);
} }
public static TFile getTFile(String path) { public static TFile getTFile(String path) {
try { try {
TFile file = new TFile(path); TFile file = new TFile(path);
@ -390,6 +391,6 @@ public class ImageCache {
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
log.warn("Imagefile does not exist: " + path); log.warn("Imagefile does not exist: " + path);
} }
return null; return null;
} }
} }