diff --git a/AaxDecrypter/UNTESTED/AaxToM4bConverter.cs b/AaxDecrypter/UNTESTED/AaxToM4bConverter.cs index 60a446a2..0e900b1c 100644 --- a/AaxDecrypter/UNTESTED/AaxToM4bConverter.cs +++ b/AaxDecrypter/UNTESTED/AaxToM4bConverter.cs @@ -105,7 +105,9 @@ namespace AaxDecrypter getASCIITag(tags.author), getASCIITag(tags.title) + ".m4b" ); - SetOutputFilename(defaultFilename); + + // set default name + SetOutputFilename(defaultFilename); await Task.Run(() => saveCover(inputFileName)); } @@ -162,6 +164,9 @@ namespace AaxDecrypter if (Path.GetExtension(outputFileName) != ".m4b") outputFileName = outputFileWithNewExt(".m4b"); + if (File.Exists(outputFileName)) + File.Delete(outputFileName); + outDir = Path.GetDirectoryName(outputFileName); } @@ -280,8 +285,9 @@ namespace AaxDecrypter return exitCode; } - // temp file names for steps 3, 4, 5 - string tempChapsPath => Path.Combine(outDir, "tempChaps.mp4"); + // temp file names for steps 3, 4, 5 + string tempChapsGuid { get; } = Guid.NewGuid().ToString().ToUpper().Replace("-", ""); + string tempChapsPath => Path.Combine(outDir, $"tempChaps_{tempChapsGuid}.mp4"); string mp4_file => outputFileWithNewExt(".mp4"); string ff_txt_file => mp4_file + ".ff.txt"; diff --git a/FileLiberator/UNTESTED/DecryptBook.cs b/FileLiberator/UNTESTED/DecryptBook.cs index 1fd72fe8..780bb828 100644 --- a/FileLiberator/UNTESTED/DecryptBook.cs +++ b/FileLiberator/UNTESTED/DecryptBook.cs @@ -94,6 +94,7 @@ namespace FileLiberator NarratorsDiscovered?.Invoke(this, converter.tags.narrator); CoverImageFilepathDiscovered?.Invoke(this, converter.coverBytes); + // override default which was set in CreateAsync converter.SetOutputFilename(proposedOutputFile); converter.DecryptProgressUpdate += (s, progress) => UpdateProgress?.Invoke(this, progress); @@ -117,46 +118,58 @@ namespace FileLiberator } private static void moveFilesToBooksDir(Book product, string outputAudioFilename) - { - // files are: temp path\author\[asin].ext - var m4bDir = new FileInfo(outputAudioFilename).Directory; - var files = m4bDir - .EnumerateFiles() - .Where(f => f.Name.ContainsInsensitive(product.AudibleProductId)) - .ToList(); + { + // create final directory. move each file into it. MOVE AUDIO FILE LAST + // new dir: safetitle_limit50char + " [" + productId + "]" - // create final directory. move each file into it. MOVE AUDIO FILE LAST - // new dir: safetitle_limit50char + " [" + productId + "]" + var destinationDir = getDestDir(product); + Directory.CreateDirectory(destinationDir); - // to prevent the paths from getting too long, we don't need after the 1st ":" for the folder - var underscoreIndex = product.Title.IndexOf(':'); - var titleDir = (underscoreIndex < 4) ? product.Title : product.Title.Substring(0, underscoreIndex); - var finalDir = FileUtility.GetValidFilename(AudibleFileStorage.BooksDirectory, titleDir, null, product.AudibleProductId); - Directory.CreateDirectory(finalDir); + var sortedFiles = getProductFilesSorted(product, outputAudioFilename); - // move audio files to the end of the collection so these files are moved last - var musicFiles = files.Where(f => AudibleFileStorage.Audio.IsFileTypeMatch(f)); - files = files - .Except(musicFiles) - .Concat(musicFiles) - .ToList(); + var musicFileExt = Path.GetExtension(outputAudioFilename).Trim('.'); - var musicFileExt = musicFiles - .Select(f => f.Extension) - .Distinct() - .Single() - .Trim('.'); + foreach (var f in sortedFiles) + { + var dest = AudibleFileStorage.Audio.IsFileTypeMatch(f) + // audio filename: safetitle_limit50char + " [" + productId + "]." + audio_ext + ? FileUtility.GetValidFilename(destinationDir, product.Title, musicFileExt, product.AudibleProductId) + // non-audio filename: safetitle_limit50char + " [" + productId + "][" + audio_ext +"]." + non_audio_ext + : FileUtility.GetValidFilename(destinationDir, product.Title, f.Extension, product.AudibleProductId, musicFileExt); - foreach (var f in files) - { - var dest = AudibleFileStorage.Audio.IsFileTypeMatch(f) - // audio filename: safetitle_limit50char + " [" + productId + "]." + audio_ext - ? FileUtility.GetValidFilename(finalDir, product.Title, musicFileExt, product.AudibleProductId) - // non-audio filename: safetitle_limit50char + " [" + productId + "][" + audio_ext +"]." + non_audio_ext - : FileUtility.GetValidFilename(finalDir, product.Title, f.Extension, product.AudibleProductId, musicFileExt); + File.Move(f.FullName, dest); + } + } - File.Move(f.FullName, dest); - } - } - } + private static string getDestDir(Book product) + { + // to prevent the paths from getting too long, we don't need after the 1st ":" for the folder + var underscoreIndex = product.Title.IndexOf(':'); + var titleDir + = underscoreIndex < 4 + ? product.Title + : product.Title.Substring(0, underscoreIndex); + var finalDir = FileUtility.GetValidFilename(AudibleFileStorage.BooksDirectory, titleDir, null, product.AudibleProductId); + return finalDir; + } + + private static List getProductFilesSorted(Book product, string outputAudioFilename) + { + // files are: temp path\author\[asin].ext + var m4bDir = new FileInfo(outputAudioFilename).Directory; + var files = m4bDir + .EnumerateFiles() + .Where(f => f.Name.ContainsInsensitive(product.AudibleProductId)) + .ToList(); + + // move audio files to the end of the collection so these files are moved last + var musicFiles = files.Where(f => AudibleFileStorage.Audio.IsFileTypeMatch(f)); + var sortedFiles = files + .Except(musicFiles) + .Concat(musicFiles) + .ToList(); + + return sortedFiles; + } + } } diff --git a/FileLiberator/UNTESTED/DownloadBook.cs b/FileLiberator/UNTESTED/DownloadBook.cs index 873df55e..a4f9d40d 100644 --- a/FileLiberator/UNTESTED/DownloadBook.cs +++ b/FileLiberator/UNTESTED/DownloadBook.cs @@ -44,6 +44,14 @@ namespace FileLiberator tempAaxFilename, (p) => api.DownloadAaxWorkaroundAsync(libraryBook.Book.AudibleProductId, tempAaxFilename, p)); + // if bad file download, a 0-33 byte file will be created + System.Threading.Thread.Sleep(100); + if (new FileInfo(actualFilePath).Length < 100) + { + File.Delete(actualFilePath); + throw new Exception("Error downloading file"); + } + return actualFilePath; } diff --git a/REFERENCE.txt b/REFERENCE.txt index 52fad71f..e080a437 100644 --- a/REFERENCE.txt +++ b/REFERENCE.txt @@ -1,6 +1,7 @@ -- begin VERSIONING --------------------------------------------------------------------------------------------------------------------- https://github.com/rmcrackan/Libation/releases +v3.1-beta.8 : Bugfix: decrypt file conflict v3.1-beta.7 : Bugfix: decrypt book with no author v3.1-beta.6 : Improved logging v3.1-beta.5 : Improved importing