Parallelize post-liberation tasks
This commit is contained in:
parent
b9c2a1cce3
commit
abdf0e7261
@ -61,31 +61,30 @@ namespace FileLiberator
|
|||||||
}
|
}
|
||||||
|
|
||||||
// decrypt failed
|
// decrypt failed
|
||||||
if (!success)
|
if (!success || getFirstAudioFile(entries) == default)
|
||||||
{
|
{
|
||||||
foreach (var tmpFile in entries.Where(f => f.FileType != FileType.AAXC))
|
await Task.WhenAll(
|
||||||
FileUtility.SaferDelete(tmpFile.Path);
|
entries
|
||||||
|
.Where(f => f.FileType != FileType.AAXC)
|
||||||
|
.Select(f => Task.Run(() => FileUtility.SaferDelete(f.Path))));
|
||||||
|
|
||||||
return abDownloader?.IsCanceled == true ?
|
return
|
||||||
new StatusHandler { "Cancelled" } :
|
abDownloader?.IsCanceled is true
|
||||||
new StatusHandler { "Decrypt failed" };
|
? new StatusHandler { "Cancelled" }
|
||||||
|
: new StatusHandler { "Decrypt failed" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// moves new files from temp dir to final dest.
|
var finalStorageDir = getDestinationDirectory(libraryBook);
|
||||||
// This could take a few seconds if moving hundreds of files.
|
|
||||||
var finalStorageDir = await Task.Run(() => moveFilesToBooksDir(libraryBook, entries));
|
|
||||||
|
|
||||||
// decrypt failed
|
Task[] finalTasks = new[]
|
||||||
if (finalStorageDir is null)
|
{
|
||||||
return new StatusHandler { "Cannot find final audio file after decryption" };
|
Task.Run(() => downloadCoverArt(libraryBook)),
|
||||||
|
Task.Run(() => moveFilesToBooksDir(libraryBook, entries)),
|
||||||
|
Task.Run(() => libraryBook.Book.UpdateBookStatus(LiberatedStatus.Liberated)),
|
||||||
|
Task.Run(() => WindowsDirectory.SetCoverAsFolderIcon(libraryBook.Book.PictureId, finalStorageDir))
|
||||||
|
};
|
||||||
|
|
||||||
if (Configuration.Instance.DownloadCoverArt)
|
await Task.WhenAll(finalTasks);
|
||||||
downloadCoverArt(libraryBook);
|
|
||||||
|
|
||||||
// contains logic to check for config setting and OS
|
|
||||||
WindowsDirectory.SetCoverAsFolderIcon(pictureId: libraryBook.Book.PictureId, directory: finalStorageDir);
|
|
||||||
|
|
||||||
libraryBook.Book.UpdateBookStatus(LiberatedStatus.Liberated);
|
|
||||||
|
|
||||||
return new StatusHandler();
|
return new StatusHandler();
|
||||||
}
|
}
|
||||||
@ -335,16 +334,10 @@ namespace FileLiberator
|
|||||||
|
|
||||||
/// <summary>Move new files to 'Books' directory</summary>
|
/// <summary>Move new files to 'Books' directory</summary>
|
||||||
/// <returns>Return directory if audiobook file(s) were successfully created and can be located on disk. Else null.</returns>
|
/// <returns>Return directory if audiobook file(s) were successfully created and can be located on disk. Else null.</returns>
|
||||||
private static string moveFilesToBooksDir(LibraryBook libraryBook, List<FilePathCache.CacheEntry> entries)
|
private static void moveFilesToBooksDir(LibraryBook libraryBook, List<FilePathCache.CacheEntry> entries)
|
||||||
{
|
{
|
||||||
// create final directory. move each file into it
|
// create final directory. move each file into it
|
||||||
var destinationDir = AudibleFileStorage.Audio.GetDestinationDirectory(libraryBook);
|
var destinationDir = getDestinationDirectory(libraryBook);
|
||||||
Directory.CreateDirectory(destinationDir);
|
|
||||||
|
|
||||||
FilePathCache.CacheEntry getFirstAudio() => entries.FirstOrDefault(f => f.FileType == FileType.Audio);
|
|
||||||
|
|
||||||
if (getFirstAudio() == default)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
for (var i = 0; i < entries.Count; i++)
|
for (var i = 0; i < entries.Count; i++)
|
||||||
{
|
{
|
||||||
@ -359,20 +352,31 @@ namespace FileLiberator
|
|||||||
|
|
||||||
var cue = entries.FirstOrDefault(f => f.FileType == FileType.Cue);
|
var cue = entries.FirstOrDefault(f => f.FileType == FileType.Cue);
|
||||||
if (cue != default)
|
if (cue != default)
|
||||||
Cue.UpdateFileName(cue.Path, getFirstAudio().Path);
|
Cue.UpdateFileName(cue.Path, getFirstAudioFile(entries).Path);
|
||||||
|
|
||||||
AudibleFileStorage.Audio.Refresh();
|
AudibleFileStorage.Audio.Refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string getDestinationDirectory(LibraryBook libraryBook)
|
||||||
|
{
|
||||||
|
var destinationDir = AudibleFileStorage.Audio.GetDestinationDirectory(libraryBook);
|
||||||
|
if (!Directory.Exists(destinationDir))
|
||||||
|
Directory.CreateDirectory(destinationDir);
|
||||||
return destinationDir;
|
return destinationDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static FilePathCache.CacheEntry getFirstAudioFile(IEnumerable<FilePathCache.CacheEntry> entries)
|
||||||
|
=> entries.FirstOrDefault(f => f.FileType == FileType.Audio);
|
||||||
|
|
||||||
private static void downloadCoverArt(LibraryBook libraryBook)
|
private static void downloadCoverArt(LibraryBook libraryBook)
|
||||||
{
|
{
|
||||||
|
if (!Configuration.Instance.DownloadCoverArt) return;
|
||||||
|
|
||||||
var coverPath = "[null]";
|
var coverPath = "[null]";
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var destinationDir = AudibleFileStorage.Audio.GetDestinationDirectory(libraryBook);
|
var destinationDir = getDestinationDirectory(libraryBook);
|
||||||
coverPath = AudibleFileStorage.Audio.GetBooksDirectoryFilename(libraryBook, ".jpg");
|
coverPath = AudibleFileStorage.Audio.GetBooksDirectoryFilename(libraryBook, ".jpg");
|
||||||
coverPath = Path.Combine(destinationDir, Path.GetFileName(coverPath));
|
coverPath = Path.Combine(destinationDir, Path.GetFileName(coverPath));
|
||||||
|
|
||||||
|
|||||||
@ -9,10 +9,12 @@ namespace LibationFileManager
|
|||||||
{
|
{
|
||||||
public static class WindowsDirectory
|
public static class WindowsDirectory
|
||||||
{
|
{
|
||||||
|
|
||||||
public static void SetCoverAsFolderIcon(string pictureId, string directory)
|
public static void SetCoverAsFolderIcon(string pictureId, string directory)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
//Currently only works for Windows and macOS
|
||||||
if (!Configuration.Instance.UseCoverAsFolderIcon || Configuration.IsLinux)
|
if (!Configuration.Instance.UseCoverAsFolderIcon || Configuration.IsLinux)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user