From 48ffc40abb9c8cf1b01d573447381caf037faf12 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Mon, 9 May 2022 23:53:10 -0600 Subject: [PATCH] Move cover download to processable --- Source/FileLiberator/Processable.cs | 30 +++++++++++++++++++ .../ProcessorAutomationController.cs | 18 +---------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/Source/FileLiberator/Processable.cs b/Source/FileLiberator/Processable.cs index 0c7743e7..66e36d1c 100644 --- a/Source/FileLiberator/Processable.cs +++ b/Source/FileLiberator/Processable.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using DataLayer; using Dinah.Core; using Dinah.Core.ErrorHandling; +using LibationFileManager; namespace FileLiberator { @@ -47,9 +48,38 @@ namespace FileLiberator = (await ProcessAsync(libraryBook)) ?? new StatusHandler { "Processable should never return a null status" }; + if (status.IsSuccess) + DownloadCoverArt(libraryBook); + return status; } + private void DownloadCoverArt(LibraryBook libraryBook) + { + var destinationDir = AudibleFileStorage.Audio.GetDestinationDirectory(libraryBook); + var coverPath = FileManager.FileUtility.GetValidFilename(System.IO.Path.Combine(destinationDir, "Cover.jpg"), "", true); + + if (System.IO.File.Exists(coverPath)) return; + + try + { + (string picId, PictureSize size) = libraryBook.Book.PictureId_1215 is null ? + (libraryBook.Book.PictureId, PictureSize._500x500) : + (libraryBook.Book.PictureId_1215, PictureSize._1215x1215); + + var picBytes = PictureStorage.GetPictureSynchronously(new PictureDefinition(picId, size)); + + if (picBytes.Length > 0) + System.IO.File.WriteAllBytes(coverPath, picBytes); + } + catch (Exception ex) + { + //Failure to download cover art should not be + //considered a failure to download the book + Serilog.Log.Logger.Error(ex.Message); + } + } + public async Task TryProcessAsync(LibraryBook libraryBook) => Validate(libraryBook) ? await ProcessAsync(libraryBook) diff --git a/Source/LibationWinForms/BookLiberation/ProcessorAutomationController.cs b/Source/LibationWinForms/BookLiberation/ProcessorAutomationController.cs index a9f751aa..dca83b84 100644 --- a/Source/LibationWinForms/BookLiberation/ProcessorAutomationController.cs +++ b/Source/LibationWinForms/BookLiberation/ProcessorAutomationController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading.Tasks; using System.Windows.Forms; using DataLayer; @@ -201,23 +201,7 @@ namespace LibationWinForms.BookLiberation var statusHandler = await Processable.ProcessSingleAsync(libraryBook, validate); if (statusHandler.IsSuccess) - { - var destinationDir = AudibleFileStorage.Audio.GetDestinationDirectory(libraryBook); - var coverPath = FileManager.FileUtility.GetValidFilename(System.IO.Path.Combine(destinationDir, "Cover.jpg"), "", true); - - try - { - var picBytes = PictureStorage.GetPictureSynchronously(new PictureDefinition(libraryBook.Book.PictureId_1215, PictureSize._1215x1215)); - System.IO.File.WriteAllBytes(coverPath, picBytes); - } - catch(Exception ex) - { - LogMe.Error(ex.Message); - } - //Failure to download cover art should not be - //considered a failure to download the book return true; - } foreach (var errorMessage in statusHandler.Errors) LogMe.Error(errorMessage);