From ef275ebf62608b1bd2c7e48862e723cd822d93c2 Mon Sep 17 00:00:00 2001
From: Unknwon <joe2010xtmf@163.com>
Date: Fri, 21 Nov 2014 12:51:36 -0500
Subject: [PATCH] more on change avatar

---
 cmd/serve.go                         |  2 +-
 conf/locale/locale_en-US.ini         |  9 ++++++---
 conf/locale/locale_zh-CN.ini         |  8 ++++++++
 models/user.go                       |  1 +
 modules/auth/user_form.go            |  3 ++-
 modules/setting/setting.go           |  4 ++--
 modules/ssh/ssh.go                   | 13 +++++++------
 public/ng/css/gogs.css               |  2 +-
 public/ng/less/gogs/profile.less     |  2 +-
 routers/user/setting.go              |  7 ++++++-
 templates/user/settings/profile.tmpl |  9 +++++++--
 11 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/cmd/serve.go b/cmd/serve.go
index 15d48dcbfe..2390962342 100644
--- a/cmd/serve.go
+++ b/cmd/serve.go
@@ -164,7 +164,7 @@ func runServ(k *cli.Context) {
 			log.GitLogger.Fatal(2, "User %s has no right to read repository %s", user.Name, repoPath)
 		}
 	default:
-		println("Unknown command")
+		println("Unknown command: " + cmd)
 		return
 	}
 
diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini
index 6334966602..f82e834c2e 100644
--- a/conf/locale/locale_en-US.ini
+++ b/conf/locale/locale_en-US.ini
@@ -202,10 +202,13 @@ change_username = Username Changed
 change_username_desc = Username has been changed, do you want to continue? This will affect all links relate to your account.
 continue = Continue
 cancel = Cancel
+
+enable_custom_avatar = Enable Custom Avatar
+enable_custom_avatar_helper = Enable this to disable fetch from Gravatar
 choose_new_avatar = Choose new avatar
-upload_avatar = Upload Avatar
-uploaded_avatar_not_a_image = Uploaded file is not a image
-upload_avatar_success = Your new avatar has been uploaded successfully.
+update_avatar = Update Avatar Setting
+uploaded_avatar_not_a_image = Uploaded file is not a image.
+update_avatar_success = Your avatar setting has been updated successfully.
 
 change_password = Change Password
 old_password = Current Password
diff --git a/conf/locale/locale_zh-CN.ini b/conf/locale/locale_zh-CN.ini
index 02cf77c5c9..09030a387f 100755
--- a/conf/locale/locale_zh-CN.ini
+++ b/conf/locale/locale_zh-CN.ini
@@ -173,6 +173,7 @@ target_branch_not_exist=目标分支不存在。
 
 [user]
 change_avatar=到 gravatar.com 上修改您的头像
+change_custom_avatar=到个人设置中修改头像
 join_on=加入于
 repositories=仓库列表
 activity=公开活动
@@ -202,6 +203,13 @@ change_username_desc=用户名被修改,您确定要继续操作吗?这将
 continue=继续操作
 cancel=取消操作
 
+enable_custom_avatar=启动自定义头像
+enable_custom_avatar_helper=激活该选项来禁止从 Gravatar 获取头像
+choose_new_avatar=选择新的头像
+update_avatar=更新头像设置
+uploaded_avatar_not_a_image=上传的文件不是一张图片!
+update_avatar_success=您的头像设置更新成功!
+
 change_password=修改密码
 old_password=当前密码
 new_password=新的密码
diff --git a/models/user.go b/models/user.go
index 1337ca2350..c68ae31067 100644
--- a/models/user.go
+++ b/models/user.go
@@ -163,6 +163,7 @@ func (u *User) UploadAvatar(data []byte) error {
 		return err
 	}
 
+	os.MkdirAll(setting.AvatarUploadPath, os.ModePerm)
 	fw, err := os.Create(savePath)
 	if err != nil {
 		sess.Rollback()
diff --git a/modules/auth/user_form.go b/modules/auth/user_form.go
index afdd8be0c9..4dfe2499f1 100644
--- a/modules/auth/user_form.go
+++ b/modules/auth/user_form.go
@@ -89,7 +89,8 @@ func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs binding.Errors)
 }
 
 type UploadAvatarForm struct {
-	Avatar *multipart.FileHeader `form:"avatar" binding:"Required"`
+	Enable bool                  `form:"enable"`
+	Avatar *multipart.FileHeader `form:"avatar"`
 }
 
 func (f *UploadAvatarForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 49bd11c4c2..cf34c2edc6 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -20,7 +20,7 @@ import (
 	"github.com/macaron-contrib/session"
 
 	"github.com/gogits/gogs/modules/log"
-	// "github.com/gogits/gogs-ng/modules/ssh"
+	// "github.com/gogits/gogs/modules/ssh"
 )
 
 type Scheme string
@@ -488,5 +488,5 @@ func NewServices() {
 	newRegisterMailService()
 	newNotifyMailService()
 	newWebhookService()
-	// ssh.Listen("2022")
+	// ssh.Listen("2222")
 }
diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go
index 814a3dd1f4..26169e0f3d 100644
--- a/modules/ssh/ssh.go
+++ b/modules/ssh/ssh.go
@@ -13,9 +13,8 @@ import (
 	"os/exec"
 	"strings"
 
-	"code.google.com/p/go.crypto/ssh"
-
 	"github.com/Unknwon/com"
+	"golang.org/x/crypto/ssh"
 
 	"github.com/gogits/gogs/modules/log"
 )
@@ -35,8 +34,11 @@ func handleServerConn(keyId string, chans <-chan ssh.NewChannel) {
 		go func(in <-chan *ssh.Request) {
 			defer channel.Close()
 			for req := range in {
-				ok, payload := false, strings.TrimLeft(string(req.Payload), "\x00")
+				ok, payload := false, strings.TrimLeft(string(req.Payload), "\x00&")
 				fmt.Println("Request:", req.Type, req.WantReply, payload)
+				if req.WantReply {
+					fmt.Println(req.Reply(true, nil))
+				}
 				switch req.Type {
 				case "env":
 					args := strings.Split(strings.Replace(payload, "\x00", "", -1), "\v")
@@ -54,7 +56,7 @@ func handleServerConn(keyId string, chans <-chan ssh.NewChannel) {
 				case "exec":
 					os.Setenv("SSH_ORIGINAL_COMMAND", strings.TrimLeft(payload, "'("))
 					log.Info("Payload: %v", strings.TrimLeft(payload, "'("))
-					cmd := exec.Command("/Users/jiahuachen/Applications/Go/src/github.com/gogits/gogs-ng/gogs-ng", "serv", "key-"+keyId)
+					cmd := exec.Command("/Users/jiahuachen/Applications/Go/src/github.com/gogits/gogs/gogs", "serv", "key-"+keyId)
 					cmd.Stdout = channel
 					cmd.Stdin = channel
 					cmd.Stderr = channel.Stderr()
@@ -65,7 +67,6 @@ func handleServerConn(keyId string, chans <-chan ssh.NewChannel) {
 					}
 				}
 				fmt.Println("Done:", ok)
-				req.Reply(ok, nil) // BUG: Git on Mac seems not know this reply and hang?
 			}
 			fmt.Println("Done!!!")
 		}(requests)
@@ -101,7 +102,7 @@ func Listen(port string) {
 	config := &ssh.ServerConfig{
 		PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
 			// keyCache[string(ssh.MarshalAuthorizedKey(key))] = 2
-			return &ssh.Permissions{Extensions: map[string]string{"key-id": "2"}}, nil
+			return &ssh.Permissions{Extensions: map[string]string{"key-id": "1"}}, nil
 		},
 	}
 
diff --git a/public/ng/css/gogs.css b/public/ng/css/gogs.css
index 961ad6a865..1bb1e84ad1 100644
--- a/public/ng/css/gogs.css
+++ b/public/ng/css/gogs.css
@@ -2414,5 +2414,5 @@ textarea#issue-add-content {
   border-bottom: 2px solid #D26911;
 }
 #profile-body {
-  margin-left: 10px;
+  margin-left: 20px;
 }
diff --git a/public/ng/less/gogs/profile.less b/public/ng/less/gogs/profile.less
index ae0a2b2201..1102ef7820 100644
--- a/public/ng/less/gogs/profile.less
+++ b/public/ng/less/gogs/profile.less
@@ -55,5 +55,5 @@
 	}
 }
 #profile-body {
-	margin-left: 10px;
+	margin-left: 20px;
 }
\ No newline at end of file
diff --git a/routers/user/setting.go b/routers/user/setting.go
index 559e10fd89..7aeb349727 100644
--- a/routers/user/setting.go
+++ b/routers/user/setting.go
@@ -88,6 +88,11 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
 func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
 	defer ctx.Redirect(setting.AppSubUrl + "/user/settings")
 
+	ctx.User.UseCustomAvatar = form.Enable
+	if err := models.UpdateUser(ctx.User); err != nil {
+		ctx.Flash.Error(err.Error())
+	}
+
 	if form.Avatar != nil {
 		fr, err := form.Avatar.Open()
 		if err != nil {
@@ -108,8 +113,8 @@ func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
 			ctx.Flash.Error(err.Error())
 			return
 		}
-		ctx.Flash.Success(ctx.Tr("settings.upload_avatar_success"))
 	}
+	ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
 }
 
 func SettingsPassword(ctx *middleware.Context) {
diff --git a/templates/user/settings/profile.tmpl b/templates/user/settings/profile.tmpl
index 85db6f8951..827586faea 100644
--- a/templates/user/settings/profile.tmpl
+++ b/templates/user/settings/profile.tmpl
@@ -58,13 +58,18 @@
                             <hr>
                             <form class="form form-align" id="user-profile-form" action="{{AppSubUrl}}/user/settings/avatar" method="post" enctype="multipart/form-data">
                                 {{.CsrfTokenHtml}}
+                                <div class="field">
+                                    <label for="enable">{{.i18n.Tr "settings.enable_custom_avatar"}}</label>
+                                    <input class="ipt-chk" id="enable" name="enable" type="checkbox" {{if .SignedUser.UseCustomAvatar}}checked{{end}} />
+                                    <span>{{.i18n.Tr "settings.enable_custom_avatar_helper"}}</span>
+                                </div>
                                 <div class="field">
                                     <label>{{.i18n.Tr "settings.choose_new_avatar"}}</label>
-                                    <input name="avatar" type="file" required />
+                                    <input name="avatar" type="file" />
                                 </div>
                                 <div class="field">
                                     <label></label>
-                                    <button class="btn btn-green btn-large btn-radius">{{.i18n.Tr "settings.upload_avatar"}}</button>
+                                    <button class="btn btn-green btn-large btn-radius">{{.i18n.Tr "settings.update_avatar"}}</button>
                                 </div>
                             </form>
                         </div>