about summary refs log tree commit diff
path: root/Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs
blob: 4820a65a70c4fa37249398a43da8560dd852fbbf (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using ArcaneLibs.Extensions;
using LibMatrix.HomeserverEmulator.Services;
using LibMatrix.Services;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;

namespace LibMatrix.HomeserverEmulator.Controllers.Media;

[ApiController]
[Route("/_matrix/media/{version}/")]
public class MediaController(
    ILogger<MediaController> logger,
    TokenService tokenService,
    UserStore userStore,
    HSEConfiguration cfg,
    HomeserverResolverService hsResolver,
    MediaStore mediaStore)
    : ControllerBase {
    [HttpPost("upload")]
    public async Task<object> UploadMedia([FromHeader(Name = "Content-Type")] string ContentType, [FromQuery] string filename, [FromBody] Stream file) {
        var token = tokenService.GetAccessTokenOrNull(HttpContext);
        if (token == null)
            throw new MatrixException() {
                ErrorCode = "M_MISSING_TOKEN",
                Error = "Missing token"
            };

        var user = await userStore.GetUserByToken(token);
        if (user == null)
            throw new MatrixException() {
                ErrorCode = "M_UNKNOWN_TOKEN",
                Error = "No such user"
            };

        var mediaId = Guid.NewGuid().ToString();
        var media = new {
            content_uri = $"mxc://{tokenService.GenerateServerName(HttpContext)}/{mediaId}"
        };
        return media;
    }

    private Dictionary<string, SemaphoreSlim> downloadLocks = new();

    [HttpGet("download/{serverName}/{mediaId}")]
    public async Task DownloadMedia(string serverName, string mediaId) {
        while (true)
            try {
                if (cfg.StoreData) {
                    SemaphoreSlim ss;
                    if (!downloadLocks.ContainsKey(serverName + mediaId))
                        downloadLocks[serverName + mediaId] = new SemaphoreSlim(1);
                    ss = downloadLocks[serverName + mediaId];
                    await ss.WaitAsync();
                    var serverMediaPath = Path.Combine(cfg.DataStoragePath, "media", serverName);
                    Directory.CreateDirectory(serverMediaPath);
                    var mediaPath = Path.Combine(serverMediaPath, mediaId);
                    if (System.IO.File.Exists(mediaPath)) {
                        ss.Release();
                        await using var stream = new FileStream(mediaPath, FileMode.Open);
                        await stream.CopyToAsync(Response.Body);
                        return;
                    }
                    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"
                            };
                        await using var stream = System.IO.File.OpenWrite(mediaPath);
                        using var response = await new HttpClient().GetAsync(mediaUrl);
                        await response.Content.CopyToAsync(stream);
                        await stream.FlushAsync();
                        ss.Release();
                        await DownloadMedia(serverName, mediaId);
                        return;
                    }
                }
                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 response = await new HttpClient().GetAsync(mediaUrl);
                    await response.Content.CopyToAsync(Response.Body);
                    return;
                }

                return;
            }
            catch (IOException) {
                //ignored
            }
    }

    [HttpGet("thumbnail/{serverName}/{mediaId}")]
    public async Task DownloadThumbnail(string serverName, string mediaId) {
        await DownloadMedia(serverName, mediaId);
    }

    [HttpGet("preview_url")]
    public async Task<JsonObject> GetPreviewUrl([FromQuery] string url) {
        JsonObject data = new();

        using var hc = new HttpClient();
        using var response = await hc.GetAsync(url);
        var doc = await response.Content.ReadAsStringAsync();
        var match = Regex.Match(doc, "<meta property=\"(.*?)\" content=\"(.*?)\"");

        while (match.Success) {
            data[match.Groups[1].Value] = match.Groups[2].Value;
            match = match.NextMatch();
        }

        return data;
    }
}