Browse Source

#1191 allow upload avatar for org

Signed-off-by: Unknwon <u@gogs.io>
for-closed-social
Unknwon 9 years ago
parent
commit
c38754d432
6 changed files with 52 additions and 20 deletions
  1. +1
    -0
      cmd/web.go
  2. +1
    -0
      conf/locale/locale_en-US.ini
  3. +2
    -2
      modules/bindata/bindata.go
  4. +12
    -0
      routers/org/setting.go
  5. +22
    -18
      routers/user/setting.go
  6. +14
    -0
      templates/org/settings/options.tmpl

+ 1
- 0
cmd/web.go View File

@ -406,6 +406,7 @@ func runWeb(ctx *cli.Context) {
m.Group("/settings", func() { m.Group("/settings", func() {
m.Combo("").Get(org.Settings). m.Combo("").Get(org.Settings).
Post(bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost) Post(bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost)
m.Post("/avatar", binding.MultipartForm(auth.UploadAvatarForm{}), org.SettingsAvatar)
m.Group("/hooks", func() { m.Group("/hooks", func() {
m.Get("", org.Webhooks) m.Get("", org.Webhooks)

+ 1
- 0
conf/locale/locale_en-US.ini View File

@ -656,6 +656,7 @@ settings.location = Location
settings.update_settings = Update Settings settings.update_settings = Update Settings
settings.update_setting_success = Organization settings has been updated successfully. settings.update_setting_success = Organization settings has been updated successfully.
settings.change_orgname_prompt = This change will affect how links relate to the organization. settings.change_orgname_prompt = This change will affect how links relate to the organization.
settings.update_avatar_success = Organization avatar setting has been updated successfully.
settings.delete = Delete Organization settings.delete = Delete Organization
settings.delete_account = Delete This Organization settings.delete_account = Delete This Organization
settings.delete_prompt = The organization will be permanently removed, and this <strong>CANNOT</strong> be undone! settings.delete_prompt = The organization will be permanently removed, and this <strong>CANNOT</strong> be undone!

+ 2
- 2
modules/bindata/bindata.go
File diff suppressed because it is too large
View File


+ 12
- 0
routers/org/setting.go View File

@ -12,6 +12,7 @@ import (
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/routers/user"
) )
const ( const (
@ -73,6 +74,17 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
ctx.Redirect(org.HomeLink() + "/settings") ctx.Redirect(org.HomeLink() + "/settings")
} }
func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
form.Enable = true
if err := user.UpdateAvatarSetting(ctx, form, ctx.Org.Organization); err != nil {
ctx.Flash.Error(err.Error())
} else {
ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success"))
}
ctx.Redirect(ctx.Org.OrgLink + "/settings")
}
func SettingsDelete(ctx *middleware.Context) { func SettingsDelete(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("org.settings") ctx.Data["Title"] = ctx.Tr("org.settings")
ctx.Data["PageIsSettingsDelete"] = true ctx.Data["PageIsSettingsDelete"] = true

+ 22
- 18
routers/user/setting.go View File

@ -5,6 +5,8 @@
package user package user
import ( import (
"errors"
"fmt"
"io/ioutil" "io/ioutil"
"strings" "strings"
@ -87,45 +89,47 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
} }
// FIXME: limit size. // FIXME: limit size.
func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
defer ctx.Redirect(setting.AppSubUrl + "/user/settings")
ctx.User.UseCustomAvatar = form.Enable
func UpdateAvatarSetting(ctx *middleware.Context, form auth.UploadAvatarForm, ctxUser *models.User) error {
ctxUser.UseCustomAvatar = form.Enable
if form.Avatar != nil { if form.Avatar != nil {
fr, err := form.Avatar.Open() fr, err := form.Avatar.Open()
if err != nil { if err != nil {
ctx.Flash.Error(err.Error())
return
return fmt.Errorf("Avatar.Open: %v", err)
} }
data, err := ioutil.ReadAll(fr) data, err := ioutil.ReadAll(fr)
if err != nil { if err != nil {
ctx.Flash.Error(err.Error())
return
return fmt.Errorf("ReadAll: %v", err)
} }
if _, ok := base.IsImageFile(data); !ok { if _, ok := base.IsImageFile(data); !ok {
ctx.Flash.Error(ctx.Tr("settings.uploaded_avatar_not_a_image"))
return
return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image"))
} }
if err = ctx.User.UploadAvatar(data); err != nil {
ctx.Flash.Error(err.Error())
return
if err = ctxUser.UploadAvatar(data); err != nil {
return fmt.Errorf("UploadAvatar: %v", err)
} }
} else { } else {
// In case no avatar at all. // In case no avatar at all.
if form.Enable && !com.IsFile(ctx.User.CustomAvatarPath()) { if form.Enable && !com.IsFile(ctx.User.CustomAvatarPath()) {
ctx.Flash.Error(ctx.Tr("settings.no_custom_avatar_available"))
return
return errors.New(ctx.Tr("settings.no_custom_avatar_available"))
} }
} }
if err := models.UpdateUser(ctx.User); err != nil {
if err := models.UpdateUser(ctxUser); err != nil {
return fmt.Errorf("UpdateUser: %v", err)
}
return nil
}
func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
if err := UpdateAvatarSetting(ctx, form, ctx.User); err != nil {
ctx.Flash.Error(err.Error()) ctx.Flash.Error(err.Error())
return
} else {
ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
} }
ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
ctx.Redirect(setting.AppSubUrl + "/user/settings")
} }
func SettingsEmails(ctx *middleware.Context) { func SettingsEmails(ctx *middleware.Context) {

+ 14
- 0
templates/org/settings/options.tmpl View File

@ -37,6 +37,20 @@
<button class="ui green button">{{$.i18n.Tr "org.settings.update_settings"}}</button> <button class="ui green button">{{$.i18n.Tr "org.settings.update_settings"}}</button>
</div> </div>
</form> </form>
<div class="ui divider"></div>
<form class="ui form" action="{{.Link}}/avatar" method="post" enctype="multipart/form-data">
{{.CsrfTokenHtml}}
<div class="inline field">
<label for="avatar">{{.i18n.Tr "settings.choose_new_avatar"}}</label>
<input name="avatar" type="file" >
</div>
<div class="field">
<button class="ui green button">{{$.i18n.Tr "settings.update_avatar"}}</button>
</div>
</form>
</div> </div>
</div> </div>
</div> </div>

Loading…
Cancel
Save