From cb038a49c417813bbb09f770e49aa28169a83710 Mon Sep 17 00:00:00 2001 From: Rory& Date: Tue, 2 Jul 2024 02:00:43 +0200 Subject: Authenticated media foundations --- MatrixUtils.Web/Shared/MxcAvatar.razor | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 MatrixUtils.Web/Shared/MxcAvatar.razor (limited to 'MatrixUtils.Web/Shared/MxcAvatar.razor') diff --git a/MatrixUtils.Web/Shared/MxcAvatar.razor b/MatrixUtils.Web/Shared/MxcAvatar.razor new file mode 100644 index 0000000..09ea790 --- /dev/null +++ b/MatrixUtils.Web/Shared/MxcAvatar.razor @@ -0,0 +1,58 @@ +@using System.Security +@using System.Security.Cryptography +@using Blazored.SessionStorage.JsonConverters + + +@code { + private string _mxcUri; + private string _style; + private Stream _stream; + + [Parameter] + public string MxcUri { + get => _mxcUri ?? ""; + set { + if(_mxcUri == value) return; + _mxcUri = value; + UriHasChanged(value); + } + } + + [Parameter] + public bool Circular { get; set; } + + [Parameter] + public int Size { get; set; } = 48; + + [Parameter] + public string SizeUnit { get; set; } = "px"; + + [Parameter] + public AuthenticatedHomeserverGeneric? Homeserver { get; set; } + + private string StyleString => $"{(Circular ? "border-radius: 50%;" : "")} width: {Size}{SizeUnit}; height: {Size}{SizeUnit}; object-fit: cover;"; + + private static readonly string Prefix = "mxc://"; + private static readonly int PrefixLength = Prefix.Length; + + private async Task UriHasChanged(string value) { + if (!value.StartsWith(Prefix)) { + // Console.WriteLine($"UriHasChanged: {value} does not start with {Prefix}, passing as resolved URI!!!"); + // ResolvedUri = value; + return; + } + + if (Homeserver is null) { + Console.WriteLine("Homeserver is required for MxcAvatar"); + return; + } + + var uri = value[PrefixLength..].Split('/'); + // Console.WriteLine($"UriHasChanged: {value} {uri[0]}"); + var url = $"/_matrix/media/v3/download/{uri[0]}/{uri[1]}"; + Console.WriteLine($"ResolvedUri: {url}"); + _stream = await Homeserver.ClientHttpClient.GetStreamAsync(url); + StateHasChanged(); + } + +} \ No newline at end of file -- cgit 1.4.1