-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathPreviewImage.razor
More file actions
47 lines (40 loc) · 1.29 KB
/
Copy pathPreviewImage.razor
File metadata and controls
47 lines (40 loc) · 1.29 KB
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
@using Microsoft.AspNetCore.StaticFiles
@if (string.IsNullOrEmpty(PreviewImageUrlFallback))
{
<img src="@PreviewImageUrl"
alt="Preview image blogpost"
loading="@LazyLoadTag"
decoding="@DecodingTag"
class="position-absolute top-0 start-0 w-100 h-100 object-fit-cover" />
}
else
{
<picture class="position-absolute top-0 start-0 w-100 h-100">
<source srcset="@PreviewImageUrl" type="@GetMimeType()" />
<img src="@PreviewImageUrlFallback"
alt="Preview image blogpost"
loading="@LazyLoadTag"
decoding="@DecodingTag"
class="w-100 h-100 object-fit-cover" />
</picture>
}
@code {
[Parameter, EditorRequired]
public required string PreviewImageUrl { get; set; }
[Parameter]
public string? PreviewImageUrlFallback { get; set; }
[Parameter]
public bool LazyLoadImage { get; set; }
private string LazyLoadTag => LazyLoadImage ? "lazy" : "eager";
private string DecodingTag => LazyLoadImage ? "async" : "auto";
private static readonly FileExtensionContentTypeProvider Provider = new();
static PreviewImage()
{
Provider.Mappings.TryAdd(".avif", "image/avif");
}
private string? GetMimeType()
{
Provider.TryGetContentType(PreviewImageUrl, out var contentType);
return contentType;
}
}