-
Notifications
You must be signed in to change notification settings - Fork 20
/
GravatarHtmlHelper_NetCore.cs
executable file
·162 lines (135 loc) · 6.61 KB
/
GravatarHtmlHelper_NetCore.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
/// <summary>
/// Globally Recognised Avatar - http://gravatar.com
/// </summary>
/// <remarks>
/// This implementation by Andrew Freemantle - http://www.fatlemon.co.uk/
/// <para>Source, Wiki and Issues: https://github.com/AndrewFreemantle/Gravatar-HtmlHelper </para>
/// </remarks>
public static class GravatarHtmlHelper {
/// <summary>
/// In addition to allowing you to use your own image, Gravatar has a number of built in options which you can also use as defaults. Most of these work by taking the requested email hash and using it to generate a themed image that is unique to that email address
/// </summary>
public enum DefaultImage {
/// <summary>Default Gravatar logo</summary>
[DescriptionAttribute("")]
Default,
/// <summary>404 - do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response</summary>
[DescriptionAttribute("404")]
Http404,
/// <summary>Mystery-Man - a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)</summary>
[DescriptionAttribute("mm")]
MysteryMan,
/// <summary>Identicon - a geometric pattern based on an email hash</summary>
[DescriptionAttribute("identicon")]
Identicon,
/// <summary>MonsterId - a generated 'monster' with different colors, faces, etc</summary>
[DescriptionAttribute("monsterid")]
MonsterId,
/// <summary>Wavatar - generated faces with differing features and backgrounds</summary>
[DescriptionAttribute("wavatar")]
Wavatar,
/// <summary>Retro - awesome generated, 8-bit arcade-style pixelated faces</summary>
[DescriptionAttribute("retro")]
Retro
}
/// <summary>
/// Gravatar allows users to self-rate their images so that they can indicate if an image is appropriate for a certain audience. By default, only 'G' rated images are displayed unless you indicate that you would like to see higher ratings
/// </summary>
public enum Rating {
/// <summary>Suitable for display on all websites with any audience type</summary>
[DescriptionAttribute("g")]
G,
/// <summary>May contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence</summary>
[DescriptionAttribute("pg")]
PG,
/// <summary>May contain such things as harsh profanity, intense violence, nudity, or hard drug use</summary>
[DescriptionAttribute("r")]
R,
/// <summary>May contain hardcore sexual imagery or extremely disturbing violence</summary>
[DescriptionAttribute("x")]
X
}
/// <summary>
/// Returns a Globally Recognised Avatar as an <img /> - http://gravatar.com
/// </summary>
/// <param name="emailAddress">Email Address for the Gravatar</param>
/// <param name="defaultImage">Default image if user hasn't created a Gravatar</param>
/// <param name="size">Size in pixels (default: 80)</param>
/// <param name="defaultImageUrl">URL to a custom default image (e.g: 'Url.Content("~/images/no-grvatar.png")' )</param>
/// <param name="forceDefaultImage">Prefer the default image over the users own Gravatar</param>
/// <param name="rating">Gravatar content rating (note that Gravatars are self-rated)</param>
/// <param name="forceSecureRequest">Always do secure (https) requests</param>
public static IHtmlContent GravatarImage(
this IHtmlHelper htmlHelper,
string emailAddress,
int size = 80,
DefaultImage defaultImage = DefaultImage.Default,
string defaultImageUrl = "",
bool forceDefaultImage = false,
Rating rating = Rating.G,
bool forceSecureRequest = false,
string cssClass = "gravatar",
string alt = "Gravatar image") {
var imgTag = new TagBuilder("img");
emailAddress = string.IsNullOrEmpty(emailAddress) ? string.Empty : emailAddress.Trim().ToLower();
imgTag.Attributes.Add("src",
string.Format("{0}://{1}.gravatar.com/avatar/{2}?s={3}{4}{5}{6}",
htmlHelper.ViewContext.HttpContext.Request.IsHttps || forceSecureRequest ? "https" : "http",
htmlHelper.ViewContext.HttpContext.Request.IsHttps || forceSecureRequest ? "secure" : "www",
GetMd5Hash(emailAddress),
size.ToString(),
"&d=" + (!string.IsNullOrEmpty(defaultImageUrl) ? htmlHelper.UrlEncoder.Encode(defaultImageUrl) : defaultImage.GetDescription()),
forceDefaultImage ? "&f=y" : "",
"&r=" + rating.GetDescription()
)
);
imgTag.Attributes.Add("class", cssClass);
imgTag.Attributes.Add("alt", alt);
imgTag.TagRenderMode = TagRenderMode.SelfClosing;
return imgTag;
}
/// <summary>
/// Generates an MD5 hash of the given string
/// </summary>
/// <remarks>Source: http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5.aspx </remarks>
private static string GetMd5Hash(string input) {
// Convert the input string to a byte array and compute the hash.
byte[] data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
/// <summary>
/// Returns the value of a DescriptionAttribute for a given Enum value
/// </summary>
/// <remarks>Source: http://blogs.msdn.com/b/abhinaba/archive/2005/10/21/483337.aspx </remarks>
/// <param name="en"></param>
/// <returns></returns>
private static string GetDescription(this Enum en) {
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Count() > 0)
return ((DescriptionAttribute)attrs.First()).Description;
}
return en.ToString();
}
}