answer/pkg/gravatar/gravatar.go

33 lines
747 B
Go
Raw Normal View History

2022-10-28 10:50:36 +08:00
package gravatar
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/url"
2023-05-15 11:20:50 +08:00
"github.com/answerdev/answer/internal/base/constant"
2022-10-28 10:50:36 +08:00
)
// GetAvatarURL get avatar url from gravatar by email
func GetAvatarURL(email string) string {
h := md5.New()
h.Write([]byte(email))
2023-05-15 11:20:50 +08:00
return constant.DefaultGravatarBaseURL + hex.EncodeToString(h.Sum(nil))
2022-10-28 10:50:36 +08:00
}
// 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()
2022-10-28 11:01:42 +08:00
query.Set("s", fmt.Sprintf("%d", sizePixel))
2022-10-28 10:50:36 +08:00
originalURL.RawQuery = query.Encode()
return originalURL.String()
}