// GenerateRandomAvatar generates a random avatar for user. func (u *User) GenerateRandomAvatar() error { seed := u.Email if len(seed) == 0 { seed = u.Name } img, err := avatar.RandomImage([]byte(seed)) if err != nil { return fmt.Errorf("RandomImage: %v", err) } if err = os.MkdirAll(filepath.Dir(u.CustomAvatarPath()), os.ModePerm); err != nil { return fmt.Errorf("MkdirAll: %v", err) } fw, err := os.Create(u.CustomAvatarPath()) if err != nil { return fmt.Errorf("Create: %v", err) } defer fw.Close() if err = jpeg.Encode(fw, img, nil); err != nil { return fmt.Errorf("Encode: %v", err) } log.Info("New random avatar created: %d", u.Id) return nil }
func (u *User) RelAvatarLink() string { defaultImgUrl := "/img/avatar_default.jpg" if u.Id == -1 { return defaultImgUrl } imgPath := path.Join(setting.AvatarUploadPath, com.ToStr(u.Id)) switch { case u.UseCustomAvatar: if !com.IsExist(imgPath) { return defaultImgUrl } return "/avatars/" + com.ToStr(u.Id) case setting.DisableGravatar, setting.OfflineMode: 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, "MkdirAll: %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 } log.Info("New random avatar created: %d", u.Id) } return "/avatars/" + com.ToStr(u.Id) case setting.Service.EnableCacheAvatar: return "/avatar/" + u.Avatar } return setting.GravatarSource + u.Avatar }