update desktop image fetcher for fullborder downloads

This commit is contained in:
Anthony Calosa
2020-08-22 21:09:26 +08:00
parent 3dfb3f10dc
commit 45b205c6fe

View File

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