From c7bac99a906b27fe59ccb670acf3ea8a985186a0 Mon Sep 17 00:00:00 2001 From: Laurent Clouet <4409640-laurent.clouet@users.noreply.gitlab.com> Date: Mon, 30 Jun 2025 09:58:10 +0200 Subject: [PATCH] Fix: #60 wrong progress on download error --- .../com/sheepit/client/network/DownloadProgress.java | 12 ++++++++++++ .../com/sheepit/client/network/ServerRequest.java | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sheepit/client/network/DownloadProgress.java b/src/main/java/com/sheepit/client/network/DownloadProgress.java index 92ad74a..2efea5a 100644 --- a/src/main/java/com/sheepit/client/network/DownloadProgress.java +++ b/src/main/java/com/sheepit/client/network/DownloadProgress.java @@ -41,6 +41,18 @@ public class DownloadProgress { public synchronized void addProgress(long progress) { this.partial += progress; + this.updateUI(); + } + + /** + * In case of an issue on a download, remove the already downloaded progress + */ + public synchronized void removeProgress(long progress) { + this.partial -= progress; + this.updateUI(); + } + + private void updateUI() { if (this.total != 0) { gui.status(String.format(this.guiPattern + " %.0f %%", 100.0f * this.partial / this.total)); } diff --git a/src/main/java/com/sheepit/client/network/ServerRequest.java b/src/main/java/com/sheepit/client/network/ServerRequest.java index ea699b2..ff1d658 100644 --- a/src/main/java/com/sheepit/client/network/ServerRequest.java +++ b/src/main/java/com/sheepit/client/network/ServerRequest.java @@ -518,7 +518,8 @@ public class ServerRequest extends Thread { this.log.debug("Server::HTTPGetFile destination: " + destination_); InputStream is = null; OutputStream output = null; - + + long written = 0; try { Response response = this.HTTPRequest(url_); @@ -534,7 +535,6 @@ public class ServerRequest extends Thread { // only update the gui every 1MB byte[] buffer = new byte[1024 * 1024]; int len; - long written = 0; this.log.debug("Downloading file from " + response.request().url().host()); LocalDateTime startRequestTime = LocalDateTime.now(); @@ -574,6 +574,8 @@ public class ServerRequest extends Thread { throw new SheepItExceptionNoSpaceLeftOnDevice(); } + gui_.getDownloadProgress().removeProgress(written); + StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); this.log.error("Server::HTTPGetFile Exception " + e + " stacktrace " + sw.toString());