delete downloaded files if the download fails
This commit is contained in:
parent
2bd28f0796
commit
5715f97956
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user