Minor refactoring

This commit is contained in:
Robert McRackan 2021-07-27 11:03:26 -04:00
parent efca1f9c1d
commit b6c9a82c68
5 changed files with 26 additions and 24 deletions

View File

@ -21,8 +21,8 @@ namespace AaxDecrypter
public event EventHandler<int> DecryptProgressUpdate; public event EventHandler<int> DecryptProgressUpdate;
public event EventHandler<TimeSpan> DecryptTimeRemaining; public event EventHandler<TimeSpan> DecryptTimeRemaining;
public string AppName { get; set; } = nameof(AaxcDownloadConverter); public string AppName { get; set; } = nameof(AaxcDownloadConverter);
public string OutputFileName { get; private set; }
private string outputFileName { get; }
private string cacheDir { get; } private string cacheDir { get; }
private DownloadLicense downloadLicense { get; } private DownloadLicense downloadLicense { get; }
private AaxFile aaxFile; private AaxFile aaxFile;
@ -32,19 +32,19 @@ namespace AaxDecrypter
private StepSequence steps { get; } private StepSequence steps { get; }
private NetworkFileStreamPersister nfsPersister; private NetworkFileStreamPersister nfsPersister;
private bool isCanceled { get; set; } private bool isCanceled { get; set; }
private string jsonDownloadState => Path.Combine(cacheDir, Path.GetFileNameWithoutExtension(OutputFileName) + ".json"); private string jsonDownloadState => Path.Combine(cacheDir, Path.GetFileNameWithoutExtension(outputFileName) + ".json");
private string tempFile => PathLib.ReplaceExtension(jsonDownloadState, ".aaxc"); private string tempFile => PathLib.ReplaceExtension(jsonDownloadState, ".aaxc");
public AaxcDownloadConverter(string outFileName, string cacheDirectory, DownloadLicense dlLic, OutputFormat outputFormat) public AaxcDownloadConverter(string outFileName, string cacheDirectory, DownloadLicense dlLic, OutputFormat outputFormat)
{ {
ArgumentValidator.EnsureNotNullOrWhiteSpace(outFileName, nameof(outFileName)); ArgumentValidator.EnsureNotNullOrWhiteSpace(outFileName, nameof(outFileName));
OutputFileName = outFileName; outputFileName = outFileName;
var outDir = Path.GetDirectoryName(OutputFileName);
var outDir = Path.GetDirectoryName(outputFileName);
if (!Directory.Exists(outDir)) if (!Directory.Exists(outDir))
throw new ArgumentNullException(nameof(outDir), "Directory does not exist"); throw new ArgumentNullException(nameof(outDir), "Directory does not exist");
if (File.Exists(OutputFileName)) if (File.Exists(outputFileName))
File.Delete(OutputFileName); File.Delete(outputFileName);
if (!Directory.Exists(cacheDirectory)) if (!Directory.Exists(cacheDirectory))
throw new ArgumentNullException(nameof(cacheDirectory), "Directory does not exist"); throw new ArgumentNullException(nameof(cacheDirectory), "Directory does not exist");
@ -127,10 +127,12 @@ namespace AaxDecrypter
} }
private NetworkFileStreamPersister NewNetworkFilePersister() private NetworkFileStreamPersister NewNetworkFilePersister()
{ {
var headers = new System.Net.WebHeaderCollection(); var headers = new System.Net.WebHeaderCollection
headers.Add("User-Agent", downloadLicense.UserAgent); {
{ "User-Agent", downloadLicense.UserAgent }
};
NetworkFileStream networkFileStream = new NetworkFileStream(tempFile, new Uri(downloadLicense.DownloadUrl), 0, headers); var networkFileStream = new NetworkFileStream(tempFile, new Uri(downloadLicense.DownloadUrl), 0, headers);
return new NetworkFileStreamPersister(networkFileStream, jsonDownloadState); return new NetworkFileStreamPersister(networkFileStream, jsonDownloadState);
} }
@ -139,10 +141,10 @@ namespace AaxDecrypter
DecryptProgressUpdate?.Invoke(this, 0); DecryptProgressUpdate?.Invoke(this, 0);
if (File.Exists(OutputFileName)) if (File.Exists(outputFileName))
FileExt.SafeDelete(OutputFileName); FileExt.SafeDelete(outputFileName);
FileStream outFile = File.OpenWrite(OutputFileName); FileStream outFile = File.OpenWrite(outputFileName);
aaxFile.SetDecryptionKey(downloadLicense.AudibleKey, downloadLicense.AudibleIV); aaxFile.SetDecryptionKey(downloadLicense.AudibleKey, downloadLicense.AudibleIV);
@ -161,7 +163,7 @@ namespace AaxDecrypter
{ {
//This handles a special case where the aaxc file doesn't contain cover art and //This handles a special case where the aaxc file doesn't contain cover art and
//Libation downloaded it instead (Animal Farm). Currently only works for Mp4a files. //Libation downloaded it instead (Animal Farm). Currently only works for Mp4a files.
using var decryptedBook = new Mp4File(OutputFileName, FileAccess.ReadWrite); using var decryptedBook = new Mp4File(outputFileName, FileAccess.ReadWrite);
decryptedBook.AppleTags?.SetCoverArt(coverArt); decryptedBook.AppleTags?.SetCoverArt(coverArt);
decryptedBook.Save(); decryptedBook.Save();
decryptedBook.Close(); decryptedBook.Close();
@ -193,7 +195,7 @@ namespace AaxDecrypter
// not a critical step. its failure should not prevent future steps from running // not a critical step. its failure should not prevent future steps from running
try try
{ {
File.WriteAllText(PathLib.ReplaceExtension(OutputFileName, ".cue"), Cue.CreateContents(Path.GetFileName(OutputFileName), downloadLicense.ChapterInfo)); File.WriteAllText(PathLib.ReplaceExtension(outputFileName, ".cue"), Cue.CreateContents(Path.GetFileName(outputFileName), downloadLicense.ChapterInfo));
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -207,7 +209,7 @@ namespace AaxDecrypter
// not a critical step. its failure should not prevent future steps from running // not a critical step. its failure should not prevent future steps from running
try try
{ {
File.WriteAllText(PathLib.ReplaceExtension(OutputFileName, ".nfo"), NFO.CreateContents(AppName, aaxFile, downloadLicense.ChapterInfo)); File.WriteAllText(PathLib.ReplaceExtension(outputFileName, ".nfo"), NFO.CreateContents(AppName, aaxFile, downloadLicense.ChapterInfo));
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -97,10 +97,10 @@ namespace FileLiberator
_ => throw new NotImplementedException(), _ => throw new NotImplementedException(),
}; };
var proposedOutputFile = Path.Combine(destinationDir, $"{PathLib.ToPathSafeString(libraryBook.Book.Title)} [{libraryBook.Book.AudibleProductId}].{extension}"); var outFileName = Path.Combine(destinationDir, $"{PathLib.ToPathSafeString(libraryBook.Book.Title)} [{libraryBook.Book.AudibleProductId}].{extension}");
aaxcDownloader = new AaxcDownloadConverter(proposedOutputFile, cacheDir, aaxcDecryptDlLic, format) { AppName = "Libation" }; aaxcDownloader = new AaxcDownloadConverter(outFileName, cacheDir, aaxcDecryptDlLic, format) { AppName = "Libation" };
aaxcDownloader.DecryptProgressUpdate += (s, progress) => UpdateProgress?.Invoke(this, progress); aaxcDownloader.DecryptProgressUpdate += (s, progress) => UpdateProgress?.Invoke(this, progress);
aaxcDownloader.DecryptTimeRemaining += (s, remaining) => UpdateRemainingTime?.Invoke(this, remaining); aaxcDownloader.DecryptTimeRemaining += (s, remaining) => UpdateRemainingTime?.Invoke(this, remaining);
aaxcDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt; aaxcDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt;
@ -113,7 +113,7 @@ namespace FileLiberator
if (!success) if (!success)
return null; return null;
return aaxcDownloader.OutputFileName; return outFileName;
} }
finally finally
{ {

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> --> <!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>5.3.9.2</Version> <Version>5.3.9.6</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -200,9 +200,8 @@ namespace LibationSearchEngine
/// <param name="overwrite"></param> /// <param name="overwrite"></param>
public void CreateNewIndex(bool overwrite = true) public void CreateNewIndex(bool overwrite = true)
{ {
// 300 products // 300 titles: 200- 400 ms
// 1st run after app is started: 400ms // 1021 titles: 1777-2250 ms
// subsequent runs: 200ms
var sw = System.Diagnostics.Stopwatch.StartNew(); var sw = System.Diagnostics.Stopwatch.StartNew();
var stamps = new List<long>(); var stamps = new List<long>();
void log() => stamps.Add(sw.ElapsedMilliseconds); void log() => stamps.Add(sw.ElapsedMilliseconds);

View File

@ -179,7 +179,8 @@ namespace LibationWinForms
if (FileManager.AudibleFileStorage.Audio.Exists(productId)) if (FileManager.AudibleFileStorage.Audio.Exists(productId))
{ {
var filePath = FileManager.AudibleFileStorage.Audio.GetPath(productId); var filePath = FileManager.AudibleFileStorage.Audio.GetPath(productId);
Go.To.File(filePath); if (Go.To.File(filePath))
MessageBox.Show($"File not found:\r\n{filePath}");
return; return;
} }