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 1 commit
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
Prev Previous commit
Next Next commit
As per @lafriks and @guillep2k
Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Mar 8, 2020
commit c6cca50ba908f6243c120b33ec255f6b65ef73ef
13 changes: 5 additions & 8 deletions models/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ import (
"code.gitea.io/gitea/modules/setting"
)

var ()

// EmailHash represents a pre-generated hash map
type EmailHash struct {
ID int64 `xorm:"pk autoincr"`
Email string `xorm:"UNIQUE NOT NULL"`
MD5Sum string `xorm:"md5_sum UNIQUE NOT NULL"`
Hash string `xorm:"pk hash UNIQUE NOT NULL"`
zeripath marked this conversation as resolved.
Show resolved Hide resolved
Email string `xorm:"UNIQUE NOT NULL"`
}

// GetEmailForHash converts a provided md5sum to the email
func GetEmailForHash(md5Sum string) (string, error) {
return cache.GetString("Avatar:"+md5Sum, func() (string, error) {
emailHash := EmailHash{
MD5Sum: strings.ToLower(strings.TrimSpace(md5Sum)),
Hash: strings.ToLower(strings.TrimSpace(md5Sum)),
}

_, err := x.Get(&emailHash)
Expand All @@ -41,8 +38,8 @@ func AvatarLink(email string) string {
sum := fmt.Sprintf("%x", md5.Sum([]byte(lowerEmail)))
_, _ = cache.GetString("Avatar:"+sum, func() (string, error) {
emailHash := &EmailHash{
Email: lowerEmail,
MD5Sum: sum,
Email: lowerEmail,
Hash: sum,
}
_, _ = x.Insert(emailHash)
return lowerEmail, nil
Expand Down
2 changes: 1 addition & 1 deletion routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func RegisterRoutes(m *macaron.Macaron) {
})
// ***** END: User *****

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

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

Expand Down
4 changes: 2 additions & 2 deletions routers/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func Avatar(ctx *context.Context) {
ctx.Redirect(user.RealSizedAvatarLink(size))
}

// AvatarByEmail redirects the browser to the appropriate Avatar link
func AvatarByEmail(ctx *context.Context) {
// AvatarByEmailHash redirects the browser to the appropriate Avatar link
func AvatarByEmailHash(ctx *context.Context) {
hash := ctx.Params(":hash")
if len(hash) == 0 {
ctx.ServerError("invalid avatar hash", errors.New("hash cannot be empty"))
Expand Down