Skip to content

Commit b9140e2

Browse files
committed
Refactor refreshAvatar()
1 parent 501f0dc commit b9140e2

8 files changed

+286
-174
lines changed

object/avatar_util.go

-167
This file was deleted.

object/user.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type User struct {
4444
FirstName string `xorm:"varchar(100)" json:"firstName"`
4545
LastName string `xorm:"varchar(100)" json:"lastName"`
4646
Avatar string `xorm:"varchar(500)" json:"avatar"`
47+
AvatarType string `xorm:"varchar(100)" json:"avatarType"`
4748
PermanentAvatar string `xorm:"varchar(500)" json:"permanentAvatar"`
4849
Email string `xorm:"varchar(100) index" json:"email"`
4950
EmailVerified bool `json:"emailVerified"`

object/user_avatar.go

+85-3
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,78 @@ package object
1616

1717
import (
1818
"bytes"
19+
"fmt"
20+
"io"
21+
"net/http"
1922
"strings"
2023

2124
"github.com/casdoor/casdoor/proxy"
2225
)
2326

27+
func downloadImage(client *http.Client, url string) (*bytes.Buffer, string, error) {
28+
// Download the image
29+
req, err := http.NewRequest("GET", url, nil)
30+
if err != nil {
31+
return nil, "", err
32+
}
33+
34+
resp, err := client.Do(req)
35+
if err != nil {
36+
fmt.Printf("downloadImage() error for url [%s]: %s\n", url, err.Error())
37+
if strings.Contains(err.Error(), "EOF") {
38+
return nil, "", nil
39+
} else {
40+
return nil, "", err
41+
}
42+
}
43+
defer resp.Body.Close()
44+
45+
if resp.StatusCode != http.StatusOK {
46+
fmt.Printf("downloadImage() error for url [%s]: %s\n", url, resp.Status)
47+
if resp.StatusCode == 404 {
48+
return nil, "", nil
49+
} else {
50+
return nil, "", fmt.Errorf("failed to download gravatar image: %s", resp.Status)
51+
}
52+
}
53+
54+
// Get the content type and determine the file extension
55+
contentType := resp.Header.Get("Content-Type")
56+
fileExtension := ""
57+
switch contentType {
58+
case "image/jpeg":
59+
fileExtension = ".jpg"
60+
case "image/png":
61+
fileExtension = ".png"
62+
case "image/gif":
63+
fileExtension = ".gif"
64+
case "image/vnd.microsoft.icon":
65+
fileExtension = ".ico"
66+
case "image/x-icon":
67+
fileExtension = ".ico"
68+
default:
69+
return nil, "", fmt.Errorf("unsupported content type: %s", contentType)
70+
}
71+
72+
// Save the image to a bytes.Buffer
73+
buffer := &bytes.Buffer{}
74+
_, err = io.Copy(buffer, resp.Body)
75+
if err != nil {
76+
return nil, "", err
77+
}
78+
79+
return buffer, fileExtension, nil
80+
}
81+
2482
func (user *User) refreshAvatar() (bool, error) {
2583
var err error
2684
var fileBuffer *bytes.Buffer
2785
var ext string
2886

29-
// Gravatar + Identicon
30-
if strings.Contains(user.Avatar, "Gravatar") && user.Email != "" {
87+
// Gravatar
88+
if (user.AvatarType == "Auto" || user.AvatarType == "Gravatar") && user.Email != "" {
3189
client := proxy.ProxyHttpClient
90+
3291
has, err := hasGravatar(client, user.Email)
3392
if err != nil {
3493
return false, err
@@ -39,14 +98,37 @@ func (user *User) refreshAvatar() (bool, error) {
3998
if err != nil {
4099
return false, err
41100
}
101+
102+
if fileBuffer != nil {
103+
user.AvatarType = "Gravatar"
104+
}
105+
}
106+
}
107+
108+
// Favicon
109+
if fileBuffer == nil && (user.AvatarType == "Auto" || user.AvatarType == "Favicon") {
110+
client := proxy.ProxyHttpClient
111+
112+
fileBuffer, ext, err = getFaviconFileBuffer(client, user.Email)
113+
if err != nil {
114+
return false, err
115+
}
116+
117+
if fileBuffer != nil {
118+
user.AvatarType = "Favicon"
42119
}
43120
}
44121

45-
if fileBuffer == nil && strings.Contains(user.Avatar, "Identicon") {
122+
// Identicon
123+
if fileBuffer == nil && (user.AvatarType == "Auto" || user.AvatarType == "Identicon") {
46124
fileBuffer, ext, err = getIdenticonFileBuffer(user.Name)
47125
if err != nil {
48126
return false, err
49127
}
128+
129+
if fileBuffer != nil {
130+
user.AvatarType = "Identicon"
131+
}
50132
}
51133

52134
if fileBuffer != nil {

object/user_avatar_favicon.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package object
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"net/http"
21+
"strings"
22+
)
23+
24+
func getFaviconFileBuffer(client *http.Client, email string) (*bytes.Buffer, string, error) {
25+
tokens := strings.Split(email, "@")
26+
domain := tokens[1]
27+
if domain == "gmail.com" || domain == "163.com" || domain == "qq.com" {
28+
return nil, "", nil
29+
}
30+
31+
//htmlUrl := fmt.Sprintf("https://%s", domain)
32+
//buffer, fileExtension, err := downloadImage(client, htmlUrl)
33+
34+
faviconUrl := fmt.Sprintf("https://%s/favicon.ico", domain)
35+
return downloadImage(client, faviconUrl)
36+
}

0 commit comments

Comments
 (0)