-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImageSource.cs
77 lines (69 loc) · 2.84 KB
/
ImageSource.cs
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
70
71
72
73
74
75
76
77
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.PWABuilder.ManifestCreator
{
public class ImageSource
{
/// <summary>
/// Creates a new ImageSource from the specific HTML node. Inspects the node and finds href or content attribute, type attribute, and sizes attribute.
/// Works best on HTML nodes like: <link rel="icon" href="/images/foo.png" type="image/png" sizes="32x32" />
/// </summary>
/// <param name="node">The parent node whose children to select.</param>
/// <returns>The content attribute value of the child nodes.</returns>
public ImageSource(HtmlNode node)
{
var href = node.GetAttributeValue("href", string.Empty);
var content = node.GetAttributeValue("content", string.Empty);
var url = string.IsNullOrWhiteSpace(href) ? content : href;
// See if we have a mime type. If not, best guess it.
var mimeType = node.GetAttributeValue("type", string.Empty);
if (mimeType == null && url != null)
{
mimeType = GuessMimeTypeFromUrl(url);
}
this.Url = url ?? string.Empty;
this.MimeType = mimeType;
this.Sizes = node.GetAttributeValue("sizes", string.Empty);
}
public ImageSource(string url, string? mimeType, string? sizes)
{
this.Url = url;
this.MimeType = mimeType;
this.Sizes = sizes;
}
public string Url { get; private set; }
public string? MimeType { get; private set; }
public string? Sizes { get; private set; }
private static string? GuessMimeTypeFromUrl(string url)
{
var extensionMimeTypes = new Dictionary<string, string>
{
{ ".png", "image/png" },
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".gif", "image/gif" },
{ ".ico", "image/x-icon" },
{ ".svg", "image/svg+xml" },
{ ".webp", "image/webp" }
};
return extensionMimeTypes.FirstOrDefault(ext => url.Contains(ext.Key)).Value;
}
public class ImageSourceUrlComparer : System.Collections.Generic.IEqualityComparer<ImageSource>
{
public bool Equals(ImageSource? x, ImageSource? y)
{
var xUrl = x?.Url;
var yUrl = y?.Url;
return string.Equals(xUrl, yUrl, StringComparison.Ordinal);
}
public int GetHashCode([DisallowNull] ImageSource obj)
{
return obj.Url?.GetHashCode(StringComparison.Ordinal) ?? 0;
}
}
}
}