Added some more handling for missing images.

This commit is contained in:
LevelX2 2015-05-07 23:54:28 +02:00
parent 0ef2919a08
commit 951198a578
2 changed files with 44 additions and 39 deletions

View file

@ -7,6 +7,7 @@ import com.mortennobel.imagescaling.ResampleOp;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -92,16 +93,20 @@ public class ImageCache {
if (path == null) { if (path == null) {
return null; return null;
} }
TFile file = new TFile(path); TFile file = getTFile(path);
if (!file.exists()) { if (file == null) {
log.debug("File does not exist: " + file.toString());
return null; return null;
} }
if (thumbnail && path.endsWith(".jpg")) { if (thumbnail && path.endsWith(".jpg")) {
String thumbnailPath = buildThumbnailPath(path); String thumbnailPath = buildThumbnailPath(path);
TFile thumbnailFile = new TFile(thumbnailPath); TFile thumbnailFile = null;
if (thumbnailFile.exists()) { try {
thumbnailFile = new TFile(thumbnailPath);
} catch (Exception ex) {
}
if (thumbnailFile != null && thumbnailFile.exists()) {
log.debug("loading thumbnail for " + key + ", path="+thumbnailPath); log.debug("loading thumbnail for " + key + ", path="+thumbnailPath);
return loadImage(thumbnailFile); return loadImage(thumbnailFile);
} else { } else {
@ -135,15 +140,21 @@ public class ImageCache {
CardDownloadData info = new CardDownloadData("Morph", "KTK", 0, false, 0, "KTK"); CardDownloadData info = new CardDownloadData("Morph", "KTK", 0, false, 0, "KTK");
info.setToken(true); info.setToken(true);
String path = CardImageUtils.generateTokenImagePath(info); String path = CardImageUtils.generateTokenImagePath(info);
TFile file = new TFile(path); if (path == null) {
return loadImage(file); return null;
}
TFile file = getTFile(path);
return loadImage(file);
} }
public static BufferedImage getManifestImage() { public static BufferedImage getManifestImage() {
CardDownloadData info = new CardDownloadData("Manifest", "FRF", 0, false, 0, "FRF"); CardDownloadData info = new CardDownloadData("Manifest", "FRF", 0, false, 0, "FRF");
info.setToken(true); info.setToken(true);
String path = CardImageUtils.generateTokenImagePath(info); String path = CardImageUtils.generateTokenImagePath(info);
TFile file = new TFile(path); if (path == null) {
return null;
}
TFile file = getTFile(path);
return loadImage(file); return loadImage(file);
} }
@ -196,29 +207,13 @@ public class ImageCache {
return getImage(key); return getImage(key);
} }
// /**
// * Returns the Image corresponding to the Path
// */
// private static BufferedImage getImageByPath(String path) {
// if (path == null) {
// return null;
// }
// TFile file = new TFile(path);
// if (!file.exists()) {
// log.warn("File does not exist: " + file.toString());
// return null;
// }
// return getWizardsCard(loadImage(file));
//
// }
/** /**
* Returns the Image corresponding to the key * Returns the Image corresponding to the key
*/ */
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
@ -266,15 +261,18 @@ public class ImageCache {
* @return {@link BufferedImage} * @return {@link BufferedImage}
*/ */
public static BufferedImage loadImage(TFile file) { public static BufferedImage loadImage(TFile file) {
if (file == null) {
return null;
}
BufferedImage image = null; BufferedImage image = null;
if (!file.exists()) { if (!file.exists()) {
log.debug("File does not exist: " + file.toString()); log.debug("File does not exist: " + file.toString());
return null; return null;
} }
try { try {
TFileInputStream inputStream = new TFileInputStream(file); try (TFileInputStream inputStream = new TFileInputStream(file)) {
image = ImageIO.read(inputStream); image = ImageIO.read(inputStream);
inputStream.close(); }
} catch (Exception e) { } catch (Exception e) {
log.error(e, e); log.error(e, e);
} }
@ -284,17 +282,13 @@ public class ImageCache {
public static BufferedImage makeThumbnail(BufferedImage original, String path) { public static BufferedImage makeThumbnail(BufferedImage original, String path) {
BufferedImage image = getResizedImage(original, Constants.THUMBNAIL_SIZE_FULL); BufferedImage image = getResizedImage(original, Constants.THUMBNAIL_SIZE_FULL);
TFile imageFile = new TFile(path); TFile imageFile = getTFile(path);
if (imageFile == null) {
return null;
}
try { try {
TFileOutputStream outputStream = null; try (TFileOutputStream outputStream = new TFileOutputStream(imageFile)) {
try {
//log.debug("thumbnail path:"+path);
outputStream = new TFileOutputStream(imageFile);
ImageIO.write(image, "jpg", outputStream); ImageIO.write(image, "jpg", outputStream);
} finally {
if (outputStream != null) {
outputStream.close();
}
} }
} catch (IOException e) { } catch (IOException e) {
log.error(e,e); log.error(e,e);
@ -382,4 +376,14 @@ public class ImageCache {
return getFullSizeImage(original, scale); return getFullSizeImage(original, scale);
} }
public static TFile getTFile(String path) {
try {
TFile file = new TFile(path);
return file;
} catch (NullPointerException ex) {
log.warn("Imagefile does not exist: " + path);
}
return null;
}
} }

View file

@ -4,13 +4,14 @@ import java.util.HashMap;
import mage.client.constants.Constants; import mage.client.constants.Constants;
import mage.client.dialog.PreferencesDialog; import mage.client.dialog.PreferencesDialog;
import net.java.truevfs.access.TFile; import net.java.truevfs.access.TFile;
import org.apache.log4j.Logger;
import org.mage.plugins.card.images.CardDownloadData; import org.mage.plugins.card.images.CardDownloadData;
import org.mage.plugins.card.properties.SettingsManager; import org.mage.plugins.card.properties.SettingsManager;
public class CardImageUtils { public class CardImageUtils {
private static final HashMap<CardDownloadData, String> pathCache = new HashMap<>(); private static final HashMap<CardDownloadData, String> pathCache = new HashMap<>();
private static final Logger log = Logger.getLogger(CardImageUtils.class);
/** /**
* *
@ -35,7 +36,7 @@ public class CardImageUtils {
return filePath; return filePath;
} }
} }
log.warn("Token image file not found: " + card.getTokenSetCode() + " - " + card.getName());
return null; return null;
} }