summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-08-09 11:46:10 +0800
committerUnknwon <u@gogs.io>2015-08-09 11:46:10 +0800
commit4b43ffc96cf16cd49b5796feca8ad853f3e3a8ee (patch)
tree85306f0d5e79394a0dcc4dca4c1cd5e3256471ce /models
parent56a8d573b0346dce9f6843d61997e5794bc16e4e (diff)
downloadgitea-4b43ffc96cf16cd49b5796feca8ad853f3e3a8ee.tar.gz
gitea-4b43ffc96cf16cd49b5796feca8ad853f3e3a8ee.zip
Generate random avatar based on e-mail when disable Gravatar
Diffstat (limited to 'models')
-rw-r--r--models/user.go32
1 files changed, 31 insertions, 1 deletions
diff --git a/models/user.go b/models/user.go
index 6dd31536fb..65b23f791a 100644
--- a/models/user.go
+++ b/models/user.go
@@ -13,7 +13,9 @@ import (
"fmt"
"image"
"image/jpeg"
+ _ "image/jpeg"
"os"
+ "path"
"path/filepath"
"strings"
"time"
@@ -116,11 +118,39 @@ func (u *User) HomeLink() string {
// AvatarLink returns user gravatar link.
func (u *User) AvatarLink() string {
+ defaultImgUrl := setting.AppSubUrl + "/img/avatar_default.jpg"
+ imgPath := path.Join(setting.AvatarUploadPath, com.ToStr(u.Id))
switch {
case u.UseCustomAvatar:
+ if !com.IsExist(imgPath) {
+ return defaultImgUrl
+ }
return setting.AppSubUrl + "/avatars/" + com.ToStr(u.Id)
case setting.DisableGravatar, setting.OfflineMode:
- return setting.AppSubUrl + "/img/avatar_default.jpg"
+ if !com.IsExist(imgPath) {
+ img, err := avatar.RandomImage([]byte(u.Email))
+ if err != nil {
+ log.Error(3, "RandomImage: %v", err)
+ return defaultImgUrl
+ }
+ if err = os.MkdirAll(path.Dir(imgPath), os.ModePerm); err != nil {
+ log.Error(3, "Create: %v", err)
+ return defaultImgUrl
+ }
+ fw, err := os.Create(imgPath)
+ if err != nil {
+ log.Error(3, "Create: %v", err)
+ return defaultImgUrl
+ }
+ defer fw.Close()
+
+ if err = jpeg.Encode(fw, img, nil); err != nil {
+ log.Error(3, "Encode: %v", err)
+ return defaultImgUrl
+ }
+ }
+
+ return setting.AppSubUrl + "/avatars/" + com.ToStr(u.Id)
case setting.Service.EnableCacheAvatar:
return setting.AppSubUrl + "/avatar/" + u.Avatar
}