mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 06:36:31 +00:00
50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using System.Text;
|
|
using Azaion.Common.DTO.Config;
|
|
using Azaion.Common.Extensions;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Azaion.Common.Services;
|
|
|
|
public class FileConfigurationStore : IConfigurationStore
|
|
{
|
|
private readonly string _configPath;
|
|
private readonly IFileSystem _fileSystem;
|
|
private static readonly Guid SaveConfigTaskId = Guid.NewGuid();
|
|
|
|
public FileConfigurationStore(string configPath, IFileSystem fileSystem)
|
|
{
|
|
_configPath = configPath;
|
|
_fileSystem = fileSystem;
|
|
}
|
|
|
|
public async Task<AppConfig> LoadAsync(CancellationToken ct = default)
|
|
{
|
|
if (!_fileSystem.FileExists(_configPath))
|
|
return new AppConfig();
|
|
|
|
var json = Encoding.UTF8.GetString(await _fileSystem.ReadAllBytesAsync(_configPath, ct));
|
|
return JsonConvert.DeserializeObject<AppConfig>(json) ?? new AppConfig();
|
|
}
|
|
|
|
public Task SaveAsync(AppConfig config, CancellationToken ct = default)
|
|
{
|
|
ThrottleExt.Throttle(async () =>
|
|
{
|
|
var publicConfig = new
|
|
{
|
|
config.LoaderClientConfig,
|
|
config.InferenceClientConfig,
|
|
config.GpsDeniedClientConfig,
|
|
config.DirectoriesConfig,
|
|
config.UIConfig,
|
|
config.CameraConfig
|
|
};
|
|
var json = JsonConvert.SerializeObject(publicConfig, Formatting.Indented);
|
|
await _fileSystem.WriteAllBytesAsync(_configPath, Encoding.UTF8.GetBytes(json), ct);
|
|
}, SaveConfigTaskId, TimeSpan.FromSeconds(5));
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|