diff --git a/Source/AaxDecrypter/NetworkFileStream.cs b/Source/AaxDecrypter/NetworkFileStream.cs index 259b3526..e0a6506f 100644 --- a/Source/AaxDecrypter/NetworkFileStream.cs +++ b/Source/AaxDecrypter/NetworkFileStream.cs @@ -217,40 +217,47 @@ namespace AaxDecrypter { var downloadPosition = WritePosition; var nextFlush = downloadPosition + DATA_FLUSH_SZ; - var buff = new byte[DOWNLOAD_BUFF_SZ]; - do + + try { - var bytesRead = _networkStream.Read(buff, 0, DOWNLOAD_BUFF_SZ); - _writeFile.Write(buff, 0, bytesRead); - - downloadPosition += bytesRead; - - if (downloadPosition > nextFlush) + do { - _writeFile.Flush(); - WritePosition = downloadPosition; - Update(); - nextFlush = downloadPosition + DATA_FLUSH_SZ; - downloadedPiece.Set(); - } + var bytesRead = _networkStream.Read(buff, 0, DOWNLOAD_BUFF_SZ); + _writeFile.Write(buff, 0, bytesRead); - } while (downloadPosition < ContentLength && !IsCancelled); + downloadPosition += bytesRead; - _writeFile.Close(); - _networkStream.Close(); - WritePosition = downloadPosition; - Update(); + if (downloadPosition > nextFlush) + { + _writeFile.Flush(); + WritePosition = downloadPosition; + Update(); + nextFlush = downloadPosition + DATA_FLUSH_SZ; + downloadedPiece.Set(); + } - downloadedPiece.Set(); - downloadEnded.Set(); + } while (downloadPosition < ContentLength && !IsCancelled); - if (!IsCancelled && WritePosition < ContentLength) - throw new WebException($"Downloaded size (0x{WritePosition:X10}) is less than {nameof(ContentLength)} (0x{ContentLength:X10})."); + _writeFile.Close(); + _networkStream.Close(); + WritePosition = downloadPosition; + Update(); - if (WritePosition > ContentLength) - throw new WebException($"Downloaded size (0x{WritePosition:X10}) is greater than {nameof(ContentLength)} (0x{ContentLength:X10})."); + downloadedPiece.Set(); + downloadEnded.Set(); + if (!IsCancelled && WritePosition < ContentLength) + throw new WebException($"Downloaded size (0x{WritePosition:X10}) is less than {nameof(ContentLength)} (0x{ContentLength:X10})."); + + if (WritePosition > ContentLength) + throw new WebException($"Downloaded size (0x{WritePosition:X10}) is greater than {nameof(ContentLength)} (0x{ContentLength:X10})."); + } + catch (Exception ex) + { + Serilog.Log.Error(ex, "An error was encountered while downloading {Uri}", Uri); + IsCancelled = true; + } } #endregion diff --git a/Source/LibationWinForms/GridView/GridEntry.cs b/Source/LibationWinForms/GridView/GridEntry.cs index 6d7e9cd8..b20c1287 100644 --- a/Source/LibationWinForms/GridView/GridEntry.cs +++ b/Source/LibationWinForms/GridView/GridEntry.cs @@ -100,9 +100,9 @@ namespace LibationWinForms.GridView { #nullable enable public static IEnumerable Series(this IEnumerable gridEntries) - => gridEntries.Where(i => i is SeriesEntry).Cast(); + => gridEntries.OfType(); public static IEnumerable LibraryBooks(this IEnumerable gridEntries) - => gridEntries.Where(i => i is LibraryBookEntry).Cast(); + => gridEntries.OfType(); public static LibraryBookEntry? FindBookByAsin(this IEnumerable gridEntries, string audibleProductID) => gridEntries.FirstOrDefault(i => i.AudibleProductId == audibleProductID); public static SeriesEntry? FindBookSeriesEntry(this IEnumerable gridEntries, IEnumerable matchSeries)