From e2adac937af6e0bb6f6b7c8d153a7c3507842f24 Mon Sep 17 00:00:00 2001 From: Glax Date: Wed, 28 Feb 2024 02:13:07 +0100 Subject: [PATCH] Fix settings not being loaded from settingsfile --- CLI/Program.cs | 23 ++++++++++++++++++++++- Tranga/TrangaSettings.cs | 35 ++++++++++++++++------------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/CLI/Program.cs b/CLI/Program.cs index 9543773..4bb7182 100644 --- a/CLI/Program.cs +++ b/CLI/Program.cs @@ -46,8 +46,29 @@ internal sealed class TrangaCli : Command string? logFilePath = settings.fileLoggerPath ?? ""; Logger logger = new(enabledLoggers.ToArray(), Console.Out, Console.OutputEncoding, logFilePath); + + TrangaSettings? trangaSettings = null; - TrangaSettings trangaSettings = new (settings.downloadLocation, settings.workingDirectory, settings.apiPort); + if (settings.downloadLocation is not null && settings.workingDirectory is not null) + { + trangaSettings = new TrangaSettings(settings.downloadLocation, settings.workingDirectory); + }else if (settings.downloadLocation is not null) + { + if (trangaSettings is null) + trangaSettings = new TrangaSettings(downloadLocation: settings.downloadLocation); + else + trangaSettings = new TrangaSettings(downloadLocation: settings.downloadLocation, settings.workingDirectory); + }else if (settings.workingDirectory is not null) + { + if (trangaSettings is null) + trangaSettings = new TrangaSettings(downloadLocation: settings.workingDirectory); + else + trangaSettings = new TrangaSettings(settings.downloadLocation, settings.workingDirectory); + } + else + { + trangaSettings = new TrangaSettings(); + } Directory.CreateDirectory(trangaSettings.downloadLocation); Directory.CreateDirectory(trangaSettings.workingDirectory); diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index da474a5..ca03338 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -36,21 +36,28 @@ public class TrangaSettings public TrangaSettings(string? downloadLocation = null, string? workingDirectory = null, int? apiPortNumber = null) { - string lockFilePath = $"{settingsFilePath}.lock"; - if (File.Exists(settingsFilePath) && !File.Exists(lockFilePath)) + 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(settingsFilePath); + string settingsStr = File.ReadAllText(sfp); TrangaSettings settings = JsonConvert.DeserializeObject(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(settingsFilePath)) + 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"); @@ -58,6 +65,8 @@ public class TrangaSettings } else {//Settingsfile is locked + this.requestLimits = DefaultRequestLimits; + this.userAgent = DefaultUserAgent; this.apiPortNumber = apiPortNumber!.Value; this.downloadLocation = downloadLocation!; this.workingDirectory = workingDirectory!; @@ -130,24 +139,12 @@ public class TrangaSettings { if (File.Exists(settingsFilePath)) { - bool inUse = true; - while (inUse) - { - try - { - using FileStream stream = new(settingsFilePath, FileMode.Open, FileAccess.Read, FileShare.None); - stream.Close(); - inUse = false; - } - catch (IOException) - { - Thread.Sleep(100); - } - } + while(GlobalBase.IsFileInUse(settingsFilePath)) + Thread.Sleep(100); } else Directory.CreateDirectory(new FileInfo(settingsFilePath).DirectoryName!); - File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this)); + File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented)); } public string GetFullCoverPath(Manga manga)