Merge pull request 'delete downloaded files if the download fails' (#82) from waffle.lord/SPT-AKI-Installer:impr/remove-failed-downloads into master

Reviewed-on: CWX/SPT-AKI-Installer#82
This commit is contained in:
IsWaffle 2024-05-06 19:25:09 +00:00
commit 080cc0ab35
3 changed files with 25 additions and 8 deletions

View File

@ -1,7 +1,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using Serilog;
using SPTInstaller.Models;
namespace SPTInstaller.Helpers;
@ -146,7 +145,20 @@ public static class DownloadCacheHelper
// Use the provided extension method
using (var file = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None))
await _httpClient.DownloadDataAsync(targetLink, file, progress);
{
if (!await _httpClient.DownloadDataAsync(targetLink, file, progress))
{
Log.Error($"Download failed: {targetLink}");
outputFile.Refresh();
if (outputFile.Exists)
{
outputFile.Delete();
return null;
}
}
}
outputFile.Refresh();
@ -224,6 +236,7 @@ public static class DownloadCacheHelper
return cachedFile;
}
Log.Information($"Downloading File: {targetLink}");
return await DownloadFileAsync(fileName, targetLink, progress);
}
catch (Exception ex)
@ -250,6 +263,7 @@ public static class DownloadCacheHelper
if (CheckCacheHash(fileName, expectedHash, out var cacheFile))
return cacheFile;
Log.Information($"Downloading File: {targetLink}");
return await DownloadFileAsync(fileName, targetLink, progress);
}
catch (Exception ex)

View File

@ -6,7 +6,7 @@ namespace SPTInstaller.Helpers;
public static class HttpClientProgressExtensions
{
public static async Task DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination,
public static async Task<bool> DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination,
IProgress<double> progress = null, CancellationToken cancellationToken = default(CancellationToken))
{
using (var response = await client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead))
@ -18,20 +18,21 @@ public static class HttpClientProgressExtensions
if (progress is null || !contentLength.HasValue)
{
await download.CopyToAsync(destination);
return;
return true;
}
// Such progress and contentLength much reporting Wow!
var progressWrapper = new Progress<long>(totalBytes =>
progress.Report(GetProgressPercentage(totalBytes, contentLength.Value)));
await download.CopyToAsync(destination, 81920, progressWrapper, cancellationToken);
var readBytes = await download.CopyToAsync(destination, 81920, progressWrapper, cancellationToken);
return readBytes == contentLength.Value;
}
}
float GetProgressPercentage(float totalBytes, float currentBytes) => (totalBytes / currentBytes) * 100f;
}
static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize,
static async Task<long> CopyToAsync(this Stream source, Stream destination, int bufferSize,
IProgress<long> progress = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (bufferSize < 0)
@ -55,5 +56,7 @@ public static class HttpClientProgressExtensions
totalBytesRead += bytesRead;
progress?.Report(totalBytesRead);
}
return totalBytesRead;
}
}

View File

@ -9,8 +9,8 @@
<PackageIcon>icon.ico</PackageIcon>
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
<Configurations>Debug;Release;TEST</Configurations>
<AssemblyVersion>2.63</AssemblyVersion>
<FileVersion>2.63</FileVersion>
<AssemblyVersion>2.64</AssemblyVersion>
<FileVersion>2.64</FileVersion>
<Company>SPT-AKI</Company>
</PropertyGroup>