Compare commits
No commits in common. "2d82279d98dd6a1228a47078a0ef6d6f1c3a5ee1" and "a4aa5718705be7ee36e366b43c964ca99b054b0d" have entirely different histories.
2d82279d98
...
a4aa571870
@ -20,7 +20,7 @@ public class Logger : TextWriter
|
|||||||
this.Encoding = encoding ?? Encoding.ASCII;
|
this.Encoding = encoding ?? Encoding.ASCII;
|
||||||
if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null)
|
if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null)
|
||||||
_fileLogger = new FileLogger(logFilePath, encoding);
|
_fileLogger = new FileLogger(logFilePath, encoding);
|
||||||
else if(enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is null)
|
else
|
||||||
{
|
{
|
||||||
_fileLogger = null;
|
_fileLogger = null;
|
||||||
throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}");
|
throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}");
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
using Logging;
|
using Logging;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Tranga.LibraryConnectors;
|
|
||||||
using Tranga.NotificationConnectors;
|
|
||||||
|
|
||||||
namespace Tranga;
|
namespace Tranga;
|
||||||
|
|
||||||
@ -9,23 +6,17 @@ public abstract class GlobalBase
|
|||||||
{
|
{
|
||||||
protected Logger? logger { get; init; }
|
protected Logger? logger { get; init; }
|
||||||
protected TrangaSettings settings { get; init; }
|
protected TrangaSettings settings { get; init; }
|
||||||
private HashSet<NotificationConnector> notificationConnectors { get; init; }
|
|
||||||
private HashSet<LibraryConnector> libraryConnectors { get; init; }
|
|
||||||
|
|
||||||
protected GlobalBase(GlobalBase clone)
|
public GlobalBase(GlobalBase clone)
|
||||||
{
|
{
|
||||||
this.logger = clone.logger;
|
this.logger = clone.logger;
|
||||||
this.settings = clone.settings;
|
this.settings = clone.settings;
|
||||||
this.notificationConnectors = clone.notificationConnectors;
|
|
||||||
this.libraryConnectors = clone.libraryConnectors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected GlobalBase(Logger? logger, TrangaSettings settings)
|
public GlobalBase(Logger? logger, TrangaSettings settings)
|
||||||
{
|
{
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
this.notificationConnectors = settings.LoadNotificationConnectors();
|
|
||||||
this.libraryConnectors = settings.LoadLibraryConnectors();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Log(string message)
|
protected void Log(string message)
|
||||||
@ -38,38 +29,6 @@ public abstract class GlobalBase
|
|||||||
Log(string.Format(fStr, replace));
|
Log(string.Format(fStr, replace));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void SendNotifications(string title, string text)
|
|
||||||
{
|
|
||||||
foreach (NotificationConnector nc in notificationConnectors)
|
|
||||||
nc.SendNotification(title, text);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void AddNotificationConnector(NotificationConnector notificationConnector)
|
|
||||||
{
|
|
||||||
notificationConnectors.RemoveWhere(nc => nc.GetType() == notificationConnector.GetType());
|
|
||||||
notificationConnectors.Add(notificationConnector);
|
|
||||||
|
|
||||||
while(IsFileInUse(settings.notificationConnectorsFilePath))
|
|
||||||
Thread.Sleep(100);
|
|
||||||
File.WriteAllText(settings.notificationConnectorsFilePath, JsonConvert.SerializeObject(notificationConnectors));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void UpdateLibraries()
|
|
||||||
{
|
|
||||||
foreach(LibraryConnector lc in libraryConnectors)
|
|
||||||
lc.UpdateLibrary();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void AddLibraryConnector(LibraryConnector libraryConnector)
|
|
||||||
{
|
|
||||||
libraryConnectors.RemoveWhere(lc => lc.GetType() == libraryConnector.GetType());
|
|
||||||
libraryConnectors.Add(libraryConnector);
|
|
||||||
|
|
||||||
while(IsFileInUse(settings.libraryConnectorsFilePath))
|
|
||||||
Thread.Sleep(100);
|
|
||||||
File.WriteAllText(settings.libraryConnectorsFilePath, JsonConvert.SerializeObject(libraryConnectors));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected bool IsFileInUse(string filePath)
|
protected bool IsFileInUse(string filePath)
|
||||||
{
|
{
|
||||||
if (!File.Exists(filePath))
|
if (!File.Exists(filePath))
|
||||||
|
@ -6,7 +6,7 @@ public class DownloadChapter : Job
|
|||||||
{
|
{
|
||||||
public Chapter chapter { get; init; }
|
public Chapter chapter { get; init; }
|
||||||
|
|
||||||
public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter) : base(clone, connector)
|
public DownloadChapter(MangaConnector connector, Chapter chapter) : base(connector)
|
||||||
{
|
{
|
||||||
this.chapter = chapter;
|
this.chapter = chapter;
|
||||||
}
|
}
|
||||||
@ -16,8 +16,6 @@ public class DownloadChapter : Job
|
|||||||
Task downloadTask = new(delegate
|
Task downloadTask = new(delegate
|
||||||
{
|
{
|
||||||
mangaConnector.DownloadChapter(chapter, this.progressToken);
|
mangaConnector.DownloadChapter(chapter, this.progressToken);
|
||||||
UpdateLibraries();
|
|
||||||
SendNotifications("Chapter downloaded", $"{chapter.parentPublication.sortName} - {chapter.chapterNumber}");
|
|
||||||
});
|
});
|
||||||
downloadTask.Start();
|
downloadTask.Start();
|
||||||
return Array.Empty<Job>();
|
return Array.Empty<Job>();
|
||||||
|
@ -6,7 +6,7 @@ public class DownloadNewChapters : Job
|
|||||||
{
|
{
|
||||||
public Publication publication { get; init; }
|
public Publication publication { get; init; }
|
||||||
|
|
||||||
public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Publication publication, bool recurring = false) : base (clone, connector, recurring)
|
public DownloadNewChapters(MangaConnector connector, Publication publication, bool recurring = false) : base (connector, recurring)
|
||||||
{
|
{
|
||||||
this.publication = publication;
|
this.publication = publication;
|
||||||
}
|
}
|
||||||
@ -18,7 +18,7 @@ public class DownloadNewChapters : Job
|
|||||||
List<Job> subJobs = new();
|
List<Job> subJobs = new();
|
||||||
foreach (Chapter chapter in chapters)
|
foreach (Chapter chapter in chapters)
|
||||||
{
|
{
|
||||||
DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter);
|
DownloadChapter downloadChapterJob = new(this.mangaConnector, chapter);
|
||||||
subJobs.Add(downloadChapterJob);
|
subJobs.Add(downloadChapterJob);
|
||||||
}
|
}
|
||||||
progressToken.Complete();
|
progressToken.Complete();
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Tranga.Jobs;
|
namespace Tranga.Jobs;
|
||||||
|
|
||||||
public abstract class Job : GlobalBase
|
public abstract class Job
|
||||||
{
|
{
|
||||||
public MangaConnector mangaConnector { get; init; }
|
public MangaConnector mangaConnector { get; init; }
|
||||||
public ProgressToken progressToken { get; private set; }
|
public ProgressToken progressToken { get; private set; }
|
||||||
@ -11,7 +11,7 @@ public abstract class Job : GlobalBase
|
|||||||
public DateTime? lastExecution { get; private set; }
|
public DateTime? lastExecution { get; private set; }
|
||||||
public DateTime nextExecution => NextExecution();
|
public DateTime nextExecution => NextExecution();
|
||||||
|
|
||||||
public Job(GlobalBase clone, MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone)
|
public Job(MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null)
|
||||||
{
|
{
|
||||||
this.mangaConnector = connector;
|
this.mangaConnector = connector;
|
||||||
this.progressToken = new ProgressToken(0);
|
this.progressToken = new ProgressToken(0);
|
||||||
@ -21,7 +21,7 @@ public abstract class Job : GlobalBase
|
|||||||
this.recurrenceTime = recurrenceTime;
|
this.recurrenceTime = recurrenceTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Job(GlobalBase clone, MangaConnector connector, ProgressToken progressToken, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone)
|
public Job(MangaConnector connector, ProgressToken progressToken, bool recurring = false, TimeSpan? recurrenceTime = null)
|
||||||
{
|
{
|
||||||
this.mangaConnector = connector;
|
this.mangaConnector = connector;
|
||||||
this.progressToken = progressToken;
|
this.progressToken = progressToken;
|
||||||
@ -31,7 +31,7 @@ public abstract class Job : GlobalBase
|
|||||||
this.recurrenceTime = recurrenceTime;
|
this.recurrenceTime = recurrenceTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Job(GlobalBase clone, MangaConnector connector, int taskIncrements, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone)
|
public Job(MangaConnector connector, int taskIncrements, bool recurring = false, TimeSpan? recurrenceTime = null)
|
||||||
{
|
{
|
||||||
this.mangaConnector = connector;
|
this.mangaConnector = connector;
|
||||||
this.progressToken = new ProgressToken(taskIncrements);
|
this.progressToken = new ProgressToken(taskIncrements);
|
||||||
|
@ -14,6 +14,13 @@ public class JobBoss : GlobalBase
|
|||||||
this.mangaConnectorJobQueue = new();
|
this.mangaConnectorJobQueue = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void MonitorJobs()
|
||||||
|
{
|
||||||
|
foreach (Job job in jobs.Where(job => job.nextExecution < DateTime.Now && !QueueContainsJob(job)).OrderBy(job => job.nextExecution))
|
||||||
|
AddJobToQueue(job);
|
||||||
|
CheckJobQueue();
|
||||||
|
}
|
||||||
|
|
||||||
public void AddJob(Job job)
|
public void AddJob(Job job)
|
||||||
{
|
{
|
||||||
this.jobs.Add(job);
|
this.jobs.Add(job);
|
||||||
@ -46,10 +53,8 @@ public class JobBoss : GlobalBase
|
|||||||
AddJobToQueue(job);
|
AddJobToQueue(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CheckJobs()
|
private void CheckJobQueue()
|
||||||
{
|
{
|
||||||
foreach (Job job in jobs.Where(job => job.nextExecution < DateTime.Now && !QueueContainsJob(job)).OrderBy(job => job.nextExecution))
|
|
||||||
AddJobToQueue(job);
|
|
||||||
foreach (Queue<Job> jobQueue in mangaConnectorJobQueue.Values)
|
foreach (Queue<Job> jobQueue in mangaConnectorJobQueue.Values)
|
||||||
{
|
{
|
||||||
Job queueHead = jobQueue.Peek();
|
Job queueHead = jobQueue.Peek();
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
using System.Net;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Text;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Tranga;
|
|
||||||
|
|
||||||
public class Server : GlobalBase
|
|
||||||
{
|
|
||||||
private readonly HttpListener _listener = new ();
|
|
||||||
private readonly Tranga _parent;
|
|
||||||
|
|
||||||
public Server(Tranga parent) : base(parent)
|
|
||||||
{
|
|
||||||
this._parent = parent;
|
|
||||||
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
||||||
this._listener.Prefixes.Add($"http://*:{settings.apiPortNumber}/");
|
|
||||||
else
|
|
||||||
this._listener.Prefixes.Add($"http://localhost:{settings.apiPortNumber}/");
|
|
||||||
Thread t = new (Listen);
|
|
||||||
t.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Listen()
|
|
||||||
{
|
|
||||||
this._listener.Start();
|
|
||||||
foreach(string prefix in this._listener.Prefixes)
|
|
||||||
Log($"Listening on {prefix}");
|
|
||||||
while (this._listener.IsListening && _parent.keepRunning)
|
|
||||||
{
|
|
||||||
HttpListenerContext context = this._listener.GetContextAsync().Result;
|
|
||||||
Log($"{context.Request.HttpMethod} {context.Request.Url} {context.Request.UserAgent}");
|
|
||||||
Task t = new(() =>
|
|
||||||
{
|
|
||||||
if(context.Request.HttpMethod == "OPTIONS")
|
|
||||||
SendResponse(HttpStatusCode.OK, context.Response);
|
|
||||||
else
|
|
||||||
HandleRequest(context);
|
|
||||||
});
|
|
||||||
t.Start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleRequest(HttpListenerContext context)
|
|
||||||
{
|
|
||||||
HttpListenerRequest request = context.Request;
|
|
||||||
HttpListenerResponse response = context.Response;
|
|
||||||
if(request.Url!.LocalPath.Contains("favicon"))
|
|
||||||
SendResponse(HttpStatusCode.NoContent, response);
|
|
||||||
|
|
||||||
SendResponse(HttpStatusCode.NotFound, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null)
|
|
||||||
{
|
|
||||||
//logger?.WriteLine(this.GetType().ToString(), $"Sending response: {statusCode}");
|
|
||||||
response.StatusCode = (int)statusCode;
|
|
||||||
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
|
|
||||||
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE");
|
|
||||||
response.AddHeader("Access-Control-Max-Age", "1728000");
|
|
||||||
response.AppendHeader("Access-Control-Allow-Origin", "*");
|
|
||||||
response.ContentType = "application/json";
|
|
||||||
try
|
|
||||||
{
|
|
||||||
response.OutputStream.Write(content is not null
|
|
||||||
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
|
|
||||||
: Array.Empty<byte>());
|
|
||||||
response.OutputStream.Close();
|
|
||||||
}
|
|
||||||
catch (HttpListenerException)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
using Logging;
|
|
||||||
using Tranga.Jobs;
|
|
||||||
|
|
||||||
namespace Tranga;
|
|
||||||
|
|
||||||
public partial class Tranga : GlobalBase
|
|
||||||
{
|
|
||||||
public bool keepRunning;
|
|
||||||
private JobBoss _jobBoss;
|
|
||||||
private Server server;
|
|
||||||
|
|
||||||
public Tranga(Logger? logger, TrangaSettings settings) : base(logger, settings)
|
|
||||||
{
|
|
||||||
keepRunning = true;
|
|
||||||
_jobBoss = new(this);
|
|
||||||
StartJobBoss();
|
|
||||||
this.server = new Server(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void StartJobBoss()
|
|
||||||
{
|
|
||||||
Thread t = new (() =>
|
|
||||||
{
|
|
||||||
while (keepRunning)
|
|
||||||
{
|
|
||||||
_jobBoss.CheckJobs();
|
|
||||||
Thread.Sleep(1000);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
t.Start();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,136 +0,0 @@
|
|||||||
using Logging;
|
|
||||||
|
|
||||||
namespace Tranga;
|
|
||||||
|
|
||||||
public partial class Tranga : GlobalBase
|
|
||||||
{
|
|
||||||
|
|
||||||
public static void Main(string[] args)
|
|
||||||
{
|
|
||||||
string[]? help = GetArg(args, ArgEnum.Help);
|
|
||||||
if (help is not null)
|
|
||||||
{
|
|
||||||
PrintHelp();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
string[]? consoleLogger = GetArg(args, ArgEnum.ConsoleLogger);
|
|
||||||
string[]? fileLogger = GetArg(args, ArgEnum.FileLogger);
|
|
||||||
string? filePath = fileLogger?[0];//TODO validate path
|
|
||||||
|
|
||||||
List<Logger.LoggerType> enabledLoggers = new();
|
|
||||||
if(consoleLogger is not null)
|
|
||||||
enabledLoggers.Add(Logger.LoggerType.ConsoleLogger);
|
|
||||||
if (fileLogger is not null)
|
|
||||||
enabledLoggers.Add(Logger.LoggerType.FileLogger);
|
|
||||||
Logger logger = new(enabledLoggers.ToArray(), Console.Out, Console.OutputEncoding, filePath);
|
|
||||||
|
|
||||||
TrangaSettings? settings = null;
|
|
||||||
string[]? downloadLocationPath = GetArg(args, ArgEnum.DownloadLocation);
|
|
||||||
string[]? workingDirectory = GetArg(args, ArgEnum.WorkingDirectory);
|
|
||||||
|
|
||||||
if (downloadLocationPath is not null && workingDirectory is not null)
|
|
||||||
{
|
|
||||||
settings = new TrangaSettings(downloadLocationPath[0], workingDirectory[0]);
|
|
||||||
}else if (downloadLocationPath is not null)
|
|
||||||
{
|
|
||||||
if (settings is null)
|
|
||||||
settings = new TrangaSettings(downloadLocation: downloadLocationPath[0]);
|
|
||||||
else
|
|
||||||
settings = new TrangaSettings(downloadLocation: downloadLocationPath[0], settings.workingDirectory);
|
|
||||||
}else if (workingDirectory is not null)
|
|
||||||
{
|
|
||||||
if (settings is null)
|
|
||||||
settings = new TrangaSettings(downloadLocation: workingDirectory[0]);
|
|
||||||
else
|
|
||||||
settings = new TrangaSettings(settings.downloadLocation, workingDirectory[0]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
settings = new TrangaSettings();
|
|
||||||
}
|
|
||||||
|
|
||||||
Directory.CreateDirectory(settings.downloadLocation);//TODO validate path
|
|
||||||
Directory.CreateDirectory(settings.workingDirectory);//TODO validate path
|
|
||||||
|
|
||||||
Tranga _ = new (logger, settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void PrintHelp()
|
|
||||||
{
|
|
||||||
Console.WriteLine("Tranga-Help:");
|
|
||||||
foreach (Argument argument in arguments.Values)
|
|
||||||
{
|
|
||||||
foreach(string name in argument.names)
|
|
||||||
Console.Write("{0} ", name);
|
|
||||||
if(argument.parameterCount > 0)
|
|
||||||
Console.Write($"<{argument.parameterCount}>");
|
|
||||||
Console.Write("\r\n {0}\r\n", argument.helpText);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns an array containing the parameters for the argument.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="args">List of argument-strings</param>
|
|
||||||
/// <param name="arg">Requested parameter</param>
|
|
||||||
/// <returns>
|
|
||||||
/// If there are no parameters for an argument, returns an empty array.
|
|
||||||
/// If the argument is not found returns null.
|
|
||||||
/// </returns>
|
|
||||||
private static string[]? GetArg(string[] args, ArgEnum arg)
|
|
||||||
{
|
|
||||||
List<string> argsList = args.ToList();
|
|
||||||
List<string> ret = new();
|
|
||||||
foreach (string name in arguments[arg].names)
|
|
||||||
{
|
|
||||||
int argIndex = argsList.IndexOf(name);
|
|
||||||
if (argIndex != -1)
|
|
||||||
{
|
|
||||||
if (arguments[arg].parameterCount == 0)
|
|
||||||
return ret.ToArray();
|
|
||||||
for (int parameterIndex = 1; parameterIndex <= arguments[arg].parameterCount; parameterIndex++)
|
|
||||||
{
|
|
||||||
if(argIndex + parameterIndex >= argsList.Count || args[argIndex + parameterIndex].Contains('-'))//End of arguments, or no parameter provided, when one is required
|
|
||||||
Console.WriteLine($"No parameter provided for argument {name}. -h for help.");
|
|
||||||
ret.Add(args[argIndex + parameterIndex]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret.Any() ? ret.ToArray() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Dictionary<ArgEnum, Argument> arguments = new()
|
|
||||||
{
|
|
||||||
{ ArgEnum.DownloadLocation, new(new []{"-d", "--downloadLocation"}, 1, "Directory to which downloaded Manga are saved") },
|
|
||||||
{ ArgEnum.WorkingDirectory, new(new []{"-w", "--workingDirectory"}, 1, "Directory in which application-data is saved") },
|
|
||||||
{ ArgEnum.ConsoleLogger, new(new []{"-c", "--consoleLogger"}, 0, "Enables the consoleLogger") },
|
|
||||||
{ ArgEnum.FileLogger, new(new []{"-f", "--fileLogger"}, 1, "Enables the fileLogger, Directory where logfiles are saved") },
|
|
||||||
{ ArgEnum.Help, new(new []{"-h", "--help"}, 0, "Print this") }
|
|
||||||
//{ ArgEnum., new(new []{""}, 1, "") }
|
|
||||||
};
|
|
||||||
|
|
||||||
internal enum ArgEnum
|
|
||||||
{
|
|
||||||
TrangaSettings,
|
|
||||||
DownloadLocation,
|
|
||||||
WorkingDirectory,
|
|
||||||
ConsoleLogger,
|
|
||||||
FileLogger,
|
|
||||||
Help
|
|
||||||
}
|
|
||||||
|
|
||||||
private struct Argument
|
|
||||||
{
|
|
||||||
public string[] names { get; }
|
|
||||||
public byte parameterCount { get; }
|
|
||||||
public string helpText { get; }
|
|
||||||
|
|
||||||
public Argument(string[] names, byte parameterCount, string helpText)
|
|
||||||
{
|
|
||||||
this.names = names;
|
|
||||||
this.parameterCount = parameterCount;
|
|
||||||
this.helpText = helpText;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,59 +9,38 @@ public class TrangaSettings
|
|||||||
{
|
{
|
||||||
public string downloadLocation { get; private set; }
|
public string downloadLocation { get; private set; }
|
||||||
public string workingDirectory { get; init; }
|
public string workingDirectory { get; init; }
|
||||||
public int apiPortNumber { get; init; }
|
|
||||||
[JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
|
[JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
|
||||||
[JsonIgnore] public string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json");
|
|
||||||
|
|
||||||
[JsonIgnore] public string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json");
|
|
||||||
[JsonIgnore] public string tasksFilePath => Path.Join(workingDirectory, "tasks.json");
|
[JsonIgnore] public string tasksFilePath => Path.Join(workingDirectory, "tasks.json");
|
||||||
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
|
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
|
||||||
public ushort? version { get; set; }
|
public ushort? version { get; set; }
|
||||||
|
|
||||||
public TrangaSettings(string? downloadLocation = null, string? workingDirectory = null, int apiPortNumber = 6531)
|
public TrangaSettings(string? downloadLocation = null, string? workingDirectory = null)
|
||||||
{
|
{
|
||||||
this.apiPortNumber = apiPortNumber;
|
|
||||||
downloadLocation ??= Path.Join(Directory.GetCurrentDirectory(), "Downloads");
|
downloadLocation ??= Path.Join(Directory.GetCurrentDirectory(), "Downloads");
|
||||||
workingDirectory ??= Directory.GetCurrentDirectory();
|
workingDirectory ??= Directory.GetCurrentDirectory();
|
||||||
if (downloadLocation.Length < 1 || workingDirectory.Length < 1)
|
if (downloadLocation.Length < 1 || workingDirectory.Length < 1)
|
||||||
throw new ArgumentException("Download-location and working-directory paths can not be empty!");
|
throw new ArgumentException("Download-location and working-directory paths can not be empty!");
|
||||||
this.workingDirectory = workingDirectory;
|
this.workingDirectory = workingDirectory;
|
||||||
this.downloadLocation = downloadLocation;
|
this.downloadLocation = downloadLocation;
|
||||||
|
|
||||||
if (File.Exists(settingsFilePath))
|
|
||||||
{
|
|
||||||
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(File.ReadAllText(settingsFilePath))!;
|
|
||||||
this.downloadLocation = settings.downloadLocation;
|
|
||||||
this.workingDirectory = settings.workingDirectory;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashSet<LibraryConnector> LoadLibraryConnectors()
|
public static TrangaSettings LoadSettings(string importFilePath, Logger? logger)
|
||||||
{
|
{
|
||||||
if (!File.Exists(libraryConnectorsFilePath))
|
if (!File.Exists(importFilePath))
|
||||||
return new HashSet<LibraryConnector>();
|
return new TrangaSettings();
|
||||||
return JsonConvert.DeserializeObject<HashSet<LibraryConnector>>(File.ReadAllText(libraryConnectorsFilePath),
|
|
||||||
new JsonSerializerSettings()
|
string toRead = File.ReadAllText(importFilePath);
|
||||||
|
TrangaSettings? settings = JsonConvert.DeserializeObject<TrangaSettings>(File.ReadAllText(importFilePath),
|
||||||
|
new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
Converters =
|
Converters =
|
||||||
{
|
{
|
||||||
|
new NotificationManagerJsonConverter(),
|
||||||
new LibraryManagerJsonConverter()
|
new LibraryManagerJsonConverter()
|
||||||
}
|
}
|
||||||
})!;
|
});
|
||||||
}
|
return settings ?? new TrangaSettings();
|
||||||
|
|
||||||
public HashSet<NotificationConnector> LoadNotificationConnectors()
|
|
||||||
{
|
|
||||||
if (!File.Exists(notificationConnectorsFilePath))
|
|
||||||
return new HashSet<NotificationConnector>();
|
|
||||||
return JsonConvert.DeserializeObject<HashSet<NotificationConnector>>(File.ReadAllText(libraryConnectorsFilePath),
|
|
||||||
new JsonSerializerSettings()
|
|
||||||
{
|
|
||||||
Converters =
|
|
||||||
{
|
|
||||||
new NotificationManagerJsonConverter()
|
|
||||||
}
|
|
||||||
})!;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ExportSettings()
|
public void ExportSettings()
|
||||||
|
Loading…
Reference in New Issue
Block a user