about summary refs log tree commit diff
path: root/MatrixUtils.Web/Shared/MxcAvatar.razor
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-07-02 02:00:43 +0200
committerRory& <root@rory.gay>2024-07-02 02:00:43 +0200
commitcb038a49c417813bbb09f770e49aa28169a83710 (patch)
treef16944061c8ba7a8714607fd269875d71ea18e41 /MatrixUtils.Web/Shared/MxcAvatar.razor
parentSome cleanup, update libs (diff)
downloadMatrixUtils-cb038a49c417813bbb09f770e49aa28169a83710.tar.xz
Authenticated media foundations HEAD main dev/media-streaming
Diffstat (limited to 'MatrixUtils.Web/Shared/MxcAvatar.razor')
-rw-r--r--MatrixUtils.Web/Shared/MxcAvatar.razor58
1 files changed, 58 insertions, 0 deletions
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
+<StreamedImage Stream="@_stream" style="@StyleString"/>
+
+@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