TransitionalFileLocator to trust Book values, not hit db directly

This commit is contained in:
Robert McRackan 2021-07-29 10:20:27 -04:00
parent 621fb68cd8
commit 204e77008b
12 changed files with 40 additions and 45 deletions

View File

@ -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... // 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 enum AudioFileState { full, aax, none }
private static AudioFileState getAudioFileState(string productId) private static AudioFileState getAudioFileState(Book book)
=> TransitionalFileLocator.Audio_Exists(productId) ? AudioFileState.full => TransitionalFileLocator.Audio_Exists(book) ? AudioFileState.full
: TransitionalFileLocator.AAXC_Exists(productId) ? AudioFileState.aax : TransitionalFileLocator.AAXC_Exists(book) ? AudioFileState.aax
: AudioFileState.none; : AudioFileState.none;
public record LibraryStats(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress, int pdfsDownloaded, int pdfsNotDownloaded) { } public record LibraryStats(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress, int pdfsDownloaded, int pdfsNotDownloaded) { }
public static LibraryStats GetCounts() public static LibraryStats GetCounts()
@ -190,7 +190,7 @@ namespace ApplicationServices
var results = libraryBooks var results = libraryBooks
.AsParallel() .AsParallel()
.Select(lb => getAudioFileState(lb.Book.AudibleProductId)) .Select(lb => getAudioFileState(lb.Book))
.ToList(); .ToList();
var booksFullyBackedUp = results.Count(r => r == AudioFileState.full); var booksFullyBackedUp = results.Count(r => r == AudioFileState.full);
var booksDownloadedOnly = results.Count(r => r == AudioFileState.aax); var booksDownloadedOnly = results.Count(r => r == AudioFileState.aax);
@ -201,7 +201,7 @@ namespace ApplicationServices
var boolResults = libraryBooks var boolResults = libraryBooks
.AsParallel() .AsParallel()
.Where(lb => lb.Book.Supplements.Any()) .Where(lb => lb.Book.Supplements.Any())
.Select(lb => TransitionalFileLocator.PDF_Exists(lb.Book.AudibleProductId)) .Select(lb => TransitionalFileLocator.PDF_Exists(lb.Book))
.ToList(); .ToList();
var pdfsDownloaded = boolResults.Count(r => r); var pdfsDownloaded = boolResults.Count(r => r);
var pdfsNotDownloaded = boolResults.Count(r => !r); var pdfsNotDownloaded = boolResults.Count(r => !r);

View File

@ -9,41 +9,38 @@ namespace ApplicationServices
{ {
public static class TransitionalFileLocator 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 ?? ""; var loc = book?.UserDefinedItem?.BookLocation ?? "";
if (File.Exists(loc)) if (File.Exists(loc))
return 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; var status = book?.UserDefinedItem?.PdfStatus;
if (status.HasValue && status.Value == LiberatedStatus.Liberated) if (status.HasValue && status.Value == LiberatedStatus.Liberated)
return true; 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; var status = book?.UserDefinedItem?.BookStatus;
// true since Error == libhack // true since Error == libhack
if (status != LiberatedStatus.NotLiberated) if (status.HasValue && status.Value != LiberatedStatus.NotLiberated)
return true; 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 // 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);
} }
} }
} }

View File

@ -24,7 +24,7 @@ namespace FileLiberator
public DownloadPdf DownloadPdf { get; } = new DownloadPdf(); public DownloadPdf DownloadPdf { get; } = new DownloadPdf();
public bool Validate(LibraryBook libraryBook) 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() // do NOT use ConfigureAwait(false) on ProcessAsync()
// often calls events which prints to forms in the UI context // often calls events which prints to forms in the UI context

View File

@ -35,7 +35,7 @@ namespace FileLiberator
public bool Validate(LibraryBook libraryBook) 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)); return path?.ToLower()?.EndsWith(".m4b") == true && !File.Exists(Mp3FileName(path));
} }
@ -47,7 +47,7 @@ namespace FileLiberator
try 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 = new Mp4File(m4bPath, FileAccess.Read);
m4bBook.ConversionProgressUpdate += M4bBook_ConversionProgressUpdate; m4bBook.ConversionProgressUpdate += M4bBook_ConversionProgressUpdate;

View File

@ -34,7 +34,7 @@ namespace FileLiberator
try 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" }; return new StatusHandler { "Cannot find decrypt. Final audio file already exists" };
var outputAudioFilename = await aaxToM4bConverterDecryptAsync(AudibleFileStorage.DownloadsInProgress, AudibleFileStorage.DecryptInProgress, libraryBook); var outputAudioFilename = await aaxToM4bConverterDecryptAsync(AudibleFileStorage.DownloadsInProgress, AudibleFileStorage.DecryptInProgress, libraryBook);
@ -46,7 +46,7 @@ namespace FileLiberator
// moves files and returns dest dir // moves files and returns dest dir
_ = moveFilesToBooksDir(libraryBook.Book, outputAudioFilename); _ = moveFilesToBooksDir(libraryBook.Book, outputAudioFilename);
var finalAudioExists = ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book.AudibleProductId); var finalAudioExists = ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book);
if (!finalAudioExists) if (!finalAudioExists)
return new StatusHandler { "Cannot find final audio file after decryption" }; return new StatusHandler { "Cannot find final audio file after decryption" };
@ -219,7 +219,7 @@ namespace FileLiberator
} }
public bool Validate(LibraryBook libraryBook) public bool Validate(LibraryBook libraryBook)
=> !ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book.AudibleProductId); => !ApplicationServices.TransitionalFileLocator.Audio_Exists(libraryBook.Book);
public void Cancel() public void Cancel()
{ {

View File

@ -15,7 +15,7 @@ namespace FileLiberator
{ {
public override bool Validate(LibraryBook libraryBook) public override bool Validate(LibraryBook libraryBook)
=> !string.IsNullOrWhiteSpace(getdownloadUrl(libraryBook)) => !string.IsNullOrWhiteSpace(getdownloadUrl(libraryBook))
&& !ApplicationServices.TransitionalFileLocator.PDF_Exists(libraryBook.Book.AudibleProductId); && !ApplicationServices.TransitionalFileLocator.PDF_Exists(libraryBook.Book);
public override async Task<StatusHandler> ProcessItemAsync(LibraryBook libraryBook) public override async Task<StatusHandler> ProcessItemAsync(LibraryBook libraryBook)
{ {
@ -32,7 +32,7 @@ namespace FileLiberator
private static string getProposedDownloadFilePath(LibraryBook libraryBook) private static string getProposedDownloadFilePath(LibraryBook libraryBook)
{ {
// if audio file exists, get it's dir. else return base Book dir // 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); var file = getdownloadUrl(libraryBook);
if (existingPath != null) if (existingPath != null)
@ -61,7 +61,7 @@ namespace FileLiberator
} }
private static StatusHandler verifyDownload(LibraryBook libraryBook) 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 { "Downloaded PDF cannot be found" }
: new StatusHandler(); : new StatusHandler();
} }

View File

@ -23,13 +23,6 @@ namespace FileLiberator
.GetLibrary_Flat_NoTracking() .GetLibrary_Flat_NoTracking()
.Where(libraryBook => processable.Validate(libraryBook)); .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<StatusHandler> ProcessSingleAsync(this IProcessable processable, LibraryBook libraryBook) public static async Task<StatusHandler> ProcessSingleAsync(this IProcessable processable, LibraryBook libraryBook)
{ {
if (!processable.Validate(libraryBook)) if (!processable.Validate(libraryBook))

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> --> <!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>5.4.4.1</Version> <Version>5.4.5.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -378,6 +378,11 @@ namespace LibationLauncher
Log.Logger.Information("Begin Libation. {@DebugInfo}", new Log.Logger.Information("Begin Libation. {@DebugInfo}", new
{ {
Version = BuildVersion.ToString(), Version = BuildVersion.ToString(),
#if DEBUG
Mode = "Debug",
#else
Mode = "Release",
#endif
LogLevel_Verbose_Enabled = Log.Logger.IsVerboseEnabled(), LogLevel_Verbose_Enabled = Log.Logger.IsVerboseEnabled(),
LogLevel_Debug_Enabled = Log.Logger.IsDebugEnabled(), LogLevel_Debug_Enabled = Log.Logger.IsDebugEnabled(),

View File

@ -51,15 +51,14 @@ namespace LibationWinForms.BookLiberation
public static class ProcessorAutomationController public static class ProcessorAutomationController
{ {
public static async Task BackupSingleBookAsync(string productId, EventHandler<LibraryBook> completedAction = null) public static async Task BackupSingleBookAsync(LibraryBook libraryBook, EventHandler<LibraryBook> 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); var backupBook = getWiredUpBackupBook(completedAction);
(Action unsubscribeEvents, LogMe logMe) = attachToBackupsForm(backupBook); (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 // continue even if libraryBook is null. we'll display even that in the processing box
await new BackupSingle(logMe, backupBook, libraryBook).RunBackupAsync(); await new BackupSingle(logMe, backupBook, libraryBook).RunBackupAsync();

View File

@ -13,6 +13,7 @@ namespace LibationWinForms
private Book book => libraryBook.Book; private Book book => libraryBook.Book;
public Book GetBook() => book; public Book GetBook() => book;
public LibraryBook GetLibraryBook() => libraryBook;
public GridEntry(LibraryBook libraryBook) => this.libraryBook = libraryBook; public GridEntry(LibraryBook libraryBook) => this.libraryBook = libraryBook;
@ -26,15 +27,15 @@ namespace LibationWinForms
public enum LiberatedState { NotDownloaded, PartialDownload, Liberated } public enum LiberatedState { NotDownloaded, PartialDownload, Liberated }
[Browsable(false)] [Browsable(false)]
public LiberatedState Liberated_Status public LiberatedState Liberated_Status
=> ApplicationServices.TransitionalFileLocator.Audio_Exists(book.AudibleProductId) ? LiberatedState.Liberated => ApplicationServices.TransitionalFileLocator.Audio_Exists(book) ? LiberatedState.Liberated
: ApplicationServices.TransitionalFileLocator.AAXC_Exists(book.AudibleProductId) ? LiberatedState.PartialDownload : ApplicationServices.TransitionalFileLocator.AAXC_Exists(book) ? LiberatedState.PartialDownload
: LiberatedState.NotDownloaded; : LiberatedState.NotDownloaded;
public enum PdfState { NoPdf, Downloaded, NotDownloaded } public enum PdfState { NoPdf, Downloaded, NotDownloaded }
[Browsable(false)] [Browsable(false)]
public PdfState Pdf_Status public PdfState Pdf_Status
=> !book.Supplements.Any() ? PdfState.NoPdf => !book.Supplements.Any() ? PdfState.NoPdf
: ApplicationServices.TransitionalFileLocator.PDF_Exists(book.AudibleProductId) ? PdfState.Downloaded : ApplicationServices.TransitionalFileLocator.PDF_Exists(book) ? PdfState.Downloaded
: PdfState.NotDownloaded; : PdfState.NotDownloaded;
// displayValues is what gets displayed // displayValues is what gets displayed

View File

@ -169,19 +169,19 @@ namespace LibationWinForms
{ {
if (!isColumnValid(e, LIBERATE)) if (!isColumnValid(e, LIBERATE))
return; return;
var productId = getGridEntry(e.RowIndex).GetBook().AudibleProductId; var libraryBook = getGridEntry(e.RowIndex).GetLibraryBook();
// liberated: open explorer to file // 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)) if (!Go.To.File(filePath))
MessageBox.Show($"File not found:\r\n{filePath}"); MessageBox.Show($"File not found:\r\n{filePath}");
return; return;
} }
await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(productId, (_, __) => RefreshRow(productId)); await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(libraryBook, (_, __) => RefreshRow(libraryBook.Book.AudibleProductId));
} }
#endregion #endregion