diff --git a/Source/FileLiberator/DownloadDecryptBook.cs b/Source/FileLiberator/DownloadDecryptBook.cs index b03334a0..ea9d3125 100644 --- a/Source/FileLiberator/DownloadDecryptBook.cs +++ b/Source/FileLiberator/DownloadDecryptBook.cs @@ -71,6 +71,8 @@ namespace FileLiberator // moves new files from temp dir to final dest var movedAudioFile = moveFilesToBooksDir(libraryBook, entries); + DownloadCoverArt(libraryBook); + // decrypt failed if (!movedAudioFile) return new StatusHandler { "Cannot find final audio file after decryption" }; @@ -187,31 +189,27 @@ namespace FileLiberator } } - NAudio.Lame.LameConfig lameConfig = new(); - - - lameConfig.Mode = NAudio.Lame.MPEGMode.Mono; + audiobookDlLic.LameConfig = new(); + audiobookDlLic.LameConfig.Mode = NAudio.Lame.MPEGMode.Mono; if (config.LameTargetBitrate) { if (config.LameConstantBitrate) - lameConfig.BitRate = config.LameBitrate; + audiobookDlLic.LameConfig.BitRate = config.LameBitrate; else { - lameConfig.ABRRateKbps = config.LameBitrate; - lameConfig.VBR = NAudio.Lame.VBRMode.ABR; - lameConfig.WriteVBRTag = true; + audiobookDlLic.LameConfig.ABRRateKbps = config.LameBitrate; + audiobookDlLic.LameConfig.VBR = NAudio.Lame.VBRMode.ABR; + audiobookDlLic.LameConfig.WriteVBRTag = true; } } else { - lameConfig.VBR = NAudio.Lame.VBRMode.Default; - lameConfig.VBRQuality = config.LameVBRQuality; - lameConfig.WriteVBRTag = true; + audiobookDlLic.LameConfig.VBR = NAudio.Lame.VBRMode.Default; + audiobookDlLic.LameConfig.VBRQuality = config.LameVBRQuality; + audiobookDlLic.LameConfig.WriteVBRTag = true; } - audiobookDlLic.LameConfig = lameConfig; - return audiobookDlLic; } @@ -277,5 +275,34 @@ namespace FileLiberator return true; } + + private void DownloadCoverArt(LibraryBook libraryBook) + { + var destinationDir = AudibleFileStorage.Audio.GetDestinationDirectory(libraryBook); + var coverPath = AudibleFileStorage.Audio.GetBooksDirectoryFilename(libraryBook, ".jpg"); + coverPath = Path.Combine(destinationDir, Path.GetFileName(coverPath)); + + try + { + if (File.Exists(coverPath)) + FileUtility.SaferDelete(coverPath); + + (string picId, PictureSize size) = libraryBook.Book.PictureLarge is null ? + (libraryBook.Book.PictureId, PictureSize.Native) : + (libraryBook.Book.PictureLarge, PictureSize.Native); + + var picBytes = PictureStorage.GetPictureSynchronously(new PictureDefinition(picId, size)); + + if (picBytes.Length > 0) + 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); + } + } + } } diff --git a/Source/FileLiberator/Processable.cs b/Source/FileLiberator/Processable.cs index 56ca47a5..da66f0f6 100644 --- a/Source/FileLiberator/Processable.cs +++ b/Source/FileLiberator/Processable.cs @@ -48,38 +48,9 @@ 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.PictureLarge is null ? - (libraryBook.Book.PictureId, PictureSize.Native) : - (libraryBook.Book.PictureLarge, PictureSize.Native); - - 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)