diff --git a/SPTInstaller/Helpers/DownloadCacheHelper.cs b/SPTInstaller/Helpers/DownloadCacheHelper.cs index 45ddb8b..3843030 100644 --- a/SPTInstaller/Helpers/DownloadCacheHelper.cs +++ b/SPTInstaller/Helpers/DownloadCacheHelper.cs @@ -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) diff --git a/SPTInstaller/Helpers/HttpClientProgressExtensions.cs b/SPTInstaller/Helpers/HttpClientProgressExtensions.cs index f7868de..da0ace1 100644 --- a/SPTInstaller/Helpers/HttpClientProgressExtensions.cs +++ b/SPTInstaller/Helpers/HttpClientProgressExtensions.cs @@ -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 DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination, IProgress 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(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 CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress progress = null, CancellationToken cancellationToken = default(CancellationToken)) { if (bufferSize < 0) @@ -55,5 +56,7 @@ public static class HttpClientProgressExtensions totalBytesRead += bytesRead; progress?.Report(totalBytesRead); } + + return totalBytesRead; } } \ No newline at end of file diff --git a/SPTInstaller/SPTInstaller.csproj b/SPTInstaller/SPTInstaller.csproj index df64e08..40bd3d9 100644 --- a/SPTInstaller/SPTInstaller.csproj +++ b/SPTInstaller/SPTInstaller.csproj @@ -9,8 +9,8 @@ icon.ico Assets\icon.ico Debug;Release;TEST - 2.63 - 2.63 + 2.64 + 2.64 SPT-AKI