From 3941906d7220288d709a218de4a0b855ddc61f20 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Sat, 22 Aug 2020 07:32:39 -0400 Subject: [PATCH] Book download validation: librarybook.accountname and/or book.locale is null/blank --- .../UNTESTED/LibraryImporter.cs | 11 +++++++++ FileLiberator/UNTESTED/DownloadBook.cs | 24 +++++++++++++++++++ LibationLauncher/LibationLauncher.csproj | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/DtoImporterService/UNTESTED/LibraryImporter.cs b/DtoImporterService/UNTESTED/LibraryImporter.cs index 752ddbc8..8b1cd0de 100644 --- a/DtoImporterService/UNTESTED/LibraryImporter.cs +++ b/DtoImporterService/UNTESTED/LibraryImporter.cs @@ -23,6 +23,17 @@ namespace DtoImporterService private int upsertLibraryBooks(IEnumerable items) { + // technically, we should be able to have duplicate books from separate accounts. + // this would violate the current pk and would be difficult to deal with elsewhere: + // - what to show in the grid + // - which to consider liberated + // + // sqlite cannot alter pk. the work around is an extensive headache. it'll be fixed in pre .net5/efcore5 + // + // currently, inserting LibraryBook will throw error if the same book is in multiple accounts for the same region. + // + // CURRENT SOLUTION: don't re-insert + var currentLibraryProductIds = DbContext.Library.Select(l => l.Book.AudibleProductId).ToList(); var newItems = items.Where(dto => !currentLibraryProductIds.Contains(dto.ProductId)).ToList(); diff --git a/FileLiberator/UNTESTED/DownloadBook.cs b/FileLiberator/UNTESTED/DownloadBook.cs index dc5afe65..808b5c85 100644 --- a/FileLiberator/UNTESTED/DownloadBook.cs +++ b/FileLiberator/UNTESTED/DownloadBook.cs @@ -43,6 +43,8 @@ namespace FileLiberator private async Task downloadBookAsync(LibraryBook libraryBook, string tempAaxFilename) { + validate(libraryBook); + var api = await AudibleApiActions.GetApiAsync(libraryBook.Account, libraryBook.Book.Locale); var actualFilePath = await PerformDownloadAsync( @@ -77,6 +79,28 @@ namespace FileLiberator throw ex; } + private static void validate(LibraryBook libraryBook) + { + string errorString(string field) + => $"{errorTitle()}\r\nCannot download book. {field} is not known. Try re-importing the account which owns this book."; + + string errorTitle() + { + var title + = (libraryBook.Book.Title.Length > 53) + ? $"{libraryBook.Book.Title.Truncate(50)}..." + : libraryBook.Book.Title; + var errorBookTitle = $"{title} [{libraryBook.Book.AudibleProductId}]"; + return errorBookTitle; + }; + + if (string.IsNullOrWhiteSpace(libraryBook.Account)) + throw new Exception(errorString("Account")); + + if (string.IsNullOrWhiteSpace(libraryBook.Book.Locale)) + throw new Exception(errorString("Locale")); + } + private void moveBook(LibraryBook libraryBook, string actualFilePath) { var newAaxFilename = FileUtility.GetValidFilename( diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index 9d0834ff..fcfcebbd 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 3.1.12.221 + 3.1.12.222