aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorSergey Dryabzhinsky <sergey.dryabzhinsky@gmail.com>2018-06-17 15:21:52 +0300
committer无闻 <u@gogs.io>2018-06-17 20:21:52 +0800
commit303fa37b608a4925f5a0137c89e20a906b3d9fdb (patch)
tree1642a7370034bc92173a2567d72ddc61e3e63a49 /routes
parentef02414d7e1d8cc33ece4dc3d6084ba2cd5014cc (diff)
repo: support avatars (#5221)
* First code for repository avatars * Last code for repository avatars - add new option for repo avatars location on filesystem - add route catch in web - add new fields to repo model - add migration - update settings handlers - update repo header template * Update locale messages * Add repo avatars to home page * Add repo avatars to organization right panel * Show repo avatars in repo list * Remove AvatarEamil field, remove Gravatar support, use generic locale messages * Fix migration * Fix seed and not used tool * Revert public css changes, add them to less files * Latest lessc (2.6.0) don't put result into file but output to stdout So redirect output to file * Simplify things: - migration don't needed, and table changes too - just upload file to repo avatar storage - or generate random image * Fix repo image seed - name not unique * Get rid of not needed model fields * Class value is enough, remove height attribute * Don't generate random avatar for repository - use html and semantic ui icons if no avatar found * Update styles and templates for repo - use repo icon as default avatar - use globe icon for public repos - add micro style for repo avatars at dashboard * Remvoe redundant empty line * Fix nl2br filter - must return string * Fix css style for micro-repo-avatar in dashboard list * Remove `|len`, works fine w/o it. * Update after review 2: - use static route for repository avatar - format images settings block in settings * Update after review 2: - no random avatar for repo * Update after review 2: - no random avatar for repo 2 - update imports - update UploadAvatar* functions * Update after review 2: - update templates * Fix trace call * Remove unused immport since we use static route for repo avatars.
Diffstat (limited to 'routes')
-rw-r--r--routes/repo/setting.go58
1 files changed, 57 insertions, 1 deletions
diff --git a/routes/repo/setting.go b/routes/repo/setting.go
index 0b0f294d..da9d8fe4 100644
--- a/routes/repo/setting.go
+++ b/routes/repo/setting.go
@@ -8,9 +8,10 @@ import (
"fmt"
"strings"
"time"
+ "io/ioutil"
log "gopkg.in/clog.v1"
-
+ "github.com/Unknwon/com"
"github.com/gogs/git-module"
"github.com/gogs/gogs/models"
@@ -19,10 +20,12 @@ import (
"github.com/gogs/gogs/pkg/form"
"github.com/gogs/gogs/pkg/mailer"
"github.com/gogs/gogs/pkg/setting"
+ "github.com/gogs/gogs/pkg/tool"
)
const (
SETTINGS_OPTIONS = "repo/settings/options"
+ SETTINGS_REPO_AVATAR = "repo/settings/avatar"
SETTINGS_COLLABORATION = "repo/settings/collaboration"
SETTINGS_BRANCHES = "repo/settings/branches"
SETTINGS_PROTECTED_BRANCH = "repo/settings/protected_branch"
@@ -632,3 +635,56 @@ func DeleteDeployKey(c *context.Context) {
"redirect": c.Repo.RepoLink + "/settings/keys",
})
}
+
+func SettingsAvatar(c *context.Context) {
+ c.Title("settings.avatar")
+ c.PageIs("SettingsAvatar")
+ c.Success(SETTINGS_REPO_AVATAR)
+}
+
+func SettingsAvatarPost(c *context.Context, f form.Avatar) {
+ f.Source = form.AVATAR_LOCAL
+ if err := UpdateAvatarSetting(c, f); err != nil {
+ c.Flash.Error(err.Error())
+ } else {
+ c.Flash.Success(c.Tr("settings.update_avatar_success"))
+ }
+ c.SubURLRedirect(c.Repo.RepoLink + "/settings")
+}
+
+func SettingsDeleteAvatar(c *context.Context) {
+ if err := c.Repo.Repository.DeleteAvatar(); err != nil {
+ c.Flash.Error(fmt.Sprintf("DeleteAvatar: %v", err))
+ }
+ c.SubURLRedirect(c.Repo.RepoLink + "/settings")
+}
+
+// FIXME: limit size.
+func UpdateAvatarSetting(c *context.Context, f form.Avatar) error {
+ ctxRepo := c.Repo.Repository;
+ if f.Avatar != nil {
+ r, err := f.Avatar.Open()
+ if err != nil {
+ return fmt.Errorf("Avatar.Open: %v", err)
+ }
+ defer r.Close()
+
+ data, err := ioutil.ReadAll(r)
+ if err != nil {
+ return fmt.Errorf("ioutil.ReadAll: %v", err)
+ }
+ if !tool.IsImageFile(data) {
+ return errors.New(c.Tr("settings.uploaded_avatar_not_a_image"))
+ }
+ if err = ctxRepo.UploadAvatar(data); err != nil {
+ return fmt.Errorf("UploadAvatar: %v", err)
+ }
+ } else {
+ // No avatar is uploaded but setting has been changed to enable
+ // No random avatar here.
+ if !com.IsFile(ctxRepo.CustomAvatarPath()) {
+ log.Trace("No avatar was uploaded for repo: %d. Default icon will appear instead.", ctxRepo.ID)
+ }
+ }
+ return nil
+}