From 204e77008bf98d12b9dcbcbfbaf95b118ff5d0d5 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Thu, 29 Jul 2021 10:20:27 -0400 Subject: [PATCH] TransitionalFileLocator to trust Book values, not hit db directly --- ApplicationServices/LibraryCommands.cs | 10 ++++----- .../TransitionalFileLocator.cs | 21 ++++++++----------- FileLiberator/BackupBook.cs | 2 +- FileLiberator/ConvertToMp3.cs | 4 ++-- FileLiberator/DownloadDecryptBook.cs | 6 +++--- FileLiberator/DownloadPdf.cs | 6 +++--- FileLiberator/IProcessableExt.cs | 7 ------- LibationLauncher/LibationLauncher.csproj | 2 +- LibationLauncher/Program.cs | 5 +++++ .../ProcessorAutomationController.cs | 5 ++--- LibationWinForms/GridEntry.cs | 7 ++++--- LibationWinForms/ProductsGrid.cs | 10 ++++----- 12 files changed, 40 insertions(+), 45 deletions(-) diff --git a/ApplicationServices/LibraryCommands.cs b/ApplicationServices/LibraryCommands.cs index f4b3c057..7538f6cd 100644 --- a/ApplicationServices/LibraryCommands.cs +++ b/ApplicationServices/LibraryCommands.cs @@ -179,9 +179,9 @@ namespace ApplicationServices // this is a query, not command so maybe I should make a LibraryQueries. except there's already one of those... private enum AudioFileState { full, aax, none } - private static AudioFileState getAudioFileState(string productId) - => TransitionalFileLocator.Audio_Exists(productId) ? AudioFileState.full - : TransitionalFileLocator.AAXC_Exists(productId) ? AudioFileState.aax + private static AudioFileState getAudioFileState(Book book) + => TransitionalFileLocator.Audio_Exists(book) ? AudioFileState.full + : TransitionalFileLocator.AAXC_Exists(book) ? AudioFileState.aax : AudioFileState.none; public record LibraryStats(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress, int pdfsDownloaded, int pdfsNotDownloaded) { } public static LibraryStats GetCounts() @@ -190,7 +190,7 @@ namespace ApplicationServices var results = libraryBooks .AsParallel() - .Select(lb => getAudioFileState(lb.Book.AudibleProductId)) + .Select(lb => getAudioFileState(lb.Book)) .ToList(); var booksFullyBackedUp = results.Count(r => r == AudioFileState.full); var booksDownloadedOnly = results.Count(r => r == AudioFileState.aax); @@ -201,7 +201,7 @@ namespace ApplicationServices var boolResults = libraryBooks .AsParallel() .Where(lb => lb.Book.Supplements.Any()) - .Select(lb => TransitionalFileLocator.PDF_Exists(lb.Book.AudibleProductId)) + .Select(lb => TransitionalFileLocator.PDF_Exists(lb.Book)) .ToList(); var pdfsDownloaded = boolResults.Count(r => r); var pdfsNotDownloaded = boolResults.Count(r => !r); diff --git a/ApplicationServices/TransitionalFileLocator.cs b/ApplicationServices/TransitionalFileLocator.cs index 8b25065f..e560e56b 100644 --- a/ApplicationServices/TransitionalFileLocator.cs +++ b/ApplicationServices/TransitionalFileLocator.cs @@ -9,41 +9,38 @@ namespace ApplicationServices { public static class TransitionalFileLocator { - public static string Audio_GetPath(string productId) + public static string Audio_GetPath(Book book) { - var book = DbContexts.GetContext().GetBook_Flat_NoTracking(productId); var loc = book?.UserDefinedItem?.BookLocation ?? ""; if (File.Exists(loc)) return loc; - return AudibleFileStorage.Audio.GetPath(productId); + return AudibleFileStorage.Audio.GetPath(book.AudibleProductId); } - public static bool PDF_Exists(string productId) + public static bool PDF_Exists(Book book) { - var book = DbContexts.GetContext().GetBook_Flat_NoTracking(productId); var status = book?.UserDefinedItem?.PdfStatus; if (status.HasValue && status.Value == LiberatedStatus.Liberated) return true; - return AudibleFileStorage.PDF.Exists(productId); + return AudibleFileStorage.PDF.Exists(book.AudibleProductId); } - public static bool Audio_Exists(string productId) + public static bool Audio_Exists(Book book) { - var book = DbContexts.GetContext().GetBook_Flat_NoTracking(productId); var status = book?.UserDefinedItem?.BookStatus; // true since Error == libhack - if (status != LiberatedStatus.NotLiberated) + if (status.HasValue && status.Value != LiberatedStatus.NotLiberated) return true; - return AudibleFileStorage.Audio.Exists(productId); + return AudibleFileStorage.Audio.Exists(book.AudibleProductId); } - public static bool AAXC_Exists(string productId) + public static bool AAXC_Exists(Book book) { // this one will actually stay the same. centralizing helps with organization in the interim though - return AudibleFileStorage.AAXC.Exists(productId); + return AudibleFileStorage.AAXC.Exists(book.AudibleProductId); } } } diff --git a/FileLiberator/BackupBook.cs b/FileLiberator/BackupBook.cs index 85ff0f62..718e0e7f 100644 --- a/FileLiberator/BackupBook.cs +++ b/FileLiberator/BackupBook.cs @@ -24,7 +24,7 @@ namespace FileLiberator public DownloadPdf DownloadPdf { get; } = new DownloadPdf(); public bool Validate(LibraryBook libraryBook) - => !ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book.AudibleProductId); + => !ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book); // do NOT use ConfigureAwait(false) on ProcessAsync() // often calls events which prints to forms in the UI context diff --git a/FileLiberator/ConvertToMp3.cs b/FileLiberator/ConvertToMp3.cs index 88ee925b..92eddcfa 100644 --- a/FileLiberator/ConvertToMp3.cs +++ b/FileLiberator/ConvertToMp3.cs @@ -35,7 +35,7 @@ namespace FileLiberator public bool Validate(LibraryBook libraryBook) { - var path = ApplicationServices.TransitionalFileLocator.Audio_GetPath(libraryBook.Book.AudibleProductId); + var path = ApplicationServices.TransitionalFileLocator.Audio_GetPath(libraryBook.Book); return path?.ToLower()?.EndsWith(".m4b") == true && !File.Exists(Mp3FileName(path)); } @@ -47,7 +47,7 @@ namespace FileLiberator try { - var m4bPath = ApplicationServices.TransitionalFileLocator.Audio_GetPath(libraryBook.Book.AudibleProductId); + var m4bPath = ApplicationServices.TransitionalFileLocator.Audio_GetPath(libraryBook.Book); m4bBook = new Mp4File(m4bPath, FileAccess.Read); m4bBook.ConversionProgressUpdate += M4bBook_ConversionProgressUpdate; diff --git a/FileLiberator/DownloadDecryptBook.cs b/FileLiberator/DownloadDecryptBook.cs index 90cd77c4..a2b961c6 100644 --- a/FileLiberator/DownloadDecryptBook.cs +++ b/FileLiberator/DownloadDecryptBook.cs @@ -34,7 +34,7 @@ namespace FileLiberator try { - if (ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book.AudibleProductId)) + if (ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book)) return new StatusHandler { "Cannot find decrypt. Final audio file already exists" }; var outputAudioFilename = await aaxToM4bConverterDecryptAsync(AudibleFileStorage.DownloadsInProgress, AudibleFileStorage.DecryptInProgress, libraryBook); @@ -46,7 +46,7 @@ namespace FileLiberator // moves files and returns dest dir _ = moveFilesToBooksDir(libraryBook.Book, outputAudioFilename); - var finalAudioExists = ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book.AudibleProductId); + var finalAudioExists = ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book); if (!finalAudioExists) return new StatusHandler { "Cannot find final audio file after decryption" }; @@ -219,7 +219,7 @@ namespace FileLiberator } public bool Validate(LibraryBook libraryBook) - => !ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book.AudibleProductId); + => !ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book); public void Cancel() { diff --git a/FileLiberator/DownloadPdf.cs b/FileLiberator/DownloadPdf.cs index f6b525cc..8b496011 100644 --- a/FileLiberator/DownloadPdf.cs +++ b/FileLiberator/DownloadPdf.cs @@ -15,7 +15,7 @@ namespace FileLiberator { public override bool Validate(LibraryBook libraryBook) => !string.IsNullOrWhiteSpace(getdownloadUrl(libraryBook)) - && !ApplicationServices.TransitionalFileLocator.PDF_Exists(libraryBook.Book.AudibleProductId); + && !ApplicationServices.TransitionalFileLocator.PDF_Exists(libraryBook.Book); public override async Task ProcessItemAsync(LibraryBook libraryBook) { @@ -32,7 +32,7 @@ namespace FileLiberator private static string getProposedDownloadFilePath(LibraryBook libraryBook) { // if audio file exists, get it's dir. else return base Book dir - var existingPath = Path.GetDirectoryName(ApplicationServices.TransitionalFileLocator.Audio_GetPath(libraryBook.Book.AudibleProductId)); + var existingPath = Path.GetDirectoryName(ApplicationServices.TransitionalFileLocator.Audio_GetPath(libraryBook.Book)); var file = getdownloadUrl(libraryBook); if (existingPath != null) @@ -61,7 +61,7 @@ namespace FileLiberator } private static StatusHandler verifyDownload(LibraryBook libraryBook) - => !ApplicationServices.TransitionalFileLocator.PDF_Exists(libraryBook.Book.AudibleProductId) + => !ApplicationServices.TransitionalFileLocator.PDF_Exists(libraryBook.Book) ? new StatusHandler { "Downloaded PDF cannot be found" } : new StatusHandler(); } diff --git a/FileLiberator/IProcessableExt.cs b/FileLiberator/IProcessableExt.cs index af4e33cb..eff721d2 100644 --- a/FileLiberator/IProcessableExt.cs +++ b/FileLiberator/IProcessableExt.cs @@ -23,13 +23,6 @@ namespace FileLiberator .GetLibrary_Flat_NoTracking() .Where(libraryBook => processable.Validate(libraryBook)); - public static LibraryBook GetSingleLibraryBook(string productId) - { - using var context = DbContexts.GetContext(); - var libraryBook = context.GetLibraryBook_Flat_NoTracking(productId); - return libraryBook; - } - public static async Task ProcessSingleAsync(this IProcessable processable, LibraryBook libraryBook) { if (!processable.Validate(libraryBook)) diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index b0787196..6235c009 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 5.4.4.1 + 5.4.5.1 diff --git a/LibationLauncher/Program.cs b/LibationLauncher/Program.cs index e281fdb0..43ad8a62 100644 --- a/LibationLauncher/Program.cs +++ b/LibationLauncher/Program.cs @@ -378,6 +378,11 @@ namespace LibationLauncher Log.Logger.Information("Begin Libation. {@DebugInfo}", new { Version = BuildVersion.ToString(), +#if DEBUG + Mode = "Debug", +#else + Mode = "Release", +#endif LogLevel_Verbose_Enabled = Log.Logger.IsVerboseEnabled(), LogLevel_Debug_Enabled = Log.Logger.IsDebugEnabled(), diff --git a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs index 0023cc5a..f378bae7 100644 --- a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs +++ b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs @@ -51,15 +51,14 @@ namespace LibationWinForms.BookLiberation public static class ProcessorAutomationController { - public static async Task BackupSingleBookAsync(string productId, EventHandler completedAction = null) + public static async Task BackupSingleBookAsync(LibraryBook libraryBook, EventHandler completedAction = null) { - Serilog.Log.Logger.Information("Begin " + nameof(BackupSingleBookAsync) + " {@DebugInfo}", new { productId }); + Serilog.Log.Logger.Information("Begin backup single {@DebugInfo}", new { libraryBook?.Book?.AudibleProductId }); var backupBook = getWiredUpBackupBook(completedAction); (Action unsubscribeEvents, LogMe logMe) = attachToBackupsForm(backupBook); - var libraryBook = IProcessableExt.GetSingleLibraryBook(productId); // continue even if libraryBook is null. we'll display even that in the processing box await new BackupSingle(logMe, backupBook, libraryBook).RunBackupAsync(); diff --git a/LibationWinForms/GridEntry.cs b/LibationWinForms/GridEntry.cs index 92b41699..c6e3e42b 100644 --- a/LibationWinForms/GridEntry.cs +++ b/LibationWinForms/GridEntry.cs @@ -13,6 +13,7 @@ namespace LibationWinForms private Book book => libraryBook.Book; public Book GetBook() => book; + public LibraryBook GetLibraryBook() => libraryBook; public GridEntry(LibraryBook libraryBook) => this.libraryBook = libraryBook; @@ -26,15 +27,15 @@ namespace LibationWinForms public enum LiberatedState { NotDownloaded, PartialDownload, Liberated } [Browsable(false)] public LiberatedState Liberated_Status - => ApplicationServices.TransitionalFileLocator.Audio_Exists(book.AudibleProductId) ? LiberatedState.Liberated - : ApplicationServices.TransitionalFileLocator.AAXC_Exists(book.AudibleProductId) ? LiberatedState.PartialDownload + => ApplicationServices.TransitionalFileLocator.Audio_Exists(book) ? LiberatedState.Liberated + : ApplicationServices.TransitionalFileLocator.AAXC_Exists(book) ? LiberatedState.PartialDownload : LiberatedState.NotDownloaded; public enum PdfState { NoPdf, Downloaded, NotDownloaded } [Browsable(false)] public PdfState Pdf_Status => !book.Supplements.Any() ? PdfState.NoPdf - : ApplicationServices.TransitionalFileLocator.PDF_Exists(book.AudibleProductId) ? PdfState.Downloaded + : ApplicationServices.TransitionalFileLocator.PDF_Exists(book) ? PdfState.Downloaded : PdfState.NotDownloaded; // displayValues is what gets displayed diff --git a/LibationWinForms/ProductsGrid.cs b/LibationWinForms/ProductsGrid.cs index ed400ed3..7defd867 100644 --- a/LibationWinForms/ProductsGrid.cs +++ b/LibationWinForms/ProductsGrid.cs @@ -169,19 +169,19 @@ namespace LibationWinForms { if (!isColumnValid(e, LIBERATE)) return; - - var productId = getGridEntry(e.RowIndex).GetBook().AudibleProductId; + + var libraryBook = getGridEntry(e.RowIndex).GetLibraryBook(); // liberated: open explorer to file - if (TransitionalFileLocator.Audio_Exists(productId)) + if (TransitionalFileLocator.Audio_Exists(libraryBook.Book)) { - var filePath = TransitionalFileLocator.Audio_GetPath(productId); + var filePath = TransitionalFileLocator.Audio_GetPath(libraryBook.Book); if (!Go.To.File(filePath)) MessageBox.Show($"File not found:\r\n{filePath}"); return; } - await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(productId, (_, __) => RefreshRow(productId)); + await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(libraryBook, (_, __) => RefreshRow(libraryBook.Book.AudibleProductId)); } #endregion