AI-Studio/app/MindWork AI Studio/Settings/SettingsManager.cs

113 lines
3.8 KiB
C#
Raw Normal View History

2024-04-19 19:19:13 +00:00
using System.Text.Json;
2024-07-28 19:18:17 +00:00
using System.Text.Json.Serialization;
using AIStudio.Settings.DataModel;
2024-04-19 19:19:13 +00:00
2024-04-20 15:06:50 +00:00
// ReSharper disable NotAccessedPositionalProperty.Local
2024-04-19 19:19:13 +00:00
namespace AIStudio.Settings;
2024-05-04 08:55:00 +00:00
/// <summary>
/// The settings manager.
/// </summary>
2024-09-01 18:10:03 +00:00
public sealed class SettingsManager(ILogger<SettingsManager> logger)
2024-04-19 19:19:13 +00:00
{
private const string SETTINGS_FILENAME = "settings.json";
2024-07-28 19:18:17 +00:00
private static readonly JsonSerializerOptions JSON_OPTIONS = new()
{
WriteIndented = true,
Converters = { new JsonStringEnumConverter() },
};
2024-09-01 18:10:03 +00:00
private ILogger<SettingsManager> logger = logger;
2024-07-28 19:18:17 +00:00
2024-05-04 08:55:00 +00:00
/// <summary>
/// The directory where the configuration files are stored.
/// </summary>
2024-04-19 19:19:13 +00:00
public static string? ConfigDirectory { get; set; }
2024-05-04 08:55:00 +00:00
/// <summary>
/// The directory where the data files are stored.
/// </summary>
2024-04-19 19:19:13 +00:00
public static string? DataDirectory { get; set; }
2024-05-04 08:55:00 +00:00
/// <summary>
/// The configuration data.
/// </summary>
2024-04-20 15:06:50 +00:00
public Data ConfigurationData { get; private set; } = new();
private bool IsSetUp => !string.IsNullOrWhiteSpace(ConfigDirectory) && !string.IsNullOrWhiteSpace(DataDirectory);
2024-05-04 08:55:00 +00:00
/// <summary>
/// Loads the settings from the file system.
/// </summary>
2024-04-20 15:06:50 +00:00
public async Task LoadSettings()
2024-04-19 19:19:13 +00:00
{
if(!this.IsSetUp)
2024-09-01 18:10:03 +00:00
{
this.logger.LogWarning("Cannot load settings, because the configuration is not set up yet.");
2024-04-20 15:06:50 +00:00
return;
2024-09-01 18:10:03 +00:00
}
2024-04-19 19:19:13 +00:00
var settingsPath = Path.Combine(ConfigDirectory!, SETTINGS_FILENAME);
if(!File.Exists(settingsPath))
2024-09-01 18:10:03 +00:00
{
this.logger.LogWarning("Cannot load settings, because the settings file does not exist.");
2024-04-20 15:06:50 +00:00
return;
2024-09-01 18:10:03 +00:00
}
2024-08-05 19:12:52 +00:00
// We read the `"Version": "V3"` line to determine the version of the settings file:
await foreach (var line in File.ReadLinesAsync(settingsPath))
{
if (!line.Contains("""
"Version":
"""))
continue;
// Extract the version from the line:
var settingsVersionText = line.Split('"')[3];
// Parse the version:
Enum.TryParse(settingsVersionText, out Version settingsVersion);
if(settingsVersion is Version.UNKNOWN)
{
2024-09-01 18:10:03 +00:00
this.logger.LogError("Unknown version of the settings file found.");
2024-08-05 19:12:52 +00:00
this.ConfigurationData = new();
return;
}
2024-09-01 18:10:03 +00:00
this.ConfigurationData = SettingsMigrations.Migrate(this.logger, settingsVersion, await File.ReadAllTextAsync(settingsPath), JSON_OPTIONS);
2024-04-20 15:06:50 +00:00
return;
2024-08-05 19:12:52 +00:00
}
2024-04-20 15:06:50 +00:00
2024-09-01 18:10:03 +00:00
this.logger.LogError("Failed to read the version of the settings file.");
2024-08-05 19:12:52 +00:00
this.ConfigurationData = new();
2024-04-19 19:19:13 +00:00
}
2024-04-20 15:06:50 +00:00
2024-05-04 08:55:00 +00:00
/// <summary>
/// Stores the settings to the file system.
/// </summary>
2024-04-20 15:06:50 +00:00
public async Task StoreSettings()
2024-04-19 19:19:13 +00:00
{
if(!this.IsSetUp)
2024-09-01 18:10:03 +00:00
{
this.logger.LogWarning("Cannot store settings, because the configuration is not set up yet.");
2024-04-19 19:19:13 +00:00
return;
2024-09-01 18:10:03 +00:00
}
2024-04-19 19:19:13 +00:00
var settingsPath = Path.Combine(ConfigDirectory!, SETTINGS_FILENAME);
2024-04-20 15:06:50 +00:00
if(!Directory.Exists(ConfigDirectory))
2024-09-01 18:10:03 +00:00
{
this.logger.LogInformation("Creating the configuration directory.");
2024-04-20 15:06:50 +00:00
Directory.CreateDirectory(ConfigDirectory!);
2024-09-01 18:10:03 +00:00
}
2024-07-28 19:18:17 +00:00
var settingsJson = JsonSerializer.Serialize(this.ConfigurationData, JSON_OPTIONS);
2024-04-19 19:19:13 +00:00
await File.WriteAllTextAsync(settingsPath, settingsJson);
2024-09-01 18:10:03 +00:00
this.logger.LogInformation("Stored the settings to the file system.");
2024-04-19 19:19:13 +00:00
}
2024-06-01 17:55:12 +00:00
2024-08-05 19:12:52 +00:00
public void InjectSpellchecking(Dictionary<string, object?> attributes) => attributes["spellcheck"] = this.ConfigurationData.App.EnableSpellchecking ? "true" : "false";
2024-04-19 19:19:13 +00:00
}