From 45b205c6fe7d2feae6c632d79f060e3cebb22cc4 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Sat, 22 Aug 2020 21:09:26 +0800 Subject: [PATCH] update desktop image fetcher for fullborder downloads --- .../java/forge/util/SwingImageFetcher.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/forge-gui-desktop/src/main/java/forge/util/SwingImageFetcher.java b/forge-gui-desktop/src/main/java/forge/util/SwingImageFetcher.java index bfd6258d2d7..ce496fc121f 100644 --- a/forge-gui-desktop/src/main/java/forge/util/SwingImageFetcher.java +++ b/forge-gui-desktop/src/main/java/forge/util/SwingImageFetcher.java @@ -5,6 +5,7 @@ import javax.swing.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.net.HttpURLConnection; import java.net.URL; public class SwingImageFetcher extends ImageFetcher { @@ -26,27 +27,29 @@ public class SwingImageFetcher extends ImageFetcher { } private void doFetch(String urlToDownload) throws IOException { + String newdespath = urlToDownload.contains(".fullborder.jpg") ? + TextUtil.fastReplace(destPath, ".full.jpg", ".fullborder.jpg") : destPath; URL url = new URL(urlToDownload); System.out.println("Attempting to fetch: " + url); BufferedImage image = ImageIO.read(url); // First, save to a temporary file so that nothing tries to read // a partial download. - File destFile = new File(destPath + ".tmp"); + File destFile = new File(newdespath + ".tmp"); // need to check directory folder for mkdir destFile.getParentFile().mkdirs(); if (ImageIO.write(image, "jpg", destFile)) { // Now, rename it to the correct name. - if (destFile.renameTo(new File(destPath))) { - System.out.println("Saved image to " + destPath); + if (destFile.renameTo(new File(newdespath))) { + System.out.println("Saved image to " + newdespath); SwingUtilities.invokeLater(notifyObservers); } else { - System.err.println("Failed to rename image to " + destPath); + System.err.println("Failed to rename image to " + newdespath); } } 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"); + String newPath = newdespath.replace(".jpg", ".png"); if (destFile.renameTo(new File(newPath))) { System.out.println("Saved image to " + newPath); SwingUtilities.invokeLater(notifyObservers); @@ -59,10 +62,28 @@ public class SwingImageFetcher extends ImageFetcher { } } + private String tofullBorder(String imageurl) { + if (!imageurl.contains(".full.jpg")) + return imageurl; + try { + URL url = new URL(imageurl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + //connection.setConnectTimeout(1000 * 5); //wait 5 seconds the most + //connection.setReadTimeout(1000 * 5); + conn.setRequestProperty("User-Agent", ""); + if(conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) + imageurl = TextUtil.fastReplace(imageurl, ".full.jpg", ".fullborder.jpg"); + conn.disconnect(); + return imageurl; + } catch (IOException ex) { + return imageurl; + } + } + public void run() { for (String urlToDownload : downloadUrls) { try { - doFetch(urlToDownload); + doFetch(tofullBorder(urlToDownload)); break; } catch (IOException e) { System.err.println("Failed to download card [" + destPath + "] image: " + e.getMessage());