mirror of
https://github.com/C9Glax/tranga.git
synced 2025-09-11 04:18:18 +02:00
Rework TrangaSettings
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.RegularExpressions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Tranga.LibraryConnectors;
|
||||
using Tranga.MangaConnectors;
|
||||
using Tranga.NotificationConnectors;
|
||||
@@ -9,20 +8,20 @@ using static System.IO.UnixFileMode;
|
||||
|
||||
namespace Tranga;
|
||||
|
||||
public class TrangaSettings
|
||||
public static class TrangaSettings
|
||||
{
|
||||
public string downloadLocation { get; private set; }
|
||||
public string workingDirectory { get; private set; }
|
||||
public int apiPortNumber { get; init; }
|
||||
public string userAgent { get; private set; } = DefaultUserAgent;
|
||||
[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 jobsFolderPath => Path.Join(workingDirectory, "jobs");
|
||||
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
|
||||
[JsonIgnore] internal static readonly string DefaultUserAgent = $"Tranga ({Enum.GetName(Environment.OSVersion.Platform)}; {(Environment.Is64BitOperatingSystem ? "x64" : "")}) / 1.0";
|
||||
public ushort? version { get; } = 2;
|
||||
public bool aprilFoolsMode { get; private set; } = true;
|
||||
public static string downloadLocation { get; private set; } = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(Directory.GetCurrentDirectory(), "Downloads"));
|
||||
public static string workingDirectory { get; private set; } = Path.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/usr/share" : Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tranga-api");
|
||||
public static int apiPortNumber { get; private set; } = 6531;
|
||||
public static string userAgent { get; private set; } = DefaultUserAgent;
|
||||
[JsonIgnore] public static string settingsFilePath => Path.Join(workingDirectory, "settings.json");
|
||||
[JsonIgnore] public static string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json");
|
||||
[JsonIgnore] public static string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json");
|
||||
[JsonIgnore] public static string jobsFolderPath => Path.Join(workingDirectory, "jobs");
|
||||
[JsonIgnore] public static string coverImageCache => Path.Join(workingDirectory, "imageCache");
|
||||
public static ushort? version { get; } = 2;
|
||||
public static bool aprilFoolsMode { get; private set; } = true;
|
||||
[JsonIgnore]internal static readonly Dictionary<RequestType, int> DefaultRequestLimits = new ()
|
||||
{
|
||||
{RequestType.MangaInfo, 250},
|
||||
@@ -33,50 +32,38 @@ public class TrangaSettings
|
||||
{RequestType.Default, 60}
|
||||
};
|
||||
|
||||
public Dictionary<RequestType, int> requestLimits { get; set; } = DefaultRequestLimits;
|
||||
public static Dictionary<RequestType, int> requestLimits { get; set; } = DefaultRequestLimits;
|
||||
|
||||
public TrangaSettings(string? downloadLocation = null, string? workingDirectory = null, int? apiPortNumber = null)
|
||||
public static void LoadFromWorkingDirectory(string directory)
|
||||
{
|
||||
string wd = workingDirectory ?? Path.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/usr/share" : Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tranga-api");
|
||||
string sfp = Path.Join(wd, "settings.json");
|
||||
|
||||
string lockFilePath = $"{sfp}.lock";
|
||||
if (File.Exists(sfp) && !File.Exists(lockFilePath))
|
||||
{//Load from settings file
|
||||
FileStream lockFile = File.Create(lockFilePath,0, FileOptions.DeleteOnClose); //lock settingsfile
|
||||
string settingsStr = File.ReadAllText(sfp);
|
||||
settingsStr = Regex.Replace(settingsStr, @"""MangaDexAuthor"": [0-9]+,", "");//https://github.com/C9Glax/tranga/pull/161 Remove sometime in the future :3
|
||||
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(settingsStr)!;
|
||||
this.requestLimits = settings.requestLimits;
|
||||
this.userAgent = settings.userAgent;
|
||||
this.downloadLocation = downloadLocation ?? settings.downloadLocation;
|
||||
this.workingDirectory = workingDirectory ?? settings.workingDirectory;
|
||||
this.apiPortNumber = apiPortNumber ?? settings.apiPortNumber;
|
||||
lockFile.Close(); //unlock settingsfile
|
||||
}
|
||||
else if(!File.Exists(sfp))
|
||||
{//No settings file exists
|
||||
if (downloadLocation?.Length < 1 || workingDirectory?.Length < 1)
|
||||
throw new ArgumentException("Download-location and working-directory paths can not be empty!");
|
||||
this.requestLimits = DefaultRequestLimits;
|
||||
this.userAgent = DefaultUserAgent;
|
||||
this.apiPortNumber = apiPortNumber ?? 6531;
|
||||
this.downloadLocation = downloadLocation ?? (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(Directory.GetCurrentDirectory(), "Downloads"));
|
||||
this.workingDirectory = workingDirectory ?? Path.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/usr/share" : Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tranga-api");
|
||||
ExportSettings();
|
||||
TrangaSettings.workingDirectory = directory;
|
||||
if (!File.Exists(settingsFilePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{//Settingsfile is locked
|
||||
this.requestLimits = DefaultRequestLimits;
|
||||
this.userAgent = DefaultUserAgent;
|
||||
this.apiPortNumber = apiPortNumber!.Value;
|
||||
this.downloadLocation = downloadLocation!;
|
||||
this.workingDirectory = workingDirectory!;
|
||||
{
|
||||
Deserialize(File.ReadAllText(settingsFilePath));
|
||||
}
|
||||
UpdateDownloadLocation(this.downloadLocation, false);
|
||||
|
||||
Directory.CreateDirectory(downloadLocation);
|
||||
Directory.CreateDirectory(workingDirectory);
|
||||
ExportSettings();
|
||||
}
|
||||
|
||||
public HashSet<LibraryConnector> LoadLibraryConnectors(GlobalBase clone)
|
||||
public static void CreateOrUpdate(string? downloadDirectory = null, string? pWorkingDirectory = null, int? pApiPortNumber = null, string? pUserAgent = null, bool? pAprilFoolsMode = null)
|
||||
{
|
||||
TrangaSettings.downloadLocation = downloadDirectory ?? TrangaSettings.downloadLocation;
|
||||
TrangaSettings.workingDirectory = pWorkingDirectory ?? TrangaSettings.workingDirectory;
|
||||
TrangaSettings.apiPortNumber = pApiPortNumber ?? TrangaSettings.apiPortNumber;
|
||||
TrangaSettings.userAgent = pUserAgent ?? TrangaSettings.userAgent;
|
||||
TrangaSettings.aprilFoolsMode = pAprilFoolsMode ?? TrangaSettings.aprilFoolsMode;
|
||||
Directory.CreateDirectory(downloadLocation);
|
||||
Directory.CreateDirectory(workingDirectory);
|
||||
ExportSettings();
|
||||
}
|
||||
|
||||
public static HashSet<LibraryConnector> LoadLibraryConnectors(GlobalBase clone)
|
||||
{
|
||||
if (!File.Exists(libraryConnectorsFilePath))
|
||||
return new HashSet<LibraryConnector>();
|
||||
@@ -90,7 +77,7 @@ public class TrangaSettings
|
||||
})!;
|
||||
}
|
||||
|
||||
public HashSet<NotificationConnector> LoadNotificationConnectors(GlobalBase clone)
|
||||
public static HashSet<NotificationConnector> LoadNotificationConnectors(GlobalBase clone)
|
||||
{
|
||||
if (!File.Exists(notificationConnectorsFilePath))
|
||||
return new HashSet<NotificationConnector>();
|
||||
@@ -104,13 +91,13 @@ public class TrangaSettings
|
||||
})!;
|
||||
}
|
||||
|
||||
public void UpdateAprilFoolsMode(bool enabled)
|
||||
public static void UpdateAprilFoolsMode(bool enabled)
|
||||
{
|
||||
this.aprilFoolsMode = enabled;
|
||||
TrangaSettings.aprilFoolsMode = enabled;
|
||||
ExportSettings();
|
||||
}
|
||||
|
||||
public void UpdateDownloadLocation(string newPath, bool moveFiles = true)
|
||||
public static void UpdateDownloadLocation(string newPath, bool moveFiles = true)
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
Directory.CreateDirectory(newPath,
|
||||
@@ -118,32 +105,44 @@ public class TrangaSettings
|
||||
else
|
||||
Directory.CreateDirectory(newPath);
|
||||
|
||||
if (moveFiles && Directory.Exists(this.downloadLocation))
|
||||
Directory.Move(this.downloadLocation, newPath);
|
||||
if (moveFiles && Directory.Exists(TrangaSettings.downloadLocation))
|
||||
Directory.Move(TrangaSettings.downloadLocation, newPath);
|
||||
|
||||
this.downloadLocation = newPath;
|
||||
TrangaSettings.downloadLocation = newPath;
|
||||
ExportSettings();
|
||||
}
|
||||
|
||||
public void UpdateWorkingDirectory(string newPath)
|
||||
public static void UpdateWorkingDirectory(string newPath)
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
Directory.CreateDirectory(newPath,
|
||||
GroupRead | GroupWrite | None | OtherRead | OtherWrite | UserRead | UserWrite);
|
||||
else
|
||||
Directory.CreateDirectory(newPath);
|
||||
Directory.Move(this.workingDirectory, newPath);
|
||||
this.workingDirectory = newPath;
|
||||
Directory.Move(TrangaSettings.workingDirectory, newPath);
|
||||
TrangaSettings.workingDirectory = newPath;
|
||||
ExportSettings();
|
||||
}
|
||||
|
||||
public void UpdateUserAgent(string? customUserAgent)
|
||||
public static void UpdateUserAgent(string? customUserAgent)
|
||||
{
|
||||
this.userAgent = customUserAgent ?? DefaultUserAgent;
|
||||
TrangaSettings.userAgent = customUserAgent ?? DefaultUserAgent;
|
||||
ExportSettings();
|
||||
}
|
||||
|
||||
public void ExportSettings()
|
||||
public static void UpdateRateLimit(RequestType requestType, int newLimit)
|
||||
{
|
||||
TrangaSettings.requestLimits[requestType] = newLimit;
|
||||
ExportSettings();
|
||||
}
|
||||
|
||||
public static void ResetRateLimits()
|
||||
{
|
||||
TrangaSettings.requestLimits = DefaultRequestLimits;
|
||||
ExportSettings();
|
||||
}
|
||||
|
||||
public static void ExportSettings()
|
||||
{
|
||||
if (File.Exists(settingsFilePath))
|
||||
{
|
||||
@@ -152,22 +151,36 @@ public class TrangaSettings
|
||||
}
|
||||
else
|
||||
Directory.CreateDirectory(new FileInfo(settingsFilePath).DirectoryName!);
|
||||
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented));
|
||||
File.WriteAllText(settingsFilePath, Serialize());
|
||||
}
|
||||
|
||||
public string GetFullCoverPath(Manga manga)
|
||||
public static string Serialize()
|
||||
{
|
||||
return Path.Join(this.coverImageCache, manga.coverFileNameInCache);
|
||||
JObject jobj = new JObject();
|
||||
jobj.Add("downloadLocation", JToken.FromObject(TrangaSettings.downloadLocation));
|
||||
jobj.Add("workingDirectory", JToken.FromObject(TrangaSettings.workingDirectory));
|
||||
jobj.Add("apiPortNumber", JToken.FromObject(TrangaSettings.apiPortNumber));
|
||||
jobj.Add("userAgent", JToken.FromObject(TrangaSettings.userAgent));
|
||||
jobj.Add("aprilFoolsMode", JToken.FromObject(TrangaSettings.aprilFoolsMode));
|
||||
jobj.Add("version", JToken.FromObject(TrangaSettings.version));
|
||||
jobj.Add("requestLimits", JToken.FromObject(TrangaSettings.requestLimits));
|
||||
return jobj.ToString();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public static void Deserialize(string serialized)
|
||||
{
|
||||
return $"TrangaSettings:\n" +
|
||||
$"\tDownloadLocation: {downloadLocation}\n" +
|
||||
$"\tworkingDirectory: {workingDirectory}\n" +
|
||||
$"\tjobsFolderPath: {jobsFolderPath}\n" +
|
||||
$"\tsettingsFilePath: {settingsFilePath}\n" +
|
||||
$"\t\tnotificationConnectors: {notificationConnectorsFilePath}\n" +
|
||||
$"\t\tlibraryConnectors: {libraryConnectorsFilePath}\n";
|
||||
JObject jobj = JObject.Parse(serialized);
|
||||
if (jobj.TryGetValue("downloadLocation", out JToken? dl))
|
||||
TrangaSettings.downloadLocation = dl.Value<string>()!;
|
||||
if (jobj.TryGetValue("workingDirectory", out JToken? wd))
|
||||
TrangaSettings.workingDirectory = wd.Value<string>()!;
|
||||
if (jobj.TryGetValue("apiPortNumber", out JToken? apn))
|
||||
TrangaSettings.apiPortNumber = apn.Value<int>();
|
||||
if (jobj.TryGetValue("userAgent", out JToken? ua))
|
||||
TrangaSettings.userAgent = ua.Value<string>()!;
|
||||
if (jobj.TryGetValue("aprilFoolsMode", out JToken? afm))
|
||||
TrangaSettings.aprilFoolsMode = afm.Value<bool>()!;
|
||||
if (jobj.TryGetValue("requestLimits", out JToken? rl))
|
||||
TrangaSettings.requestLimits = rl.ToObject<Dictionary<RequestType, int>>()!;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user