mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-13 09:17:59 +00:00
ImageFetcher: Better handling if Image can't be saved as jpg
This commit is contained in:
committed by
Michael Kamensky
parent
c31198eaea
commit
5cf1f47047
@@ -163,6 +163,10 @@ public final class ImageKeys {
|
|||||||
for (String ext : FILE_EXTENSIONS) {
|
for (String ext : FILE_EXTENSIONS) {
|
||||||
File file = new File(dir, filename + ext);
|
File file = new File(dir, filename + ext);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
file.delete();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,13 @@ final class ImageLoader extends CacheLoader<String, BufferedImage> {
|
|||||||
public BufferedImage load(String key) {
|
public BufferedImage load(String key) {
|
||||||
File file = ImageKeys.getImageFile(key);
|
File file = ImageKeys.getImageFile(key);
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
|
if (!file.exists()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
file.delete();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return ImageIO.read(file);
|
return ImageIO.read(file);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,18 +28,35 @@ public class SwingImageFetcher extends ImageFetcher {
|
|||||||
private void doFetch(String urlToDownload) throws IOException {
|
private void doFetch(String urlToDownload) throws IOException {
|
||||||
URL url = new URL(urlToDownload);
|
URL url = new URL(urlToDownload);
|
||||||
System.out.println("Attempting to fetch: " + url);
|
System.out.println("Attempting to fetch: " + url);
|
||||||
java.net.URLConnection c = url.openConnection();
|
BufferedImage image = ImageIO.read(url);
|
||||||
c.setRequestProperty("User-Agent", "");
|
|
||||||
BufferedImage image = ImageIO.read(c.getInputStream());
|
|
||||||
// First, save to a temporary file so that nothing tries to read
|
// First, save to a temporary file so that nothing tries to read
|
||||||
// a partial download.
|
// a partial download.
|
||||||
File destFile = new File(destPath + ".tmp");
|
File destFile = new File(destPath + ".tmp");
|
||||||
destFile.mkdirs();
|
// need to check directory folder for mkdir
|
||||||
ImageIO.write(image, "jpg", destFile);
|
destFile.getParentFile().mkdirs();
|
||||||
|
if (ImageIO.write(image, "jpg", destFile)) {
|
||||||
// Now, rename it to the correct name.
|
// Now, rename it to the correct name.
|
||||||
destFile.renameTo(new File(destPath));
|
if (destFile.renameTo(new File(destPath))) {
|
||||||
System.out.println("Saved image to " + destPath);
|
System.out.println("Saved image to " + destPath);
|
||||||
SwingUtilities.invokeLater(notifyObservers);
|
SwingUtilities.invokeLater(notifyObservers);
|
||||||
|
} else {
|
||||||
|
System.err.println("Failed to rename image to " + destPath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.err.println("Failed to save image from " + url + " as jpeg");
|
||||||
|
// try to save image as png instead
|
||||||
|
if (ImageIO.write(image, "png", destFile)) {
|
||||||
|
String newPath = destPath.replace(".jpg", ".png");
|
||||||
|
if (destFile.renameTo(new File(newPath))) {
|
||||||
|
System.out.println("Saved image to " + newPath);
|
||||||
|
SwingUtilities.invokeLater(notifyObservers);
|
||||||
|
} else {
|
||||||
|
System.err.println("Failed to rename image to " + newPath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.err.println("Failed to save image from " + url + " as png");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -48,7 +65,7 @@ public class SwingImageFetcher extends ImageFetcher {
|
|||||||
doFetch(urlToDownload);
|
doFetch(urlToDownload);
|
||||||
break;
|
break;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Failed to download card [" + destPath + "] image: " + e.getMessage());
|
System.err.println("Failed to download card [" + destPath + "] image: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user