From 28f738ba433fb1012f693866dc4b3f521fd824b5 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Tue, 2 Jul 2024 01:57:46 +0200 Subject: Authenticated media foundations --- ArcaneLibs | 2 +- LibMatrix/Extensions/MatrixHttpClient.Single.cs | 3 +- .../Homeservers/AuthenticatedHomeserverGeneric.cs | 32 ++++++++++++++++++++++ LibMatrix/Homeservers/RemoteHomeServer.cs | 1 + LibMatrix/RoomTypes/GenericRoom.cs | 17 +----------- 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/ArcaneLibs b/ArcaneLibs index d63d758..3fb65c1 160000 --- a/ArcaneLibs +++ b/ArcaneLibs @@ -1 +1 @@ -Subproject commit d63d7587b2e97a8925675c67a84f5ea22f6a3474 +Subproject commit 3fb65c126b591ca05e76394d4c40f74cbb70de44 diff --git a/LibMatrix/Extensions/MatrixHttpClient.Single.cs b/LibMatrix/Extensions/MatrixHttpClient.Single.cs index c9cd260..39eb7e5 100644 --- a/LibMatrix/Extensions/MatrixHttpClient.Single.cs +++ b/LibMatrix/Extensions/MatrixHttpClient.Single.cs @@ -26,7 +26,8 @@ public class MatrixHttpClient { EnableMultipleHttp2Connections = true }; Client = new HttpClient(handler) { - DefaultRequestVersion = new Version(3, 0) + DefaultRequestVersion = new Version(3, 0), + Timeout = TimeSpan.FromDays(1) }; } catch (PlatformNotSupportedException e) { diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index c729a44..40fdef3 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -61,15 +61,18 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeserver { return rooms; } + [Obsolete("Use UploadMedia instead, as this method is deprecated.")] public virtual async Task UploadFile(string fileName, IEnumerable data, string contentType = "application/octet-stream") { return await UploadFile(fileName, data.ToArray(), contentType); } + [Obsolete("Use UploadMedia instead, as this method is deprecated.")] public virtual async Task UploadFile(string fileName, byte[] data, string contentType = "application/octet-stream") { await using var ms = new MemoryStream(data); return await UploadFile(fileName, ms, contentType); } + [Obsolete("Use UploadMedia instead, as this method is deprecated.")] public virtual async Task UploadFile(string fileName, Stream fileStream, string contentType = "application/octet-stream") { var req = new HttpRequestMessage(HttpMethod.Post, $"/_matrix/media/v3/upload?filename={fileName}"); req.Content = new StreamContent(fileStream); @@ -406,4 +409,33 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeserver { public NamedFilterCache FilterCache { get; init; } public NamedFileCache FileCache { get; init; } } + +#region Authenticated Media + + // TODO: implement /_matrix/client/v1/media/config when it's actually useful - https://spec.matrix.org/v1.11/client-server-api/#get_matrixclientv1mediaconfig + + private (string ServerName, string MediaId) ParseMxcUri(string mxcUri) { + if (!mxcUri.StartsWith("mxc://")) throw new ArgumentException("Matrix Content URIs must start with 'mxc://'", nameof(mxcUri)); + var parts = mxcUri[6..].Split('/'); + if (parts.Length != 2) throw new ArgumentException($"Invalid Matrix Content URI '{mxcUri}' passed! Matrix Content URIs must exist of only 2 parts!", nameof(mxcUri)); + return (parts[0], parts[1]); + } + + public async Task GetMediaStreamAsync(string mxcUri, int timeout = 0) { + var (serverName, mediaId) = ParseMxcUri(mxcUri); + try { + var res = await ClientHttpClient.GetAsync($"/_matrix/client/v1/media/download/{serverName}/{mediaId}"); + return await res.Content.ReadAsStreamAsync(); + } + catch (LibMatrixException e) { + Console.WriteLine($"Failed to get media stream: {e.Message}"); + throw; + } + + // return default; + } + + + +#endregion } \ No newline at end of file diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs index ecf3e3a..f9e3d04 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs @@ -107,6 +107,7 @@ public class RemoteHomeserver { #endregion + [Obsolete("This call uses the deprecated unauthenticated media endpoints, please switch to the relevant AuthenticatedHomeserver methods instead.", true)] public string? ResolveMediaUri(string? mxcUri) { if (mxcUri is null) return null; if (mxcUri.StartsWith("https://")) return mxcUri; diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index fe2ee8d..b22ce2f 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -316,6 +316,7 @@ public class GenericRoom { public Task GetPowerLevelsAsync() => GetStateAsync("m.room.power_levels"); + [Obsolete("This method will be merged into GetNameAsync() in the future.")] public async Task GetNameOrFallbackAsync(int maxMemberNames = 2) { try { return await GetNameAsync(); @@ -352,22 +353,6 @@ public class GenericRoom { return Task.WhenAll(tasks); } - public async Task GetResolvedRoomAvatarUrlAsync(bool useOriginHomeserver = false) { - var avatar = await GetAvatarUrlAsync(); - if (avatar?.Url is null) return null; - if (!avatar.Url.StartsWith("mxc://")) return avatar.Url; - if (useOriginHomeserver) - try { - var hs = avatar.Url.Split('/', 3)[1]; - return await new HomeserverResolverService(NullLogger.Instance).ResolveMediaUri(hs, avatar.Url); - } - catch (Exception e) { - Console.WriteLine(e); - } - - return Homeserver.ResolveMediaUri(avatar.Url); - } - #endregion #region Simple calls -- cgit 1.4.1