about summary refs log tree commit diff
path: root/MatrixUtils.Web/Shared/MxcImage.razor
blob: 26609ee8baf99739147cc8e833e3980f93016159 (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
<AuthorizedImage src="@ResolvedUrl" AccessToken="@Homeserver?.AccessToken" style="@StyleString"/>

@code {
    [Parameter]
    public string? Uri {
        get;
        set {
            // Console.WriteLine($"New MXC uri: {value}");
            if (field == value) return;
            field = value;
            UriHasChanged(value);
        }
    }

    [Parameter]
    public bool Circular { get; set; }

    [Parameter]
    public int? Width { get; set; }

    [Parameter]
    public int? Height { get; set; }

    [Parameter]
    public string? Style {
        get;
        set {
            field = value;
            StateHasChanged();
        }
    }

    [Parameter]
    public required AuthenticatedHomeserverGeneric Homeserver { get; set; }

    private string? ResolvedUrl {
        get;
        set {
            field = value;
            StateHasChanged();
        }
    }

    private string StyleString => $"{Style} {(Circular ? "border-radius: 50%;" : "")} {(Width.HasValue ? $"width: {Width}px;" : "")} {(Height.HasValue ? $"height: {Height}px;" : "")} object-fit: cover;";

    // private static readonly string Prefix = "mxc://";
    // private static readonly int PrefixLength = Prefix.Length;

    private async Task UriHasChanged(string? value) {
        try {
            if (string.IsNullOrWhiteSpace(value)) {
                ResolvedUrl = null;
                return;
            }

            if (Homeserver is null) {
                Console.WriteLine($"Homeserver is required for MxcImage! Uri: {value}, Homeserver: {Homeserver?.ToString() ?? "null"}");
                return;
            }

            ResolvedUrl = await Homeserver.GetMediaUrlAsync(value);
            // Console.WriteLine($"[MxcImage] Resolved URL: {ResolvedUrl}");
            StateHasChanged();
        } catch (Exception e) {
            await Console.Error.WriteLineAsync($"Error resolving media URL: {e}");
        }
    }

}