From 732695c01965fa52e049d59df6d84f90397b88c7 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Mon, 12 Jul 2021 23:27:58 -0600 Subject: [PATCH] Handle possible corrupt partial download. --- AaxDecrypter/AaxcDownloadConverter.cs | 31 +++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/AaxDecrypter/AaxcDownloadConverter.cs b/AaxDecrypter/AaxcDownloadConverter.cs index cc4d80ec..72e1a4e2 100644 --- a/AaxDecrypter/AaxcDownloadConverter.cs +++ b/AaxDecrypter/AaxcDownloadConverter.cs @@ -136,18 +136,23 @@ namespace AaxDecrypter if (File.Exists(jsonDownloadState)) { - nfsPersister = new NetworkFileStreamPersister(jsonDownloadState); - //If More thaan ~1 hour has elapsed since getting the download url, it will expire. - //The new url will be to the same file. - nfsPersister.NetworkFileStream.SetUriForSameFile(new Uri(downloadLicense.DownloadUrl)); + try + { + nfsPersister = new NetworkFileStreamPersister(jsonDownloadState); + //If More thaan ~1 hour has elapsed since getting the download url, it will expire. + //The new url will be to the same file. + nfsPersister.NetworkFileStream.SetUriForSameFile(new Uri(downloadLicense.DownloadUrl)); + } + catch + { + FileExt.SafeDelete(jsonDownloadState); + FileExt.SafeDelete(tempFile); + nfsPersister = NewNetworkFilePersister(); + } } else { - var headers = new System.Net.WebHeaderCollection(); - headers.Add("User-Agent", downloadLicense.UserAgent); - - NetworkFileStream networkFileStream = new NetworkFileStream(tempFile, new Uri(downloadLicense.DownloadUrl), 0, headers); - nfsPersister = new NetworkFileStreamPersister(networkFileStream, jsonDownloadState); + nfsPersister = NewNetworkFilePersister(); } nfsPersister.NetworkFileStream.BeginDownloading(); @@ -159,6 +164,14 @@ namespace AaxDecrypter return !isCanceled; } + private NetworkFileStreamPersister NewNetworkFilePersister() + { + var headers = new System.Net.WebHeaderCollection(); + headers.Add("User-Agent", downloadLicense.UserAgent); + + NetworkFileStream networkFileStream = new NetworkFileStream(tempFile, new Uri(downloadLicense.DownloadUrl), 0, headers); + return new NetworkFileStreamPersister(networkFileStream, jsonDownloadState); + } public bool Step3_DownloadAndCombine() {