LibraryConnector-Updates work

Fixes #418
This commit is contained in:
2025-09-18 00:42:46 +02:00
parent 55fb37d62b
commit 5af1605c5b
16 changed files with 125 additions and 135 deletions

View File

@@ -54,7 +54,7 @@ builder.Services.AddSwaggerGen(opt =>
});
builder.Services.ConfigureOptions<NamedSwaggerGenOptions>();
string connectionString = $"Host={Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "localhost:5432"}; " +
string connectionString = $"Host={Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "tranga-pg:5432"}; " +
$"Database={Environment.GetEnvironmentVariable("POSTGRES_DB") ?? "postgres"}; " +
$"Username={Environment.GetEnvironmentVariable("POSTGRES_USER") ?? "postgres"}; " +
$"Password={Environment.GetEnvironmentVariable("POSTGRES_PASSWORD") ?? "postgres"}";
@@ -120,7 +120,8 @@ using (IServiceScope scope = app.Services.CreateScope())
context.Database.Migrate();
context.Notifications.RemoveRange(context.Notifications);
string[] emojis = { "(•‿•)", "(づ \u25d5‿\u25d5 )づ", "( \u02d8\u25bd\u02d8)っ\u2668", "=\uff3e\u25cf \u22cf \u25cf\uff3e=", "(ΦωΦ)", "(\u272a\u3268\u272a)", "( ノ・o・ )ノ", "(〜^\u2207^ )〜", "~(\u2267ω\u2266)~","૮ \u00b4• ﻌ \u00b4• ა", "(\u02c3ᆺ\u02c2)", "(=\ud83d\udf66 \u0f1d \ud83d\udf66=)"};
string[] emojis = ["(•‿•)", "(づ \u25d5‿\u25d5 )づ", "( \u02d8\u25bd\u02d8)っ\u2668", "=\uff3e\u25cf \u22cf \u25cf\uff3e=", "(ΦωΦ)", "(\u272a\u3268\u272a)", "( ノ・o・ )ノ", "(〜^\u2207^ )〜", "~(\u2267ω\u2266)~","૮ \u00b4• ﻌ \u00b4• ა", "(\u02c3ᆺ\u02c2)", "(=\ud83d\udf66 \u0f1d \ud83d\udf66=)"
];
context.Notifications.Add(new Notification("Tranga Started", emojis[Random.Shared.Next(0, emojis.Length - 1)], NotificationUrgency.High));
await context.Sync(CancellationToken.None);

View File

@@ -1,15 +1,12 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace API.Schema.LibraryContext.LibraryConnectors;
public class Kavita : LibraryConnector
public class Kavita(string baseUrl, string auth) : LibraryConnector(LibraryType.Kavita, baseUrl, auth)
{
public Kavita(string baseUrl, string auth) : base(LibraryType.Kavita, baseUrl, auth)
{
}
public Kavita(string baseUrl, string username, string password) :
this(baseUrl, GetToken(baseUrl, username, password))
{
@@ -51,16 +48,23 @@ public class Kavita : LibraryConnector
return "";
}
protected override void UpdateLibraryInternal()
public override async Task UpdateLibrary(CancellationToken ct)
{
foreach (KavitaLibrary lib in GetLibraries())
NetClient.MakePost($"{BaseUrl}/api/ToFileLibrary/scan?libraryId={lib.id}", "Bearer", Auth);
try
{
foreach (KavitaLibrary lib in await GetLibraries(ct))
await NetClient.MakeRequest($"{BaseUrl}/api/ToFileLibrary/scan?libraryId={lib.Id}", "Bearer", Auth, HttpMethod.Post, ct);
}
catch (Exception e)
{
Log.Error(e);
}
}
internal override bool Test()
internal override async Task<bool> Test(CancellationToken ct)
{
foreach (KavitaLibrary lib in GetLibraries())
if (NetClient.MakePost($"{BaseUrl}/api/ToFileLibrary/scan?libraryId={lib.id}", "Bearer", Auth))
foreach (KavitaLibrary lib in await GetLibraries(ct))
if (await NetClient.MakeRequest($"{BaseUrl}/api/ToFileLibrary/scan?libraryId={lib.Id}", "Bearer", Auth, HttpMethod.Post, ct) is { CanRead: true })
return true;
return false;
}
@@ -69,46 +73,28 @@ public class Kavita : LibraryConnector
/// Fetches all libraries available to the user
/// </summary>
/// <returns>Array of KavitaLibrary</returns>
private IEnumerable<KavitaLibrary> GetLibraries()
private async Task<IEnumerable<KavitaLibrary>> GetLibraries(CancellationToken ct)
{
Stream data = NetClient.MakeRequest($"{BaseUrl}/api/ToFileLibrary/libraries", "Bearer", Auth);
if (data == Stream.Null)
if(await NetClient.MakeRequest($"{BaseUrl}/api/ToFileLibrary/libraries", "Bearer", Auth, HttpMethod.Get, ct) is not { CanRead: true } data)
{
Log.Info("No libraries found");
return [];
}
JsonArray? result = JsonSerializer.Deserialize<JsonArray>(data);
if (result is null)
if(await JsonSerializer.DeserializeAsync<KavitaLibrary[]>(data, JsonSerializerOptions.Web, ct) is not { } ret)
{
Log.Info("No libraries found");
Log.Debug("Parsing libraries failed.");
return [];
}
List<KavitaLibrary> ret = new();
foreach (JsonNode? jsonNode in result)
{
JsonObject? jObject = (JsonObject?)jsonNode;
if(jObject is null)
continue;
int libraryId = jObject["id"]!.GetValue<int>();
string libraryName = jObject["name"]!.GetValue<string>();
ret.Add(new KavitaLibrary(libraryId, libraryName));
}
return ret;
}
private struct KavitaLibrary
{
public int id { get; }
[JsonProperty("id")]
public required int Id { get; init; }
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string name { get; }
public KavitaLibrary(int id, string name)
{
this.id = id;
this.name = name;
}
[JsonProperty("name")]
public required string Name { get; init; }
}
}

View File

@@ -1,29 +1,33 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace API.Schema.LibraryContext.LibraryConnectors;
public class Komga : LibraryConnector
public class Komga(string baseUrl, string auth) : LibraryConnector(LibraryType.Komga, baseUrl, auth)
{
public Komga(string baseUrl, string auth) : base(LibraryType.Komga, baseUrl, auth)
{
}
public Komga(string baseUrl, string username, string password)
: this(baseUrl, Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")))
{
}
protected override void UpdateLibraryInternal()
public override async Task UpdateLibrary(CancellationToken ct)
{
foreach (KomgaLibrary lib in GetLibraries())
NetClient.MakePost($"{BaseUrl}/api/v1/libraries/{lib.id}/scan", "Basic", Auth);
try
{
foreach (KomgaLibrary lib in await GetLibraries(ct))
await NetClient.MakeRequest($"{BaseUrl}/api/v1/libraries/{lib.Id}/scan", "Basic", Auth, HttpMethod.Post, ct);
}
catch (Exception e)
{
Log.Error(e);
}
}
internal override bool Test()
internal override async Task<bool> Test(CancellationToken ct)
{
foreach (KomgaLibrary lib in GetLibraries())
if (NetClient.MakePost($"{BaseUrl}/api/v1/libraries/{lib.id}/scan", "Basic", Auth))
foreach (KomgaLibrary lib in await GetLibraries(ct))
if (await NetClient.MakeRequest($"{BaseUrl}/api/v1/libraries/{lib.Id}/scan", "Basic", Auth, HttpMethod.Post, ct) is { CanRead: true})
return true;
return false;
}
@@ -32,44 +36,31 @@ public class Komga : LibraryConnector
/// Fetches all libraries available to the user
/// </summary>
/// <returns>Array of KomgaLibraries</returns>
private IEnumerable<KomgaLibrary> GetLibraries()
private async Task<IEnumerable<KomgaLibrary>> GetLibraries(CancellationToken ct)
{
Stream data = NetClient.MakeRequest($"{BaseUrl}/api/v1/libraries", "Basic", Auth);
if (data == Stream.Null)
if (await NetClient.MakeRequest($"{BaseUrl}/api/v1/libraries", "Basic", Auth, HttpMethod.Get, ct) is not { CanRead: true } data)
{
Log.Info("No libraries found");
return [];
}
JsonArray? result = JsonSerializer.Deserialize<JsonArray>(data);
if (result is null)
{
Log.Info("No libraries found");
Log.Debug("No libraries found");
return [];
}
HashSet<KomgaLibrary> ret = new();
foreach (JsonNode? jsonNode in result)
if (await JsonSerializer.DeserializeAsync<KomgaLibrary[]>(data, JsonSerializerOptions.Web, ct) is not
{ } ret)
{
var jObject = (JsonObject?)jsonNode;
string libraryId = jObject!["id"]!.GetValue<string>();
string libraryName = jObject["name"]!.GetValue<string>();
ret.Add(new KomgaLibrary(libraryId, libraryName));
Log.Debug("Parsing libraries failed.");
return [];
}
return ret;
return ret ;
}
private struct KomgaLibrary
private readonly record struct KomgaLibrary
{
public string id { get; }
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string name { get; }
[JsonProperty("id")]
public required string Id { get; init; }
public KomgaLibrary(string id, string name)
{
this.id = id;
this.name = name;
}
// ReSharper disable once UnusedAutoPropertyAccessor.Local
[JsonProperty("name")]
public required string Name { get; init; }
}
}

View File

@@ -2,7 +2,6 @@
using System.ComponentModel.DataAnnotations.Schema;
using log4net;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace API.Schema.LibraryContext.LibraryConnectors;
@@ -18,7 +17,7 @@ public abstract class LibraryConnector : Identifiable
: base()
{
this.LibraryType = libraryType;
this.BaseUrl = baseUrl;
this.BaseUrl = baseUrl.TrimEnd('/', ' ');
this.Auth = auth;
this.Log = LogManager.GetLogger(GetType());
}
@@ -37,8 +36,8 @@ public abstract class LibraryConnector : Identifiable
public override string ToString() => $"{base.ToString()} {this.LibraryType} {this.BaseUrl}";
protected abstract void UpdateLibraryInternal();
internal abstract bool Test();
public abstract Task UpdateLibrary(CancellationToken ct);
internal abstract Task<bool> Test(CancellationToken ct);
}
public enum LibraryType : byte

View File

@@ -1,35 +1,41 @@
using System.Net;
using System.Net.Http.Headers;
using log4net;
using HttpMethod = System.Net.Http.HttpMethod;
namespace API.Schema.LibraryContext.LibraryConnectors;
public class NetClient
{
private static ILog Log = LogManager.GetLogger(typeof(NetClient));
private static readonly ILog Log = LogManager.GetLogger(typeof(NetClient));
private static readonly HttpClient Client = new();
public static Stream MakeRequest(string url, string authScheme, string auth)
public static async Task<Stream> MakeRequest(string url, string authScheme, string auth, HttpMethod? method = null, CancellationToken? cancellationToken = null)
{
Log.Debug($"Requesting {url}");
HttpClient client = new();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authScheme, auth);
HttpRequestMessage requestMessage = new()
{
Method = HttpMethod.Get,
RequestUri = new Uri(url)
};
method ??= HttpMethod.Get;
CancellationToken ct = cancellationToken ?? CancellationToken.None;
Client.DefaultRequestHeaders.Authorization = new (authScheme, auth);
try
{
HttpResponseMessage response = client.Send(requestMessage);
HttpRequestMessage requestMessage = new()
{
Method = method,
RequestUri = new (url),
Headers =
{
{ "Accept", "application/json" },
{ "Authorization", new AuthenticationHeaderValue(authScheme, auth).ToString() }
}
};
HttpResponseMessage response = await Client.SendAsync(requestMessage, ct);
if (response.StatusCode is HttpStatusCode.Unauthorized &&
response.RequestMessage!.RequestUri!.AbsoluteUri != url)
return MakeRequest(response.RequestMessage!.RequestUri!.AbsoluteUri, authScheme, auth);
else if (response.IsSuccessStatusCode)
return response.Content.ReadAsStream();
else
return Stream.Null;
if (response.StatusCode is HttpStatusCode.Unauthorized && response.RequestMessage?.RequestUri?.AbsoluteUri is { } absoluteUri && absoluteUri != url)
return await MakeRequest(absoluteUri, authScheme, auth, method, ct);
if (response.IsSuccessStatusCode)
return await response.Content.ReadAsStreamAsync(ct);
return Stream.Null;
}
catch (Exception e)
{
@@ -45,29 +51,4 @@ public class NetClient
return Stream.Null;
}
}
public static bool MakePost(string url, string authScheme, string auth)
{
HttpClient client = new()
{
DefaultRequestHeaders =
{
{ "Accept", "application/json" },
{ "Authorization", new AuthenticationHeaderValue(authScheme, auth).ToString() }
}
};
HttpRequestMessage requestMessage = new ()
{
Method = HttpMethod.Post,
RequestUri = new Uri(url)
};
HttpResponseMessage response = client.Send(requestMessage);
if(response.StatusCode is HttpStatusCode.Unauthorized && response.RequestMessage!.RequestUri!.AbsoluteUri != url)
return MakePost(response.RequestMessage!.RequestUri!.AbsoluteUri, authScheme, auth);
else if (response.IsSuccessStatusCode)
return true;
else
return false;
}
}

View File

@@ -6,7 +6,8 @@ using API.Schema.MangaContext;
using API.Schema.MangaContext.MetadataFetchers;
using API.Schema.NotificationsContext;
using API.Workers;
using API.Workers.MaintenanceWorkers;
using API.Workers.PeriodicWorkers;
using API.Workers.PeriodicWorkers.MaintenanceWorkers;
using log4net;
using log4net.Config;
using Microsoft.EntityFrameworkCore;
@@ -30,6 +31,7 @@ public static class Tranga
internal static readonly StartNewChapterDownloadsWorker StartNewChapterDownloadsWorker = new();
internal static readonly RemoveOldNotificationsWorker RemoveOldNotificationsWorker = new();
internal static readonly UpdateCoversWorker UpdateCoversWorker = new();
internal static readonly UpdateLibraryConnectorsWorker UpdateLibraryConnectorsWorker = new();
internal static void StartLogger(FileInfo loggerConfigFile)
{
@@ -48,6 +50,7 @@ public static class Tranga
AddWorker(StartNewChapterDownloadsWorker);
AddWorker(RemoveOldNotificationsWorker);
AddWorker(UpdateCoversWorker);
AddWorker(UpdateLibraryConnectorsWorker);
}
internal static void SetServiceProvider(IServiceProvider serviceProvider)

View File

@@ -1,7 +1,7 @@
using API.Schema.MangaContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers;
namespace API.Workers.PeriodicWorkers;
/// <summary>
/// Creates Jobs to update available Chapters for all Manga that are marked for Download

View File

@@ -1,6 +1,6 @@
using API.Schema.MangaContext;
namespace API.Workers.MaintenanceWorkers;
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
public class CleanupMangaCoversWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
: BaseWorkerWithContext<MangaContext>(dependsOn), IPeriodic

View File

@@ -1,7 +1,7 @@
using API.Schema.NotificationsContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers.MaintenanceWorkers;
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
/// <summary>
/// Removes sent notifications from database

View File

@@ -2,7 +2,7 @@ using API.Schema.NotificationsContext;
using API.Schema.NotificationsContext.NotificationConnectors;
using Microsoft.EntityFrameworkCore;
namespace API.Workers;
namespace API.Workers.PeriodicWorkers;
/// <summary>
/// Send notifications to NotificationConnectors

View File

@@ -1,7 +1,7 @@
using API.Schema.MangaContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers;
namespace API.Workers.PeriodicWorkers;
/// <summary>
/// Create new Workers for Chapters on Manga marked for Download, that havent been downloaded yet.

View File

@@ -1,7 +1,7 @@
using API.Schema.MangaContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers;
namespace API.Workers.PeriodicWorkers;
/// <summary>
/// Updates the database to reflect changes made on disk

View File

@@ -1,7 +1,7 @@
using API.Schema.MangaContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers;
namespace API.Workers.PeriodicWorkers;
/// <summary>
/// Creates Workers to update covers for Manga

View File

@@ -0,0 +1,20 @@
using API.Schema.LibraryContext;
using API.Schema.LibraryContext.LibraryConnectors;
using Microsoft.EntityFrameworkCore;
namespace API.Workers.PeriodicWorkers;
public class UpdateLibraryConnectorsWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
: BaseWorkerWithContext<LibraryContext>(dependsOn), IPeriodic
{
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromMinutes(10);
protected override async Task<BaseWorker[]> DoWorkInternal()
{
List<LibraryConnector> connectors = await DbContext.LibraryConnectors.ToListAsync(CancellationToken);
foreach (LibraryConnector libraryConnector in connectors)
await libraryConnector.UpdateLibrary(CancellationToken);
return [];
}
}

View File

@@ -2,7 +2,7 @@ using API.Schema.MangaContext;
using API.Schema.MangaContext.MetadataFetchers;
using Microsoft.EntityFrameworkCore;
namespace API.Workers;
namespace API.Workers.PeriodicWorkers;
/// <summary>
/// Updates Metadata for all Manga

View File

@@ -196,6 +196,15 @@ bool retVal = xyz?
Tranga is using a **code-first** EF-Core approach. If you modify the database(context) structure you need to create a migration.
###### Configuration Environment-Variables:
| variable | default-value |
|-------------------|------------------|
| POSTGRES_HOST | `tranga-pg:5432` |
| POSTGRES_DB | `postgres` |
| POSTGRES_USER | `postgres` |
| POSTGRES_PASSWORD | `postgres` |
### A broad overview of where is what:
![Image](DB-Layout.png)