From ee05ca4eb218596f5548f48f17b8ff42546f8554 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Mon, 10 Mar 2025 09:49:22 -0600 Subject: [PATCH] Handle corrupted LibraryScans.zip file (#1185) --- Source/ApplicationServices/LibraryCommands.cs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Source/ApplicationServices/LibraryCommands.cs b/Source/ApplicationServices/LibraryCommands.cs index d8a9d2d2..9278b084 100644 --- a/Source/ApplicationServices/LibraryCommands.cs +++ b/Source/ApplicationServices/LibraryCommands.cs @@ -233,13 +233,42 @@ namespace ApplicationServices } } + private static LogArchiver? openLogArchive(string? archivePath) + { + if (string.IsNullOrWhiteSpace(archivePath)) + return null; + + try + { + return new LogArchiver(archivePath); + } + catch (System.IO.InvalidDataException) + { + try + { + Log.Logger.Warning($"Deleting corrupted {nameof(LogArchiver)} at {archivePath}"); + FileUtility.SaferDelete(archivePath); + return new LogArchiver(archivePath); + } + catch (Exception ex) + { + Log.Logger.Error(ex, $"Failed to open {nameof(LogArchiver)} at {archivePath}"); + } + } + catch (Exception ex) + { + Log.Logger.Error(ex, $"Failed to open {nameof(LogArchiver)} at {archivePath}"); + } + return null; + } + private static async Task> scanAccountsAsync(Func> apiExtendedfunc, Account[] accounts, LibraryOptions libraryOptions) { var tasks = new List>>(); await using LogArchiver? archiver = Log.Logger.IsDebugEnabled() - ? new LogArchiver(System.IO.Path.Combine(Configuration.Instance.LibationFiles, "LibraryScans.zip")) + ? openLogArchive(System.IO.Path.Combine(Configuration.Instance.LibationFiles, "LibraryScans.zip")) : default; archiver?.DeleteAllButNewestN(20);