diff --git a/Tranga/GlobalBase.cs b/Tranga/GlobalBase.cs new file mode 100644 index 0000000..5f66fcb --- /dev/null +++ b/Tranga/GlobalBase.cs @@ -0,0 +1,48 @@ +using Logging; + +namespace Tranga; + +public class GlobalBase +{ + protected Logger? logger { get; init; } + protected TrangaSettings settings { get; init; } + + public GlobalBase(GlobalBase clone) + { + this.logger = clone.logger; + this.settings = clone.settings; + } + + public GlobalBase(Logger? logger, TrangaSettings settings) + { + this.logger = logger; + this.settings = settings; + } + + protected void Log(string message) + { + logger?.WriteLine(this.GetType().Name, message); + } + + protected void Log(string fStr, params object?[] replace) + { + Log(string.Format(fStr, replace)); + } + + protected bool IsFileInUse(string filePath) + { + if (!File.Exists(filePath)) + return false; + try + { + using FileStream stream = new (filePath, FileMode.Open, FileAccess.Read, FileShare.None); + stream.Close(); + return false; + } + catch (IOException) + { + Log($"File is in use {filePath}"); + return true; + } + } +} \ No newline at end of file diff --git a/Tranga/LibraryConnectors/Kavita.cs b/Tranga/LibraryConnectors/Kavita.cs index e670eb4..e94effb 100644 --- a/Tranga/LibraryConnectors/Kavita.cs +++ b/Tranga/LibraryConnectors/Kavita.cs @@ -7,13 +7,13 @@ namespace Tranga.LibraryConnectors; public class Kavita : LibraryConnector { - public Kavita(string baseUrl, string username, string password, TBaseObject clone) : + public Kavita(string baseUrl, string username, string password, GlobalBase clone) : base(baseUrl, GetToken(baseUrl, username, password), LibraryType.Kavita, clone) { } [JsonConstructor] - public Kavita(string baseUrl, string auth, TBaseObject clone) : base(baseUrl, auth, LibraryType.Kavita, clone) + public Kavita(string baseUrl, string auth, GlobalBase clone) : base(baseUrl, auth, LibraryType.Kavita, clone) { } diff --git a/Tranga/LibraryConnectors/Komga.cs b/Tranga/LibraryConnectors/Komga.cs index 669b203..c2d4fcf 100644 --- a/Tranga/LibraryConnectors/Komga.cs +++ b/Tranga/LibraryConnectors/Komga.cs @@ -10,13 +10,13 @@ namespace Tranga.LibraryConnectors; /// public class Komga : LibraryConnector { - public Komga(string baseUrl, string username, string password, TBaseObject clone) + public Komga(string baseUrl, string username, string password, GlobalBase clone) : base(baseUrl, Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")), LibraryType.Komga, clone) { } [JsonConstructor] - public Komga(string baseUrl, string auth, TBaseObject clone) : base(baseUrl, auth, LibraryType.Komga, clone) + public Komga(string baseUrl, string auth, GlobalBase clone) : base(baseUrl, auth, LibraryType.Komga, clone) { } diff --git a/Tranga/LibraryConnectors/LibraryConnector.cs b/Tranga/LibraryConnectors/LibraryConnector.cs index 165304e..38a43a0 100644 --- a/Tranga/LibraryConnectors/LibraryConnector.cs +++ b/Tranga/LibraryConnectors/LibraryConnector.cs @@ -4,7 +4,7 @@ using Logging; namespace Tranga.LibraryConnectors; -public abstract class LibraryConnector : TBaseObject +public abstract class LibraryConnector : GlobalBase { public enum LibraryType : byte { @@ -18,7 +18,7 @@ public abstract class LibraryConnector : TBaseObject // ReSharper disable once MemberCanBeProtected.Global public string auth { get; } //Base64 encoded, if you use your password everywhere, you have problems - protected LibraryConnector(string baseUrl, string auth, LibraryType libraryType, TBaseObject clone) : base(clone) + protected LibraryConnector(string baseUrl, string auth, LibraryType libraryType, GlobalBase clone) : base(clone) { this.baseUrl = baseUrl; this.auth = auth; diff --git a/Tranga/MangaConnectors/Connector.cs b/Tranga/MangaConnectors/Connector.cs index 6d4b6c4..1c839dd 100644 --- a/Tranga/MangaConnectors/Connector.cs +++ b/Tranga/MangaConnectors/Connector.cs @@ -11,11 +11,11 @@ namespace Tranga.MangaConnectors; /// Base-Class for all Connectors /// Provides some methods to be used by all Connectors, as well as a DownloadClient /// -public abstract class Connector : TBaseObject +public abstract class Connector : GlobalBase { internal DownloadClient downloadClient { get; init; } = null!; - protected Connector(TBaseObject clone) : base(clone) + protected Connector(GlobalBase clone) : base(clone) { if (!Directory.Exists(settings.coverImageCache)) Directory.CreateDirectory(settings.coverImageCache); diff --git a/Tranga/MangaConnectors/DownloadClient.cs b/Tranga/MangaConnectors/DownloadClient.cs index b54f56a..70acaea 100644 --- a/Tranga/MangaConnectors/DownloadClient.cs +++ b/Tranga/MangaConnectors/DownloadClient.cs @@ -2,7 +2,7 @@ namespace Tranga.MangaConnectors; -internal class DownloadClient : TBaseObject +internal class DownloadClient : GlobalBase { private static readonly HttpClient Client = new() { @@ -12,7 +12,7 @@ internal class DownloadClient : TBaseObject private readonly Dictionary _lastExecutedRateLimit; private readonly Dictionary _rateLimit; - public DownloadClient(Dictionary rateLimitRequestsPerMinute, TBaseObject clone) : base(clone) + public DownloadClient(Dictionary rateLimitRequestsPerMinute, GlobalBase clone) : base(clone) { _lastExecutedRateLimit = new(); _rateLimit = new(); diff --git a/Tranga/MangaConnectors/MangaDex.cs b/Tranga/MangaConnectors/MangaDex.cs index b80ccff..7fa1af6 100644 --- a/Tranga/MangaConnectors/MangaDex.cs +++ b/Tranga/MangaConnectors/MangaDex.cs @@ -17,7 +17,7 @@ public class MangaDex : Connector Author, } - public MangaDex(TBaseObject clone) : base(clone) + public MangaDex(GlobalBase clone) : base(clone) { name = "MangaDex"; this.downloadClient = new DownloadClient(new Dictionary() diff --git a/Tranga/MangaConnectors/MangaKatana.cs b/Tranga/MangaConnectors/MangaKatana.cs index dd88eda..f0fbe40 100644 --- a/Tranga/MangaConnectors/MangaKatana.cs +++ b/Tranga/MangaConnectors/MangaKatana.cs @@ -9,7 +9,7 @@ public class MangaKatana : Connector { public override string name { get; } - public MangaKatana(TBaseObject clone) : base(clone) + public MangaKatana(GlobalBase clone) : base(clone) { this.name = "MangaKatana"; this.downloadClient = new DownloadClient(new Dictionary() diff --git a/Tranga/MangaConnectors/Manganato.cs b/Tranga/MangaConnectors/Manganato.cs index af71acf..76d0230 100644 --- a/Tranga/MangaConnectors/Manganato.cs +++ b/Tranga/MangaConnectors/Manganato.cs @@ -9,7 +9,7 @@ public class Manganato : Connector { public override string name { get; } - public Manganato(TBaseObject clone) : base(clone) + public Manganato(GlobalBase clone) : base(clone) { this.name = "Manganato"; this.downloadClient = new DownloadClient(new Dictionary() diff --git a/Tranga/MangaConnectors/Mangasee.cs b/Tranga/MangaConnectors/Mangasee.cs index e580b18..0e27809 100644 --- a/Tranga/MangaConnectors/Mangasee.cs +++ b/Tranga/MangaConnectors/Mangasee.cs @@ -14,7 +14,7 @@ public class Mangasee : Connector private IBrowser? _browser; private const string ChromiumVersion = "1154303"; - public Mangasee(TBaseObject clone) : base(clone) + public Mangasee(GlobalBase clone) : base(clone) { this.name = "Mangasee"; this.downloadClient = new DownloadClient(new Dictionary() diff --git a/Tranga/NotificationConnectors/Gotify.cs b/Tranga/NotificationConnectors/Gotify.cs index ea06e5d..a83f971 100644 --- a/Tranga/NotificationConnectors/Gotify.cs +++ b/Tranga/NotificationConnectors/Gotify.cs @@ -11,7 +11,7 @@ public class Gotify : NotificationConnector private readonly HttpClient _client = new(); [JsonConstructor] - public Gotify(string endpoint, string appToken, TBaseObject clone) : base(NotificationManagerType.Gotify, clone) + public Gotify(string endpoint, string appToken, GlobalBase clone) : base(NotificationManagerType.Gotify, clone) { this.endpoint = endpoint; this.appToken = appToken; diff --git a/Tranga/NotificationConnectors/LunaSea.cs b/Tranga/NotificationConnectors/LunaSea.cs index 07e7347..bded8cb 100644 --- a/Tranga/NotificationConnectors/LunaSea.cs +++ b/Tranga/NotificationConnectors/LunaSea.cs @@ -10,7 +10,7 @@ public class LunaSea : NotificationConnector private readonly HttpClient _client = new(); [JsonConstructor] - public LunaSea(string id, TBaseObject clone) : base(NotificationManagerType.LunaSea, clone) + public LunaSea(string id, GlobalBase clone) : base(NotificationManagerType.LunaSea, clone) { this.id = id; } diff --git a/Tranga/NotificationConnectors/NotificationConnector.cs b/Tranga/NotificationConnectors/NotificationConnector.cs index aa77a78..889de50 100644 --- a/Tranga/NotificationConnectors/NotificationConnector.cs +++ b/Tranga/NotificationConnectors/NotificationConnector.cs @@ -1,10 +1,10 @@ namespace Tranga.NotificationConnectors; -public abstract class NotificationConnector : TBaseObject +public abstract class NotificationConnector : GlobalBase { public NotificationManagerType notificationManagerType; - protected NotificationConnector(NotificationManagerType notificationManagerType, TBaseObject clone) : base(clone) + protected NotificationConnector(NotificationManagerType notificationManagerType, GlobalBase clone) : base(clone) { this.notificationManagerType = notificationManagerType; } diff --git a/Tranga/TBaseObject.cs b/Tranga/TBaseObject.cs deleted file mode 100644 index 8f95e3c..0000000 --- a/Tranga/TBaseObject.cs +++ /dev/null @@ -1,68 +0,0 @@ -using Logging; -using Tranga.LibraryConnectors; -using Tranga.NotificationConnectors; - -namespace Tranga; - -public class TBaseObject -{ - protected Logger? logger { get; init; } - protected TrangaSettings settings { get; init; } - protected HashSet notificationConnectors { get; init; } - protected HashSet libraryConnectors { get; init; } - - public TBaseObject(TBaseObject clone) - { - this.logger = clone.logger; - this.settings = clone.settings; - this.notificationConnectors = clone.notificationConnectors; - this.libraryConnectors = clone.libraryConnectors; - } - - public TBaseObject(Logger? logger, TrangaSettings settings, HashSet notificationConnectors, HashSet libraryConnectors) - { - this.logger = logger; - this.settings = settings; - this.notificationConnectors = notificationConnectors; - this.libraryConnectors = libraryConnectors; - } - - protected void Log(string message) - { - logger?.WriteLine(this.GetType().Name, message); - } - - protected void Log(string fStr, params object?[] replace) - { - Log(string.Format(fStr, replace)); - } - - protected bool IsFileInUse(string filePath) - { - if (!File.Exists(filePath)) - return false; - try - { - using FileStream stream = new (filePath, FileMode.Open, FileAccess.Read, FileShare.None); - stream.Close(); - return false; - } - catch (IOException) - { - Log($"File is in use {filePath}"); - return true; - } - } - - protected void SendNotification(string title, string message) - { - foreach(NotificationConnector nc in notificationConnectors) - nc.SendNotification(title, message); - } - - protected void UpdateLibraries() - { - foreach (LibraryConnector libraryConnector in libraryConnectors) - libraryConnector.UpdateLibrary(); - } -} \ No newline at end of file