From 3dfb3f10dc23693d48c3c20a50251cfd9f59f1a1 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Sat, 22 Aug 2020 20:50:41 +0800 Subject: [PATCH] update mobile image fetcher for fullborder downloads --- .../src/forge/util/LibGDXImageFetcher.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/forge-gui-mobile/src/forge/util/LibGDXImageFetcher.java b/forge-gui-mobile/src/forge/util/LibGDXImageFetcher.java index 6e8bc3f7ca5..9d2429b97e8 100644 --- a/forge-gui-mobile/src/forge/util/LibGDXImageFetcher.java +++ b/forge-gui-mobile/src/forge/util/LibGDXImageFetcher.java @@ -7,6 +7,7 @@ import forge.GuiBase; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URL; public class LibGDXImageFetcher extends ImageFetcher { @@ -27,6 +28,8 @@ public class LibGDXImageFetcher 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); java.net.URLConnection c = url.openConnection(); @@ -35,22 +38,40 @@ public class LibGDXImageFetcher extends ImageFetcher { InputStream is = c.getInputStream(); // First, save to a temporary file so that nothing tries to read // a partial download. - FileHandle destFile = new FileHandle(destPath + ".tmp"); - System.out.println(destPath); + FileHandle destFile = new FileHandle(newdespath + ".tmp"); + System.out.println(newdespath); destFile.parent().mkdirs(); // Conversion to JPEG will be handled differently depending on the platform Forge.getDeviceAdapter().convertToJPEG(is, new FileOutputStream(destFile.file())); - destFile.moveTo(new FileHandle(destPath)); + destFile.moveTo(new FileHandle(newdespath)); - System.out.println("Saved image to " + destPath); + System.out.println("Saved image to " + newdespath); GuiBase.getInterface().invokeInEdtLater(notifyObservers); } + 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.out.println("Failed to download card [" + destPath + "] image: " + e.getMessage());