mirror of
https://github.com/C9Glax/tranga-website.git
synced 2025-06-13 15:27:53 +02:00
@ -1,11 +1,21 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Tranga.LibraryManagers;
|
||||
|
||||
namespace Tranga;
|
||||
|
||||
public abstract class LibraryManager
|
||||
{
|
||||
public enum LibraryType : byte
|
||||
{
|
||||
Komga = 0,
|
||||
Kavita = 1
|
||||
}
|
||||
|
||||
public LibraryType libraryType;
|
||||
public string baseUrl { get; }
|
||||
protected string auth { get; } //Base64 encoded, if you use your password everywhere, you have problems
|
||||
protected Logger? logger;
|
||||
@ -79,4 +89,34 @@ public abstract class LibraryManager
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class LibraryManagerJsonConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof(LibraryManager));
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||||
{
|
||||
JObject jo = JObject.Load(reader);
|
||||
if (jo["libraryType"]!.Value<Int64>() == (Int64)LibraryType.Komga)
|
||||
return jo.ToObject<Komga>(serializer)!;
|
||||
|
||||
if (jo["libraryType"]!.Value<Int64>() == (Int64)LibraryType.Kavita)
|
||||
return jo.ToObject<Kavita>(serializer)!;
|
||||
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
public override bool CanWrite => false;
|
||||
|
||||
/// <summary>
|
||||
/// Don't call this
|
||||
/// </summary>
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
{
|
||||
throw new Exception("Dont call this");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +1,71 @@
|
||||
using Logging;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Logging;
|
||||
|
||||
namespace Tranga.LibraryManagers;
|
||||
|
||||
public class Kavita : LibraryManager
|
||||
{
|
||||
public Kavita(string baseUrl, string username, string password, Logger? logger)
|
||||
: base(baseUrl, Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")), logger)
|
||||
{
|
||||
this.libraryType = LibraryType.Kavita;
|
||||
}
|
||||
|
||||
public Kavita(string baseUrl, string auth, Logger? logger) : base(baseUrl, auth, logger)
|
||||
{
|
||||
this.libraryType = LibraryType.Kavita;
|
||||
}
|
||||
|
||||
public override void UpdateLibrary()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Updating Libraries");
|
||||
foreach (KavitaLibrary lib in GetLibraries())
|
||||
NetClient.MakePost($"{baseUrl}/api/Library/scan?libraryId={lib.id}", auth, logger);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches all libraries available to the user
|
||||
/// </summary>
|
||||
/// <returns>Array of KavitaLibrary</returns>
|
||||
private IEnumerable<KavitaLibrary> GetLibraries()
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Getting Libraries");
|
||||
Stream data = NetClient.MakeRequest($"{baseUrl}/api/Library", auth, logger);
|
||||
if (data == Stream.Null)
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"No libraries returned");
|
||||
return Array.Empty<KavitaLibrary>();
|
||||
}
|
||||
JsonArray? result = JsonSerializer.Deserialize<JsonArray>(data);
|
||||
if (result is null)
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"No libraries returned");
|
||||
return Array.Empty<KavitaLibrary>();
|
||||
}
|
||||
|
||||
HashSet<KavitaLibrary> ret = new();
|
||||
|
||||
foreach (JsonNode? jsonNode in result)
|
||||
{
|
||||
var jObject = (JsonObject?)jsonNode;
|
||||
string libraryId = jObject!["id"]!.GetValue<string>();
|
||||
string libraryName = jObject!["name"]!.GetValue<string>();
|
||||
ret.Add(new KavitaLibrary(libraryId, libraryName));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private struct KavitaLibrary
|
||||
{
|
||||
public string id { get; }
|
||||
public string name { get; }
|
||||
|
||||
public KavitaLibrary(string id, string name)
|
||||
{
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
@ -14,11 +14,13 @@ public class Komga : LibraryManager
|
||||
public Komga(string baseUrl, string username, string password, Logger? logger)
|
||||
: base(baseUrl, Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")), logger)
|
||||
{
|
||||
this.libraryType = LibraryType.Komga;
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public Komga(string baseUrl, string auth, Logger? logger) : base(baseUrl, auth, logger)
|
||||
{
|
||||
this.libraryType = LibraryType.Komga;
|
||||
}
|
||||
|
||||
public override void UpdateLibrary()
|
||||
|
@ -29,7 +29,7 @@ public class TrangaSettings
|
||||
return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory(), new HashSet<LibraryManager>());
|
||||
|
||||
string toRead = File.ReadAllText(importFilePath);
|
||||
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(toRead)!;
|
||||
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(toRead, new JsonSerializerSettings() { Converters = { new LibraryManager.LibraryManagerJsonConverter()} })!;
|
||||
if(logger is not null)
|
||||
foreach(LibraryManager lm in settings.libraryManagers)
|
||||
lm.AddLogger(logger);
|
||||
|
Reference in New Issue
Block a user