Added events to retrieve cover art upon request using PictureStorage.

This commit is contained in:
Michael Bucari-Tovo 2021-06-30 17:32:34 -06:00
parent 2955e8b464
commit 3af84af2e2
3 changed files with 56 additions and 13 deletions

View File

@ -15,6 +15,7 @@ namespace FileLiberator
{ {
public class DownloadDecryptBook : IDecryptable public class DownloadDecryptBook : IDecryptable
{ {
public event EventHandler<Action<byte[]>> RequestCoverArt;
public event EventHandler<LibraryBook> Begin; public event EventHandler<LibraryBook> Begin;
public event EventHandler<string> DecryptBegin; public event EventHandler<string> DecryptBegin;
public event EventHandler<string> TitleDiscovered; public event EventHandler<string> TitleDiscovered;
@ -72,38 +73,30 @@ namespace FileLiberator
var aaxcDecryptDlLic = new DownloadLicense(dlLic.DownloadUrl, dlLic.AudibleKey, dlLic.AudibleIV, Resources.UserAgent); var aaxcDecryptDlLic = new DownloadLicense(dlLic.DownloadUrl, dlLic.AudibleKey, dlLic.AudibleIV, Resources.UserAgent);
var destinationDirectory = Path.GetDirectoryName(destinationDir);
if (Configuration.Instance.DownloadChapters) if (Configuration.Instance.DownloadChapters)
{ {
var contentMetadata = await api.GetLibraryBookMetadataAsync(libraryBook.Book.AudibleProductId); var contentMetadata = await api.GetLibraryBookMetadataAsync(libraryBook.Book.AudibleProductId);
var aaxcDecryptChapters = new ChapterInfo(); var aaxcDecryptChapters = new ChapterInfo();
foreach (var chap in contentMetadata?.ChapterInfo?.Chapters) foreach (var chap in contentMetadata?.ChapterInfo?.Chapters)
aaxcDecryptChapters.AddChapter(new Chapter(chap.Title, chap.StartOffsetMs, chap.LengthMs)); aaxcDecryptChapters.AddChapter(new Chapter(chap.Title, chap.StartOffsetMs, chap.LengthMs));
aaxcDownloader = await AaxcDownloadConverter.CreateAsync(destinationDirectory, aaxcDecryptDlLic, aaxcDecryptChapters); aaxcDownloader = AaxcDownloadConverter.Create(destinationDir, aaxcDecryptDlLic, aaxcDecryptChapters);
} }
else else
{ {
aaxcDownloader = await AaxcDownloadConverter.CreateAsync(destinationDirectory, aaxcDecryptDlLic); aaxcDownloader = AaxcDownloadConverter.Create(destinationDir, aaxcDecryptDlLic);
} }
aaxcDownloader.AppName = "Libation"; 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);
// override default which was set in CreateAsync // override default which was set in CreateAsync
var proposedOutputFile = Path.Combine(destinationDir, $"{PathLib.ToPathSafeString(libraryBook.Book.Title)} [{libraryBook.Book.AudibleProductId}].m4b"); var proposedOutputFile = Path.Combine(destinationDir, $"{PathLib.ToPathSafeString(libraryBook.Book.Title)} [{libraryBook.Book.AudibleProductId}].m4b");
aaxcDownloader.SetOutputFilename(proposedOutputFile); aaxcDownloader.SetOutputFilename(proposedOutputFile);
aaxcDownloader.DecryptProgressUpdate += (s, progress) => UpdateProgress?.Invoke(this, progress); aaxcDownloader.DecryptProgressUpdate += (s, progress) => UpdateProgress?.Invoke(this, progress);
aaxcDownloader.DecryptTimeRemaining += (s, remaining) => UpdateRemainingTime?.Invoke(this, remaining); aaxcDownloader.DecryptTimeRemaining += (s, remaining) => UpdateRemainingTime?.Invoke(this, remaining);
aaxcDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt;
aaxcDownloader.RetrievedTags += aaxcDownloader_RetrievedTags;
// REAL WORK DONE HERE // REAL WORK DONE HERE
var success = await Task.Run(() => aaxcDownloader.Run()); 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) private static string moveFilesToBooksDir(Book product, string outputAudioFilename)
{ {
// create final directory. move each file into it. MOVE AUDIO FILE LAST // create final directory. move each file into it. MOVE AUDIO FILE LAST

View File

@ -6,6 +6,7 @@ namespace FileLiberator
{ {
event EventHandler<string> DecryptBegin; event EventHandler<string> DecryptBegin;
event EventHandler<Action<byte[]>> RequestCoverArt;
event EventHandler<string> TitleDiscovered; event EventHandler<string> TitleDiscovered;
event EventHandler<string> AuthorsDiscovered; event EventHandler<string> AuthorsDiscovered;
event EventHandler<string> NarratorsDiscovered; event EventHandler<string> NarratorsDiscovered;

View File

@ -267,6 +267,23 @@ namespace LibationWinForms.BookLiberation
void updateRemainingTime(object _, TimeSpan remaining) => decryptDialog.UpdateRemainingTime(remaining); void updateRemainingTime(object _, TimeSpan remaining) => decryptDialog.UpdateRemainingTime(remaining);
void decryptCompleted(object _, string __) => decryptDialog.Close(); void decryptCompleted(object _, string __) => decryptDialog.Close();
void requestCoverArt(object _, Action<byte[]> 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 #endregion
#region subscribe new form to model's events #region subscribe new form to model's events
@ -278,6 +295,7 @@ namespace LibationWinForms.BookLiberation
decryptBook.CoverImageFilepathDiscovered += coverImageFilepathDiscovered; decryptBook.CoverImageFilepathDiscovered += coverImageFilepathDiscovered;
decryptBook.UpdateProgress += updateProgress; decryptBook.UpdateProgress += updateProgress;
decryptBook.UpdateRemainingTime += updateRemainingTime; decryptBook.UpdateRemainingTime += updateRemainingTime;
decryptBook.RequestCoverArt += requestCoverArt;
decryptBook.DecryptCompleted += decryptCompleted; decryptBook.DecryptCompleted += decryptCompleted;
#endregion #endregion
@ -294,13 +312,25 @@ namespace LibationWinForms.BookLiberation
decryptBook.CoverImageFilepathDiscovered -= coverImageFilepathDiscovered; decryptBook.CoverImageFilepathDiscovered -= coverImageFilepathDiscovered;
decryptBook.UpdateProgress -= updateProgress; decryptBook.UpdateProgress -= updateProgress;
decryptBook.UpdateRemainingTime -= updateRemainingTime; decryptBook.UpdateRemainingTime -= updateRemainingTime;
decryptBook.RequestCoverArt -= requestCoverArt;
decryptBook.DecryptCompleted -= decryptCompleted; decryptBook.DecryptCompleted -= decryptCompleted;
decryptBook.Cancel(); decryptBook.Cancel();
}; };
#endregion #endregion
} }
private static void onPictureCached(LibraryBook libraryBook, string picId, Action<byte[]> setArt, EventHandler<string> 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) private static (AutomatedBackupsForm, LogMe) attachToBackupsForm(IDownloadableProcessable downloadable)
{ {
#region create form and logger #region create form and logger