about summary refs log tree commit diff
path: root/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs
blob: 00f2a4279d6cfa9dfe8de468c55008e6cc458242 (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
57
58
59
60
61
62
63
64
using System.Text.Json;
using LibMatrix.Services;

namespace LibMatrix.HomeserverEmulator.Services;

public class MediaStore {
    private readonly HSEConfiguration _config;
    private readonly HomeserverResolverService _hsResolver;
    private List<MediaInfo> index = new();

    public MediaStore(HSEConfiguration config, HomeserverResolverService hsResolver) {
        _config = config;
        _hsResolver = hsResolver;
        if (config.StoreData) {
            var path = Path.Combine(config.DataStoragePath, "media");
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            if (File.Exists(Path.Combine(path, "index.json")))
                index = JsonSerializer.Deserialize<List<MediaInfo>>(File.ReadAllText(Path.Combine(path, "index.json")));
        }
        else
            Console.WriteLine("Data storage is disabled, not loading rooms from disk");
    }

    // public async Task<object> UploadMedia(string userId, string mimeType, Stream stream, string? filename = null) {
        // var mediaId = $"mxc://{Guid.NewGuid().ToString()}";
        // var path = Path.Combine(_config.DataStoragePath, "media", mediaId);
        // if (!Directory.Exists(path)) Directory.CreateDirectory(path);
        // var file = Path.Combine(path, filename ?? "file");
        // await using var fs = File.Create(file);
        // await stream.CopyToAsync(fs);
        // index.Add(new() { });
        // return media;
    // }

    public async Task<Stream> GetRemoteMedia(string serverName, string mediaId) {
        if (_config.StoreData) {
            var path = Path.Combine(_config.DataStoragePath, "media", serverName, mediaId);
            if (!File.Exists(path)) {
                var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}");
                if (mediaUrl is null)
                    throw new MatrixException() {
                        ErrorCode = "M_NOT_FOUND",
                        Error = "Media not found"
                    };
                using var client = new HttpClient();
                var stream = await client.GetStreamAsync(mediaUrl);
                await using var fs = File.Create(path);
                await stream.CopyToAsync(fs);
            }
            return new FileStream(path, FileMode.Open);
        }
        else {
            var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}");
            if (mediaUrl is null)
                throw new MatrixException() {
                    ErrorCode = "M_NOT_FOUND",
                    Error = "Media not found"
                };
            using var client = new HttpClient();
            return await client.GetStreamAsync(mediaUrl);
        }
    }
    public class MediaInfo { }
}