using System.Text.Json; using ArcaneLibs.Extensions; using LibMatrix.Extensions; using LibMatrix.Interfaces.Services; using Microsoft.Extensions.Logging; namespace MatrixRoomUtils.Desktop; public class FileStorageProvider : IStorageProvider { private readonly ILogger _logger; public string TargetPath { get; } /// /// Creates a new instance of . /// /// public FileStorageProvider(string targetPath) { new Logger(new LoggerFactory()).LogInformation("test"); Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); TargetPath = targetPath; if(!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } } public async Task SaveObjectAsync(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); public async Task LoadObjectAsync(string key) => JsonSerializer.Deserialize(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); public Task ObjectExistsAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key))); public Task> GetAllKeysAsync() => Task.FromResult(Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList()); public Task DeleteObjectAsync(string key) { File.Delete(Path.Join(TargetPath, key)); return Task.CompletedTask; } public async Task SaveStreamAsync(string key, Stream stream) { Directory.CreateDirectory(Path.GetDirectoryName(Path.Join(TargetPath, key)) ?? throw new InvalidOperationException()); await using var fileStream = File.Create(Path.Join(TargetPath, key)); await stream.CopyToAsync(fileStream); } public Task LoadStreamAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key)) ? File.OpenRead(Path.Join(TargetPath, key)) : null); }