From e7a38ac30d7267132bcb527f2984edb9dd75929d Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Sun, 5 Mar 2023 09:23:03 -0500 Subject: [PATCH] add try catch and directory check to stream method --- Aki.Helper/DownloadCacheHelper.cs | 44 +++++++++++++++++++------------ 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/Aki.Helper/DownloadCacheHelper.cs b/Aki.Helper/DownloadCacheHelper.cs index e504bc1..e1e3c04 100644 --- a/Aki.Helper/DownloadCacheHelper.cs +++ b/Aki.Helper/DownloadCacheHelper.cs @@ -43,30 +43,40 @@ namespace SPT_AKI_Installer.Aki.Helper private static async Task ProcessInboundStreamAsync(FileInfo cacheFile, Stream downloadStream, string expectedHash = null) { - if (cacheFile.Exists) + try { - if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash)) + cacheFile.Refresh(); + Directory.CreateDirectory(_cachePath); + + if (cacheFile.Exists) { - return GenericResult.FromSuccess(); + if (expectedHash != null && FileHashHelper.CheckHash(cacheFile, expectedHash)) + { + return GenericResult.FromSuccess(); + } + + cacheFile.Delete(); + cacheFile.Refresh(); } - cacheFile.Delete(); - cacheFile.Refresh(); - } + using var patcherFileStream = cacheFile.Open(FileMode.Create); + { + await downloadStream.CopyToAsync(patcherFileStream); + } - using var patcherFileStream = cacheFile.Open(FileMode.Create); + patcherFileStream.Close(); + + if (expectedHash != null && !FileHashHelper.CheckHash(cacheFile, expectedHash)) + { + return GenericResult.FromError("Hash mismatch"); + } + + return GenericResult.FromSuccess(); + } + catch(Exception ex) { - await downloadStream.CopyToAsync(patcherFileStream); + return GenericResult.FromError(ex.Message); } - - patcherFileStream.Close(); - - if (expectedHash != null && !FileHashHelper.CheckHash(cacheFile, expectedHash)) - { - return GenericResult.FromError("Hash mismatch"); - } - - return GenericResult.FromSuccess(); } private static async Task ProcessInboundFileAsync(FileInfo cacheFile, string targetLink, IProgress progress, string expectedHash = null)