Download to tmpdir works, can be cancelled by closing update alert, and shows directory upon completion (only tested on Linux)

This commit is contained in:
SimonTuberlin 2017-03-22 16:51:49 +01:00 committed by Mike Rosseel
parent fb5869602e
commit 5ac1ba2e88
3 changed files with 30 additions and 9 deletions

View file

@ -44,9 +44,11 @@ public class DownloadUtil extends Task<File> {
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<File> {
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);

View file

@ -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<File> 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() {

View file

@ -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<DisplayUpdateDownloadWindow> {
private static final Logger log = LoggerFactory.getLogger(DisplayUpdateDownloadWindow.class);
private Alert alert;
private Task<File> task;
///////////////////////////////////////////////////////////////////////////////////////////
@ -88,7 +89,7 @@ public class DisplayUpdateDownloadWindow extends Overlay<DisplayUpdateDownloadWi
Button downloadButton = addButton(gridPane, ++rowIndex, "Download now");
// TODO How do we get the right URL for the download?
String url = "https://bitsquare.io/downloads" + File.separator + alert.version + File.separator;
String url = "https://github.com/bitsquare/bitsquare/releases/download/" + "v" + alert.version + "/" ;
String fileName;
if (Utilities.isOSX())
fileName = "Bitsquare-" + alert.version + ".dmg";
@ -97,26 +98,43 @@ public class DisplayUpdateDownloadWindow extends Overlay<DisplayUpdateDownloadWi
else if (Utilities.isLinux())
fileName = "Bitsquare-" + Utilities.getOSArchitecture() + "bit-" + alert.version + ".deb";
else {
fileName = "";
downloadButton.setDisable(true);
messageLabel.setText("Unable to determine the correct installer. Pleaase manually download and verify " +
"the correct file from https://bitsquare.io/downloads");
}
downloadButton.setOnAction(e -> {
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());
});