diff options
author | Rory& <root@rory.gay> | 2024-01-08 13:55:15 +0100 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-01-08 13:56:32 +0100 |
commit | ede3857084bc7c6e65b7d36cbf913b09596e2787 (patch) | |
tree | b94694c307fb831ea5e63fabde0dbb5f56f02941 /MatrixRoomUtils.Abstractions/FileStorageProvider.cs | |
parent | Small changes (diff) | |
download | MatrixUtils-ede3857084bc7c6e65b7d36cbf913b09596e2787.tar.xz |
Internal changes to policy list viewer (extensibility), fix duplicating change handler for room list page (performance), use /state in room list page before sync
Diffstat (limited to 'MatrixRoomUtils.Abstractions/FileStorageProvider.cs')
-rw-r--r-- | MatrixRoomUtils.Abstractions/FileStorageProvider.cs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Abstractions/FileStorageProvider.cs b/MatrixRoomUtils.Abstractions/FileStorageProvider.cs new file mode 100644 index 0000000..73d5604 --- /dev/null +++ b/MatrixRoomUtils.Abstractions/FileStorageProvider.cs @@ -0,0 +1,49 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using ArcaneLibs.Extensions; +using LibMatrix.Extensions; +using LibMatrix.Interfaces.Services; +using Microsoft.Extensions.Logging; + +namespace MatrixRoomUtils.Abstractions; + +public class FileStorageProvider : IStorageProvider { + private readonly ILogger<FileStorageProvider> _logger; + + public string TargetPath { get; } + + /// <summary> + /// Creates a new instance of <see cref="FileStorageProvider" />. + /// </summary> + /// <param name="targetPath"></param> + public FileStorageProvider(string targetPath) { + // new Logger<FileStorageProvider>(new LoggerFactory()).LogInformation("test"); + Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); + TargetPath = targetPath; + if (!Directory.Exists(targetPath)) { + Directory.CreateDirectory(targetPath); + } + } + + public async Task SaveObjectAsync<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); + + [RequiresUnreferencedCode("This API uses reflection to deserialize JSON")] + public async Task<T?> LoadObjectAsync<T>(string key) => JsonSerializer.Deserialize<T>(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); + + public Task<bool> ObjectExistsAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key))); + + public Task<List<string>> 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<Stream?> LoadStreamAsync(string key) => Task.FromResult<Stream?>(File.Exists(Path.Join(TargetPath, key)) ? File.OpenRead(Path.Join(TargetPath, key)) : null); +} |