Browse Source

fix: email and avatar

master 1.5.6
OhYee 7 months ago
parent
commit
8b2d390e83
  1. 2
      api/avatar.go
  2. 38
      api/pkg/avatar/main.go
  3. 30
      api/pkg/avatar/main_test.go
  4. 3
      api/pkg/email/main.go
  5. 3
      http/http.go

2
api/avatar.go

@ -5,8 +5,6 @@ import ( @@ -5,8 +5,6 @@ import (
"github.com/OhYee/blotter/register"
)
const defaultAvatar = "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
// AvatarRequest request of avatar api
type AvatarRequest struct {
Email string `json:"email"`

38
api/pkg/avatar/main.go

@ -17,6 +17,13 @@ import ( @@ -17,6 +17,13 @@ import (
)
const DefaultAvatar = "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
const DefaultAvatarMD5 = "1fbf1eeb622038a1ea2e62036d33788a"
func md5Encode(source []byte) string {
hash := md5.New()
hash.Write(source)
return hex.EncodeToString(hash.Sum(nil))
}
func makeHTTPClient() *http.Client {
keys := []string{"HTTP_PROXY", "HTTPS_PROXY", "PROXY", "http_proxy", "https_proxy", "proxy"}
@ -93,14 +100,30 @@ func GetGithubAvatar(email string) (avatar string) { @@ -93,14 +100,30 @@ func GetGithubAvatar(email string) (avatar string) {
return
}
// GetCravatar get Gravatar of the gmail
func GetCravatar(email string) (avatar string) {
avatar = fmt.Sprintf(
"https://cravatar.cn/avatar/%s?s=640&d=404",
md5Encode([]byte(strings.ToLower(email))),
)
resp, err := makeHTTPClient().Get(avatar)
if err != nil || resp == nil || resp.StatusCode == 404 {
avatar = ""
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil || md5Encode(body) == DefaultAvatarMD5 {
avatar = ""
}
return
}
// GetGravatar get Gravatar of the gmail
func GetGravatar(email string) (avatar string) {
hash := md5.New()
hash.Write([]byte(strings.ToLower(email)))
avatar = fmt.Sprintf(
"https://www.gravatar.com/avatar/%s?size=640&default=%s",
hex.EncodeToString(hash.Sum(nil)),
md5Encode([]byte(strings.ToLower(email))),
url.QueryEscape(DefaultAvatar),
)
@ -112,7 +135,7 @@ func GetGravatar(email string) (avatar string) { @@ -112,7 +135,7 @@ func GetGravatar(email string) (avatar string) {
return
}
var qqRegexp = regexp.MustCompile("^\\d+$")
var qqRegexp = regexp.MustCompile(`^\d+$`)
func GetQQAvatar(email string) (avatar string) {
if strings.HasSuffix(email, "@qq.com") {
@ -128,6 +151,7 @@ type avatarGetter = func(string) string @@ -128,6 +151,7 @@ type avatarGetter = func(string) string
var avatarGetters = []avatarGetter{
GetGithubAvatar,
GetCravatar,
GetGravatar,
GetQQAvatar,
}
@ -148,6 +172,10 @@ func Get(email string) (avatar string) { @@ -148,6 +172,10 @@ func Get(email string) (avatar string) {
break
}
}
if avatar == "" {
avatar = DefaultAvatar
}
if avatar != "" {
avatarMap.PutWithExpired(email, avatar, time.Hour*24*7)
}

30
api/pkg/avatar/main_test.go

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
package avatar
import "testing"
func TestGet(t *testing.T) {
tests := []struct {
email string
wantAvatar string
}{
{
email: "oyohyee@oyohyee.com",
wantAvatar: "https://avatars.githubusercontent.com/u/13498329?v=4",
},
{
email: "896817156@qq.com",
wantAvatar: "https://cravatar.cn/avatar/c92b45d4385ba3d97a777e63bb519908?s=640&d=404",
},
{
email: "404@oyohyee.com",
wantAvatar: DefaultAvatar,
},
}
for _, tt := range tests {
t.Run(tt.email, func(t *testing.T) {
if gotAvatar := Get(tt.email); gotAvatar != tt.wantAvatar {
t.Errorf("Get() = %v, want %v", gotAvatar, tt.wantAvatar)
}
})
}
}

3
api/pkg/email/main.go

@ -65,10 +65,9 @@ func Send(hostWithPort, username, user, password string, ssl bool, subject, body @@ -65,10 +65,9 @@ func Send(hostWithPort, username, user, password string, ssl bool, subject, body
return
}
msg := []byte(fmt.Sprintf(
"To: %s\r\nFrom: %s<%s>\r\nSubject: %s\r\nContent-Type: text/html;charset=UTF-8\r\n\r\n%s",
"To: %s\r\nFrom: %s<%s>\r\nSubject: =?UTF-8?B?%s?=\r\nContent-Type: text/html;charset=UTF-8\r\n\r\n%s",
strings.Join(to, ","),
username, user,
subject, body,
))
if _, err = w.Write(msg); err != nil {

3
http/http.go

@ -16,9 +16,10 @@ func (handle Handle) ServeHTTP(rep http.ResponseWriter, req *http.Request) { @@ -16,9 +16,10 @@ func (handle Handle) ServeHTTP(rep http.ResponseWriter, req *http.Request) {
context := register.NewHTTPContext(req, rep)
// CORS
context.AddHeader("Access-Control-Allow-Origin", "*")
context.AddHeader("Access-Control-Allow-Origin", req.Header.Get("Origin"))
context.AddHeader("Access-Control-Allow-Headers", "*")
context.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
context.AddHeader("Access-Control-Allow-Credentials", "true")
if context.Request.Method == "OPTIONS" {
context.Success()
return

Loading…
Cancel
Save