summary refs log tree commit diff
path: root/extra/admin-api/Interop/Spacebar.Interop.Cdn.Abstractions/FilesystemFileSource.cs
blob: a3f0a7d878b93b2ad7ceecc901490164f5ffb577 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
using ArcaneLibs;

namespace Spacebar.Interop.Cdn.Abstractions;

public class FilesystemFileSource(string baseUrl) : IFileSource {
    public string BaseUrl => baseUrl;

    public async Task Init(CancellationToken? cancellationToken = null) {
        foreach (var dir in new[] { "avatars", "banners", "icons", "stickers", "emojis" }) {
            var fullPath = Path.Join(baseUrl, dir);
            if (!Directory.Exists(fullPath))
                Directory.CreateDirectory(fullPath);
        }
    }

    public async Task<FileInfo> GetFile(string path, CancellationToken? cancellationToken = null) {
        await using var rs = File.OpenRead(Path.Join(baseUrl, path));
        var ms = new MemoryStream();
        await rs.CopyToAsync(ms);
        return new() {
            Stream = ms,
            MimeType = "MIME/TYPE"
        };
    }

    public Task<bool> FileExists(string path, CancellationToken? cancellationToken = null) {
        return Task.FromResult(File.Exists(Path.Join(baseUrl, path)));
    }

    public async Task WriteFile(string path, Stream stream) {
        var fullPath = Path.Join(baseUrl, path);
        // Console.WriteLine($"Writing file to {fullPath}... ");
        if (!Directory.Exists(Path.GetDirectoryName(fullPath)!))
            Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);

        await using var fs = File.Create(fullPath);
        await stream.CopyToAsync(fs);
    }

    // private string GetMimeType(Stream stream)
    // {
    //     using var mic = new MagickImageCollection(stream);
    //     return Mimes.GetMime(mic.First().Format);
    // }
    public Task<bool> DirectoryExists(string path) {
        return Task.FromResult(Directory.Exists(Path.Join(baseUrl, path)));
    }
}