Combine Streamable and Processable, remove unused events.

This commit is contained in:
Michael Bucari-Tovo 2022-06-13 21:40:37 -06:00
parent eb513f563e
commit 2bc74d5378
5 changed files with 65 additions and 114 deletions

View File

@ -20,11 +20,9 @@ namespace FileLiberator
private long fileSize; private long fileSize;
private static string Mp3FileName(string m4bPath) => Path.ChangeExtension(m4bPath ?? "", ".mp3"); private static string Mp3FileName(string m4bPath) => Path.ChangeExtension(m4bPath ?? "", ".mp3");
private bool cancelled = false;
public override void Cancel() public override void Cancel()
{ {
m4bBook?.Cancel(); m4bBook?.Cancel();
cancelled = true;
} }
public static bool ValidateMp3(LibraryBook libraryBook) public static bool ValidateMp3(LibraryBook libraryBook)
@ -39,8 +37,6 @@ namespace FileLiberator
{ {
OnBegin(libraryBook); OnBegin(libraryBook);
OnStreamingBegin($"Begin converting {libraryBook} to mp3");
try try
{ {
var m4bPath = AudibleFileStorage.Audio.GetPath(libraryBook.Book.AudibleProductId); var m4bPath = AudibleFileStorage.Audio.GetPath(libraryBook.Book.AudibleProductId);
@ -73,7 +69,6 @@ namespace FileLiberator
} }
finally finally
{ {
OnStreamingCompleted($"Completed converting to mp3: {libraryBook.Book.Title}");
OnCompleted(libraryBook); OnCompleted(libraryBook);
} }
} }

View File

@ -94,10 +94,6 @@ namespace FileLiberator
} }
private async Task<bool> downloadAudiobookAsync(LibraryBook libraryBook) private async Task<bool> downloadAudiobookAsync(LibraryBook libraryBook)
{
OnStreamingBegin($"Begin decrypting {libraryBook}");
try
{ {
var config = Configuration.Instance; var config = Configuration.Instance;
@ -139,11 +135,6 @@ namespace FileLiberator
return success; return success;
} }
finally
{
OnStreamingCompleted($"Completed downloading and decrypting {libraryBook.Book.Title}");
}
}
private static DownloadOptions BuildDownloadOptions(Configuration config, AudibleApi.Common.ContentLicense contentLic) private static DownloadOptions BuildDownloadOptions(Configuration config, AudibleApi.Common.ContentLicense contentLic)
{ {
@ -175,7 +166,6 @@ namespace FileLiberator
if (config.AllowLibationFixup || outputFormat == OutputFormat.Mp3) if (config.AllowLibationFixup || outputFormat == OutputFormat.Mp3)
{ {
long startMs = audiobookDlLic.TrimOutputToChapterLength ? long startMs = audiobookDlLic.TrimOutputToChapterLength ?
contentLic.ContentMetadata.ChapterInfo.BrandIntroDurationMs : 0; contentLic.ContentMetadata.ChapterInfo.BrandIntroDurationMs : 0;

View File

@ -56,10 +56,6 @@ namespace FileLiberator
=> libraryBook?.Book?.Supplements?.FirstOrDefault()?.Url; => libraryBook?.Book?.Supplements?.FirstOrDefault()?.Url;
private async Task<string> downloadPdfAsync(LibraryBook libraryBook, string proposedDownloadFilePath) private async Task<string> downloadPdfAsync(LibraryBook libraryBook, string proposedDownloadFilePath)
{
OnStreamingBegin(proposedDownloadFilePath);
try
{ {
var api = await libraryBook.GetApiAsync(); var api = await libraryBook.GetApiAsync();
var downloadUrl = await api.GetPdfDownloadLinkAsync(libraryBook.Book.AudibleProductId); var downloadUrl = await api.GetPdfDownloadLinkAsync(libraryBook.Book.AudibleProductId);
@ -74,11 +70,6 @@ namespace FileLiberator
OnStatusUpdate(actualDownloadedFilePath); OnStatusUpdate(actualDownloadedFilePath);
return actualDownloadedFilePath; return actualDownloadedFilePath;
} }
finally
{
OnStreamingCompleted(proposedDownloadFilePath);
}
}
private static StatusHandler verifyDownload(string actualDownloadedFilePath) private static StatusHandler verifyDownload(string actualDownloadedFilePath)
=> !File.Exists(actualDownloadedFilePath) => !File.Exists(actualDownloadedFilePath)

View File

@ -5,17 +5,22 @@ using System.Threading.Tasks;
using DataLayer; using DataLayer;
using Dinah.Core; using Dinah.Core;
using Dinah.Core.ErrorHandling; using Dinah.Core.ErrorHandling;
using Dinah.Core.Net.Http;
using LibationFileManager; using LibationFileManager;
namespace FileLiberator namespace FileLiberator
{ {
public abstract class Processable : Streamable public abstract class Processable
{ {
public abstract string Name { get; } public abstract string Name { get; }
public event EventHandler<LibraryBook> Begin; public event EventHandler<LibraryBook> Begin;
/// <summary>General string message to display. DON'T rely on this for success, failure, or control logic</summary> /// <summary>General string message to display. DON'T rely on this for success, failure, or control logic</summary>
public event EventHandler<string> StatusUpdate; public event EventHandler<string> StatusUpdate;
/// <summary>Fired when a file is successfully saved to disk</summary>
public event EventHandler<(string id, string path)> FileCreated;
public event EventHandler<DownloadProgress> StreamingProgressChanged;
public event EventHandler<TimeSpan> StreamingTimeRemaining;
public event EventHandler<LibraryBook> Completed; public event EventHandler<LibraryBook> Completed;
@ -69,6 +74,23 @@ namespace FileLiberator
StatusUpdate?.Invoke(this, statusUpdate); StatusUpdate?.Invoke(this, statusUpdate);
} }
protected void OnFileCreated(LibraryBook libraryBook, string path)
{
Serilog.Log.Logger.Information("File created {@DebugInfo}", new { Name = nameof(FileCreated), libraryBook.Book.AudibleProductId, path });
FilePathCache.Insert(libraryBook.Book.AudibleProductId, path);
FileCreated?.Invoke(this, (libraryBook.Book.AudibleProductId, path));
}
protected void OnStreamingProgressChanged(DownloadProgress progress)
=> OnStreamingProgressChanged(null, progress);
protected void OnStreamingProgressChanged(object _, DownloadProgress progress)
=> StreamingProgressChanged?.Invoke(this, progress);
protected void OnStreamingTimeRemaining(TimeSpan timeRemaining)
=> OnStreamingTimeRemaining(null, timeRemaining);
protected void OnStreamingTimeRemaining(object _, TimeSpan timeRemaining)
=> StreamingTimeRemaining?.Invoke(this, timeRemaining);
protected void OnCompleted(LibraryBook libraryBook) protected void OnCompleted(LibraryBook libraryBook)
{ {
Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(Completed), Book = libraryBook.LogFriendly() }); Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(Completed), Book = libraryBook.LogFriendly() });

View File

@ -1,47 +0,0 @@
using System;
using Dinah.Core.Net.Http;
namespace FileLiberator
{
public abstract class Streamable
{
public event EventHandler<string> StreamingBegin;
public event EventHandler<DownloadProgress> StreamingProgressChanged;
public event EventHandler<TimeSpan> StreamingTimeRemaining;
public event EventHandler<string> StreamingCompleted;
/// <summary>Fired when a file is successfully saved to disk</summary>
public event EventHandler<(string id, string path)> FileCreated;
protected void OnStreamingBegin(string filePath)
{
Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(StreamingBegin), Message = filePath });
StreamingBegin?.Invoke(this, filePath);
}
protected void OnStreamingProgressChanged(DownloadProgress progress) => OnStreamingProgressChanged(null, progress);
protected void OnStreamingProgressChanged(object _, DownloadProgress progress)
{
StreamingProgressChanged?.Invoke(this, progress);
}
protected void OnStreamingTimeRemaining(TimeSpan timeRemaining) => OnStreamingTimeRemaining(null, timeRemaining);
protected void OnStreamingTimeRemaining(object _, TimeSpan timeRemaining)
{
StreamingTimeRemaining?.Invoke(this, timeRemaining);
}
protected void OnStreamingCompleted(string filePath)
{
Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(StreamingCompleted), Message = filePath });
StreamingCompleted?.Invoke(this, filePath);
}
protected void OnFileCreated(DataLayer.LibraryBook libraryBook, string path) => OnFileCreated(libraryBook.Book.AudibleProductId, path);
protected void OnFileCreated(string id, string path)
{
Serilog.Log.Logger.Information("File created {@DebugInfo}", new { Name = nameof(FileCreated), id, path });
LibationFileManager.FilePathCache.Insert(id, path);
FileCreated?.Invoke(this, (id, path));
}
}
}