From 2ed1076fab2cda601a584a88052ca51a7bdc5055 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Tue, 24 Jan 2023 23:13:50 -0700 Subject: [PATCH] Cleanup --- .../AaxcDownloadMultiConverter.cs | 18 ++--- Source/AaxDecrypter/AudiobookDownloadBase.cs | 12 ++- Source/AaxDecrypter/Cue.cs | 78 +++++++++---------- .../MultiConvertFileProperties.cs | 16 ++-- .../UnencryptedAudiobookDownloader.cs | 16 ++-- 5 files changed, 68 insertions(+), 72 deletions(-) diff --git a/Source/AaxDecrypter/AaxcDownloadMultiConverter.cs b/Source/AaxDecrypter/AaxcDownloadMultiConverter.cs index 2e2fb1fd..0a8fb4e9 100644 --- a/Source/AaxDecrypter/AaxcDownloadMultiConverter.cs +++ b/Source/AaxDecrypter/AaxcDownloadMultiConverter.cs @@ -116,21 +116,19 @@ That naming may not be desirable for everyone, but it's an easy change to instea moveMoovToBeginning(workingFileStream?.Name).GetAwaiter().GetResult(); - newSplitCallback.OutputFile = createOutputFileStream(props); + newSplitCallback.OutputFile = workingFileStream = createOutputFileStream(props); newSplitCallback.TrackTitle = DownloadOptions.GetMultipartTitle(props); newSplitCallback.TrackNumber = currentChapter; newSplitCallback.TrackCount = splitChapters.Count; - FileStream createOutputFileStream(MultiConvertFileProperties multiConvertFileProperties) - { - var fileName = DownloadOptions.GetMultipartFileName(multiConvertFileProperties); - FileUtility.SaferDelete(fileName); + OnFileCreated(workingFileStream.Name); + } - workingFileStream = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); - OnFileCreated(fileName); - - return workingFileStream; - } + FileStream createOutputFileStream(MultiConvertFileProperties multiConvertFileProperties) + { + var fileName = DownloadOptions.GetMultipartFileName(multiConvertFileProperties); + FileUtility.SaferDelete(fileName); + return File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); } } diff --git a/Source/AaxDecrypter/AudiobookDownloadBase.cs b/Source/AaxDecrypter/AudiobookDownloadBase.cs index 90bcb2df..e4bafeb6 100644 --- a/Source/AaxDecrypter/AudiobookDownloadBase.cs +++ b/Source/AaxDecrypter/AudiobookDownloadBase.cs @@ -21,7 +21,6 @@ namespace AaxDecrypter public event EventHandler FileCreated; public bool IsCanceled { get; protected set; } - protected AsyncStepSequence AsyncSteps { get; } = new(); protected string OutputFileName { get; } protected IDownloadOptions DownloadOptions { get; } @@ -66,10 +65,10 @@ namespace AaxDecrypter public async Task RunAsync() { - AsyncSteps[$"Final Step: Cleanup"] = CleanupAsync; + AsyncSteps[$"Cleanup"] = CleanupAsync; (bool success, var elapsed) = await AsyncSteps.RunAsync(); - var speedup = DownloadOptions.RuntimeLength.TotalSeconds / elapsed.TotalSeconds; + var speedup = DownloadOptions.RuntimeLength / elapsed; Serilog.Log.Information($"Speedup is {speedup:F0}x realtime."); return success; @@ -119,7 +118,7 @@ namespace AaxDecrypter protected async Task Step_CreateCueAsync() { - if (!DownloadOptions.CreateCueSheet) return true; + if (!DownloadOptions.CreateCueSheet) return !IsCanceled; // not a critical step. its failure should not prevent future steps from running try @@ -159,7 +158,7 @@ namespace AaxDecrypter else FileUtility.SaferDelete(tempFilePath); - return true; + return !IsCanceled; } private NetworkFileStreamPersister OpenNetworkFileStream() @@ -184,8 +183,7 @@ namespace AaxDecrypter } finally { - if (nfsp?.NetworkFileStream is not null) - nfsp.NetworkFileStream.SpeedLimit = DownloadOptions.DownloadSpeedBps; + nfsp.NetworkFileStream.SpeedLimit = DownloadOptions.DownloadSpeedBps; } NetworkFileStreamPersister newNetworkFilePersister() diff --git a/Source/AaxDecrypter/Cue.cs b/Source/AaxDecrypter/Cue.cs index af9a0730..02753a4e 100644 --- a/Source/AaxDecrypter/Cue.cs +++ b/Source/AaxDecrypter/Cue.cs @@ -5,56 +5,56 @@ using System.Text; namespace AaxDecrypter { - public static class Cue - { - public static string CreateContents(string filePath, ChapterInfo chapters) - { - var stringBuilder = new StringBuilder(); + public static class Cue + { + public static string CreateContents(string filePath, ChapterInfo chapters) + { + var stringBuilder = new StringBuilder(); - stringBuilder.AppendLine(GetFileLine(filePath, "MP3")); + stringBuilder.AppendLine(GetFileLine(filePath, "MP3")); - var startOffset = chapters.StartOffset; + var startOffset = chapters.StartOffset; - var trackCount = 1; - foreach (var c in chapters.Chapters) - { - var startTime = c.StartOffset - startOffset; + var trackCount = 1; + foreach (var c in chapters.Chapters) + { + var startTime = c.StartOffset - startOffset; - stringBuilder.AppendLine($"TRACK {trackCount++} AUDIO"); - stringBuilder.AppendLine($" TITLE \"{c.Title}\""); - stringBuilder.AppendLine($" INDEX 01 {(int)startTime.TotalMinutes}:{startTime:ss}:{(int)(startTime.Milliseconds * 75d / 1000):D2}"); - } + stringBuilder.AppendLine($"TRACK {trackCount++} AUDIO"); + stringBuilder.AppendLine($" TITLE \"{c.Title}\""); + stringBuilder.AppendLine($" INDEX 01 {(int)startTime.TotalMinutes}:{startTime:ss}:{(int)(startTime.Milliseconds * 75d / 1000):D2}"); + } - return stringBuilder.ToString(); - } + return stringBuilder.ToString(); + } - public static void UpdateFileName(FileInfo cueFileInfo, string audioFilePath) - => UpdateFileName(cueFileInfo.FullName, audioFilePath); + public static void UpdateFileName(FileInfo cueFileInfo, string audioFilePath) + => UpdateFileName(cueFileInfo.FullName, audioFilePath); - public static void UpdateFileName(string cueFilePath, FileInfo audioFileInfo) - => UpdateFileName(cueFilePath, audioFileInfo.FullName); + public static void UpdateFileName(string cueFilePath, FileInfo audioFileInfo) + => UpdateFileName(cueFilePath, audioFileInfo.FullName); - public static void UpdateFileName(FileInfo cueFileInfo, FileInfo audioFileInfo) - => UpdateFileName(cueFileInfo.FullName, audioFileInfo.FullName); + public static void UpdateFileName(FileInfo cueFileInfo, FileInfo audioFileInfo) + => UpdateFileName(cueFileInfo.FullName, audioFileInfo.FullName); - public static void UpdateFileName(string cueFilePath, string audioFilePath) - { - var cueContents = File.ReadAllLines(cueFilePath); + public static void UpdateFileName(string cueFilePath, string audioFilePath) + { + var cueContents = File.ReadAllLines(cueFilePath); - for (var i = 0; i < cueContents.Length; i++) - { - var line = cueContents[i]; - if (!line.Trim().StartsWith("FILE") || !line.Contains(' ')) - continue; + for (var i = 0; i < cueContents.Length; i++) + { + var line = cueContents[i]; + if (!line.Trim().StartsWith("FILE") || !line.Contains(' ')) + continue; - var fileTypeBegins = line.LastIndexOf(" ") + 1; - cueContents[i] = GetFileLine(audioFilePath, line[fileTypeBegins..]); - break; - } + var fileTypeBegins = line.LastIndexOf(" ") + 1; + cueContents[i] = GetFileLine(audioFilePath, line[fileTypeBegins..]); + break; + } - File.WriteAllLines(cueFilePath, cueContents); - } + File.WriteAllLines(cueFilePath, cueContents); + } - private static string GetFileLine(string filePath, string audioType) => $"FILE {Path.GetFileName(filePath).SurroundWithQuotes()} {audioType}"; - } + private static string GetFileLine(string filePath, string audioType) => $"FILE {Path.GetFileName(filePath).SurroundWithQuotes()} {audioType}"; + } } diff --git a/Source/AaxDecrypter/MultiConvertFileProperties.cs b/Source/AaxDecrypter/MultiConvertFileProperties.cs index 58fe335a..bfb9d9f7 100644 --- a/Source/AaxDecrypter/MultiConvertFileProperties.cs +++ b/Source/AaxDecrypter/MultiConvertFileProperties.cs @@ -2,12 +2,12 @@ namespace AaxDecrypter { - public class MultiConvertFileProperties - { - public string OutputFileName { get; set; } - public int PartsPosition { get; set; } - public int PartsTotal { get; set; } - public string Title { get; set; } - public DateTime FileDate { get; } = DateTime.Now; - } + public class MultiConvertFileProperties + { + public string OutputFileName { get; set; } + public int PartsPosition { get; set; } + public int PartsTotal { get; set; } + public string Title { get; set; } + public DateTime FileDate { get; } = DateTime.Now; + } } diff --git a/Source/AaxDecrypter/UnencryptedAudiobookDownloader.cs b/Source/AaxDecrypter/UnencryptedAudiobookDownloader.cs index 64580c6d..88a43678 100644 --- a/Source/AaxDecrypter/UnencryptedAudiobookDownloader.cs +++ b/Source/AaxDecrypter/UnencryptedAudiobookDownloader.cs @@ -38,28 +38,28 @@ namespace AaxDecrypter if (double.IsNormal(estTimeRemaining)) OnDecryptTimeRemaining(TimeSpan.FromSeconds(estTimeRemaining)); - var progressPercent = (double)InputFileStream.WritePosition / InputFileStream.Length; + var progressPercent = 100d * InputFileStream.WritePosition / InputFileStream.Length; OnDecryptProgressUpdate( new DownloadProgress { - ProgressPercentage = 100 * progressPercent, - BytesReceived = (long)(InputFileStream.Length * progressPercent), + ProgressPercentage = progressPercent, + BytesReceived = InputFileStream.WritePosition, TotalBytesToReceive = InputFileStream.Length }); await Task.Delay(200); } - FinalizeDownload(); - - if (!IsCanceled) + if (IsCanceled) + return false; + else { + FinalizeDownload(); FileUtility.SaferMove(InputFileStream.SaveFilePath, OutputFileName); OnFileCreated(OutputFileName); + return true; } - - return !IsCanceled; } } }