From 5d98295c59f6d54f46ca9ef5a931375c8af1d391 Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 3 Jun 2023 15:17:08 +0200 Subject: [PATCH] #33 Preparation: TrangaSettings now stores Hashset of LibraryManagers --- Tranga-API/Program.cs | 6 ++-- Tranga-CLI/Tranga_Cli.cs | 15 +++++---- Tranga/LibraryManagers/Kavita.cs | 15 +++++++++ Tranga/TaskManager.cs | 33 +++++++++++--------- Tranga/TrangaSettings.cs | 13 ++++---- Tranga/TrangaTask.cs | 6 ++-- Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs | 15 --------- Tranga/TrangaTasks/UpdateLibrariesTask.cs | 16 ++++++++++ 8 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 Tranga/LibraryManagers/Kavita.cs delete mode 100644 Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs create mode 100644 Tranga/TrangaTasks/UpdateLibrariesTask.cs diff --git a/Tranga-API/Program.cs b/Tranga-API/Program.cs index d6c9ba6..b019d0c 100644 --- a/Tranga-API/Program.cs +++ b/Tranga-API/Program.cs @@ -17,7 +17,7 @@ TrangaSettings settings; if (File.Exists(settingsFilePath)) settings = TrangaSettings.LoadSettings(settingsFilePath, logger); else - settings = new TrangaSettings(downloadFolderPath, applicationFolderPath, null); + settings = new TrangaSettings(downloadFolderPath, applicationFolderPath, new HashSet()); Directory.CreateDirectory(settings.workingDirectory); Directory.CreateDirectory(settings.downloadLocation); @@ -211,6 +211,8 @@ app.MapDelete("/Queue/Dequeue", (string taskType, string? connectorName, string? app.MapGet("/Settings/Get", () => taskManager.settings); -app.MapPost("/Settings/Update", (string? downloadLocation, string? komgaUrl, string? komgaAuth) => taskManager.UpdateSettings(downloadLocation, komgaUrl, komgaAuth) ); +app.MapPost("/Settings/Update", + (string? downloadLocation, string? komgaUrl, string? komgaAuth, string? kavitaUrl, string? kavitaAuth) => + taskManager.UpdateSettings(downloadLocation, komgaUrl, komgaAuth, kavitaUrl, kavitaAuth)); app.Run(); \ No newline at end of file diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index d8d654d..f845a8b 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -29,7 +29,7 @@ public static class Tranga_Cli Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, Console.Out.Encoding, logFilePath); logger.WriteLine("Tranga_CLI", "Loading Taskmanager."); - TrangaSettings settings = File.Exists(settingsFilePath) ? TrangaSettings.LoadSettings(settingsFilePath, logger) : new TrangaSettings(Directory.GetCurrentDirectory(), applicationFolderPath, null); + TrangaSettings settings = File.Exists(settingsFilePath) ? TrangaSettings.LoadSettings(settingsFilePath, logger) : new TrangaSettings(Directory.GetCurrentDirectory(), applicationFolderPath, new HashSet()); logger.WriteLine("Tranga_CLI", "User Input"); @@ -39,8 +39,10 @@ public static class Tranga_Cli tmpPath = Console.ReadLine(); if (tmpPath.Length > 0) settings.downloadLocation = tmpPath; + + Komga? komga = (Komga?)settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Komga)); - Console.WriteLine($"Komga BaseURL [{settings.komga?.baseUrl}]:"); + Console.WriteLine($"Komga BaseURL [{komga?.baseUrl}]:"); string? tmpUrl = Console.ReadLine(); while (tmpUrl is null) tmpUrl = Console.ReadLine(); @@ -70,8 +72,9 @@ public static class Tranga_Cli tmpPass += keyInfo.KeyChar; } } while (key != ConsoleKey.Enter); - - settings.komga = new Komga(tmpUrl, tmpUser, tmpPass, logger); + + settings.libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga)); + settings.libraryManagers.Add(new Komga(tmpUrl, tmpUser, tmpPass, logger)); } logger.WriteLine("Tranga_CLI", "Loaded."); @@ -333,7 +336,7 @@ public static class Tranga_Cli TrangaTask.Task task = (TrangaTask.Task)tmpTask; Connector? connector = null; - if (task != TrangaTask.Task.UpdateKomgaLibrary) + if (task != TrangaTask.Task.UpdateLibraries) { connector = SelectConnector(taskManager.GetAvailableConnectors().Values.ToArray(), logger); if (connector is null) @@ -341,7 +344,7 @@ public static class Tranga_Cli } Publication? publication = null; - if (task != TrangaTask.Task.UpdateKomgaLibrary) + if (task != TrangaTask.Task.UpdateLibraries) { publication = SelectPublication(taskManager, connector!, logger); if (publication is null) diff --git a/Tranga/LibraryManagers/Kavita.cs b/Tranga/LibraryManagers/Kavita.cs new file mode 100644 index 0000000..49817ad --- /dev/null +++ b/Tranga/LibraryManagers/Kavita.cs @@ -0,0 +1,15 @@ +using Logging; + +namespace Tranga.LibraryManagers; + +public class Kavita : LibraryManager +{ + public Kavita(string baseUrl, string auth, Logger? logger) : base(baseUrl, auth, logger) + { + } + + public override void UpdateLibrary() + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index ecdcec5..ae3317e 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -19,7 +19,6 @@ public class TaskManager private readonly Dictionary> _taskQueue = new(); public TrangaSettings settings { get; } private Logger? logger { get; } - public Komga? komga => settings.komga; /// Local path to save data (Manga) to /// Path to the working directory @@ -28,16 +27,12 @@ public class TaskManager /// The Komga username /// The Komga password /// - public TaskManager(string downloadFolderPath, string workingDirectory, string imageCachePath, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null) + public TaskManager(string downloadFolderPath, string workingDirectory, string imageCachePath, HashSet libraryManagers, Logger? logger = null) { this.logger = logger; _allTasks = new HashSet(); - Komga? newKomga = null; - if (komgaBaseUrl != null && komgaUsername != null && komgaPassword != null) - newKomga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger); - - this.settings = new TrangaSettings(downloadFolderPath, workingDirectory, newKomga); + this.settings = new TrangaSettings(downloadFolderPath, workingDirectory, libraryManagers); ExportDataAndSettings(); this._connectors = new Connector[] @@ -52,10 +47,18 @@ public class TaskManager taskChecker.Start(); } - public void UpdateSettings(string? downloadLocation, string? komgaUrl, string? komgaAuth) + public void UpdateSettings(string? downloadLocation, string? komgaUrl, string? komgaAuth, string? kavitaUrl, string? kavitaAuth) { if (komgaUrl is not null && komgaAuth is not null && komgaUrl.Length > 0 && komgaAuth.Length > 0) - settings.komga = new Komga(komgaUrl, komgaAuth, logger); + { + settings.libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga)); + settings.libraryManagers.Add(new Komga(komgaUrl, komgaAuth, logger)); + } + if (kavitaUrl is not null && kavitaAuth is not null && kavitaUrl.Length > 0 && kavitaAuth.Length > 0) + { + settings.libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita)); + settings.libraryManagers.Add(new Kavita(kavitaUrl, kavitaAuth, logger)); + } if (downloadLocation is not null && downloadLocation.Length > 0) settings.downloadLocation = downloadLocation; ExportDataAndSettings(); @@ -104,7 +107,7 @@ public class TaskManager foreach (TrangaTask task in _allTasks.Where(aTask => aTask.ShouldExecute())) { task.state = TrangaTask.ExecutionState.Enqueued; - if(task.task == TrangaTask.Task.UpdateKomgaLibrary) + if(task.task == TrangaTask.Task.UpdateLibraries) ExecuteTaskNow(task); else { @@ -147,12 +150,12 @@ public class TaskManager logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {task} {connectorName} {publication?.sortName}"); TrangaTask? newTask = null; - if (task == TrangaTask.Task.UpdateKomgaLibrary) + if (task == TrangaTask.Task.UpdateLibraries) { - newTask = new UpdateKomgaLibraryTask(task, reoccurrence); + newTask = new UpdateLibrariesTask(task, reoccurrence); logger?.WriteLine(this.GetType().ToString(), $"Removing old {task}-Task."); //Only one UpdateKomgaLibrary Task - _allTasks.RemoveWhere(trangaTask => trangaTask.task is TrangaTask.Task.UpdateKomgaLibrary); + _allTasks.RemoveWhere(trangaTask => trangaTask.task is TrangaTask.Task.UpdateLibraries); _allTasks.Add(newTask); logger?.WriteLine(this.GetType().ToString(), $"Added new Task {newTask}"); }else if (task == TrangaTask.Task.DownloadNewChapters) @@ -193,9 +196,9 @@ public class TaskManager public void DeleteTask(TrangaTask.Task task, string? connectorName, Publication? publication) { logger?.WriteLine(this.GetType().ToString(), $"Removing Task {task} {publication?.sortName}"); - if (task == TrangaTask.Task.UpdateKomgaLibrary) + if (task == TrangaTask.Task.UpdateLibraries) { - _allTasks.RemoveWhere(uTask => uTask.task == TrangaTask.Task.UpdateKomgaLibrary); + _allTasks.RemoveWhere(uTask => uTask.task == TrangaTask.Task.UpdateLibraries); logger?.WriteLine(this.GetType().ToString(), $"Removed Task {task} from all Tasks."); } else if (connectorName is null) diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index b916a57..ccc1ec0 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -12,26 +12,27 @@ public class TrangaSettings [JsonIgnore]public string tasksFilePath => Path.Join(workingDirectory, "tasks.json"); [JsonIgnore]public string knownPublicationsPath => Path.Join(workingDirectory, "knownPublications.json"); [JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache"); - public Komga? komga { get; set; } + public readonly HashSet libraryManagers; - public TrangaSettings(string downloadLocation, string workingDirectory, Komga? komga) + public TrangaSettings(string downloadLocation, string workingDirectory, HashSet libraryManagers) { if (downloadLocation.Length < 1 || workingDirectory.Length < 1) throw new ArgumentException("Download-location and working-directory paths can not be empty!"); this.workingDirectory = workingDirectory; this.downloadLocation = downloadLocation; - this.komga = komga; + this.libraryManagers = libraryManagers; } public static TrangaSettings LoadSettings(string importFilePath, Logger? logger) { if (!File.Exists(importFilePath)) - return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory(), null); + return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory(), new HashSet()); string toRead = File.ReadAllText(importFilePath); TrangaSettings settings = JsonConvert.DeserializeObject(toRead)!; - if(settings.komga is not null && logger is not null) - settings.komga.AddLogger(logger); + if(logger is not null) + foreach(LibraryManager lm in settings.libraryManagers) + lm.AddLogger(logger); return settings; } diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index f91e216..e6c429a 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -74,7 +74,7 @@ public abstract class TrangaTask public enum Task : byte { DownloadNewChapters = 2, - UpdateKomgaLibrary = 3 + UpdateLibraries = 3 } public override string ToString() @@ -95,8 +95,8 @@ public abstract class TrangaTask if (jo["task"]!.Value() == (Int64)Task.DownloadNewChapters) return jo.ToObject(serializer)!; - if (jo["task"]!.Value() == (Int64)Task.UpdateKomgaLibrary) - return jo.ToObject(serializer)!; + if (jo["task"]!.Value() == (Int64)Task.UpdateLibraries) + return jo.ToObject(serializer)!; throw new Exception(); } diff --git a/Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs b/Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs deleted file mode 100644 index c6acacf..0000000 --- a/Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Logging; - -namespace Tranga.TrangaTasks; - -public class UpdateKomgaLibraryTask : TrangaTask -{ - public UpdateKomgaLibraryTask(Task task, TimeSpan reoccurrence) : base(task, null, null, reoccurrence) - { - } - - protected override void ExecuteTask(TaskManager taskManager, Logger? logger) - { - taskManager.komga?.UpdateLibrary(); - } -} \ No newline at end of file diff --git a/Tranga/TrangaTasks/UpdateLibrariesTask.cs b/Tranga/TrangaTasks/UpdateLibrariesTask.cs new file mode 100644 index 0000000..86841d5 --- /dev/null +++ b/Tranga/TrangaTasks/UpdateLibrariesTask.cs @@ -0,0 +1,16 @@ +using Logging; + +namespace Tranga.TrangaTasks; + +public class UpdateLibrariesTask : TrangaTask +{ + public UpdateLibrariesTask(Task task, TimeSpan reoccurrence) : base(task, null, null, reoccurrence) + { + } + + protected override void ExecuteTask(TaskManager taskManager, Logger? logger) + { + foreach(LibraryManager lm in taskManager.settings.libraryManagers) + lm.UpdateLibrary(); + } +} \ No newline at end of file