Skip to content

Commit

Permalink
fix: fix saving settings in v3
Browse files Browse the repository at this point in the history
when body have validation errors
  • Loading branch information
revam committed Nov 15, 2023
1 parent e75c08a commit fbfa242
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
11 changes: 4 additions & 7 deletions Shoko.Server/API/v3/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,12 @@ public ActionResult SetSettings([FromBody] JsonPatchDocument<ServerSettings> 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();
}

Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/Settings/ISettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
40 changes: 20 additions & 20 deletions Shoko.Server/Settings/SettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public SettingsProvider(ILogger<SettingsProvider> 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;
}

Expand All @@ -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()
Expand Down Expand Up @@ -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<ServerSettings>(settings);
return Deserialize<ServerSettings>(settings);
}
catch (Exception e)
{
_logger.LogError(e, "An error occurred while loading the settings from file");
}

if (delete)
{
File.Delete(path);
return new();
}
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fbfa242

Please sign in to comment.