diff --git a/common/src/main/java/io/bitsquare/common/util/DownloadUtil.java b/common/src/main/java/io/bitsquare/common/util/DownloadUtil.java index b91893d0fa..0fe6a63a61 100644 --- a/common/src/main/java/io/bitsquare/common/util/DownloadUtil.java +++ b/common/src/main/java/io/bitsquare/common/util/DownloadUtil.java @@ -44,9 +44,11 @@ public class DownloadUtil extends Task { public DownloadUtil (final String fileURL) { this.fileURL = fileURL; this.saveDir = System.getProperty("java.io.tmpdir"); + System.out.println("Auto-selected temp dir " + this.saveDir); } @Override protected File call() throws Exception{ + System.out.println("Task started...."); URL url = new URL(fileURL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); @@ -88,6 +90,8 @@ public class DownloadUtil extends Task { int totalRead = 0; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = inputStream.read(buffer)) != -1) { + if (this.isCancelled()) + break; outputStream.write(buffer, 0, bytesRead); totalRead += bytesRead; updateProgress(totalRead, contentLength); diff --git a/common/src/main/java/io/bitsquare/common/util/Utilities.java b/common/src/main/java/io/bitsquare/common/util/Utilities.java index 177a9cc5fb..8b2df2f214 100644 --- a/common/src/main/java/io/bitsquare/common/util/Utilities.java +++ b/common/src/main/java/io/bitsquare/common/util/Utilities.java @@ -36,9 +36,7 @@ import org.slf4j.LoggerFactory; import javax.crypto.Cipher; import java.awt.*; import java.io.*; -import java.net.HttpURLConnection; import java.net.URI; -import java.net.URL; import java.net.URLConnection; import java.security.NoSuchAlgorithmException; import java.util.Locale; @@ -209,7 +207,7 @@ public class Utilities { } } - public static File downloadFile(String fileURL, String saveDir, ProgressIndicator indicator) throws IOException { + public static Task downloadFile(String fileURL, String saveDir, ProgressIndicator indicator) throws IOException { DownloadUtil task; if (saveDir != null) task = new DownloadUtil(fileURL, saveDir); @@ -221,7 +219,8 @@ public class Utilities { } Thread th = new Thread(task); th.start(); - return null; // TODO ! + // TODO: check for problems when creating task + return task; } public static void printSystemLoad() { diff --git a/gui/src/main/java/io/bitsquare/gui/main/overlays/windows/DisplayUpdateDownloadWindow.java b/gui/src/main/java/io/bitsquare/gui/main/overlays/windows/DisplayUpdateDownloadWindow.java index 2450a94b12..84cee5fe13 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/overlays/windows/DisplayUpdateDownloadWindow.java +++ b/gui/src/main/java/io/bitsquare/gui/main/overlays/windows/DisplayUpdateDownloadWindow.java @@ -19,8 +19,8 @@ package io.bitsquare.gui.main.overlays.windows; import io.bitsquare.alert.Alert; import io.bitsquare.common.util.Utilities; -import io.bitsquare.gui.components.HyperlinkWithIcon; import io.bitsquare.gui.main.overlays.Overlay; +import javafx.concurrent.Task; import javafx.geometry.Insets; import javafx.scene.control.Button; import javafx.scene.control.Label; @@ -40,6 +40,7 @@ import static io.bitsquare.gui.util.FormBuilder.addMultilineLabel; public class DisplayUpdateDownloadWindow extends Overlay { private static final Logger log = LoggerFactory.getLogger(DisplayUpdateDownloadWindow.class); private Alert alert; + private Task task; /////////////////////////////////////////////////////////////////////////////////////////// @@ -88,7 +89,7 @@ public class DisplayUpdateDownloadWindow extends Overlay { + String source = url.concat(fileName); indicator.setVisible(true); + downloadButton.setDisable(true); try { - Utilities.downloadFile(url, null, indicator); + System.out.println("Button: " + source); + task = Utilities.downloadFile(source, null, indicator); + task.setOnSucceeded(evt -> { + if (task.getValue() == null) { + messageLabel.setText("Download failed. Please download and verify the correct file yourself from https://bitsquare.io/downloads/"); + return; + } + try { + Utilities.openDirectory(task.getValue()); + messageLabel.setText("Successfully downloaded file " + fileName); + } catch (IOException exc) { + messageLabel.setText("Unable to open download directory " + task.getValue() + " in file browser."); + exc.printStackTrace(); + } + }); } catch (IOException exception) { messageLabel.setText("Unable to download files.\n" + "Please manually download and verify the file from https://bitsquare.io/downloads"); - downloadButton.setDisable(true); - } }); closeButton = new Button("Close"); closeButton.setOnAction(e -> { hide(); + if (task != null && task.isRunning()) + task.cancel(); closeHandlerOptional.ifPresent(closeHandler -> closeHandler.run()); });