2
0

Rename TBaseObject -> GlobalBase

Remove Notification and Library Connectors from GlobalBase
This commit is contained in:
glax 2023-08-01 18:24:19 +02:00
parent c45e4ddf90
commit e4086a8892
14 changed files with 66 additions and 86 deletions

48
Tranga/GlobalBase.cs Normal file
View File

@ -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;
}
}
}

View File

@ -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)
{
}

View File

@ -10,13 +10,13 @@ namespace Tranga.LibraryConnectors;
/// </summary>
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)
{
}

View File

@ -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;

View File

@ -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
/// </summary>
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);

View File

@ -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<byte, DateTime> _lastExecutedRateLimit;
private readonly Dictionary<byte, TimeSpan> _rateLimit;
public DownloadClient(Dictionary<byte, int> rateLimitRequestsPerMinute, TBaseObject clone) : base(clone)
public DownloadClient(Dictionary<byte, int> rateLimitRequestsPerMinute, GlobalBase clone) : base(clone)
{
_lastExecutedRateLimit = new();
_rateLimit = new();

View File

@ -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<byte, int>()

View File

@ -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<byte, int>()

View File

@ -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<byte, int>()

View File

@ -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<byte, int>()

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<NotificationConnector> notificationConnectors { get; init; }
protected HashSet<LibraryConnector> 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<NotificationConnector> notificationConnectors, HashSet<LibraryConnector> 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();
}
}