From 17df25399f5a66888075c6f98d0b0637e4664901 Mon Sep 17 00:00:00 2001 From: Tim Scott Date: Sun, 24 Feb 2019 15:28:53 -0600 Subject: [PATCH] Use pattern matching to capture directory listing from the server URL string. Add fallback to PNG for download if JPG is not found. Correct quest-opponent-icons.txt. Space reference should be "%20". --- forge-gui/res/lists/quest-opponent-icons.txt | 2 +- .../forge/download/GuiDownloadService.java | 56 ++++++++++++++----- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/forge-gui/res/lists/quest-opponent-icons.txt b/forge-gui/res/lists/quest-opponent-icons.txt index ff4525f1020..28cad0cfa4f 100644 --- a/forge-gui/res/lists/quest-opponent-icons.txt +++ b/forge-gui/res/lists/quest-opponent-icons.txt @@ -242,7 +242,7 @@ https://downloads.cardforge.org/images/icons/Einstein.jpg https://downloads.cardforge.org/images/icons/Ekolo.jpg https://downloads.cardforge.org/images/icons/Elashub.jpg https://downloads.cardforge.org/images/icons/Eldrazi.jpg -https://downloads.cardforge.org/images/icons/Eldritch Onslaught.jpg +https://downloads.cardforge.org/images/icons/Eldritch%20Onslaught.jpg https://downloads.cardforge.org/images/icons/Electro.jpg https://downloads.cardforge.org/images/icons/Elegua.jpg https://downloads.cardforge.org/images/icons/Elementalist.jpg diff --git a/forge-gui/src/main/java/forge/download/GuiDownloadService.java b/forge-gui/src/main/java/forge/download/GuiDownloadService.java index eadd9cc4e8d..c0a22f27e85 100644 --- a/forge-gui/src/main/java/forge/download/GuiDownloadService.java +++ b/forge-gui/src/main/java/forge/download/GuiDownloadService.java @@ -17,7 +17,28 @@ */ package forge.download; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.ConnectException; +import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.MalformedURLException; +import java.net.Proxy; +import java.net.URL; +import java.net.URLDecoder; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.tuple.Pair; + import com.esotericsoftware.minlog.Log; + import forge.FThreads; import forge.GuiBase; import forge.UiCommand; @@ -28,13 +49,6 @@ import forge.interfaces.ITextField; import forge.properties.ForgeConstants; import forge.util.FileUtil; import forge.util.HttpUtil; -import org.apache.commons.lang3.tuple.Pair; - -import java.io.*; -import java.net.*; -import java.util.HashSet; -import java.util.Map; -import java.util.Map.Entry; @SuppressWarnings("serial") public abstract class GuiDownloadService implements Runnable { @@ -260,6 +274,19 @@ public abstract class GuiDownloadService implements Runnable { // don't allow redirections here -- they indicate 'file not found' on the server conn.setInstanceFollowRedirects(false); conn.connect(); + + // if file is not found and this is a JPG, give PNG a shot... + if ((conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) && (url.endsWith(".jpg"))) + { + conn.disconnect(); + System.out.println(" File not found: " + url); + url = url.substring(0,url.length() - 4) + ".png"; + imageUrl = new URL(url); + conn = (HttpURLConnection) imageUrl.openConnection(p); + conn.setInstanceFollowRedirects(false); + conn.connect(); + } + switch (conn.getResponseCode()) { case HttpURLConnection.HTTP_OK: fos = new FileOutputStream(fileDest); @@ -353,15 +380,16 @@ public abstract class GuiDownloadService implements Runnable { if (response == null) return null; String[] strings = response.split("([A-Z0-9_]+)/<"); + Matcher matcher; + for (String s : strings) { - int idx = s.indexOf('/'); - if (!Character.isLetterOrDigit(s.charAt(0)) || idx > 4 || idx == -1) { - continue; + matcher = pattern.matcher(s); + if (matcher.find()) { + existingSets.add(matcher.group(1)); } - - String set = s.substring(0, idx); - existingSets.add(set); } return existingSets;