From 1cd2284c41bbe85d41566915965ad2acdb1a61f5 Mon Sep 17 00:00:00 2001 From: Michael Babienco Date: Thu, 4 Apr 2024 20:16:23 +0900 Subject: [PATCH] Fix some issues with file downloaders -Make sure IsDownloading is set -Consistently delete file if canceled in both web and local file downloader -A few other tweaks --- .../Downloaders/LocalFileDownloader.cs | 18 +++++++++++++++++- .../Downloaders/WebFileDownloader.cs | 15 +++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/NetSparkle/Downloaders/LocalFileDownloader.cs b/src/NetSparkle/Downloaders/LocalFileDownloader.cs index 57785816..06b82a14 100644 --- a/src/NetSparkle/Downloaders/LocalFileDownloader.cs +++ b/src/NetSparkle/Downloaders/LocalFileDownloader.cs @@ -23,6 +23,7 @@ public class LocalFileDownloader : IUpdateDownloader, IDisposable { private ILogger _logger; private CancellationTokenSource _cancellationTokenSource; + private string _downloadFileLocation; /// /// Default constructor for the local file downloader. @@ -31,6 +32,7 @@ public LocalFileDownloader() { _logger = new LogWriter(); _cancellationTokenSource = new CancellationTokenSource(); + _downloadFileLocation = ""; } /// @@ -42,6 +44,7 @@ public LocalFileDownloader(ILogger logger) { _logger = logger; _cancellationTokenSource = new CancellationTokenSource(); + _downloadFileLocation = ""; } /// @@ -72,6 +75,14 @@ public void CancelDownload() { _cancellationTokenSource.Cancel(); DownloadFileCompleted?.Invoke(this, new AsyncCompletedEventArgs(null, true, null)); + _cancellationTokenSource = new CancellationTokenSource(); + IsDownloading = false; + if (_downloadFileLocation != "" && File.Exists(_downloadFileLocation)) + { + try { + File.Delete(_downloadFileLocation); + } catch {} + } } /// @@ -102,6 +113,7 @@ private async Task CopyFileAsync(string sourceFile, string destinationFile, Canc var bufferSize = 4096; var buffer = new byte[bufferSize]; int bytesRead = 0; + _downloadFileLocation = destinationFile; long totalRead = 0; try { @@ -118,6 +130,7 @@ private async Task CopyFileAsync(string sourceFile, string destinationFile, Canc await destinationStream.WriteAsync(buffer, 0, bytesRead, cancellationToken); if (cancellationToken.IsCancellationRequested) { + destinationStream.Close(); Cancel(destinationFile); wasCanceled = true; break; @@ -134,6 +147,7 @@ private async Task CopyFileAsync(string sourceFile, string destinationFile, Canc LogWriter.PrintMessage("Error: {0}", e.Message); Cancel(destinationFile); DownloadFileCompleted?.Invoke(this, new AsyncCompletedEventArgs(e, true, null)); + IsDownloading = false; } } @@ -141,7 +155,9 @@ private void Cancel(string destinationFile) { if (File.Exists(destinationFile)) { - File.Delete(destinationFile); + try { + File.Delete(destinationFile); + } catch {} } IsDownloading = false; } diff --git a/src/NetSparkle/Downloaders/WebFileDownloader.cs b/src/NetSparkle/Downloaders/WebFileDownloader.cs index 06f00a5b..dd1f4644 100644 --- a/src/NetSparkle/Downloaders/WebFileDownloader.cs +++ b/src/NetSparkle/Downloaders/WebFileDownloader.cs @@ -1,13 +1,9 @@ using NetSparkleUpdater.Events; using NetSparkleUpdater.Interfaces; using System; -using System.Collections.Generic; using System.ComponentModel; using System.IO; -using System.Net; using System.Net.Http; -using System.Net.Http.Headers; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -26,6 +22,7 @@ public class WebFileDownloader : IUpdateDownloader, IDisposable private HttpClient _httpClient; private ILogger _logger; private CancellationTokenSource _cts; + private string _downloadFileLocation; /// /// Default constructor for the web client file downloader. @@ -78,6 +75,7 @@ public void PrepareToDownloadFile() { try { + _cts?.Cancel(); _httpClient.CancelPendingRequests(); } catch {} } @@ -164,6 +162,7 @@ private async Task StartFileDownloadAsync(Uri uri, string downloadFilePath) long totalLength = 0; long totalRead = 0; long readCount = 0; + _downloadFileLocation = downloadFilePath; using (FileStream fileStream = new FileStream(downloadFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 8192, true)) using (Stream contentStream = await response.Content.ReadAsStreamAsync()) { @@ -223,6 +222,14 @@ public void CancelDownload() _cts.Cancel(); _httpClient.CancelPendingRequests(); } catch {} + if (File.Exists(_downloadFileLocation)) + { + try { + File.Delete(_downloadFileLocation); + } catch {} + } + IsDownloading = false; + _cts = new CancellationTokenSource(); } private async Task RetrieveDestinationFileNameAsyncForUri(Uri uri)