Images: improved error messages, added cancel after too many errors;

This commit is contained in:
Oleg Agafonov 2019-01-20 17:30:53 +04:00
parent b7615a2cdb
commit 72c9370d98

View file

@ -11,7 +11,6 @@ import mage.client.dialog.PreferencesDialog;
import mage.client.util.CardLanguage; import mage.client.util.CardLanguage;
import mage.client.util.sets.ConstructedFormats; import mage.client.util.sets.ConstructedFormats;
import mage.remote.Connection; import mage.remote.Connection;
import mage.util.StreamUtils;
import net.java.truevfs.access.TFile; import net.java.truevfs.access.TFile;
import net.java.truevfs.access.TFileOutputStream; import net.java.truevfs.access.TFileOutputStream;
import net.java.truevfs.access.TVFS; import net.java.truevfs.access.TVFS;
@ -26,8 +25,8 @@ import java.awt.event.ItemEvent;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.file.AccessDeniedException; import java.nio.file.AccessDeniedException;
import java.util.*;
import java.util.List; import java.util.List;
import java.util.*;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -49,8 +48,11 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
private static final String ALL_STANDARD_IMAGES = "- STANDARD images"; private static final String ALL_STANDARD_IMAGES = "- STANDARD images";
private static final String ALL_TOKENS = "- TOKEN images"; private static final String ALL_TOKENS = "- TOKEN images";
private static final int MAX_ERRORS_COUNT_BEFORE_CANCEL = 50;
private DownloadImagesDialog uiDialog; private DownloadImagesDialog uiDialog;
private boolean needCancel; private boolean needCancel;
private int errorCount;
private int cardIndex; private int cardIndex;
private List<CardInfo> cardsAll; private List<CardInfo> cardsAll;
@ -110,19 +112,28 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
// show dialog // show dialog
instance.setNeedCancel(false); instance.setNeedCancel(false);
instance.resetErrorCount();
instance.uiDialog.showDialog(); instance.uiDialog.showDialog();
instance.uiDialog.dispose(); instance.uiDialog.dispose();
instance.setNeedCancel(true); instance.setNeedCancel(true);
} }
public boolean getNeedCancel() { private boolean getNeedCancel() {
return this.needCancel; return this.needCancel || (this.errorCount > MAX_ERRORS_COUNT_BEFORE_CANCEL);
} }
public void setNeedCancel(boolean needCancel) { private void setNeedCancel(boolean needCancel) {
this.needCancel = needCancel; this.needCancel = needCancel;
} }
private void incErrorCount() {
this.errorCount = this.errorCount + 1;
}
private void resetErrorCount() {
this.errorCount = 0;
}
public DownloadPicturesService(JFrame frame) { public DownloadPicturesService(JFrame frame) {
// init service and dialog // init service and dialog
cardsAll = Collections.synchronizedList(new ArrayList<>()); cardsAll = Collections.synchronizedList(new ArrayList<>());
@ -788,29 +799,43 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
// download // download
selectedSource.doPause(url.getPath()); selectedSource.doPause(url.getPath());
httpConn = url.openConnection(p); httpConn = url.openConnection(p);
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); if (httpConn != null) {
httpConn.connect();
int responseCode = ((HttpURLConnection) httpConn).getResponseCode();
// check result httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
if (responseCode != 200) { try {
// show errors only on full fail (all urls were not work) httpConn.connect();
errorsList.add("Image download for " + card.getName() } catch (SocketException e) {
+ (!card.getDownloadName().equals(card.getName()) ? " downloadname: " + card.getDownloadName() : "") incErrorCount();
+ " (" + card.getSet() + ") failed - responseCode: " + responseCode + " url: " + url.toString()); errorsList.add("Wrong image URL or java app is not allowed to use network. Check your firewall or proxy settings. Error: " + e.getMessage() + ". Image URL: " + url.toString());
break;
if (logger.isDebugEnabled()) { } catch (UnknownHostException e) {
// Shows the returned html from the request to the web server incErrorCount();
logger.debug("Returned HTML ERROR:\n" + convertStreamToString(((HttpURLConnection) httpConn).getErrorStream())); errorsList.add("Unknown site. Check your DNS settings. Error: " + e.getMessage() + ". Image URL: " + url.toString());
break;
} }
int responseCode = ((HttpURLConnection) httpConn).getResponseCode();
// go to next try // check result
continue; if (responseCode != 200) {
} else { // show errors only on full fail (all urls were not work)
// all fine errorsList.add("Image download for " + card.getName()
isDownloadOK = true; + (!card.getDownloadName().equals(card.getName()) ? " downloadname: " + card.getDownloadName() : "")
break; + " (" + card.getSet() + ") failed - responseCode: " + responseCode + " url: " + url.toString());
if (logger.isDebugEnabled()) {
// Shows the returned html from the request to the web server
logger.debug("Returned HTML ERROR:\n" + convertStreamToString(((HttpURLConnection) httpConn).getErrorStream()));
}
// go to next try
continue;
} else {
// all fine
isDownloadOK = true;
break;
}
} }
} }
@ -868,9 +893,11 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
} }
} catch (AccessDeniedException e) { } catch (AccessDeniedException e) {
incErrorCount();
logger.error("Can't access to files: " + card.getName() + "(" + card.getSet() + "). Try rebooting your system to remove the file lock."); logger.error("Can't access to files: " + card.getName() + "(" + card.getSet() + "). Try rebooting your system to remove the file lock.");
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage(), e); incErrorCount();
logger.error("Unknown error: " + e.getMessage(), e);
} finally { } finally {
} }