Formatting

This commit is contained in:
Michael Bucari-Tovo 2022-06-19 12:59:35 -06:00
parent 4658afdc20
commit 86a39f10d1
4 changed files with 123 additions and 123 deletions

View File

@ -13,27 +13,27 @@ namespace AaxDecrypter
{ {
protected override StepSequence Steps { get; } protected override StepSequence Steps { get; }
private Func<MultiConvertFileProperties, string> multipartFileNameCallback { get; } private Func<MultiConvertFileProperties, string> multipartFileNameCallback { get; }
private static TimeSpan minChapterLength { get; } = TimeSpan.FromSeconds(3); private static TimeSpan minChapterLength { get; } = TimeSpan.FromSeconds(3);
private List<string> multiPartFilePaths { get; } = new List<string>(); private List<string> multiPartFilePaths { get; } = new List<string>();
public AaxcDownloadMultiConverter(string outFileName, string cacheDirectory, DownloadOptions dlLic, public AaxcDownloadMultiConverter(string outFileName, string cacheDirectory, DownloadOptions dlLic,
Func<MultiConvertFileProperties, string> multipartFileNameCallback = null) Func<MultiConvertFileProperties, string> multipartFileNameCallback = null)
: base(outFileName, cacheDirectory, dlLic) : base(outFileName, cacheDirectory, dlLic)
{ {
Steps = new StepSequence Steps = new StepSequence
{ {
Name = "Download and Convert Aaxc To " + DownloadOptions.OutputFormat, Name = "Download and Convert Aaxc To " + DownloadOptions.OutputFormat,
["Step 1: Get Aaxc Metadata"] = Step_GetMetadata, ["Step 1: Get Aaxc Metadata"] = Step_GetMetadata,
["Step 2: Download Decrypted Audiobook"] = Step_DownloadAudiobookAsMultipleFilesPerChapter, ["Step 2: Download Decrypted Audiobook"] = Step_DownloadAudiobookAsMultipleFilesPerChapter,
["Step 3: Cleanup"] = Step_Cleanup, ["Step 3: Cleanup"] = Step_Cleanup,
}; };
this.multipartFileNameCallback = multipartFileNameCallback ?? MultiConvertFileProperties.DefaultMultipartFilename; this.multipartFileNameCallback = multipartFileNameCallback ?? MultiConvertFileProperties.DefaultMultipartFilename;
} }
/* /*
https://github.com/rmcrackan/Libation/pull/127#issuecomment-939088489 https://github.com/rmcrackan/Libation/pull/127#issuecomment-939088489
If the chapter truly is empty, that is, 0 audio frames in length, then yes it is ignored. If the chapter truly is empty, that is, 0 audio frames in length, then yes it is ignored.
@ -56,95 +56,95 @@ The book will be split into the following files:
01:41:00 - 02:05:00 | Book - 04 - Chapter 4.m4b 01:41:00 - 02:05:00 | Book - 04 - Chapter 4.m4b
That naming may not be desirable for everyone, but it's an easy change to instead use the last of the combined chapter's title in the file name. That naming may not be desirable for everyone, but it's an easy change to instead use the last of the combined chapter's title in the file name.
*/ */
private bool Step_DownloadAudiobookAsMultipleFilesPerChapter() private bool Step_DownloadAudiobookAsMultipleFilesPerChapter()
{ {
var zeroProgress = Step_DownloadAudiobook_Start(); var zeroProgress = Step_DownloadAudiobook_Start();
var chapters = DownloadOptions.ChapterInfo.Chapters; var chapters = DownloadOptions.ChapterInfo.Chapters;
// Ensure split files are at least minChapterLength in duration. // Ensure split files are at least minChapterLength in duration.
var splitChapters = new ChapterInfo(DownloadOptions.ChapterInfo.StartOffset); var splitChapters = new ChapterInfo(DownloadOptions.ChapterInfo.StartOffset);
var runningTotal = TimeSpan.Zero; var runningTotal = TimeSpan.Zero;
string title = ""; string title = "";
for (int i = 0; i < chapters.Count; i++) for (int i = 0; i < chapters.Count; i++)
{ {
if (runningTotal == TimeSpan.Zero) if (runningTotal == TimeSpan.Zero)
title = chapters[i].Title; title = chapters[i].Title;
runningTotal += chapters[i].Duration; runningTotal += chapters[i].Duration;
if (runningTotal >= minChapterLength) if (runningTotal >= minChapterLength)
{ {
splitChapters.AddChapter(title, runningTotal); splitChapters.AddChapter(title, runningTotal);
runningTotal = TimeSpan.Zero; runningTotal = TimeSpan.Zero;
} }
} }
// reset, just in case // reset, just in case
multiPartFilePaths.Clear(); multiPartFilePaths.Clear();
ConversionResult result; ConversionResult result;
AaxFile.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate; AaxFile.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate;
if (DownloadOptions.OutputFormat == OutputFormat.M4b) if (DownloadOptions.OutputFormat == OutputFormat.M4b)
result = ConvertToMultiMp4a(splitChapters); result = ConvertToMultiMp4a(splitChapters);
else else
result = ConvertToMultiMp3(splitChapters); result = ConvertToMultiMp3(splitChapters);
AaxFile.ConversionProgressUpdate -= AaxFile_ConversionProgressUpdate; AaxFile.ConversionProgressUpdate -= AaxFile_ConversionProgressUpdate;
Step_DownloadAudiobook_End(zeroProgress); Step_DownloadAudiobook_End(zeroProgress);
return result == ConversionResult.NoErrorsDetected; return result == ConversionResult.NoErrorsDetected;
} }
private ConversionResult ConvertToMultiMp4a(ChapterInfo splitChapters) private ConversionResult ConvertToMultiMp4a(ChapterInfo splitChapters)
{ {
var chapterCount = 0; var chapterCount = 0;
return AaxFile.ConvertToMultiMp4a(splitChapters, newSplitCallback => return AaxFile.ConvertToMultiMp4a(splitChapters, newSplitCallback =>
{ {
createOutputFileStream(++chapterCount, splitChapters, newSplitCallback); createOutputFileStream(++chapterCount, splitChapters, newSplitCallback);
newSplitCallback.TrackNumber = chapterCount; newSplitCallback.TrackNumber = chapterCount;
newSplitCallback.TrackCount = splitChapters.Count; newSplitCallback.TrackCount = splitChapters.Count;
}, DownloadOptions.TrimOutputToChapterLength); }, DownloadOptions.TrimOutputToChapterLength);
} }
private ConversionResult ConvertToMultiMp3(ChapterInfo splitChapters) private ConversionResult ConvertToMultiMp3(ChapterInfo splitChapters)
{ {
var chapterCount = 0; var chapterCount = 0;
return AaxFile.ConvertToMultiMp3(splitChapters, newSplitCallback => return AaxFile.ConvertToMultiMp3(splitChapters, newSplitCallback =>
{ {
createOutputFileStream(++chapterCount, splitChapters, newSplitCallback); createOutputFileStream(++chapterCount, splitChapters, newSplitCallback);
newSplitCallback.TrackNumber = chapterCount; newSplitCallback.TrackNumber = chapterCount;
newSplitCallback.TrackCount = splitChapters.Count; newSplitCallback.TrackCount = splitChapters.Count;
}, DownloadOptions.LameConfig, DownloadOptions.TrimOutputToChapterLength); }, DownloadOptions.LameConfig, DownloadOptions.TrimOutputToChapterLength);
} }
private void createOutputFileStream(int currentChapter, ChapterInfo splitChapters, NewSplitCallback newSplitCallback) private void createOutputFileStream(int currentChapter, ChapterInfo splitChapters, NewSplitCallback newSplitCallback)
{ {
var fileName = multipartFileNameCallback(new() var fileName = multipartFileNameCallback(new()
{ {
OutputFileName = OutputFileName, OutputFileName = OutputFileName,
PartsPosition = currentChapter, PartsPosition = currentChapter,
PartsTotal = splitChapters.Count, PartsTotal = splitChapters.Count,
Title = newSplitCallback?.Chapter?.Title, Title = newSplitCallback?.Chapter?.Title,
}); });
fileName = FileUtility.GetValidFilename(fileName); fileName = FileUtility.GetValidFilename(fileName);
multiPartFilePaths.Add(fileName); multiPartFilePaths.Add(fileName);
FileUtility.SaferDelete(fileName); FileUtility.SaferDelete(fileName);
newSplitCallback.OutputFile = File.Open(fileName, FileMode.OpenOrCreate); newSplitCallback.OutputFile = File.Open(fileName, FileMode.OpenOrCreate);
OnFileCreated(fileName); OnFileCreated(fileName);
} }
} }
} }

View File

@ -11,45 +11,45 @@ namespace AaxDecrypter
{ {
protected override StepSequence Steps { get; } protected override StepSequence Steps { get; }
public AaxcDownloadSingleConverter(string outFileName, string cacheDirectory, DownloadOptions dlLic) public AaxcDownloadSingleConverter(string outFileName, string cacheDirectory, DownloadOptions dlLic)
: base(outFileName, cacheDirectory, dlLic) : base(outFileName, cacheDirectory, dlLic)
{ {
Steps = new StepSequence Steps = new StepSequence
{ {
Name = "Download and Convert Aaxc To " + DownloadOptions.OutputFormat, Name = "Download and Convert Aaxc To " + DownloadOptions.OutputFormat,
["Step 1: Get Aaxc Metadata"] = Step_GetMetadata, ["Step 1: Get Aaxc Metadata"] = Step_GetMetadata,
["Step 2: Download Decrypted Audiobook"] = Step_DownloadAudiobookAsSingleFile, ["Step 2: Download Decrypted Audiobook"] = Step_DownloadAudiobookAsSingleFile,
["Step 3: Create Cue"] = Step_CreateCue, ["Step 3: Create Cue"] = Step_CreateCue,
["Step 4: Cleanup"] = Step_Cleanup, ["Step 4: Cleanup"] = Step_Cleanup,
}; };
} }
private bool Step_DownloadAudiobookAsSingleFile() private bool Step_DownloadAudiobookAsSingleFile()
{ {
var zeroProgress = Step_DownloadAudiobook_Start(); var zeroProgress = Step_DownloadAudiobook_Start();
FileUtility.SaferDelete(OutputFileName); FileUtility.SaferDelete(OutputFileName);
var outputFile = File.Open(OutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); var outputFile = File.Open(OutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
OnFileCreated(OutputFileName); OnFileCreated(OutputFileName);
AaxFile.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate; AaxFile.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate;
var decryptionResult var decryptionResult
= DownloadOptions.OutputFormat == OutputFormat.M4b = DownloadOptions.OutputFormat == OutputFormat.M4b
? AaxFile.ConvertToMp4a(outputFile, DownloadOptions.ChapterInfo, DownloadOptions.TrimOutputToChapterLength) ? AaxFile.ConvertToMp4a(outputFile, DownloadOptions.ChapterInfo, DownloadOptions.TrimOutputToChapterLength)
: AaxFile.ConvertToMp3(outputFile, DownloadOptions.LameConfig, DownloadOptions.ChapterInfo, DownloadOptions.TrimOutputToChapterLength); : AaxFile.ConvertToMp3(outputFile, DownloadOptions.LameConfig, DownloadOptions.ChapterInfo, DownloadOptions.TrimOutputToChapterLength);
AaxFile.ConversionProgressUpdate -= AaxFile_ConversionProgressUpdate; AaxFile.ConversionProgressUpdate -= AaxFile_ConversionProgressUpdate;
DownloadOptions.ChapterInfo = AaxFile.Chapters; DownloadOptions.ChapterInfo = AaxFile.Chapters;
Step_DownloadAudiobook_End(zeroProgress); Step_DownloadAudiobook_End(zeroProgress);
var success = decryptionResult == ConversionResult.NoErrorsDetected && !IsCanceled; var success = decryptionResult == ConversionResult.NoErrorsDetected && !IsCanceled;
if (success) if (success)
base.OnFileCreated(OutputFileName); base.OnFileCreated(OutputFileName);
return success; return success;
} }
} }
} }

View File

@ -41,12 +41,12 @@ namespace FileManager
//Build the filename in parts, replacing replacement parameters with //Build the filename in parts, replacing replacement parameters with
//their values, and storing the parts in a list. //their values, and storing the parts in a list.
while(!string.IsNullOrEmpty(filename)) while(!string.IsNullOrEmpty(filename))
{ {
int openIndex = filename.IndexOf('<'); int openIndex = filename.IndexOf('<');
int closeIndex = filename.IndexOf('>'); int closeIndex = filename.IndexOf('>');
if (openIndex == 0 && closeIndex > 0) if (openIndex == 0 && closeIndex > 0)
{ {
var key = filename[..(closeIndex + 1)]; var key = filename[..(closeIndex + 1)];
if (paramReplacements.ContainsKey(key)) if (paramReplacements.ContainsKey(key))
@ -57,22 +57,22 @@ namespace FileManager
filename = filename[(closeIndex + 1)..]; filename = filename[(closeIndex + 1)..];
} }
else if (openIndex > 0 && closeIndex > openIndex) else if (openIndex > 0 && closeIndex > openIndex)
{ {
var other = filename[..openIndex]; var other = filename[..openIndex];
filenameParts.Add(new StringBuilder(other)); filenameParts.Add(new StringBuilder(other));
filename = filename[openIndex..]; filename = filename[openIndex..];
} }
else else
{ {
filenameParts.Add(new StringBuilder(filename)); filenameParts.Add(new StringBuilder(filename));
filename = string.Empty; filename = string.Empty;
} }
} }
//Remove 1 character from the end of the longest filename part until //Remove 1 character from the end of the longest filename part until
//the total filename is less than max filename length //the total filename is less than max filename length
while(filenameParts.Sum(p => p.Length) > LongPath.MaxFilenameLength) while(filenameParts.Sum(p => p.Length) > LongPath.MaxFilenameLength)
{ {
int maxLength = filenameParts.Max(p => p.Length); int maxLength = filenameParts.Max(p => p.Length);
var maxEntry = filenameParts.First(p => p.Length == maxLength); var maxEntry = filenameParts.First(p => p.Length == maxLength);

View File

@ -208,7 +208,7 @@ namespace FileManager
}); });
/// <summary>Move file. No error when source does not exist. Retry up to 3 times before throwing exception.</summary> /// <summary>Move file. No error when source does not exist. Retry up to 3 times before throwing exception.</summary>
public static void SaferMove(LongPath source, LongPath destination) public static void SaferMove(LongPath source, LongPath destination)
=> retryPolicy.Execute(() => => retryPolicy.Execute(() =>
{ {
try try
@ -260,9 +260,9 @@ namespace FileManager
catch (UnauthorizedAccessException) { } catch (UnauthorizedAccessException) { }
catch (PathTooLongException) { } catch (PathTooLongException) { }
catch(Exception ex) catch(Exception ex)
{ {
} }
} }
try try