Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make avatar lookup occur at image request #10540

Merged
merged 17 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func SizedAvatarLink(email string, size int) string {
// which includes app sub-url as prefix. However, it is possible
// to return full URL if user enables Gravatar-like service.
func AvatarLink(email string) string {
return SizedAvatarLink(email, DefaultAvatarSize)
return setting.AppSubURL + "/avatar/" + base64.RawURLEncoding.EncodeToString([]byte(email))
}

// FileSize calculates the file size and generate user-friendly string.
Expand Down
5 changes: 3 additions & 2 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package base

import (
"encoding/base64"
"net/url"
"testing"

Expand Down Expand Up @@ -92,11 +93,11 @@ func TestSizedAvatarLink(t *testing.T) {

func TestAvatarLink(t *testing.T) {
disableGravatar()
assert.Equal(t, "/img/avatar_default.png", AvatarLink("[email protected]"))
assert.Equal(t, "/avatar/"+base64.RawURLEncoding.EncodeToString([]byte("[email protected]")), AvatarLink("[email protected]"))

enableGravatar(t)
assert.Equal(t,
"https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon",
"/avatar/"+base64.RawURLEncoding.EncodeToString([]byte("gitea@example.com")),
AvatarLink("[email protected]"),
)
}
Expand Down
3 changes: 2 additions & 1 deletion modules/repository/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package repository

import (
"container/list"
"encoding/base64"
"testing"
"time"

Expand Down Expand Up @@ -114,7 +115,7 @@ func TestPushCommits_AvatarLink(t *testing.T) {
pushCommits.AvatarLink("[email protected]"))

assert.Equal(t,
"https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon",
"/avatar/"+base64.RawURLEncoding.EncodeToString([]byte("nonexistent@example.com")),
pushCommits.AvatarLink("[email protected]"))
}

Expand Down
2 changes: 2 additions & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ func RegisterRoutes(m *macaron.Macaron) {
})
// ***** END: User *****

m.Get("/avatar/:email", user.AvatarByEmail)

adminReq := context.Toggle(&context.ToggleOptions{SignInRequired: true, AdminRequired: true})

// ***** START: Admin *****
Expand Down
23 changes: 23 additions & 0 deletions routers/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
package user

import (
"encoding/base64"
"errors"
"strconv"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
)
Expand Down Expand Up @@ -41,3 +44,23 @@ func Avatar(ctx *context.Context) {

ctx.Redirect(user.RealSizedAvatarLink(size))
}

// AvatarByEmail redirects the browser to the appropriate Avatar link
func AvatarByEmail(ctx *context.Context) {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
email := ctx.Params(":email")
addr, err := base64.RawURLEncoding.DecodeString(email)
email = string(addr)
if err != nil {
ctx.ServerError("invalid email address", err)
return
} else if email == "" {
ctx.ServerError("invalid email address", errors.New("email cannot be empty"))
return
}
size := ctx.QueryInt("size")
if size == 0 {
size = base.DefaultAvatarSize
}

ctx.Redirect(base.SizedAvatarLink(email, size))
}