Skip to content

Commit

Permalink
Fix some issues with file downloaders
Browse files Browse the repository at this point in the history
-Make sure IsDownloading is set
-Consistently delete file if canceled in both web and local file downloader
-A few other tweaks
  • Loading branch information
Deadpikle committed Apr 4, 2024
1 parent 6237314 commit 1cd2284
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 17 additions & 1 deletion src/NetSparkle/Downloaders/LocalFileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class LocalFileDownloader : IUpdateDownloader, IDisposable
{
private ILogger _logger;
private CancellationTokenSource _cancellationTokenSource;
private string _downloadFileLocation;

/// <summary>
/// Default constructor for the local file downloader.
Expand All @@ -31,6 +32,7 @@ public LocalFileDownloader()
{
_logger = new LogWriter();
_cancellationTokenSource = new CancellationTokenSource();
_downloadFileLocation = "";
}

/// <summary>
Expand All @@ -42,6 +44,7 @@ public LocalFileDownloader(ILogger logger)
{
_logger = logger;
_cancellationTokenSource = new CancellationTokenSource();
_downloadFileLocation = "";
}

/// <summary>
Expand Down Expand Up @@ -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 {}
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -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
{
Expand All @@ -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;
Expand All @@ -134,14 +147,17 @@ 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;
}
}

private void Cancel(string destinationFile)
{
if (File.Exists(destinationFile))
{
File.Delete(destinationFile);
try {
File.Delete(destinationFile);
} catch {}
}
IsDownloading = false;
}
Expand Down
15 changes: 11 additions & 4 deletions src/NetSparkle/Downloaders/WebFileDownloader.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -26,6 +22,7 @@ public class WebFileDownloader : IUpdateDownloader, IDisposable
private HttpClient _httpClient;
private ILogger _logger;
private CancellationTokenSource _cts;
private string _downloadFileLocation;

/// <summary>
/// Default constructor for the web client file downloader.
Expand Down Expand Up @@ -78,6 +75,7 @@ public void PrepareToDownloadFile()
{
try
{
_cts?.Cancel();
_httpClient.CancelPendingRequests();
} catch {}
}
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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<string> RetrieveDestinationFileNameAsyncForUri(Uri uri)
Expand Down

0 comments on commit 1cd2284

Please sign in to comment.