Fix temp file reuse/cleanup. Add retain aax option.

This commit is contained in:
Michael Bucari-Tovo 2022-05-08 11:08:23 -06:00
parent 8af60b56b6
commit 05f25a88c6
13 changed files with 90 additions and 55 deletions

View file

@ -39,7 +39,7 @@ namespace FileLiberator
/// File name: final file name.
/// </summary>
public static string GetInProgressFilename(this AudioFileStorage _, LibraryBook libraryBook, string extension)
=> Templates.File.GetFilename(libraryBook.ToDto(), AudibleFileStorage.DecryptInProgressDirectory, extension);
=> Templates.File.GetFilename(libraryBook.ToDto(), AudibleFileStorage.DecryptInProgressDirectory, extension, returnFirstExisting: true);
/// <summary>
/// PDF: audio file does not exist

View file

@ -61,7 +61,12 @@ namespace FileLiberator
// decrypt failed
if (!success)
{
foreach (var tmpFile in entries)
FileUtility.SaferDelete(tmpFile.Path);
return new StatusHandler { "Decrypt failed" };
}
// moves new files from temp dir to final dest
var movedAudioFile = moveFilesToBooksDir(libraryBook, entries);
@ -92,7 +97,7 @@ namespace FileLiberator
var api = await libraryBook.GetApiAsync();
var contentLic = await api.GetDownloadLicenseAsync(libraryBook.Book.AudibleProductId);
var audiobookDlLic = BuildDownloadLicense(config, contentLic);
var audiobookDlLic = BuildDownloadOptions(config, contentLic);
var outFileName = AudibleFileStorage.Audio.GetInProgressFilename(libraryBook, audiobookDlLic.OutputFormat.ToString().ToLower());
var cacheDir = AudibleFileStorage.DownloadsInProgressDirectory;
@ -132,28 +137,28 @@ namespace FileLiberator
}
}
private static DownloadLicense BuildDownloadLicense(Configuration config, AudibleApi.Common.ContentLicense contentLic)
private static DownloadOptions BuildDownloadOptions(Configuration config, AudibleApi.Common.ContentLicense contentLic)
{
//I assume if ContentFormat == "MPEG" that the delivered file is an unencrypted mp3.
//I also assume that if DrmType != Adrm, the file will be an mp3.
//These assumptions may be wrong, and only time and bug reports will tell.
var outputFormat =
contentLic?.ContentMetadata?.ContentReference?.ContentFormat == "MPEG" ||
(config.AllowLibationFixup && config.DecryptToLossy) ?
bool encrypted = contentLic.DrmType == AudibleApi.Common.DrmType.Adrm;
var outputFormat = !encrypted || (config.AllowLibationFixup && config.DecryptToLossy) ?
OutputFormat.Mp3 : OutputFormat.M4b;
var audiobookDlLic = new DownloadLicense
var audiobookDlLic = new DownloadOptions
(
contentLic?.ContentMetadata?.ContentUrl?.OfflineUrl,
Resources.USER_AGENT
)
{
AudibleKey = contentLic?.Voucher?.Key,
AudibleIV = contentLic?.Voucher?.Iv,
OutputFormat = outputFormat,
TrimOutputToChapterLength = config.StripAudibleBrandAudio
TrimOutputToChapterLength = config.StripAudibleBrandAudio,
RetainEncryptedFile = config.RetainAaxFile && encrypted
};
if (config.AllowLibationFixup || outputFormat == OutputFormat.Mp3)