@@ -16,19 +16,78 @@ package object
16
16
17
17
import (
18
18
"bytes"
19
+ "fmt"
20
+ "io"
21
+ "net/http"
19
22
"strings"
20
23
21
24
"github.com/casdoor/casdoor/proxy"
22
25
)
23
26
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
+
24
82
func (user * User ) refreshAvatar () (bool , error ) {
25
83
var err error
26
84
var fileBuffer * bytes.Buffer
27
85
var ext string
28
86
29
- // Gravatar + Identicon
30
- if strings . Contains (user .Avatar , "Gravatar" ) && user .Email != "" {
87
+ // Gravatar
88
+ if (user .AvatarType == "Auto" || user . AvatarType == "Gravatar" ) && user .Email != "" {
31
89
client := proxy .ProxyHttpClient
90
+
32
91
has , err := hasGravatar (client , user .Email )
33
92
if err != nil {
34
93
return false , err
@@ -39,14 +98,37 @@ func (user *User) refreshAvatar() (bool, error) {
39
98
if err != nil {
40
99
return false , err
41
100
}
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"
42
119
}
43
120
}
44
121
45
- if fileBuffer == nil && strings .Contains (user .Avatar , "Identicon" ) {
122
+ // Identicon
123
+ if fileBuffer == nil && (user .AvatarType == "Auto" || user .AvatarType == "Identicon" ) {
46
124
fileBuffer , ext , err = getIdenticonFileBuffer (user .Name )
47
125
if err != nil {
48
126
return false , err
49
127
}
128
+
129
+ if fileBuffer != nil {
130
+ user .AvatarType = "Identicon"
131
+ }
50
132
}
51
133
52
134
if fileBuffer != nil {
0 commit comments