summary refs log tree commit diff
path: root/extra/admin-api/Interop/Spacebar.Interop.Cdn.Abstractions/FilesystemFileSource.cs
blob: 8e0f2e77697ac46db96956f11eb79c50b15c238b (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
49
50
51
52
53
54
55
56
namespace Spacebar.Interop.Cdn.Abstractions;

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

    private string GetPathSafe(string path) {
        var p = Path.Join(baseUrl, path);
        if (!p.StartsWith(baseUrl)) throw new UnauthorizedAccessException("Invalid path: " + p);
        Console.WriteLine($"Resolved \"safe\" path: {p}");
        return p;
    }
    
    public async Task<IFileSource> 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);
        }

        return this;
    }

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

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

    public async Task WriteFile(string path, Stream stream) {
        var fullPath = GetPathSafe(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(GetPathSafe(path)));
    }
}