From 3d6657b483c62f6feb4fa6bb248ace9dcdd40b41 Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 30 Jul 2023 23:31:25 +0200 Subject: [PATCH 01/19] Moved libraryManagers, notificationManagers and logger to commonObjects class. --- API/Program.cs | 20 ++-- Tranga/CommonObjects.cs | 117 ++++++++++++++++++++++ Tranga/Connectors/Connector.cs | 22 ++-- Tranga/Connectors/MangaDex.cs | 24 ++--- Tranga/Connectors/MangaKatana.cs | 12 +-- Tranga/Connectors/Manganato.cs | 12 +-- Tranga/Connectors/Mangasee.cs | 26 ++--- Tranga/TaskManager.cs | 46 +++++---- Tranga/TrangaSettings.cs | 75 +++++--------- Tranga/TrangaTasks/DownloadChapterTask.cs | 4 +- Tranga/TrangaTasks/TrangaTask.cs | 4 +- 11 files changed, 229 insertions(+), 133 deletions(-) create mode 100644 Tranga/CommonObjects.cs diff --git a/API/Program.cs b/API/Program.cs index da4f4bf..c2e0237 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -27,25 +27,25 @@ public static class Program TrangaSettings settings; if (File.Exists(settingsFilePath)) - settings = TrangaSettings.LoadSettings(settingsFilePath, logger); + settings = TrangaSettings.LoadSettings(settingsFilePath); else - settings = new TrangaSettings(downloadFolderPath, applicationFolderPath, new HashSet(), new HashSet(), logger); + settings = new TrangaSettings(downloadFolderPath, applicationFolderPath); Directory.CreateDirectory(settings.workingDirectory); Directory.CreateDirectory(settings.downloadLocation); Directory.CreateDirectory(settings.coverImageCache); - settings.logger?.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); - settings.logger?.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); - settings.logger?.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); - settings.logger?.WriteLine("Tranga",$"Logfile-Path: {logFilePath}"); - settings.logger?.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}"); + logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); + logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); + logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); + logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}"); + logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}"); - settings.logger?.WriteLine("Tranga", "Loading Taskmanager."); - TaskManager taskManager = new (settings); + logger.WriteLine("Tranga", "Loading Taskmanager."); + TaskManager taskManager = new (settings, logger); Server server = new (6531, taskManager); - foreach(NotificationManager nm in taskManager.settings.notificationManagers) + foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers) nm.SendNotification("Tranga-API", "Started Tranga-API"); } } diff --git a/Tranga/CommonObjects.cs b/Tranga/CommonObjects.cs new file mode 100644 index 0000000..c4f12bd --- /dev/null +++ b/Tranga/CommonObjects.cs @@ -0,0 +1,117 @@ +using Logging; +using Newtonsoft.Json; +using Tranga.LibraryManagers; +using Tranga.NotificationManagers; + +namespace Tranga; + +public class CommonObjects +{ + public HashSet libraryManagers { get; init; } + public HashSet notificationManagers { get; init; } + public Logger? logger { get; init; } + [JsonIgnore]private string settingsFilePath { get; init; } + + public CommonObjects(HashSet? libraryManagers, HashSet? notificationManagers, Logger? logger, string settingsFilePath) + { + this.libraryManagers = libraryManagers??new(); + this.notificationManagers = notificationManagers??new(); + this.logger = logger; + this.settingsFilePath = settingsFilePath; + } + + public static CommonObjects LoadSettings(string settingsFilePath, Logger? logger) + { + if (!File.Exists(settingsFilePath)) + return new CommonObjects(null, null, logger, settingsFilePath); + + string toRead = File.ReadAllText(settingsFilePath); + TrangaSettings.SettingsJsonObject settings = JsonConvert.DeserializeObject( + toRead, + new JsonSerializerSettings + { + Converters = + { + new NotificationManager.NotificationManagerJsonConverter(), + new LibraryManager.LibraryManagerJsonConverter() + } + })!; + + if(settings.co is null) + return new CommonObjects(null, null, logger, settingsFilePath); + + if (logger is not null) + { + foreach (LibraryManager lm in settings.co.libraryManagers) + lm.AddLogger(logger); + foreach(NotificationManager nm in settings.co.notificationManagers) + nm.AddLogger(logger); + } + + return settings.co; + } + + public void ExportSettings() + { + if (File.Exists(settingsFilePath)) + { + bool inUse = true; + while (inUse) + { + try + { + using FileStream stream = new (settingsFilePath, FileMode.Open, FileAccess.Read, FileShare.None); + stream.Close(); + inUse = false; + } + catch (IOException) + { + inUse = true; + Thread.Sleep(50); + } + } + } + + string toRead = File.ReadAllText(settingsFilePath); + TrangaSettings.SettingsJsonObject? settings = JsonConvert.DeserializeObject(toRead, + new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } }); + settings = new TrangaSettings.SettingsJsonObject(settings?.ts, this); + File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(settings)); + } + + public void UpdateSettings(TrangaSettings.UpdateField field, params string[] values) + { + switch (field) + { + case TrangaSettings.UpdateField.Komga: + if (values.Length != 2) + return; + libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga)); + libraryManagers.Add(new Komga(values[0], values[1], this.logger)); + break; + case TrangaSettings.UpdateField.Kavita: + if (values.Length != 3) + return; + libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita)); + libraryManagers.Add(new Kavita(values[0], values[1], values[2], this.logger)); + break; + case TrangaSettings.UpdateField.Gotify: + if (values.Length != 2) + return; + notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(Gotify)); + Gotify newGotify = new(values[0], values[1], this.logger); + notificationManagers.Add(newGotify); + newGotify.SendNotification("Success!", "Gotify was added to Tranga!"); + break; + case TrangaSettings.UpdateField.LunaSea: + if(values.Length != 1) + return; + notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(LunaSea)); + LunaSea newLunaSea = new(values[0], this.logger); + notificationManagers.Add(newLunaSea); + newLunaSea.SendNotification("Success!", "LunaSea was added to Tranga!"); + break; + } + ExportSettings(); + } +} \ No newline at end of file diff --git a/Tranga/Connectors/Connector.cs b/Tranga/Connectors/Connector.cs index 9233409..391eaa9 100644 --- a/Tranga/Connectors/Connector.cs +++ b/Tranga/Connectors/Connector.cs @@ -14,12 +14,14 @@ namespace Tranga.Connectors; /// public abstract class Connector { + protected CommonObjects commonObjects; protected TrangaSettings settings { get; } internal DownloadClient downloadClient { get; init; } = null!; - protected Connector(TrangaSettings settings) + protected Connector(TrangaSettings settings, CommonObjects commonObjects) { this.settings = settings; + this.commonObjects = commonObjects; if (!Directory.Exists(settings.coverImageCache)) Directory.CreateDirectory(settings.coverImageCache); } @@ -63,11 +65,11 @@ public abstract class Connector Chapter[] newChapters = this.GetChapters(publication, language); collection.Add(publication); NumberFormatInfo decimalPoint = new (){ NumberDecimalSeparator = "." }; - settings.logger?.WriteLine(this.GetType().ToString(), "Checking for duplicates"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), "Checking for duplicates"); List newChaptersList = newChapters.Where(nChapter => float.Parse(nChapter.chapterNumber, decimalPoint) > publication.ignoreChaptersBelow && !nChapter.CheckChapterIsDownloaded(settings.downloadLocation)).ToList(); - settings.logger?.WriteLine(this.GetType().ToString(), $"{newChaptersList.Count} new chapters."); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"{newChaptersList.Count} new chapters."); return newChaptersList; } @@ -161,19 +163,19 @@ public abstract class Connector /// Publication to retrieve Cover for public void CopyCoverFromCacheToDownloadLocation(Publication publication) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName} -> {publication.internalId}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName} -> {publication.internalId}"); //Check if Publication already has a Folder and cover string publicationFolder = publication.CreatePublicationFolder(settings.downloadLocation); DirectoryInfo dirInfo = new (publicationFolder); if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase))) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}"); return; } string fileInCache = Path.Join(settings.coverImageCache, publication.coverFileNameInCache); string newFilePath = Path.Join(publicationFolder, $"cover.{Path.GetFileName(fileInCache).Split('.')[^1]}" ); - settings.logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {fileInCache} -> {newFilePath}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {fileInCache} -> {newFilePath}"); File.Copy(fileInCache, newFilePath, true); if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) File.SetUnixFileMode(newFilePath, GroupRead | GroupWrite | OtherRead | OtherWrite | UserRead | UserWrite); @@ -211,7 +213,7 @@ public abstract class Connector { if (cancellationToken?.IsCancellationRequested ?? false) return HttpStatusCode.RequestTimeout; - settings.logger?.WriteLine("Connector", $"Downloading Images for {saveArchiveFilePath}"); + commonObjects.logger?.WriteLine("Connector", $"Downloading Images for {saveArchiveFilePath}"); //Check if Publication Directory already exists string directoryPath = Path.GetDirectoryName(saveArchiveFilePath)!; if (!Directory.Exists(directoryPath)) @@ -229,7 +231,7 @@ public abstract class Connector { string[] split = imageUrl.Split('.'); string extension = split[^1]; - settings.logger?.WriteLine("Connector", $"Downloading Image {chapter + 1:000}/{imageUrls.Length:000} {parentTask.publication.sortName} {parentTask.publication.internalId} Vol.{parentTask.chapter.volumeNumber} Ch.{parentTask.chapter.chapterNumber} {parentTask.progress:P2}"); + commonObjects.logger?.WriteLine("Connector", $"Downloading Image {chapter + 1:000}/{imageUrls.Length:000} {parentTask.publication.sortName} {parentTask.publication.internalId} Vol.{parentTask.chapter.volumeNumber} Ch.{parentTask.chapter.chapterNumber} {parentTask.progress:P2}"); HttpStatusCode status = DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), requestType, referrer); if ((int)status < 200 || (int)status >= 300) return status; @@ -241,7 +243,7 @@ public abstract class Connector if(comicInfoPath is not null) File.Copy(comicInfoPath, Path.Join(tempFolder, "ComicInfo.xml")); - settings.logger?.WriteLine("Connector", $"Creating archive {saveArchiveFilePath}"); + commonObjects.logger?.WriteLine("Connector", $"Creating archive {saveArchiveFilePath}"); //ZIP-it and ship-it ZipFile.CreateFromDirectory(tempFolder, saveArchiveFilePath); if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) @@ -263,7 +265,7 @@ public abstract class Connector using MemoryStream ms = new(); coverResult.result.CopyTo(ms); File.WriteAllBytes(saveImagePath, ms.ToArray()); - settings.logger?.WriteLine(this.GetType().ToString(), $"Saving image to {saveImagePath}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Saving image to {saveImagePath}"); return filename; } } \ No newline at end of file diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index 4ff9d2c..b63f7a3 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -18,7 +18,7 @@ public class MangaDex : Connector Author, } - public MangaDex(TrangaSettings settings) : base(settings) + public MangaDex(TrangaSettings settings, CommonObjects commonObjects) : base(settings, commonObjects) { name = "MangaDex"; this.downloadClient = new DownloadClient(new Dictionary() @@ -28,12 +28,12 @@ public class MangaDex : Connector {(byte)RequestType.AtHomeServer, 40}, {(byte)RequestType.CoverUrl, 250}, {(byte)RequestType.Author, 250} - }, settings.logger); + }, commonObjects.logger); } protected override Publication[] GetPublicationsInternal(string publicationTitle = "") { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); const int limit = 100; //How many values we want returned at once int offset = 0; //"Page" int total = int.MaxValue; //How many total results are there, is updated on first request @@ -59,7 +59,7 @@ public class MangaDex : Connector //Loop each Manga and extract information from JSON foreach (JsonNode? mangeNode in mangaInResult) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting publication data. {++loadedPublicationData}/{total}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting publication data. {++loadedPublicationData}/{total}"); JsonObject manga = (JsonObject)mangeNode!; JsonObject attributes = manga["attributes"]!.AsObject(); @@ -146,13 +146,13 @@ public class MangaDex : Connector } } - settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting publications (title={publicationTitle})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Done getting publications (title={publicationTitle})"); return publications.ToArray(); } public override Chapter[] GetChapters(Publication publication, string language = "") { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); const int limit = 100; //How many values we want returned at once int offset = 0; //"Page" int total = int.MaxValue; //How many total results are there, is updated on first request @@ -203,7 +203,7 @@ public class MangaDex : Connector { NumberDecimalSeparator = "." }; - settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting {chapters.Count} Chapters for {publication.internalId}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Done getting {chapters.Count} Chapters for {publication.internalId}"); return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray(); } @@ -211,7 +211,7 @@ public class MangaDex : Connector { if (cancellationToken?.IsCancellationRequested ?? false) return HttpStatusCode.RequestTimeout; - settings.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); //Request URLs for Chapter-Images DownloadClient.RequestResult requestResult = downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'", (byte)RequestType.AtHomeServer); @@ -238,10 +238,10 @@ public class MangaDex : Connector private string? GetCoverUrl(string publicationId, string? posterId) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting CoverUrl for {publicationId}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting CoverUrl for {publicationId}"); if (posterId is null) { - settings.logger?.WriteLine(this.GetType().ToString(), $"No posterId, aborting"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"No posterId, aborting"); return null; } @@ -257,7 +257,7 @@ public class MangaDex : Connector string fileName = result["data"]!["attributes"]!["fileName"]!.GetValue(); string coverUrl = $"https://uploads.mangadex.org/covers/{publicationId}/{fileName}"; - settings.logger?.WriteLine(this.GetType().ToString(), $"Got Cover-Url for {publicationId} -> {coverUrl}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Got Cover-Url for {publicationId} -> {coverUrl}"); return coverUrl; } @@ -276,7 +276,7 @@ public class MangaDex : Connector string authorName = result["data"]!["attributes"]!["name"]!.GetValue(); ret.Add(authorName); - settings.logger?.WriteLine(this.GetType().ToString(), $"Got author {authorId} -> {authorName}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Got author {authorId} -> {authorName}"); } return ret; } diff --git a/Tranga/Connectors/MangaKatana.cs b/Tranga/Connectors/MangaKatana.cs index 93a0441..e5cec4d 100644 --- a/Tranga/Connectors/MangaKatana.cs +++ b/Tranga/Connectors/MangaKatana.cs @@ -10,18 +10,18 @@ public class MangaKatana : Connector { public override string name { get; } - public MangaKatana(TrangaSettings settings) : base(settings) + public MangaKatana(TrangaSettings settings, CommonObjects commonObjects) : base(settings, commonObjects) { this.name = "MangaKatana"; this.downloadClient = new DownloadClient(new Dictionary() { {1, 60} - }, settings.logger); + }, commonObjects.logger); } protected override Publication[] GetPublicationsInternal(string publicationTitle = "") { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); string sanitizedTitle = string.Concat(Regex.Matches(publicationTitle, "[A-z]* *")).ToLower().Replace(' ', '_'); string requestUrl = $"https://mangakatana.com/?search={sanitizedTitle}&search_by=book_name"; DownloadClient.RequestResult requestResult = @@ -135,7 +135,7 @@ public class MangaKatana : Connector public override Chapter[] GetChapters(Publication publication, string language = "") { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); string requestUrl = $"https://mangakatana.com/manga/{publication.publicationId}"; // Leaving this in for verification if the page exists DownloadClient.RequestResult requestResult = @@ -149,7 +149,7 @@ public class MangaKatana : Connector NumberDecimalSeparator = "." }; List chapters = ParseChaptersFromHtml(publication, requestUrl); - settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray(); } @@ -182,7 +182,7 @@ public class MangaKatana : Connector { if (cancellationToken?.IsCancellationRequested ?? false) return HttpStatusCode.RequestTimeout; - settings.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); string requestUrl = chapter.url; // Leaving this in to check if the page exists DownloadClient.RequestResult requestResult = diff --git a/Tranga/Connectors/Manganato.cs b/Tranga/Connectors/Manganato.cs index 19baee7..cfc9791 100644 --- a/Tranga/Connectors/Manganato.cs +++ b/Tranga/Connectors/Manganato.cs @@ -10,18 +10,18 @@ public class Manganato : Connector { public override string name { get; } - public Manganato(TrangaSettings settings) : base(settings) + public Manganato(TrangaSettings settings, CommonObjects commonObjects) : base(settings, commonObjects) { this.name = "Manganato"; this.downloadClient = new DownloadClient(new Dictionary() { {1, 60} - }, settings.logger); + }, commonObjects.logger); } protected override Publication[] GetPublicationsInternal(string publicationTitle = "") { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); string sanitizedTitle = string.Join('_', Regex.Matches(publicationTitle, "[A-z]*")).ToLower(); string requestUrl = $"https://manganato.com/search/story/{sanitizedTitle}"; DownloadClient.RequestResult requestResult = @@ -125,7 +125,7 @@ public class Manganato : Connector public override Chapter[] GetChapters(Publication publication, string language = "") { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); string requestUrl = $"https://chapmanganato.com/{publication.publicationId}"; DownloadClient.RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); @@ -138,7 +138,7 @@ public class Manganato : Connector NumberDecimalSeparator = "." }; List chapters = ParseChaptersFromHtml(publication, requestResult.result); - settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray(); } @@ -171,7 +171,7 @@ public class Manganato : Connector { if (cancellationToken?.IsCancellationRequested ?? false) return HttpStatusCode.RequestTimeout; - settings.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); string requestUrl = chapter.url; DownloadClient.RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); diff --git a/Tranga/Connectors/Mangasee.cs b/Tranga/Connectors/Mangasee.cs index c82dd7b..ea9de48 100644 --- a/Tranga/Connectors/Mangasee.cs +++ b/Tranga/Connectors/Mangasee.cs @@ -15,13 +15,13 @@ public class Mangasee : Connector private IBrowser? _browser; private const string ChromiumVersion = "1154303"; - public Mangasee(TrangaSettings settings) : base(settings) + public Mangasee(TrangaSettings settings, CommonObjects commonObjects) : base(settings, commonObjects) { this.name = "Mangasee"; this.downloadClient = new DownloadClient(new Dictionary() { { 1, 60 } - }, settings.logger); + }, commonObjects.logger); Task d = new Task(DownloadBrowser); d.Start(); @@ -34,31 +34,31 @@ public class Mangasee : Connector browserFetcher.Remove(rev); if (!browserFetcher.LocalRevisions().Contains(ChromiumVersion)) { - settings.logger?.WriteLine(this.GetType().ToString(), "Downloading headless browser"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), "Downloading headless browser"); DateTime last = DateTime.Now.Subtract(TimeSpan.FromSeconds(5)); browserFetcher.DownloadProgressChanged += (_, args) => { double currentBytes = Convert.ToDouble(args.BytesReceived) / Convert.ToDouble(args.TotalBytesToReceive); if (args.TotalBytesToReceive == args.BytesReceived) { - settings.logger?.WriteLine(this.GetType().ToString(), "Browser downloaded."); + commonObjects.logger?.WriteLine(this.GetType().ToString(), "Browser downloaded."); } else if (DateTime.Now > last.AddSeconds(5)) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Browser download progress: {currentBytes:P2}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Browser download progress: {currentBytes:P2}"); last = DateTime.Now; } }; if (!browserFetcher.CanDownloadAsync(ChromiumVersion).Result) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Can't download browser version {ChromiumVersion}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Can't download browser version {ChromiumVersion}"); return; } await browserFetcher.DownloadAsync(ChromiumVersion); } - settings.logger?.WriteLine(this.GetType().ToString(), "Starting browser."); + commonObjects.logger?.WriteLine(this.GetType().ToString(), "Starting browser."); this._browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true, @@ -73,7 +73,7 @@ public class Mangasee : Connector protected override Publication[] GetPublicationsInternal(string publicationTitle = "") { - settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); string requestUrl = $"https://mangasee123.com/_search.php"; DownloadClient.RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); @@ -98,7 +98,7 @@ public class Mangasee : Connector queryFiltered = queryFiltered.Where(item => item.Value >= publicationTitle.Split(' ').Length - 1) .ToDictionary(item => item.Key, item => item.Value); - settings.logger?.WriteLine(this.GetType().ToString(), $"Got {queryFiltered.Count} Publications (title={publicationTitle})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Got {queryFiltered.Count} Publications (title={publicationTitle})"); HashSet ret = new(); List orderedFiltered = @@ -111,7 +111,7 @@ public class Mangasee : Connector downloadClient.MakeRequest($"https://mangasee123.com/manga/{orderedItem.i}", 1); if ((int)requestResult.statusCode >= 200 || (int)requestResult.statusCode < 300) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Retrieving Publication info: {orderedItem.s} {index++}/{orderedFiltered.Count}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Retrieving Publication info: {orderedItem.s} {index++}/{orderedFiltered.Count}"); ret.Add(ParseSinglePublicationFromHtml(requestResult.result, orderedItem.s, orderedItem.i, orderedItem.a)); } } @@ -235,7 +235,7 @@ public class Mangasee : Connector { NumberDecimalSeparator = "." }; - settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); return ret.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray(); } @@ -245,13 +245,13 @@ public class Mangasee : Connector return HttpStatusCode.RequestTimeout; while (this._browser is null && !(cancellationToken?.IsCancellationRequested??false)) { - settings.logger?.WriteLine(this.GetType().ToString(), "Waiting for headless browser to download..."); + commonObjects.logger?.WriteLine(this.GetType().ToString(), "Waiting for headless browser to download..."); Thread.Sleep(1000); } if (cancellationToken?.IsCancellationRequested??false) return HttpStatusCode.RequestTimeout; - settings.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); IPage page = _browser!.NewPageAsync().Result; IResponse response = page.GoToAsync(chapter.url).Result; if (response.Ok) diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 11c12d5..132f929 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -16,21 +16,23 @@ public class TaskManager private bool _continueRunning = true; private readonly Connector[] _connectors; public TrangaSettings settings { get; } + public CommonObjects commonObjects { get; init; } - public TaskManager(TrangaSettings settings) + public TaskManager(TrangaSettings settings, Logging.Logger? logger) { - settings.logger?.WriteLine("Tranga", value: "\n"+ - @"-----------------------------------------------------------------"+"\n"+ - @" |¯¯¯¯¯¯|°|¯¯¯¯¯¯\ /¯¯¯¯¯¯| |¯¯¯\|¯¯¯| /¯¯¯¯¯¯\' /¯¯¯¯¯¯| "+"\n"+ - @" | | | x <|' / ! | | '| | (/¯¯¯\° / ! | "+ "\n"+ - @" ¯|__|¯ |__|\\__\\ /___/¯|_'| |___|\\__| \\_____/' /___/¯|_'| "+ "\n"+ - @"-----------------------------------------------------------------"); + commonObjects = CommonObjects.LoadSettings(settings.settingsFilePath, logger); + commonObjects.logger?.WriteLine("Tranga", value: "\n"+ + @"-----------------------------------------------------------------"+"\n"+ + @" |¯¯¯¯¯¯|°|¯¯¯¯¯¯\ /¯¯¯¯¯¯| |¯¯¯\|¯¯¯| /¯¯¯¯¯¯\' /¯¯¯¯¯¯| "+"\n"+ + @" | | | x <|' / ! | | '| | (/¯¯¯\° / ! | "+ "\n"+ + @" ¯|__|¯ |__|\\__\\ /___/¯|_'| |___|\\__| \\_____/' /___/¯|_'| "+ "\n"+ + @"-----------------------------------------------------------------"); this._connectors = new Connector[] { - new MangaDex(settings), - new Manganato(settings), - new Mangasee(settings), - new MangaKatana(settings) + new MangaDex(settings, commonObjects), + new Manganato(settings, commonObjects), + new Mangasee(settings, commonObjects), + new MangaKatana(settings, commonObjects) }; this.settings = settings; @@ -47,7 +49,7 @@ public class TaskManager /// private void TaskCheckerThread() { - settings.logger?.WriteLine(this.GetType().ToString(), "Starting TaskCheckerThread."); + commonObjects.logger?.WriteLine(this.GetType().ToString(), "Starting TaskCheckerThread."); int waitingTasksCount = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting); while (_continueRunning) { @@ -146,7 +148,7 @@ public class TaskManager { case TrangaTask.Task.UpdateLibraries: //Only one UpdateKomgaLibrary Task - settings.logger?.WriteLine(this.GetType().ToString(), $"Replacing old {newTask.task}-Task."); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Replacing old {newTask.task}-Task."); if (GetTasksMatching(newTask).FirstOrDefault() is { } exists) _allTasks.Remove(exists); _allTasks.Add(newTask); @@ -155,19 +157,19 @@ public class TaskManager default: if (!GetTasksMatching(newTask).Any()) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {newTask}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {newTask}"); _allTasks.Add(newTask); ExportDataAndSettings(); } else - settings.logger?.WriteLine(this.GetType().ToString(), $"Task already exists {newTask}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Task already exists {newTask}"); break; } } public void DeleteTask(TrangaTask removeTask) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Removing Task {removeTask}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Removing Task {removeTask}"); if(_allTasks.Contains(removeTask)) _allTasks.Remove(removeTask); removeTask.parentTask?.RemoveChildTask(removeTask); @@ -319,7 +321,7 @@ public class TaskManager /// If force is true, tasks are aborted. public void Shutdown(bool force = false) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Shutting down (forced={force})"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Shutting down (forced={force})"); _continueRunning = false; ExportDataAndSettings(); @@ -329,16 +331,16 @@ public class TaskManager //Wait for tasks to finish while(_allTasks.Any(task => task.state is TrangaTask.ExecutionState.Running or TrangaTask.ExecutionState.Enqueued)) Thread.Sleep(10); - settings.logger?.WriteLine(this.GetType().ToString(), "Tasks finished. Bye!"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), "Tasks finished. Bye!"); Environment.Exit(0); } private void ImportData() { - settings.logger?.WriteLine(this.GetType().ToString(), "Importing Data"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), "Importing Data"); if (File.Exists(settings.tasksFilePath)) { - settings.logger?.WriteLine(this.GetType().ToString(), $"Importing tasks from {settings.tasksFilePath}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Importing tasks from {settings.tasksFilePath}"); string buffer = File.ReadAllText(settings.tasksFilePath); this._allTasks = JsonConvert.DeserializeObject>(buffer, new JsonSerializerSettings() { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!; } @@ -359,10 +361,10 @@ public class TaskManager /// private void ExportDataAndSettings() { - settings.logger?.WriteLine(this.GetType().ToString(), $"Exporting settings to {settings.settingsFilePath}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Exporting settings to {settings.settingsFilePath}"); settings.ExportSettings(); - settings.logger?.WriteLine(this.GetType().ToString(), $"Exporting tasks to {settings.tasksFilePath}"); + commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Exporting tasks to {settings.tasksFilePath}"); while(IsFileInUse(settings.tasksFilePath)) Thread.Sleep(50); File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(this._allTasks)); diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index fdeddfe..ae1d835 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -1,4 +1,5 @@ -using Logging; +using System.Text.Json.Nodes; +using Logging; using Newtonsoft.Json; using Tranga.LibraryManagers; using Tranga.NotificationManagers; @@ -12,41 +13,26 @@ public class TrangaSettings [JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json"); [JsonIgnore] public string tasksFilePath => Path.Join(workingDirectory, "tasks.json"); [JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache"); - public HashSet libraryManagers { get; } - public HashSet notificationManagers { get; } public ushort? version { get; set; } - [JsonIgnore] public Logger? logger { get; init; } - public TrangaSettings(string downloadLocation, string workingDirectory, HashSet? libraryManagers, - HashSet? notificationManagers, Logger? logger) + public TrangaSettings(string downloadLocation, string workingDirectory) { 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.libraryManagers = libraryManagers??new(); - this.notificationManagers = notificationManagers??new(); - this.logger = logger; } - public static TrangaSettings LoadSettings(string importFilePath, Logger? logger) + public static TrangaSettings LoadSettings(string importFilePath) { if (!File.Exists(importFilePath)) - return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), - Directory.GetCurrentDirectory(), new HashSet(), new HashSet(), logger); + return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory()); string toRead = File.ReadAllText(importFilePath); - TrangaSettings settings = JsonConvert.DeserializeObject(toRead, + SettingsJsonObject settings = JsonConvert.DeserializeObject(toRead, new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } })!; - if (logger is not null) - { - foreach (LibraryManager lm in settings.libraryManagers) - lm.AddLogger(logger); - foreach(NotificationManager nm in settings.notificationManagers) - nm.AddLogger(logger); - } + return settings.ts ?? new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory()); - return settings; } public void ExportSettings() @@ -69,7 +55,12 @@ public class TrangaSettings } } } - File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this)); + + string toRead = File.ReadAllText(settingsFilePath); + SettingsJsonObject? settings = JsonConvert.DeserializeObject(toRead, + new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } }); + settings = new SettingsJsonObject(this, settings?.co); + File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(settings)); } public void UpdateSettings(UpdateField field, params string[] values) @@ -81,37 +72,21 @@ public class TrangaSettings return; this.downloadLocation = values[0]; break; - case UpdateField.Komga: - if (values.Length != 2) - return; - libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga)); - libraryManagers.Add(new Komga(values[0], values[1], this.logger)); - break; - case UpdateField.Kavita: - if (values.Length != 3) - return; - libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita)); - libraryManagers.Add(new Kavita(values[0], values[1], values[2], this.logger)); - break; - case UpdateField.Gotify: - if (values.Length != 2) - return; - notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(Gotify)); - Gotify newGotify = new(values[0], values[1], this.logger); - notificationManagers.Add(newGotify); - newGotify.SendNotification("Success!", "Gotify was added to Tranga!"); - break; - case UpdateField.LunaSea: - if(values.Length != 1) - return; - notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(LunaSea)); - LunaSea newLunaSea = new(values[0], this.logger); - notificationManagers.Add(newLunaSea); - newLunaSea.SendNotification("Success!", "LunaSea was added to Tranga!"); - break; } ExportSettings(); } public enum UpdateField { DownloadLocation, Komga, Kavita, Gotify, LunaSea} + + internal class SettingsJsonObject + { + internal TrangaSettings? ts; + internal CommonObjects? co; + + public SettingsJsonObject(TrangaSettings? ts, CommonObjects? co) + { + this.ts = ts; + this.co = co; + } + } } \ No newline at end of file diff --git a/Tranga/TrangaTasks/DownloadChapterTask.cs b/Tranga/TrangaTasks/DownloadChapterTask.cs index a70e350..8134be6 100644 --- a/Tranga/TrangaTasks/DownloadChapterTask.cs +++ b/Tranga/TrangaTasks/DownloadChapterTask.cs @@ -32,10 +32,10 @@ public class DownloadChapterTask : TrangaTask HttpStatusCode downloadSuccess = connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken); if ((int)downloadSuccess >= 200 && (int)downloadSuccess < 300) { - foreach(NotificationManager nm in taskManager.settings.notificationManagers) + foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers) nm.SendNotification("Chapter downloaded", $"{this.publication.sortName} {this.chapter.chapterNumber} {this.chapter.name}"); - foreach (LibraryManager lm in taskManager.settings.libraryManagers) + foreach (LibraryManager lm in taskManager.commonObjects.libraryManagers) lm.UpdateLibrary(); } return downloadSuccess; diff --git a/Tranga/TrangaTasks/TrangaTask.cs b/Tranga/TrangaTasks/TrangaTask.cs index b3e4e60..189884c 100644 --- a/Tranga/TrangaTasks/TrangaTask.cs +++ b/Tranga/TrangaTasks/TrangaTask.cs @@ -66,7 +66,7 @@ public abstract class TrangaTask /// public void Execute(TaskManager taskManager, CancellationToken? cancellationToken = null) { - taskManager.settings.logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}"); + taskManager.commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}"); this.state = ExecutionState.Running; this.executionStarted = DateTime.Now; this.lastChange = DateTime.Now; @@ -89,7 +89,7 @@ public abstract class TrangaTask if (this is DownloadChapterTask) taskManager.DeleteTask(this); - taskManager.settings.logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}"); + taskManager.commonObjects.logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}"); } public void AddChildTask(TrangaTask childTask) From fa19d3da146e646b514c7a9b28cf121949d05ad4 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:01:18 +0200 Subject: [PATCH 02/19] Fix missing file on loading settings/commonobjects --- Tranga/CommonObjects.cs | 15 +++++++++++---- Tranga/TrangaSettings.cs | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Tranga/CommonObjects.cs b/Tranga/CommonObjects.cs index c4f12bd..a97e117 100644 --- a/Tranga/CommonObjects.cs +++ b/Tranga/CommonObjects.cs @@ -53,6 +53,7 @@ public class CommonObjects public void ExportSettings() { + TrangaSettings.SettingsJsonObject? settings = null; if (File.Exists(settingsFilePath)) { bool inUse = true; @@ -70,11 +71,17 @@ public class CommonObjects Thread.Sleep(50); } } + string toRead = File.ReadAllText(settingsFilePath); + settings = JsonConvert.DeserializeObject(toRead, + new JsonSerializerSettings + { + Converters = + { + new NotificationManager.NotificationManagerJsonConverter(), + new LibraryManager.LibraryManagerJsonConverter() + } + }); } - - string toRead = File.ReadAllText(settingsFilePath); - TrangaSettings.SettingsJsonObject? settings = JsonConvert.DeserializeObject(toRead, - new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } }); settings = new TrangaSettings.SettingsJsonObject(settings?.ts, this); File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(settings)); } diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index ae1d835..1a20d78 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -37,6 +37,7 @@ public class TrangaSettings public void ExportSettings() { + SettingsJsonObject? settings = null; if (File.Exists(settingsFilePath)) { bool inUse = true; @@ -54,11 +55,17 @@ public class TrangaSettings Thread.Sleep(50); } } + string toRead = File.ReadAllText(settingsFilePath); + settings = JsonConvert.DeserializeObject(toRead, + new JsonSerializerSettings + { + Converters = + { + new NotificationManager.NotificationManagerJsonConverter(), + new LibraryManager.LibraryManagerJsonConverter() + } + }); } - - string toRead = File.ReadAllText(settingsFilePath); - SettingsJsonObject? settings = JsonConvert.DeserializeObject(toRead, - new JsonSerializerSettings { Converters = { new NotificationManager.NotificationManagerJsonConverter(), new LibraryManager.LibraryManagerJsonConverter() } }); settings = new SettingsJsonObject(this, settings?.co); File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(settings)); } From 140074208f8fa35621e0a2e4d7565613c92b3ebb Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:31:19 +0200 Subject: [PATCH 03/19] Merged API and CLI into one. --- API/API.csproj | 21 -- API/Program.cs | 52 ---- API/Dockerfile => Dockerfile | 6 +- Logging/Logger.cs | 2 +- Tranga-CLI/Tranga-CLI.csproj | 16 -- Tranga.sln | 12 - {API => Tranga/API}/RequestHandler.cs | 2 +- {API => Tranga/API}/Server.cs | 9 +- Tranga/TaskManager.cs | 2 +- Tranga-CLI/Tranga_Cli.cs => Tranga/Tranga.cs | 254 +++++++------------ Tranga/Tranga.csproj | 8 + 11 files changed, 108 insertions(+), 276 deletions(-) delete mode 100644 API/API.csproj delete mode 100644 API/Program.cs rename API/Dockerfile => Dockerfile (71%) delete mode 100644 Tranga-CLI/Tranga-CLI.csproj rename {API => Tranga/API}/RequestHandler.cs (99%) rename {API => Tranga/API}/Server.cs (91%) rename Tranga-CLI/Tranga_Cli.cs => Tranga/Tranga.cs (70%) diff --git a/API/API.csproj b/API/API.csproj deleted file mode 100644 index 0622e9c..0000000 --- a/API/API.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - Exe - net7.0 - enable - enable - Linux - - - - - .dockerignore - - - - - - - - diff --git a/API/Program.cs b/API/Program.cs deleted file mode 100644 index c2e0237..0000000 --- a/API/Program.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Runtime.InteropServices; -using Logging; -using Tranga; -using Tranga.NotificationManagers; -using Tranga.LibraryManagers; - -namespace API; - -public static class Program -{ - public static void Main(string[] args) - { - string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga-API"); - string downloadFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(applicationFolderPath, "Manga"); - string logsFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/var/logs/Tranga" : Path.Join(applicationFolderPath, "logs"); - string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"); - string settingsFilePath = Path.Join(applicationFolderPath, "settings.json"); - - Directory.CreateDirectory(logsFolderPath); - Logger logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath); - - logger.WriteLine("Tranga",value: "\n"+ - "-------------------------------------------\n"+ - " Starting Tranga-API\n"+ - "-------------------------------------------"); - logger.WriteLine("Tranga", "Loading settings."); - - TrangaSettings settings; - if (File.Exists(settingsFilePath)) - settings = TrangaSettings.LoadSettings(settingsFilePath); - else - settings = new TrangaSettings(downloadFolderPath, applicationFolderPath); - - Directory.CreateDirectory(settings.workingDirectory); - Directory.CreateDirectory(settings.downloadLocation); - Directory.CreateDirectory(settings.coverImageCache); - - logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); - logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); - logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); - logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}"); - logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}"); - - logger.WriteLine("Tranga", "Loading Taskmanager."); - TaskManager taskManager = new (settings, logger); - - Server server = new (6531, taskManager); - foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers) - nm.SendNotification("Tranga-API", "Started Tranga-API"); - } -} - diff --git a/API/Dockerfile b/Dockerfile similarity index 71% rename from API/Dockerfile rename to Dockerfile index 23e3804..1379b93 100644 --- a/API/Dockerfile +++ b/Dockerfile @@ -2,8 +2,10 @@ FROM mcr.microsoft.com/dotnet/sdk:7.0 as build-env WORKDIR /src -COPY . /src/ -RUN dotnet restore /src/API/API.csproj +COPY Tranga /src/Tranga +COPY Logging /src/Logging +COPY Tranga.sln /src +RUN dotnet restore /src/Tranga/Tranga.csproj RUN dotnet publish -c Release -o /publish FROM glax/tranga-base:latest as runtime diff --git a/Logging/Logger.cs b/Logging/Logger.cs index 19baa2a..9ca3d28 100644 --- a/Logging/Logger.cs +++ b/Logging/Logger.cs @@ -30,7 +30,7 @@ public class Logger : TextWriter { _formattedConsoleLogger = new FormattedConsoleLogger(stdOut, encoding); } - else + else if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is null) { _formattedConsoleLogger = null; throw new ArgumentException($"stdOut can not be null for LoggerType {LoggerType.ConsoleLogger}"); diff --git a/Tranga-CLI/Tranga-CLI.csproj b/Tranga-CLI/Tranga-CLI.csproj deleted file mode 100644 index 4d6dbf4..0000000 --- a/Tranga-CLI/Tranga-CLI.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - Exe - net7.0 - Tranga_CLI - enable - enable - Linux - - - - - - - diff --git a/Tranga.sln b/Tranga.sln index 97a12c6..78a7590 100644 --- a/Tranga.sln +++ b/Tranga.sln @@ -2,12 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tranga", ".\Tranga\Tranga.csproj", "{545E81B9-D96B-4C8F-A97F-2C02414DE566}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tranga-CLI", "Tranga-CLI\Tranga-CLI.csproj", "{4899E3B2-B259-479A-B43E-042D043E9501}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logging", "Logging\Logging.csproj", "{415BE889-BB7D-426F-976F-8D977876A462}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,17 +14,9 @@ Global {545E81B9-D96B-4C8F-A97F-2C02414DE566}.Debug|Any CPU.Build.0 = Debug|Any CPU {545E81B9-D96B-4C8F-A97F-2C02414DE566}.Release|Any CPU.ActiveCfg = Release|Any CPU {545E81B9-D96B-4C8F-A97F-2C02414DE566}.Release|Any CPU.Build.0 = Release|Any CPU - {4899E3B2-B259-479A-B43E-042D043E9501}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4899E3B2-B259-479A-B43E-042D043E9501}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4899E3B2-B259-479A-B43E-042D043E9501}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4899E3B2-B259-479A-B43E-042D043E9501}.Release|Any CPU.Build.0 = Release|Any CPU {415BE889-BB7D-426F-976F-8D977876A462}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {415BE889-BB7D-426F-976F-8D977876A462}.Debug|Any CPU.Build.0 = Debug|Any CPU {415BE889-BB7D-426F-976F-8D977876A462}.Release|Any CPU.ActiveCfg = Release|Any CPU {415BE889-BB7D-426F-976F-8D977876A462}.Release|Any CPU.Build.0 = Release|Any CPU - {A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/API/RequestHandler.cs b/Tranga/API/RequestHandler.cs similarity index 99% rename from API/RequestHandler.cs rename to Tranga/API/RequestHandler.cs index 118e3f2..62f7f5c 100644 --- a/API/RequestHandler.cs +++ b/Tranga/API/RequestHandler.cs @@ -5,7 +5,7 @@ using Tranga; using Tranga.Connectors; using Tranga.TrangaTasks; -namespace API; +namespace Tranga.API; public class RequestHandler { diff --git a/API/Server.cs b/Tranga/API/Server.cs similarity index 91% rename from API/Server.cs rename to Tranga/API/Server.cs index c4dd5f3..ae913ed 100644 --- a/API/Server.cs +++ b/Tranga/API/Server.cs @@ -6,12 +6,13 @@ using Logging; using Newtonsoft.Json; using Tranga; -namespace API; +namespace Tranga.API; public class Server { private readonly HttpListener _listener = new (); private readonly RequestHandler _requestHandler; + private readonly TaskManager _taskManager; internal readonly Logger? logger; private readonly Regex _validUrl = @@ -19,12 +20,14 @@ public class Server public Server(int port, TaskManager taskManager, Logger? logger = null) { this.logger = logger; + this._taskManager = taskManager; if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) this._listener.Prefixes.Add($"http://*:{port}/"); else this._listener.Prefixes.Add($"http://localhost:{port}/"); this._requestHandler = new RequestHandler(taskManager, this); - Listen(); + Thread listenThread = new Thread(Listen); + listenThread.Start(); } private void Listen() @@ -32,7 +35,7 @@ public class Server this._listener.Start(); foreach (string prefix in this._listener.Prefixes) this.logger?.WriteLine(this.GetType().ToString(), $"Listening on {prefix}"); - while (this._listener.IsListening) + while (this._listener.IsListening && _taskManager._continueRunning) { HttpListenerContext context = this._listener.GetContextAsync().Result; Task t = new (() => diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 132f929..3ab8ba2 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -13,7 +13,7 @@ public class TaskManager public HashSet collection = new(); private HashSet _allTasks = new(); private readonly Dictionary _runningTasks = new (); - private bool _continueRunning = true; + public bool _continueRunning = true; private readonly Connector[] _connectors; public TrangaSettings settings { get; } public CommonObjects commonObjects { get; init; } diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga/Tranga.cs similarity index 70% rename from Tranga-CLI/Tranga_Cli.cs rename to Tranga/Tranga.cs index a131b5e..5757d9c 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga/Tranga.cs @@ -1,139 +1,59 @@ using System.Globalization; +using System.Runtime.InteropServices; using Logging; -using Tranga; +using Tranga.API; using Tranga.Connectors; -using Tranga.LibraryManagers; using Tranga.NotificationManagers; using Tranga.TrangaTasks; -namespace Tranga_CLI; +namespace Tranga; -/* - * This is written with pure hatred for readability. - * At some point do this properly. - * Read at own risk. - */ - -public static class Tranga_Cli +public static class Tranga { public static void Main(string[] args) { - string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga"); - string logsFolderPath = Path.Join(applicationFolderPath, "logs"); + string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga-API"); + string downloadFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(applicationFolderPath, "Manga"); + string logsFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/var/log/Tranga" : Path.Join(applicationFolderPath, "log"); string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"); string settingsFilePath = Path.Join(applicationFolderPath, "settings.json"); - - Directory.CreateDirectory(applicationFolderPath); + + Directory.CreateDirectory(logsFolderPath); + Logger logger = new(new[] { Logger.LoggerType.FileLogger }, Console.Out, Console.Out.Encoding, logFilePath); - Console.WriteLine($"Logfile-Path: {logFilePath}"); - Console.WriteLine($"Settings-File-Path: {settingsFilePath}"); + logger.WriteLine("Tranga",value: "\n"+ + "-------------------------------------------\n"+ + " Starting Tranga-API\n"+ + "-------------------------------------------"); + logger.WriteLine("Tranga", "Loading settings."); - Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, Console.Out.Encoding, logFilePath); + TrangaSettings settings = File.Exists(settingsFilePath) + ? TrangaSettings.LoadSettings(settingsFilePath) + : new TrangaSettings(downloadFolderPath, applicationFolderPath); + + Directory.CreateDirectory(settings.workingDirectory); + Directory.CreateDirectory(settings.downloadLocation); + Directory.CreateDirectory(settings.coverImageCache); + + logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); + logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); + logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); + logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}"); + logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}"); + + logger.WriteLine("Tranga", "Loading Taskmanager."); + TaskManager taskManager = new (settings, logger); - logger.WriteLine("Tranga_CLI", "Loading Taskmanager."); - TrangaSettings settings = File.Exists(settingsFilePath) ? TrangaSettings.LoadSettings(settingsFilePath, logger) : new TrangaSettings(Directory.GetCurrentDirectory(), applicationFolderPath, new HashSet(), new HashSet(), logger); - - - settings.logger?.WriteLine("Tranga_CLI", "User Input"); - Console.WriteLine($"Output folder path [{settings.downloadLocation}]:"); - string? tmpPath = Console.ReadLine(); - while(tmpPath is null) - tmpPath = Console.ReadLine(); - if (tmpPath.Length > 0) - settings.UpdateSettings(TrangaSettings.UpdateField.DownloadLocation, tmpPath); - - Console.WriteLine($"Komga BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Komga))?.baseUrl}]:"); - string? tmpUrlKomga = Console.ReadLine(); - while (tmpUrlKomga is null) - tmpUrlKomga = Console.ReadLine(); - if (tmpUrlKomga.Length > 0) - { - Console.WriteLine("Username:"); - string? tmpKomgaUser = Console.ReadLine(); - while (tmpKomgaUser is null || tmpKomgaUser.Length < 1) - tmpKomgaUser = Console.ReadLine(); - - Console.WriteLine("Password:"); - string tmpKomgaPass = string.Empty; - ConsoleKey key; - do - { - var keyInfo = Console.ReadKey(intercept: true); - key = keyInfo.Key; - - if (key == ConsoleKey.Backspace && tmpKomgaPass.Length > 0) - { - Console.Write("\b \b"); - tmpKomgaPass = tmpKomgaPass[0..^1]; - } - else if (!char.IsControl(keyInfo.KeyChar)) - { - Console.Write("*"); - tmpKomgaPass += keyInfo.KeyChar; - } - } while (key != ConsoleKey.Enter); - - settings.UpdateSettings(TrangaSettings.UpdateField.Komga, tmpUrlKomga, tmpKomgaUser, tmpKomgaPass); - } + Server _ = new (6531, taskManager); + foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers) + nm.SendNotification("Tranga-API", "Started Tranga-API"); - Console.WriteLine($"Kavita BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Kavita))?.baseUrl}]:"); - string? tmpUrlKavita = Console.ReadLine(); - while (tmpUrlKavita is null) - tmpUrlKavita = Console.ReadLine(); - if (tmpUrlKavita.Length > 0) - { - Console.WriteLine("Username:"); - string? tmpKavitaUser = Console.ReadLine(); - while (tmpKavitaUser is null || tmpKavitaUser.Length < 1) - tmpKavitaUser = Console.ReadLine(); - - Console.WriteLine("Password:"); - string tmpKavitaPass = string.Empty; - ConsoleKey key; - do - { - var keyInfo = Console.ReadKey(intercept: true); - key = keyInfo.Key; - - if (key == ConsoleKey.Backspace && tmpKavitaPass.Length > 0) - { - Console.Write("\b \b"); - tmpKavitaPass = tmpKavitaPass[0..^1]; - } - else if (!char.IsControl(keyInfo.KeyChar)) - { - Console.Write("*"); - tmpKavitaPass += keyInfo.KeyChar; - } - } while (key != ConsoleKey.Enter); - - settings.UpdateSettings(TrangaSettings.UpdateField.Kavita, tmpUrlKavita, tmpKavitaUser, tmpKavitaPass); - } - - Console.WriteLine($"Gotify BaseURL [{((Gotify?)settings.notificationManagers.FirstOrDefault(lm => lm.GetType() == typeof(Gotify)))?.endpoint}]:"); - string? tmpGotifyUrl = Console.ReadLine(); - while (tmpGotifyUrl is null) - tmpGotifyUrl = Console.ReadLine(); - if (tmpGotifyUrl.Length > 0) - { - Console.WriteLine("AppToken:"); - string? tmpGotifyAppToken = Console.ReadLine(); - while (tmpGotifyAppToken is null || tmpGotifyAppToken.Length < 1) - tmpGotifyAppToken = Console.ReadLine(); - - settings.UpdateSettings(TrangaSettings.UpdateField.Gotify, tmpGotifyUrl, tmpGotifyAppToken); - } - - logger.WriteLine("Tranga_CLI", "Loaded."); - foreach(NotificationManager nm in settings.notificationManagers) - nm.SendNotification("Tranga", "Loaded."); - TaskMode(settings, logger); + TaskMode(taskManager, logger); } - - private static void TaskMode(TrangaSettings settings, Logger logger) + + private static void TaskMode(TaskManager taskManager, Logger logger) { - TaskManager taskManager = new (settings); ConsoleKey selection = ConsoleKey.EraseEndOfFile; PrintMenu(taskManager, taskManager.settings.downloadLocation); while (selection != ConsoleKey.Q) @@ -160,22 +80,22 @@ public static class Tranga_Cli Console.ReadKey(); break; case ConsoleKey.C: - CreateTask(taskManager, logger); + CreateTask(taskManager); Console.WriteLine("Press any key."); Console.ReadKey(); break; case ConsoleKey.D: - DeleteTask(taskManager, logger); + DeleteTask(taskManager); Console.WriteLine("Press any key."); Console.ReadKey(); break; case ConsoleKey.E: - ExecuteTaskNow(taskManager, logger); + ExecuteTaskNow(taskManager); Console.WriteLine("Press any key."); Console.ReadKey(); break; case ConsoleKey.S: - SearchTasks(taskManager, logger); + SearchTasks(taskManager); Console.WriteLine("Press any key."); Console.ReadKey(); break; @@ -241,7 +161,7 @@ public static class Tranga_Cli // ReSharper disable once RedundantArgumentDefaultValue Better readability taskManager.Shutdown(false); } - + private static void PrintMenu(TaskManager taskManager, string folderPath) { int taskCount = taskManager.GetAllTasks().Length; @@ -258,10 +178,10 @@ public static class Tranga_Cli Console.WriteLine($"{"M: New Download Manga Task",-30}{"", -30}{"", -30}"); Console.WriteLine($"{"",-30}{"F: Show Log",-30}{"Q: Exit",-30}"); } - - private static void PrintTasks(TrangaTask[] tasks, Logger logger) + + private static void PrintTasks(TrangaTask[] tasks, Logger? logger) { - logger.WriteLine("Tranga_CLI", "Printing Tasks"); + logger?.WriteLine("Tranga_CLI", "Printing Tasks"); int taskCount = tasks.Length; int taskRunningCount = tasks.Count(task => task.state == TrangaTask.ExecutionState.Running); int taskEnqueuedCount = tasks.Count(task => task.state == TrangaTask.ExecutionState.Enqueued); @@ -280,19 +200,19 @@ public static class Tranga_Cli } - private static TrangaTask[] SelectTasks(TrangaTask[] tasks, Logger logger) + private static TrangaTask[] SelectTasks(TrangaTask[] tasks, Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select task"); + logger?.WriteLine("Tranga_CLI", "Menu: Select task"); if (tasks.Length < 1) { Console.Clear(); Console.WriteLine("There are no available Tasks."); - logger.WriteLine("Tranga_CLI", "No available Tasks."); + logger?.WriteLine("Tranga_CLI", "No available Tasks."); return Array.Empty(); } PrintTasks(tasks, logger); - logger.WriteLine("Tranga_CLI", "Selecting Task to Remove (from queue)"); + logger?.WriteLine("Tranga_CLI", "Selecting Task to Remove (from queue)"); Console.WriteLine("Enter q to abort"); Console.WriteLine($"Select Task(s) (0-{tasks.Length - 1}):"); @@ -304,7 +224,7 @@ public static class Tranga_Cli { Console.Clear(); Console.WriteLine("aborted."); - logger.WriteLine("Tranga_CLI", "aborted"); + logger?.WriteLine("Tranga_CLI", "aborted"); return Array.Empty(); } @@ -330,7 +250,7 @@ public static class Tranga_Cli if (connector is null) return; - Publication? publication = SelectPublication(taskManager, connector, logger); + Publication? publication = SelectPublication(taskManager, connector); if (publication is null) return; @@ -386,10 +306,10 @@ public static class Tranga_Cli } } - private static void CreateTask(TaskManager taskManager, Logger logger) + private static void CreateTask(TaskManager taskManager) { - logger.WriteLine("Tranga_CLI", "Menu: Creating Task"); - TrangaTask.Task? tmpTask = SelectTaskType(logger); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Creating Task"); + TrangaTask.Task? tmpTask = SelectTaskType(taskManager.commonObjects.logger); if (tmpTask is null) return; TrangaTask.Task task = (TrangaTask.Task)tmpTask; @@ -397,7 +317,7 @@ public static class Tranga_Cli Connector? connector = null; if (task != TrangaTask.Task.UpdateLibraries) { - connector = SelectConnector(taskManager.GetAvailableConnectors().Values.ToArray(), logger); + connector = SelectConnector(taskManager.GetAvailableConnectors().Values.ToArray(), taskManager.commonObjects.logger); if (connector is null) return; } @@ -405,22 +325,22 @@ public static class Tranga_Cli Publication? publication = null; if (task != TrangaTask.Task.UpdateLibraries) { - publication = SelectPublication(taskManager, connector!, logger); + publication = SelectPublication(taskManager, connector!); if (publication is null) return; } if (task is TrangaTask.Task.MonitorPublication) { - TimeSpan reoccurrence = SelectReoccurrence(logger); - logger.WriteLine("Tranga_CLI", "Sending Task to TaskManager"); + TimeSpan reoccurrence = SelectReoccurrence(taskManager.commonObjects.logger); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Sending Task to TaskManager"); TrangaTask newTask = new MonitorPublicationTask(connector!.name, (Publication)publication!, reoccurrence, "en"); taskManager.AddTask(newTask); Console.WriteLine(newTask); }else if (task is TrangaTask.Task.DownloadChapter) { - foreach (Chapter chapter in SelectChapters(connector!, (Publication)publication!, logger)) + foreach (Chapter chapter in SelectChapters(connector!, (Publication)publication!, taskManager.commonObjects.logger)) { TrangaTask newTask = new DownloadChapterTask(connector!.name, (Publication)publication, chapter, "en"); taskManager.AddTask(newTask); @@ -429,31 +349,31 @@ public static class Tranga_Cli } } - private static void ExecuteTaskNow(TaskManager taskManager, Logger logger) + private static void ExecuteTaskNow(TaskManager taskManager) { - logger.WriteLine("Tranga_CLI", "Menu: Executing Task"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Executing Task"); TrangaTask[] tasks = taskManager.GetAllTasks().Where(nTask => nTask.state is not TrangaTask.ExecutionState.Running).ToArray(); - TrangaTask[] selectedTasks = SelectTasks(tasks, logger); - logger.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager"); + TrangaTask[] selectedTasks = SelectTasks(tasks, taskManager.commonObjects.logger); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager"); foreach(TrangaTask task in selectedTasks) taskManager.ExecuteTaskNow(task); } - private static void DeleteTask(TaskManager taskManager, Logger logger) + private static void DeleteTask(TaskManager taskManager) { - logger.WriteLine("Tranga_CLI", "Menu: Delete Task"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Delete Task"); TrangaTask[] tasks = taskManager.GetAllTasks(); - TrangaTask[] selectedTasks = SelectTasks(tasks, logger); - logger.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager"); + TrangaTask[] selectedTasks = SelectTasks(tasks, taskManager.commonObjects.logger); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager"); foreach(TrangaTask task in selectedTasks) taskManager.DeleteTask(task); } - private static TrangaTask.Task? SelectTaskType(Logger logger) + private static TrangaTask.Task? SelectTaskType(Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select TaskType"); + logger?.WriteLine("Tranga_CLI", "Menu: Select TaskType"); Console.Clear(); string[] taskNames = Enum.GetNames(); @@ -473,7 +393,7 @@ public static class Tranga_Cli { Console.Clear(); Console.WriteLine("aborted."); - logger.WriteLine("Tranga_CLI", "aborted."); + logger?.WriteLine("Tranga_CLI", "aborted."); return null; } @@ -486,22 +406,22 @@ public static class Tranga_Cli catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); - logger.WriteLine("Tranga_CLI", e.Message); + logger?.WriteLine("Tranga_CLI", e.Message); } return null; } - private static TimeSpan SelectReoccurrence(Logger logger) + private static TimeSpan SelectReoccurrence(Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select Reoccurrence"); + logger?.WriteLine("Tranga_CLI", "Menu: Select Reoccurrence"); Console.WriteLine("Select reoccurrence Timer (Format hh:mm:ss):"); return TimeSpan.Parse(Console.ReadLine()!, new CultureInfo("en-US")); } - private static Chapter[] SelectChapters(Connector connector, Publication publication, Logger logger) + private static Chapter[] SelectChapters(Connector connector, Publication publication, Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select Chapters"); + logger?.WriteLine("Tranga_CLI", "Menu: Select Chapters"); Chapter[] availableChapters = connector.GetChapters(publication, "en"); int cIndex = 0; Console.WriteLine("Chapters:"); @@ -540,9 +460,9 @@ public static class Tranga_Cli return connector.SelectChapters(publication, selectedChapters); } - private static Connector? SelectConnector(Connector[] connectors, Logger logger) + private static Connector? SelectConnector(Connector[] connectors, Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select Connector"); + logger?.WriteLine("Tranga_CLI", "Menu: Select Connector"); Console.Clear(); int cIndex = 0; @@ -561,7 +481,7 @@ public static class Tranga_Cli { Console.Clear(); Console.WriteLine("aborted."); - logger.WriteLine("Tranga_CLI", "aborted."); + logger?.WriteLine("Tranga_CLI", "aborted."); return null; } @@ -573,15 +493,15 @@ public static class Tranga_Cli catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); - logger.WriteLine("Tranga_CLI", e.Message); + logger?.WriteLine("Tranga_CLI", e.Message); } return null; } - private static Publication? SelectPublication(TaskManager taskManager, Connector connector, Logger logger) + private static Publication? SelectPublication(TaskManager taskManager, Connector connector) { - logger.WriteLine("Tranga_CLI", "Menu: Select Publication"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Select Publication"); Console.Clear(); Console.WriteLine($"Connector: {connector.name}"); @@ -592,7 +512,7 @@ public static class Tranga_Cli if (publications.Length < 1) { - logger.WriteLine("Tranga_CLI", "No publications returned"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "No publications returned"); Console.WriteLine($"No publications for query '{query}' returned;"); return null; } @@ -613,7 +533,7 @@ public static class Tranga_Cli { Console.Clear(); Console.WriteLine("aborted."); - logger.WriteLine("Tranga_CLI", "aborted."); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "aborted."); return null; } @@ -625,21 +545,21 @@ public static class Tranga_Cli catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); - logger.WriteLine("Tranga_CLI", e.Message); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", e.Message); } return null; } - private static void SearchTasks(TaskManager taskManager, Logger logger) + private static void SearchTasks(TaskManager taskManager) { - logger.WriteLine("Tranga_CLI", "Menu: Search task"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Search task"); Console.Clear(); Console.WriteLine("Enter search query:"); string? query = Console.ReadLine(); while (query is null || query.Length < 4) query = Console.ReadLine(); PrintTasks(taskManager.GetAllTasks().Where(qTask => - qTask.ToString().ToLower().Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray(), logger); + qTask.ToString().ToLower().Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray(), taskManager.commonObjects.logger); } } \ No newline at end of file diff --git a/Tranga/Tranga.csproj b/Tranga/Tranga.csproj index a2edb49..2e4242b 100644 --- a/Tranga/Tranga.csproj +++ b/Tranga/Tranga.csproj @@ -4,6 +4,7 @@ net7.0 enable enable + Exe @@ -16,4 +17,11 @@ + + + .dockerignore + Dockerfile + + + From 4e73b0a4cfa725c5db790c6a20aa44b509a12072 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:32:42 +0200 Subject: [PATCH 04/19] wrong dockerfile --- .github/workflows/docker-image-cuttingedge.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image-cuttingedge.yml b/.github/workflows/docker-image-cuttingedge.yml index 1346138..b36acd2 100644 --- a/.github/workflows/docker-image-cuttingedge.yml +++ b/.github/workflows/docker-image-cuttingedge.yml @@ -36,7 +36,7 @@ jobs: uses: docker/build-push-action@v4.1.1 with: context: ./ - file: ./API/Dockerfile + file: ./Dockerfile #platforms: linux/amd64,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/mips64le,linux/mips64,linux/arm/v7,linux/arm/v6 platforms: linux/amd64 pull: true From eb0eb71e8634bc3c37e93cf4cef379182390fe46 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:33:57 +0200 Subject: [PATCH 05/19] wrong dockerfile --- .github/workflows/docker-image-master.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image-master.yml b/.github/workflows/docker-image-master.yml index 35fe424..995c8a2 100644 --- a/.github/workflows/docker-image-master.yml +++ b/.github/workflows/docker-image-master.yml @@ -38,7 +38,7 @@ jobs: uses: docker/build-push-action@v4.1.1 with: context: ./ - file: ./API/Dockerfile + file: ./Dockerfile #platforms: linux/amd64,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/mips64le,linux/mips64,linux/arm/v7,linux/arm/v6 platforms: linux/amd64 pull: true From e5abaa4549bec6f6f5aa0ceb79f709aaa750eec3 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:35:11 +0200 Subject: [PATCH 06/19] Wrong entrypoint --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 1379b93..bfc1bcc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,4 +12,4 @@ FROM glax/tranga-base:latest as runtime WORKDIR /publish COPY --from=build-env /publish . EXPOSE 6531 -ENTRYPOINT ["dotnet", "/publish/API.dll"] +ENTRYPOINT ["dotnet", "/publish/Tranga.exe"] From 74aca86b62b825a29d24c4ee23397245fd9b20af Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:36:56 +0200 Subject: [PATCH 07/19] Wrong entrypoint --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index bfc1bcc..17b6cde 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,4 +12,4 @@ FROM glax/tranga-base:latest as runtime WORKDIR /publish COPY --from=build-env /publish . EXPOSE 6531 -ENTRYPOINT ["dotnet", "/publish/Tranga.exe"] +ENTRYPOINT ["dotnet", "/publish/Tranga.dll"] From 5b89cbd0426ba2be9df51d7d49df8bbad7d1aeec Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:41:25 +0200 Subject: [PATCH 08/19] Only run TaskMode on Windows --- Tranga/Tranga.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index 5757d9c..d93dc67 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -49,7 +49,8 @@ public static class Tranga foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers) nm.SendNotification("Tranga-API", "Started Tranga-API"); - TaskMode(taskManager, logger); + if(!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + TaskMode(taskManager, logger); } private static void TaskMode(TaskManager taskManager, Logger logger) From 18134cdf014a31e0cb1a7a725677e6319a6c62cc Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:43:57 +0200 Subject: [PATCH 09/19] If not running cli add back console output --- Tranga/CommonObjects.cs | 2 +- Tranga/Tranga.cs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Tranga/CommonObjects.cs b/Tranga/CommonObjects.cs index a97e117..2e543eb 100644 --- a/Tranga/CommonObjects.cs +++ b/Tranga/CommonObjects.cs @@ -9,7 +9,7 @@ public class CommonObjects { public HashSet libraryManagers { get; init; } public HashSet notificationManagers { get; init; } - public Logger? logger { get; init; } + public Logger? logger { get; set; } [JsonIgnore]private string settingsFilePath { get; init; } public CommonObjects(HashSet? libraryManagers, HashSet? notificationManagers, Logger? logger, string settingsFilePath) diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index d93dc67..8a605b5 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -51,6 +51,8 @@ public static class Tranga if(!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) TaskMode(taskManager, logger); + else + taskManager.commonObjects.logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath); } private static void TaskMode(TaskManager taskManager, Logger logger) From 336e08aebf71f51af927889faec18e5c00956083 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:46:14 +0200 Subject: [PATCH 10/19] If not running cli add back console output --- Tranga/Tranga.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index 8a605b5..69f8625 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -20,7 +20,11 @@ public static class Tranga Directory.CreateDirectory(logsFolderPath); - Logger logger = new(new[] { Logger.LoggerType.FileLogger }, Console.Out, Console.Out.Encoding, logFilePath); + Logger logger; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + logger =new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath); + else + logger = new(new[] { Logger.LoggerType.FileLogger }, Console.Out, Console.Out.Encoding, logFilePath); logger.WriteLine("Tranga",value: "\n"+ "-------------------------------------------\n"+ @@ -51,8 +55,6 @@ public static class Tranga if(!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) TaskMode(taskManager, logger); - else - taskManager.commonObjects.logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath); } private static void TaskMode(TaskManager taskManager, Logger logger) From 70b1ae4812ca943670cda81dbd99b9187e2ebb99 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:52:27 +0200 Subject: [PATCH 11/19] isLinux --- Tranga/Tranga.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index 69f8625..b6845e9 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -12,19 +12,19 @@ public static class Tranga { public static void Main(string[] args) { - string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga-API"); - string downloadFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(applicationFolderPath, "Manga"); - string logsFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/var/log/Tranga" : Path.Join(applicationFolderPath, "log"); + bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga-API"); + + string downloadFolderPath = isLinux ? "/Manga" : Path.Join(applicationFolderPath, "Manga"); + string logsFolderPath = isLinux ? "/var/log/Tranga" : Path.Join(applicationFolderPath, "log"); string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"); string settingsFilePath = Path.Join(applicationFolderPath, "settings.json"); Directory.CreateDirectory(logsFolderPath); - Logger logger; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - logger =new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath); - else - logger = new(new[] { Logger.LoggerType.FileLogger }, Console.Out, Console.Out.Encoding, logFilePath); + Logger logger = isLinux + ? new Logger(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath) + : new Logger(new[] { Logger.LoggerType.FileLogger }, Console.Out, Console.Out.Encoding, logFilePath); logger.WriteLine("Tranga",value: "\n"+ "-------------------------------------------\n"+ @@ -40,6 +40,7 @@ public static class Tranga Directory.CreateDirectory(settings.downloadLocation); Directory.CreateDirectory(settings.coverImageCache); + logger.WriteLine("Tranga", $"Is Linux: {isLinux}"); logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); @@ -53,7 +54,7 @@ public static class Tranga foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers) nm.SendNotification("Tranga-API", "Started Tranga-API"); - if(!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + if(!isLinux) TaskMode(taskManager, logger); } From c91754614bca15d2a7e6da1ce0bfc87b44a493b9 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:58:22 +0200 Subject: [PATCH 12/19] weird env --- Tranga/Tranga.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index b6845e9..5de93a5 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -30,11 +30,14 @@ public static class Tranga "-------------------------------------------\n"+ " Starting Tranga-API\n"+ "-------------------------------------------"); - logger.WriteLine("Tranga", "Loading settings."); - TrangaSettings settings = File.Exists(settingsFilePath) - ? TrangaSettings.LoadSettings(settingsFilePath) - : new TrangaSettings(downloadFolderPath, applicationFolderPath); + TrangaSettings settings; + if (File.Exists(settingsFilePath)) + { + logger.WriteLine("Tranga", $"Loading settings {settingsFilePath}"); + settings = TrangaSettings.LoadSettings(settingsFilePath); + }else + settings = new TrangaSettings(downloadFolderPath, applicationFolderPath); Directory.CreateDirectory(settings.workingDirectory); Directory.CreateDirectory(settings.downloadLocation); From d1ce244135d76999922bda66940aae721c7075bb Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 01:26:38 +0200 Subject: [PATCH 13/19] New Migration to new commonObjects --- Tranga/Migrate.cs | 33 ------------------ Tranga/Migrator.cs | 79 +++++++++++++++++++++++++++++++++++++++++++ Tranga/TaskManager.cs | 1 - Tranga/Tranga.cs | 2 ++ 4 files changed, 81 insertions(+), 34 deletions(-) delete mode 100644 Tranga/Migrate.cs create mode 100644 Tranga/Migrator.cs diff --git a/Tranga/Migrate.cs b/Tranga/Migrate.cs deleted file mode 100644 index 02b4988..0000000 --- a/Tranga/Migrate.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Newtonsoft.Json; -using Tranga.TrangaTasks; - -namespace Tranga; - -public static class Migrate -{ - private static readonly ushort CurrentVersion = 16; - public static void Files(TrangaSettings settings) - { - settings.version ??= 15; - switch (settings.version) - { - case 15: - RemoveUpdateLibraryTask(settings); - break; - } - - settings.version = CurrentVersion; - settings.ExportSettings(); - } - - private static void RemoveUpdateLibraryTask(TrangaSettings settings) - { - if (!File.Exists(settings.tasksFilePath)) - return; - - string tasksJsonString = File.ReadAllText(settings.tasksFilePath); - HashSet tasks = JsonConvert.DeserializeObject>(tasksJsonString, new JsonSerializerSettings { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!; - tasks.RemoveWhere(t => t.task == TrangaTask.Task.UpdateLibraries); - File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(tasks)); - } -} \ No newline at end of file diff --git a/Tranga/Migrator.cs b/Tranga/Migrator.cs new file mode 100644 index 0000000..3ebbf96 --- /dev/null +++ b/Tranga/Migrator.cs @@ -0,0 +1,79 @@ +using System.Text.Json.Nodes; +using Newtonsoft.Json; +using Tranga.LibraryManagers; +using Tranga.NotificationManagers; +using Tranga.TrangaTasks; + +namespace Tranga; + +public static class Migrator +{ + private static readonly ushort CurrentVersion = 17; + public static void Migrate(string settingsFilePath) + { + if (!File.Exists(settingsFilePath)) + return; + JsonNode settingsNode = JsonNode.Parse(File.ReadAllText(settingsFilePath))!; + ushort version = settingsNode["version"]!.GetValue(); + switch (version) + { + case 15: + MoveToCommonObjects(settingsFilePath); + TrangaSettings.SettingsJsonObject sjo = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath))!; + RemoveUpdateLibraryTask(sjo.ts!); + break; + case 16: + MoveToCommonObjects(settingsFilePath); + break; + } + TrangaSettings.SettingsJsonObject sjo2 = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath))!; + sjo2.ts!.version = CurrentVersion; + sjo2.ts!.ExportSettings(); + } + + private static void RemoveUpdateLibraryTask(TrangaSettings settings) + { + if (!File.Exists(settings.tasksFilePath)) + return; + + string tasksJsonString = File.ReadAllText(settings.tasksFilePath); + HashSet tasks = JsonConvert.DeserializeObject>(tasksJsonString, new JsonSerializerSettings { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!; + tasks.RemoveWhere(t => t.task == TrangaTask.Task.UpdateLibraries); + File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(tasks)); + } + + public static void MoveToCommonObjects(string settingsFilePath) + { + if (!File.Exists(settingsFilePath)) + return; + + JsonNode node = JsonNode.Parse(File.ReadAllText(settingsFilePath))!; + TrangaSettings settings = new( + node["downloadLocation"]!.GetValue(), + node["workingDirectory"]!.GetValue()); + JsonArray libraryManagers = node["libraryManagers"]!.AsArray(); + JsonNode? komgaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue() == LibraryManager.LibraryType.Komga); + JsonNode? kavitaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue() == LibraryManager.LibraryType.Kavita); + HashSet lms = new(); + if (komgaNode is not null) + lms.Add(new Komga(komgaNode["baseUrl"]!.GetValue(), komgaNode["auth"]!.GetValue(), null)); + if (kavitaNode is not null) + lms.Add(new Kavita(kavitaNode["baseUrl"]!.GetValue(), kavitaNode["auth"]!.GetValue(), null)); + + JsonArray notificationManagers = node["libraryManagers"]!.AsArray(); + JsonNode? gotifyNode = notificationManagers.FirstOrDefault(nm => + nm["notificationManagerType"].GetValue() == NotificationManager.NotificationManagerType.Gotify); + JsonNode? lunaSeaNode = notificationManagers.FirstOrDefault(nm => + nm["notificationManagerType"].GetValue() == NotificationManager.NotificationManagerType.LunaSea); + HashSet nms = new(); + if (gotifyNode is not null) + nms.Add(new Gotify(gotifyNode["endpoint"]!.GetValue(), gotifyNode["appToken"]!.GetValue())); + if (lunaSeaNode is not null) + nms.Add(new LunaSea(lunaSeaNode["id"]!.GetValue())); + + CommonObjects co = new (lms, nms, null, settingsFilePath); + + TrangaSettings.SettingsJsonObject sjo = new(settings, co); + File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(sjo)); + } +} \ No newline at end of file diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 3ab8ba2..04f4afa 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -36,7 +36,6 @@ public class TaskManager }; this.settings = settings; - Migrate.Files(settings); ImportData(); ExportDataAndSettings(); Thread taskChecker = new(TaskCheckerThread); diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index 5de93a5..dc29b39 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -30,6 +30,8 @@ public static class Tranga "-------------------------------------------\n"+ " Starting Tranga-API\n"+ "-------------------------------------------"); + logger.WriteLine("Tranga", "Migrating..."); + Migrator.Migrate(settingsFilePath); TrangaSettings settings; if (File.Exists(settingsFilePath)) From 3aa299e48acf12c129ca3a9885fd531c6fd587d5 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 01:28:32 +0200 Subject: [PATCH 14/19] deserialization of enum --- Tranga/Migrator.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tranga/Migrator.cs b/Tranga/Migrator.cs index 3ebbf96..b15f1ff 100644 --- a/Tranga/Migrator.cs +++ b/Tranga/Migrator.cs @@ -52,8 +52,8 @@ public static class Migrator node["downloadLocation"]!.GetValue(), node["workingDirectory"]!.GetValue()); JsonArray libraryManagers = node["libraryManagers"]!.AsArray(); - JsonNode? komgaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue() == LibraryManager.LibraryType.Komga); - JsonNode? kavitaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue() == LibraryManager.LibraryType.Kavita); + JsonNode? komgaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue() == (byte)LibraryManager.LibraryType.Komga); + JsonNode? kavitaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue() == (byte)LibraryManager.LibraryType.Kavita); HashSet lms = new(); if (komgaNode is not null) lms.Add(new Komga(komgaNode["baseUrl"]!.GetValue(), komgaNode["auth"]!.GetValue(), null)); @@ -62,9 +62,9 @@ public static class Migrator JsonArray notificationManagers = node["libraryManagers"]!.AsArray(); JsonNode? gotifyNode = notificationManagers.FirstOrDefault(nm => - nm["notificationManagerType"].GetValue() == NotificationManager.NotificationManagerType.Gotify); + nm["notificationManagerType"].GetValue() == (byte)NotificationManager.NotificationManagerType.Gotify); JsonNode? lunaSeaNode = notificationManagers.FirstOrDefault(nm => - nm["notificationManagerType"].GetValue() == NotificationManager.NotificationManagerType.LunaSea); + nm["notificationManagerType"].GetValue() == (byte)NotificationManager.NotificationManagerType.LunaSea); HashSet nms = new(); if (gotifyNode is not null) nms.Add(new Gotify(gotifyNode["endpoint"]!.GetValue(), gotifyNode["appToken"]!.GetValue())); From 01c8784bab40e1f33485865ef5ae732643966474 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 01:30:32 +0200 Subject: [PATCH 15/19] wrong array --- Tranga/Migrator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tranga/Migrator.cs b/Tranga/Migrator.cs index b15f1ff..2d81ed5 100644 --- a/Tranga/Migrator.cs +++ b/Tranga/Migrator.cs @@ -60,7 +60,7 @@ public static class Migrator if (kavitaNode is not null) lms.Add(new Kavita(kavitaNode["baseUrl"]!.GetValue(), kavitaNode["auth"]!.GetValue(), null)); - JsonArray notificationManagers = node["libraryManagers"]!.AsArray(); + JsonArray notificationManagers = node["notificationManagers"]!.AsArray(); JsonNode? gotifyNode = notificationManagers.FirstOrDefault(nm => nm["notificationManagerType"].GetValue() == (byte)NotificationManager.NotificationManagerType.Gotify); JsonNode? lunaSeaNode = notificationManagers.FirstOrDefault(nm => From 00861c406a107bc287e27e2a526676d6dc50dfcc Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 01:42:15 +0200 Subject: [PATCH 16/19] added logging --- Tranga/Migrator.cs | 22 ++++++++++++++-------- Tranga/Tranga.cs | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Tranga/Migrator.cs b/Tranga/Migrator.cs index 2d81ed5..885bd5b 100644 --- a/Tranga/Migrator.cs +++ b/Tranga/Migrator.cs @@ -1,4 +1,5 @@ using System.Text.Json.Nodes; +using Logging; using Newtonsoft.Json; using Tranga.LibraryManagers; using Tranga.NotificationManagers; @@ -9,21 +10,22 @@ namespace Tranga; public static class Migrator { private static readonly ushort CurrentVersion = 17; - public static void Migrate(string settingsFilePath) + public static void Migrate(string settingsFilePath, Logger? logger) { if (!File.Exists(settingsFilePath)) return; JsonNode settingsNode = JsonNode.Parse(File.ReadAllText(settingsFilePath))!; ushort version = settingsNode["version"]!.GetValue(); + logger?.WriteLine("Migrator", $"Migrating {version} -> {CurrentVersion}"); switch (version) { case 15: - MoveToCommonObjects(settingsFilePath); + MoveToCommonObjects(settingsFilePath, logger); TrangaSettings.SettingsJsonObject sjo = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath))!; - RemoveUpdateLibraryTask(sjo.ts!); + RemoveUpdateLibraryTask(sjo.ts!, logger); break; case 16: - MoveToCommonObjects(settingsFilePath); + MoveToCommonObjects(settingsFilePath, logger); break; } TrangaSettings.SettingsJsonObject sjo2 = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath))!; @@ -31,27 +33,30 @@ public static class Migrator sjo2.ts!.ExportSettings(); } - private static void RemoveUpdateLibraryTask(TrangaSettings settings) + private static void RemoveUpdateLibraryTask(TrangaSettings settings, Logger? logger) { if (!File.Exists(settings.tasksFilePath)) return; + logger?.WriteLine("Migrator", "Removing old/deprecated UpdateLibraryTasks (v16)"); string tasksJsonString = File.ReadAllText(settings.tasksFilePath); HashSet tasks = JsonConvert.DeserializeObject>(tasksJsonString, new JsonSerializerSettings { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!; tasks.RemoveWhere(t => t.task == TrangaTask.Task.UpdateLibraries); File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(tasks)); } - public static void MoveToCommonObjects(string settingsFilePath) + public static void MoveToCommonObjects(string settingsFilePath, Logger? logger) { if (!File.Exists(settingsFilePath)) return; + logger?.WriteLine("Migrator", "Moving Settings to commonObjects-structure (v17)"); JsonNode node = JsonNode.Parse(File.ReadAllText(settingsFilePath))!; - TrangaSettings settings = new( + TrangaSettings ts = new( node["downloadLocation"]!.GetValue(), node["workingDirectory"]!.GetValue()); JsonArray libraryManagers = node["libraryManagers"]!.AsArray(); + logger?.WriteLine("Migrator", $"\tGot {libraryManagers.Count} libraryManagers."); JsonNode? komgaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue() == (byte)LibraryManager.LibraryType.Komga); JsonNode? kavitaNode = libraryManagers.FirstOrDefault(lm => lm["libraryType"].GetValue() == (byte)LibraryManager.LibraryType.Kavita); HashSet lms = new(); @@ -61,6 +66,7 @@ public static class Migrator lms.Add(new Kavita(kavitaNode["baseUrl"]!.GetValue(), kavitaNode["auth"]!.GetValue(), null)); JsonArray notificationManagers = node["notificationManagers"]!.AsArray(); + logger?.WriteLine("Migrator", $"\tGot {notificationManagers.Count} notificationManagers."); JsonNode? gotifyNode = notificationManagers.FirstOrDefault(nm => nm["notificationManagerType"].GetValue() == (byte)NotificationManager.NotificationManagerType.Gotify); JsonNode? lunaSeaNode = notificationManagers.FirstOrDefault(nm => @@ -73,7 +79,7 @@ public static class Migrator CommonObjects co = new (lms, nms, null, settingsFilePath); - TrangaSettings.SettingsJsonObject sjo = new(settings, co); + TrangaSettings.SettingsJsonObject sjo = new(ts, co); File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(sjo)); } } \ No newline at end of file diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index dc29b39..63a51ae 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -31,7 +31,7 @@ public static class Tranga " Starting Tranga-API\n"+ "-------------------------------------------"); logger.WriteLine("Tranga", "Migrating..."); - Migrator.Migrate(settingsFilePath); + Migrator.Migrate(settingsFilePath, logger); TrangaSettings settings; if (File.Exists(settingsFilePath)) From b3d778ff563bb472c311a1909b489a129d685cad Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 01:45:55 +0200 Subject: [PATCH 17/19] accessibility --- Tranga/TrangaSettings.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index 1a20d78..6173ac2 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -87,8 +87,8 @@ public class TrangaSettings internal class SettingsJsonObject { - internal TrangaSettings? ts; - internal CommonObjects? co; + public TrangaSettings? ts { get; } + public CommonObjects? co { get; } public SettingsJsonObject(TrangaSettings? ts, CommonObjects? co) { From eb50b84266aff2fd3f4cef8e9a4bfb954ce72105 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 01:48:40 +0200 Subject: [PATCH 18/19] Converters --- Tranga/Migrator.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Tranga/Migrator.cs b/Tranga/Migrator.cs index 885bd5b..85c15d7 100644 --- a/Tranga/Migrator.cs +++ b/Tranga/Migrator.cs @@ -28,7 +28,18 @@ public static class Migrator MoveToCommonObjects(settingsFilePath, logger); break; } - TrangaSettings.SettingsJsonObject sjo2 = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath))!; + + TrangaSettings.SettingsJsonObject sjo2 = JsonConvert.DeserializeObject( + File.ReadAllText(settingsFilePath), + new JsonSerializerSettings + { + Converters = + { + new TrangaTask.TrangaTaskJsonConverter(), + new NotificationManager.NotificationManagerJsonConverter(), + new LibraryManager.LibraryManagerJsonConverter() + } + })!; sjo2.ts!.version = CurrentVersion; sjo2.ts!.ExportSettings(); } @@ -40,7 +51,8 @@ public static class Migrator logger?.WriteLine("Migrator", "Removing old/deprecated UpdateLibraryTasks (v16)"); string tasksJsonString = File.ReadAllText(settings.tasksFilePath); - HashSet tasks = JsonConvert.DeserializeObject>(tasksJsonString, new JsonSerializerSettings { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!; + HashSet tasks = JsonConvert.DeserializeObject>(tasksJsonString, + new JsonSerializerSettings { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!; tasks.RemoveWhere(t => t.task == TrangaTask.Task.UpdateLibraries); File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(tasks)); } From e3201a9b9909ffa0cd85fc7fdf1d0679af618896 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 01:50:26 +0200 Subject: [PATCH 19/19] Ignore Logger --- Tranga/CommonObjects.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tranga/CommonObjects.cs b/Tranga/CommonObjects.cs index 2e543eb..531b3fd 100644 --- a/Tranga/CommonObjects.cs +++ b/Tranga/CommonObjects.cs @@ -9,7 +9,7 @@ public class CommonObjects { public HashSet libraryManagers { get; init; } public HashSet notificationManagers { get; init; } - public Logger? logger { get; set; } + [JsonIgnore]public Logger? logger { get; set; } [JsonIgnore]private string settingsFilePath { get; init; } public CommonObjects(HashSet? libraryManagers, HashSet? notificationManagers, Logger? logger, string settingsFilePath)