diff --git a/FileLiberator/DownloadDecryptBook.cs b/FileLiberator/DownloadDecryptBook.cs index 8eed437e..d01d5b36 100644 --- a/FileLiberator/DownloadDecryptBook.cs +++ b/FileLiberator/DownloadDecryptBook.cs @@ -15,6 +15,7 @@ namespace FileLiberator { public class DownloadDecryptBook : IDecryptable { + public event EventHandler> RequestCoverArt; public event EventHandler Begin; public event EventHandler DecryptBegin; public event EventHandler TitleDiscovered; @@ -72,38 +73,30 @@ namespace FileLiberator var aaxcDecryptDlLic = new DownloadLicense(dlLic.DownloadUrl, dlLic.AudibleKey, dlLic.AudibleIV, Resources.UserAgent); - var destinationDirectory = Path.GetDirectoryName(destinationDir); - if (Configuration.Instance.DownloadChapters) { var contentMetadata = await api.GetLibraryBookMetadataAsync(libraryBook.Book.AudibleProductId); - var aaxcDecryptChapters = new ChapterInfo(); foreach (var chap in contentMetadata?.ChapterInfo?.Chapters) aaxcDecryptChapters.AddChapter(new Chapter(chap.Title, chap.StartOffsetMs, chap.LengthMs)); - aaxcDownloader = await AaxcDownloadConverter.CreateAsync(destinationDirectory, aaxcDecryptDlLic, aaxcDecryptChapters); + aaxcDownloader = AaxcDownloadConverter.Create(destinationDir, aaxcDecryptDlLic, aaxcDecryptChapters); } else { - aaxcDownloader = await AaxcDownloadConverter.CreateAsync(destinationDirectory, aaxcDecryptDlLic); + aaxcDownloader = AaxcDownloadConverter.Create(destinationDir, aaxcDecryptDlLic); } - aaxcDownloader.AppName = "Libation"; - - TitleDiscovered?.Invoke(this, aaxcDownloader.Title); - AuthorsDiscovered?.Invoke(this, aaxcDownloader.Author); - NarratorsDiscovered?.Invoke(this, aaxcDownloader.Narrator); - - if (aaxcDownloader.CoverArt is not null) - CoverImageFilepathDiscovered?.Invoke(this, aaxcDownloader.CoverArt); + aaxcDownloader.AppName = "Libation"; // override default which was set in CreateAsync var proposedOutputFile = Path.Combine(destinationDir, $"{PathLib.ToPathSafeString(libraryBook.Book.Title)} [{libraryBook.Book.AudibleProductId}].m4b"); aaxcDownloader.SetOutputFilename(proposedOutputFile); aaxcDownloader.DecryptProgressUpdate += (s, progress) => UpdateProgress?.Invoke(this, progress); aaxcDownloader.DecryptTimeRemaining += (s, remaining) => UpdateRemainingTime?.Invoke(this, remaining); + aaxcDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt; + aaxcDownloader.RetrievedTags += aaxcDownloader_RetrievedTags; // REAL WORK DONE HERE var success = await Task.Run(() => aaxcDownloader.Run()); @@ -120,6 +113,25 @@ namespace FileLiberator } } + private void AaxcDownloader_RetrievedCoverArt(object sender, byte[] e) + { + if (e is null && Configuration.Instance.DownloadChapters) + { + RequestCoverArt?.Invoke(this, aaxcDownloader.SetCoverArt); + } + else + { + CoverImageFilepathDiscovered?.Invoke(this, e); + } + } + + private void aaxcDownloader_RetrievedTags(object sender, AaxcTagLibFile e) + { + TitleDiscovered?.Invoke(this, e.TitleSansUnabridged); + AuthorsDiscovered?.Invoke(this, e.FirstAuthor ?? "[unknown]"); + NarratorsDiscovered?.Invoke(this, e.Narrator ?? "[unknown]"); + } + private static string moveFilesToBooksDir(Book product, string outputAudioFilename) { // create final directory. move each file into it. MOVE AUDIO FILE LAST diff --git a/FileLiberator/IDecryptable.cs b/FileLiberator/IDecryptable.cs index 1c90ccfc..568c3624 100644 --- a/FileLiberator/IDecryptable.cs +++ b/FileLiberator/IDecryptable.cs @@ -6,6 +6,7 @@ namespace FileLiberator { event EventHandler DecryptBegin; + event EventHandler> RequestCoverArt; event EventHandler TitleDiscovered; event EventHandler AuthorsDiscovered; event EventHandler NarratorsDiscovered; diff --git a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs index da9ca8a1..c25f7e6a 100644 --- a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs +++ b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs @@ -267,6 +267,23 @@ namespace LibationWinForms.BookLiberation void updateRemainingTime(object _, TimeSpan remaining) => decryptDialog.UpdateRemainingTime(remaining); void decryptCompleted(object _, string __) => decryptDialog.Close(); + + void requestCoverArt(object _, Action setArt) + { + var picDef = new FileManager.PictureDefinition(libraryBook.Book.PictureId, FileManager.PictureSize._500x500); + (bool isDefault, byte[] picture) = FileManager.PictureStorage.GetPicture(picDef); + + if (isDefault) + { + void pictureCached(object _, string pictureId) => onPictureCached(libraryBook, pictureId, setArt, pictureCached); + FileManager.PictureStorage.PictureCached += pictureCached; + } + else + setArt(picture); + } + + + #endregion #region subscribe new form to model's events @@ -278,6 +295,7 @@ namespace LibationWinForms.BookLiberation decryptBook.CoverImageFilepathDiscovered += coverImageFilepathDiscovered; decryptBook.UpdateProgress += updateProgress; decryptBook.UpdateRemainingTime += updateRemainingTime; + decryptBook.RequestCoverArt += requestCoverArt; decryptBook.DecryptCompleted += decryptCompleted; #endregion @@ -294,13 +312,25 @@ namespace LibationWinForms.BookLiberation decryptBook.CoverImageFilepathDiscovered -= coverImageFilepathDiscovered; decryptBook.UpdateProgress -= updateProgress; decryptBook.UpdateRemainingTime -= updateRemainingTime; + decryptBook.RequestCoverArt -= requestCoverArt; decryptBook.DecryptCompleted -= decryptCompleted; decryptBook.Cancel(); }; #endregion } + private static void onPictureCached(LibraryBook libraryBook, string picId, Action setArt, EventHandler pictureCacheDelegate) + { + if (picId == libraryBook.Book.PictureId) + { + FileManager.PictureStorage.PictureCached -= pictureCacheDelegate; + var picDef = new FileManager.PictureDefinition(libraryBook.Book.PictureId, FileManager.PictureSize._500x500); + (_, byte[] picture) = FileManager.PictureStorage.GetPicture(picDef); + + setArt(picture); + } + } private static (AutomatedBackupsForm, LogMe) attachToBackupsForm(IDownloadableProcessable downloadable) { #region create form and logger