big changes
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using System.Text.Json;
|
||||
using ZA.CoreService.ESBCertificateManager.Models;
|
||||
|
||||
namespace ZA.CoreService.ESBCertificateManager.Services;
|
||||
|
||||
public sealed class LocalSetupSelectionStore
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions =
|
||||
new()
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
private readonly string _filePath;
|
||||
|
||||
public LocalSetupSelectionStore()
|
||||
{
|
||||
string directory = Path.Combine(
|
||||
Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.LocalApplicationData),
|
||||
"ZA.CoreService.ESBCertificateManager");
|
||||
|
||||
_filePath = Path.Combine(
|
||||
directory,
|
||||
"setup-selection.json");
|
||||
}
|
||||
|
||||
public LocalSetupSelection? Load()
|
||||
{
|
||||
if (!File.Exists(_filePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string json =
|
||||
File.ReadAllText(_filePath);
|
||||
|
||||
return JsonSerializer.Deserialize<LocalSetupSelection>(
|
||||
json,
|
||||
JsonOptions);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Eine beschädigte lokale Auswahl darf den Start
|
||||
// nicht verhindern. Das Setup wird erneut angezeigt.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Save(
|
||||
LocalSetupSelection selection)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(selection);
|
||||
|
||||
string? directory =
|
||||
Path.GetDirectoryName(_filePath);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(directory))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Der lokale Setup-Ordner konnte nicht bestimmt werden.");
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
string json =
|
||||
JsonSerializer.Serialize(
|
||||
selection,
|
||||
JsonOptions);
|
||||
|
||||
File.WriteAllText(
|
||||
_filePath,
|
||||
json);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if (File.Exists(_filePath))
|
||||
{
|
||||
File.Delete(_filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user