diff --git a/Shoko.Server/API/v3/Controllers/SettingsController.cs b/Shoko.Server/API/v3/Controllers/SettingsController.cs index 62d99beb9..c206b50e4 100644 --- a/Shoko.Server/API/v3/Controllers/SettingsController.cs +++ b/Shoko.Server/API/v3/Controllers/SettingsController.cs @@ -50,15 +50,12 @@ public ActionResult SetSettings([FromBody] JsonPatchDocument set return ValidationProblem("The settings object is invalid."); } - var existingSettings = SettingsProvider.GetSettings(); + var existingSettings = SettingsProvider.GetSettings(copy: true); settings.ApplyTo((ServerSettings)existingSettings, ModelState); - if (!skipValidation) - { - if (!TryValidateModel(existingSettings)) - return ValidationProblem(ModelState); - } + if (!skipValidation && !TryValidateModel(existingSettings)) + return ValidationProblem(ModelState); - SettingsProvider.SaveSettings(); + SettingsProvider.SaveSettings(existingSettings); return Ok(); } diff --git a/Shoko.Server/Settings/ISettingsProvider.cs b/Shoko.Server/Settings/ISettingsProvider.cs index 0c835d11f..03e443c28 100644 --- a/Shoko.Server/Settings/ISettingsProvider.cs +++ b/Shoko.Server/Settings/ISettingsProvider.cs @@ -6,7 +6,7 @@ namespace Shoko.Server.Settings; public interface ISettingsProvider { - IServerSettings GetSettings(); + IServerSettings GetSettings(bool copy = false); void SaveSettings(IServerSettings settings); void SaveSettings(); void DebugSettingsToLog(); diff --git a/Shoko.Server/Settings/SettingsProvider.cs b/Shoko.Server/Settings/SettingsProvider.cs index 791311710..087d3cadb 100644 --- a/Shoko.Server/Settings/SettingsProvider.cs +++ b/Shoko.Server/Settings/SettingsProvider.cs @@ -31,9 +31,10 @@ public SettingsProvider(ILogger logger) _logger = logger; } - public IServerSettings GetSettings() + public IServerSettings GetSettings(bool copy = false) { - if (Instance == null) LoadSettings(); + if (copy || Instance == null) + return LoadSettings(copy); return Instance; } @@ -43,28 +44,28 @@ public void SaveSettings(IServerSettings settings) SaveSettings(); } - public void LoadSettings() + public ServerSettings LoadSettings(bool copy = false) { var appPath = Utils.ApplicationPath; if (!Directory.Exists(appPath)) - { Directory.CreateDirectory(appPath); - } var path = Path.Combine(appPath, SettingsFilename); if (!File.Exists(path)) { - Instance = File.Exists(Path.Combine(appPath, "settings.json")) + var convertedInstance = File.Exists(Path.Combine(appPath, "settings.json")) ? LoadLegacySettings() - : new ServerSettings(); - SaveSettings(); - return; - } + : new(); - LoadSettingsFromFile(path); - SaveSettings(); + if (!copy) + SaveSettings(convertedInstance); + return convertedInstance; + } - Utils.SetTraceLogging(Instance.TraceLog); + var instance = LoadSettingsFromFile(path); + if (!copy) + SaveSettings(instance); + return instance; } private static ServerSettings LoadLegacySettings() @@ -253,23 +254,19 @@ public static object Deserialize(Type t, string json) return result; } - public void LoadSettingsFromFile(string path, bool delete = false) + public ServerSettings LoadSettingsFromFile(string path) { var settings = File.ReadAllText(path); settings = SettingsMigrations.MigrateSettings(settings); settings = FixNonEmittedDefaults(path, settings); try { - Instance = Deserialize(settings); + return Deserialize(settings); } catch (Exception e) { _logger.LogError(e, "An error occurred while loading the settings from file"); - } - - if (delete) - { - File.Delete(path); + return new(); } } @@ -304,6 +301,9 @@ public void SaveSettings() return; } + // Always update the trace logging settings when the settings change. + Utils.SetTraceLogging(Instance.TraceLog); + var path = Path.Combine(Utils.ApplicationPath, SettingsFilename); var context = new ValidationContext(Instance, null, null);