-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravatar.go
68 lines (56 loc) · 2.14 KB
/
gravatar.go
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
package goutils
import (
"crypto/md5"
"fmt"
"net/http"
"net/url"
"strings"
)
// Default images (used as defaultURL)
const (
HTTP404 = "404" // do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response
MysteryMan = "mm" // (mystery-man) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)
IdentIcon = "identicon" // a geometric pattern based on an email hash
MonsterID = "monsterid" // a generated 'monster' with different colors, faces, etc
Wavatar = "wavatar" // generated faces with differing features and backgrounds
Retro = "retro" // awesome generated, 8-bit arcade-style pixelated faces
)
func Hash(email string) string {
email = strings.ToLower(strings.TrimSpace(email))
hash := md5.New()
hash.Write([]byte(email))
return fmt.Sprintf("%x", hash.Sum(nil))
}
func Url(email string) string {
return "http://gravatar.com/avatar/" + Hash(email)
}
func UrlDefault(email, defaultURL string) string {
return Url(email) + "?d=" + url.QueryEscape(defaultURL)
}
// You may request images anywhere from 1px up to 512px
func UrlSize(email string, size int) string {
return fmt.Sprintf("http://gravatar.com/avatar/%s?s=%d", Hash(email), size)
}
func UrlSizeDefault(email string, size int, defaultURL string) string {
return UrlSize(email, size) + "&d=" + url.QueryEscape(defaultURL)
}
func SecureUrl(email string) string {
return "https://secure.gravatar.com/avatar/" + Hash(email)
}
func SecureUrlDefault(email, defaultURL string) string {
return SecureUrl(email) + "?d=" + url.QueryEscape(defaultURL)
}
func SecureUrlSize(email string, size int) string {
return fmt.Sprintf("https://secure.gravatar.com/avatar/%s?s=%d", Hash(email), size)
}
func SecureUrlSizeDefault(email string, size int, defaultURL string) string {
return SecureUrlSize(email, size) + "&d=" + url.QueryEscape(defaultURL)
}
func Available(email string) (ok bool, err error) {
url := fmt.Sprintf("http://gravatar.com/avatar/%s?d=404", Hash(email))
response, err := http.Get(url)
if err != nil {
return false, err
}
return response.StatusCode != 404, nil
}