diff options
author | Rory& <root@rory.gay> | 2024-09-17 04:26:25 +0200 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-09-17 04:26:25 +0200 |
commit | 37a8ac420278fd53ab8218956f1ba13692feb48e (patch) | |
tree | 83afec51130a254f6ed0ba11c45975f3f2ba8b9f /Tests/LibMatrix.HomeserverEmulator/Controllers/Media | |
parent | Drop example bots (diff) | |
parent | Move around some projects, further cleanup pending (diff) | |
download | LibMatrix-37a8ac420278fd53ab8218956f1ba13692feb48e.tar.xz |
Merge remote-tracking branch 'origin/dev/project-cleanup'
Diffstat (limited to 'Tests/LibMatrix.HomeserverEmulator/Controllers/Media')
-rw-r--r-- | Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs | 99 |
1 files changed, 0 insertions, 99 deletions
diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs deleted file mode 100644 index 7899ada..0000000 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System.Text.Json.Nodes; -using System.Text.RegularExpressions; -using ArcaneLibs.Collections; -using LibMatrix.HomeserverEmulator.Services; -using LibMatrix.Services; -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; - } - - [HttpGet("download/{serverName}/{mediaId}")] - public async Task DownloadMedia(string serverName, string mediaId) { - var stream = await DownloadRemoteMedia(serverName, mediaId); - await stream.CopyToAsync(Response.Body); - } - - [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; - } - - private async Task<Stream> DownloadRemoteMedia(string serverName, string mediaId) { - if (cfg.StoreData) { - var path = Path.Combine(cfg.DataStoragePath, "media", serverName, mediaId); - if (!System.IO.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 = System.IO.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); - } - } -} \ No newline at end of file |