mirror of https://gitee.com/answerdev/answer.git
feat: support gravatar avatar
This commit is contained in:
parent
273325db31
commit
97ab26663f
|
@ -0,0 +1,34 @@
|
|||
package gravatar
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultURLPrefix = "https://www.gravatar.com/avatar/"
|
||||
)
|
||||
|
||||
// GetAvatarURL get avatar url from gravatar by email
|
||||
func GetAvatarURL(email string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(email))
|
||||
return defaultURLPrefix + hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Resize resize avatar by pixel
|
||||
func Resize(originalAvatarURL string, sizePixel int) (resizedAvatarURL string) {
|
||||
if len(originalAvatarURL) == 0 {
|
||||
return
|
||||
}
|
||||
originalURL, err := url.Parse(originalAvatarURL)
|
||||
if err != nil {
|
||||
return originalAvatarURL
|
||||
}
|
||||
query := originalURL.Query()
|
||||
query.Set("p", fmt.Sprintf("%d", sizePixel))
|
||||
originalURL.RawQuery = query.Encode()
|
||||
return originalURL.String()
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package gravatar
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetAvatarURL(t *testing.T) {
|
||||
type args struct {
|
||||
email string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "answer@answer.com",
|
||||
args: args{email: "answer@answer.com"},
|
||||
want: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equal(t, tt.want, GetAvatarURL(tt.args.email))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResize(t *testing.T) {
|
||||
type args struct {
|
||||
originalAvatarURL string
|
||||
sizePixel int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantResizedAvatarURL string
|
||||
}{
|
||||
{
|
||||
name: "original url",
|
||||
args: args{
|
||||
originalAvatarURL: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7",
|
||||
sizePixel: 128,
|
||||
},
|
||||
wantResizedAvatarURL: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7?p=128",
|
||||
},
|
||||
{
|
||||
name: "already resized url",
|
||||
args: args{
|
||||
originalAvatarURL: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7?p=128",
|
||||
sizePixel: 64,
|
||||
},
|
||||
wantResizedAvatarURL: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7?p=64",
|
||||
},
|
||||
{
|
||||
name: "empty url",
|
||||
args: args{
|
||||
originalAvatarURL: "",
|
||||
sizePixel: 64,
|
||||
},
|
||||
wantResizedAvatarURL: "",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.wantResizedAvatarURL, Resize(tt.args.originalAvatarURL, tt.args.sizePixel), "Resize(%v, %v)", tt.args.originalAvatarURL, tt.args.sizePixel)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue