about summary refs log tree commit diff
path: root/MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs
blob: 0061c1fc7886456a5735d44afa2a18bd943c5538 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Text.Json;
using MatrixRoomUtils.Core.Extensions;
using MatrixRoomUtils.Core.Interfaces.Services;
using Microsoft.Extensions.Logging;

namespace MatrixRoomUtils.Bot; 

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 SaveObject<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), ObjectExtensions.ToJson(value));

    public async Task<T?> LoadObject<T>(string key) => JsonSerializer.Deserialize<T>(await File.ReadAllTextAsync(Path.Join(TargetPath, key)));

    public async Task<bool> ObjectExists(string key) => File.Exists(Path.Join(TargetPath, key));

    public async Task<List<string>> GetAllKeys() => Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList();

    public async Task DeleteObject(string key) => File.Delete(Path.Join(TargetPath, key));
}