update mobile image fetcher for fullborder downloads

This commit is contained in:
Anthony Calosa
2020-08-22 20:50:41 +08:00
parent 2db656b967
commit 3dfb3f10dc

View File

@@ -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());