Skip to content

Avatar refactor, move avatar code from models to models.avatars, remove duplicated code #17123

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

Merged
merged 26 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cc8a453
avatar-refactor
wxiaoguang Sep 22, 2021
c258ca3
fix unit test
wxiaoguang Sep 22, 2021
fa167dd
fix avatar link
wxiaoguang Sep 22, 2021
bb53198
fix unit test
wxiaoguang Sep 22, 2021
a74873a
optimize http cache header
wxiaoguang Sep 23, 2021
239f5b8
fix avatar size
wxiaoguang Sep 23, 2021
758b2a6
fix avatar size
wxiaoguang Sep 23, 2021
4ec07df
avatar-refactor
wxiaoguang Sep 23, 2021
d4e9afb
fix unit test
wxiaoguang Sep 23, 2021
9eb3043
fix unit test
wxiaoguang Sep 23, 2021
60d0dfd
fix appsuburl
wxiaoguang Sep 23, 2021
851167a
fix unit test
wxiaoguang Sep 23, 2021
3641257
Merge remote-tracking branch 'go-gitea/main' into avatar-refactor
wxiaoguang Sep 23, 2021
91151ed
fix merge, set cacheableRedirect cache time to 5m
wxiaoguang Sep 23, 2021
2b39ae5
fmt
wxiaoguang Sep 23, 2021
ca6ec29
Merge remote-tracking branch 'go-gitea/main' into avatar-refactor
wxiaoguang Sep 24, 2021
7c65d1e
Merge branch 'main' into avatar-refactor
6543 Sep 25, 2021
3852ca4
Merge remote-tracking branch 'go-gitea/main' into avatar-refactor
wxiaoguang Sep 27, 2021
ef27c55
clean up
wxiaoguang Sep 30, 2021
bbcf279
Merge branch 'main' into avatar-refactor
wxiaoguang Sep 30, 2021
f594a5f
fix
wxiaoguang Sep 30, 2021
4ab7616
Merge branch 'main' into avatar-refactor
6543 Oct 3, 2021
225c97d
Merge branch 'main' into avatar-refactor
lunny Oct 4, 2021
5053319
Merge branch 'main' into avatar-refactor
6543 Oct 5, 2021
43a757c
Update modules/httpcache/httpcache.go
6543 Oct 5, 2021
9f7baec
Merge branch 'main' into avatar-refactor
6543 Oct 5, 2021
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
11 changes: 2 additions & 9 deletions integrations/user_avatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"mime/multipart"
"net/http"
"net/url"
"strings"
"testing"

"code.gitea.io/gitea/models"
Expand Down Expand Up @@ -75,14 +74,8 @@ func TestUserAvatar(t *testing.T) {
user2 = db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org

req = NewRequest(t, "GET", user2.AvatarLink())
resp := session.MakeRequest(t, req, http.StatusFound)
location := resp.Header().Get("Location")
if !strings.HasPrefix(location, "/avatars") {
assert.Fail(t, "Avatar location is not local: %s", location)
}
req = NewRequest(t, "GET", location)
session.MakeRequest(t, req, http.StatusOK)
_ = session.MakeRequest(t, req, http.StatusOK)

// Can't test if the response matches because the image is regened on upload but checking that this at least doesn't give a 404 should be enough.
// Can't test if the response matches because the image is re-generated on upload but checking that this at least doesn't give a 404 should be enough.
})
}
118 changes: 73 additions & 45 deletions models/avatar.go → models/avatars/avatar.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models
package avatars

import (
"crypto/md5"
"fmt"
"net/url"
"path"
"strconv"
Expand All @@ -19,7 +17,13 @@ import (
"code.gitea.io/gitea/modules/setting"
)

// EmailHash represents a pre-generated hash map
// DefaultAvatarPixelSize is the default size in pixels of a rendered avatar
const DefaultAvatarPixelSize = 28

// AvatarRenderedSizeFactor is the factor by which the default size is increased for finer rendering
const AvatarRenderedSizeFactor = 4

// EmailHash represents a pre-generated hash map (mainly used by LibravatarURL, it queries email server's DNS records)
type EmailHash struct {
Hash string `xorm:"pk varchar(32)"`
Email string `xorm:"UNIQUE NOT NULL"`
Expand All @@ -41,18 +45,7 @@ func DefaultAvatarLink() string {
return u.String()
}

// DefaultAvatarSize is a sentinel value for the default avatar size, as
// determined by the avatar-hosting service.
const DefaultAvatarSize = -1

// DefaultAvatarPixelSize is the default size in pixels of a rendered avatar
const DefaultAvatarPixelSize = 28

// AvatarRenderedSizeFactor is the factor by which the default size is increased for finer rendering
const AvatarRenderedSizeFactor = 4

// HashEmail hashes email address to MD5 string.
// https://en.gravatar.com/site/implement/hash/
// HashEmail hashes email address to MD5 string. https://en.gravatar.com/site/implement/hash/
func HashEmail(email string) string {
return base.EncodeMD5(strings.ToLower(strings.TrimSpace(email)))
}
Expand All @@ -69,8 +62,8 @@ func GetEmailForHash(md5Sum string) (string, error) {
})
}

// LibravatarURL returns the URL for the given email. This function should only
// be called if a federated avatar service is enabled.
// LibravatarURL returns the URL for the given email. Slow due to the DNS lookup.
// This function should only be called if a federated avatar service is enabled.
func LibravatarURL(email string) (*url.URL, error) {
urlStr, err := setting.LibravatarService.FromEmail(email)
if err != nil {
Expand All @@ -85,14 +78,15 @@ func LibravatarURL(email string) (*url.URL, error) {
return u, nil
}

// HashedAvatarLink returns an avatar link for a provided email
func HashedAvatarLink(email string, size int) string {
// saveEmailHash returns an avatar link for a provided email,
// the email and hash are saved into database, which will be used by GetEmailForHash later
func saveEmailHash(email string) string {
lowerEmail := strings.ToLower(strings.TrimSpace(email))
sum := fmt.Sprintf("%x", md5.Sum([]byte(lowerEmail)))
_, _ = cache.GetString("Avatar:"+sum, func() (string, error) {
emailHash := HashEmail(lowerEmail)
_, _ = cache.GetString("Avatar:"+emailHash, func() (string, error) {
emailHash := &EmailHash{
Email: lowerEmail,
Hash: sum,
Hash: emailHash,
}
// OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors
if err := db.WithTx(func(ctx *db.Context) error {
Expand All @@ -109,39 +103,73 @@ func HashedAvatarLink(email string, size int) string {
}
return lowerEmail, nil
})
if size > 0 {
return setting.AppSubURL + "/avatar/" + url.PathEscape(sum) + "?size=" + strconv.Itoa(size)
return emailHash
}

// GenerateUserAvatarFastLink returns a fast link (302) to the user's avatar: "/user/avatar/${User.Name}/${size}"
func GenerateUserAvatarFastLink(userName string, size int) string {
if size < 0 {
size = 0
}
return setting.AppSubURL + "/avatar/" + url.PathEscape(sum)
//AppSubURL: It is either "" or starts with '/' and ends without '/', such as '/{subpath}'. question: should we use AppURL or StaticURLPrefix?
return setting.AppSubURL + "/user/avatar/" + userName + "/" + strconv.Itoa(size)
}

// MakeFinalAvatarURL constructs the final avatar URL string
func MakeFinalAvatarURL(u *url.URL, size int) string {
vals := u.Query()
vals.Set("d", "identicon")
if size != DefaultAvatarSize {
vals.Set("s", strconv.Itoa(size))
// GenerateUserAvatarImageLink returns a link for `User.Avatar` image file: "/avatars/${User.Avatar}"
func GenerateUserAvatarImageLink(userAvatar string, size int) string {
if size > 0 {
return setting.AppSubURL + "/avatars/" + userAvatar + "?size=" + strconv.Itoa(size)
}
u.RawQuery = vals.Encode()
return u.String()
return setting.AppSubURL + "/avatars/" + userAvatar
}

// SizedAvatarLink returns a sized link to the avatar for the given email address.
func SizedAvatarLink(email string, size int) string {
// generateEmailAvatarLink returns a email avatar link.
// if final is true, it may use a slow path (eg: query DNS).
// if final is false, it always uses a fast path.
func generateEmailAvatarLink(email string, size int, final bool) string {
email = strings.TrimSpace(email)
if email == "" {
return DefaultAvatarLink()
}

var avatarURL *url.URL
var err error

if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
// This is the slow path that would need to call LibravatarURL() which
// does DNS lookups. Avoid it by issuing a redirect so we don't block
// the template render with network requests.
return HashedAvatarLink(email, size)
emailHash := saveEmailHash(email)
if final {
if avatarURL, err = LibravatarURL(email); err != nil {
return DefaultAvatarLink()
}
} else {
if size > 0 {
return setting.AppSubURL + "/avatar/" + emailHash + "?size=" + strconv.Itoa(size)
}
return setting.AppSubURL + "/avatar/" + emailHash
}
} else if !setting.DisableGravatar {
// copy GravatarSourceURL, because we will modify its Path.
copyOfGravatarSourceURL := *setting.GravatarSourceURL
avatarURL = &copyOfGravatarSourceURL
avatarURLDummy := *setting.GravatarSourceURL // copy GravatarSourceURL, because we will modify its Path.
avatarURL = &avatarURLDummy
avatarURL.Path = path.Join(avatarURL.Path, HashEmail(email))
} else {
return DefaultAvatarLink()
}

return MakeFinalAvatarURL(avatarURL, size)
urlQuery := avatarURL.Query()
urlQuery.Set("d", "identicon")
if size > 0 {
urlQuery.Set("s", strconv.Itoa(size))
}
avatarURL.RawQuery = urlQuery.Encode()
return avatarURL.String()
}

//GenerateEmailAvatarFastLink returns a avatar link (fast, the link may be a delegated one: "/avatar/${hash}")
func GenerateEmailAvatarFastLink(email string, size int) string {
return generateEmailAvatarLink(email, size, false)
}

//GenerateEmailAvatarFinalLink returns a avatar final link (maybe slow)
func GenerateEmailAvatarFinalLink(email string, size int) string {
return generateEmailAvatarLink(email, size, true)
}
6 changes: 3 additions & 3 deletions models/avatar_test.go → models/avatars/avatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models
package avatars

import (
"net/url"
Expand Down Expand Up @@ -44,11 +44,11 @@ func TestSizedAvatarLink(t *testing.T) {

disableGravatar()
assert.Equal(t, "/testsuburl/assets/img/avatar_default.png",
SizedAvatarLink("gitea@example.com", 100))
GenerateEmailAvatarFastLink("gitea@example.com", 100))

enableGravatar(t)
assert.Equal(t,
"https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100",
SizedAvatarLink("gitea@example.com", 100),
GenerateEmailAvatarFastLink("gitea@example.com", 100),
)
}
65 changes: 22 additions & 43 deletions models/user_avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"fmt"
"image/png"
"io"
"strconv"
"strings"

"code.gitea.io/gitea/models/avatars"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/avatar"
"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -40,7 +39,7 @@ func (u *User) generateRandomAvatar(e db.Engine) error {
return fmt.Errorf("RandomImage: %v", err)
}

u.Avatar = HashEmail(seed)
u.Avatar = avatars.HashEmail(seed)

// Don't share the images so that we can delete them easily
if err := storage.SaveFrom(storage.Avatars, u.CustomAvatarRelativePath(), func(w io.Writer) error {
Expand All @@ -60,61 +59,41 @@ func (u *User) generateRandomAvatar(e db.Engine) error {
return nil
}

// SizedRelAvatarLink returns a link to the user's avatar via
// the local explore page. Function returns immediately.
// When applicable, the link is for an avatar of the indicated size (in pixels).
func (u *User) SizedRelAvatarLink(size int) string {
return setting.AppSubURL + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size)
}

// RealSizedAvatarLink returns a link to the user's avatar. When
// applicable, the link is for an avatar of the indicated size (in pixels).
//
// This function make take time to return when federated avatars
// are in use, due to a DNS lookup need
//
func (u *User) RealSizedAvatarLink(size int) string {
// AvatarLinkWithSize returns a link to the user's avatar with size. size <= 0 means default size
func (u *User) AvatarLinkWithSize(size int) string {
if u.ID == -1 {
return DefaultAvatarLink()
// ghost user
return avatars.DefaultAvatarLink()
}

useLocalAvatar := false
autoGenerateAvatar := false

switch {
case u.UseCustomAvatar:
if u.Avatar == "" {
return DefaultAvatarLink()
}
if size > 0 {
return setting.AppSubURL + "/avatars/" + u.Avatar + "?size=" + strconv.Itoa(size)
}
return setting.AppSubURL + "/avatars/" + u.Avatar
useLocalAvatar = true
case setting.DisableGravatar, setting.OfflineMode:
if u.Avatar == "" {
useLocalAvatar = true
autoGenerateAvatar = true
}

if useLocalAvatar {
if u.Avatar == "" && autoGenerateAvatar {
if err := u.GenerateRandomAvatar(); err != nil {
log.Error("GenerateRandomAvatar: %v", err)
}
}
if size > 0 {
return setting.AppSubURL + "/avatars/" + u.Avatar + "?size=" + strconv.Itoa(size)
if u.Avatar == "" {
return avatars.DefaultAvatarLink()
}
return setting.AppSubURL + "/avatars/" + u.Avatar
return avatars.GenerateUserAvatarImageLink(u.Avatar, size)
}
return SizedAvatarLink(u.AvatarEmail, size)
return avatars.GenerateEmailAvatarFastLink(u.AvatarEmail, size)
}

// RelAvatarLink returns a relative link to the user's avatar. The link
// may either be a sub-URL to this site, or a full URL to an external avatar
// service.
func (u *User) RelAvatarLink() string {
return u.SizedRelAvatarLink(DefaultAvatarSize)
}

// AvatarLink returns user avatar absolute link.
// AvatarLink returns a avatar link with default size
func (u *User) AvatarLink() string {
link := u.RelAvatarLink()
if link[0] == '/' && link[1] != '/' {
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL)[1:]
}
return link
return u.AvatarLinkWithSize(0)
}

// UploadAvatar saves custom avatar for user.
Expand Down
19 changes: 12 additions & 7 deletions modules/httpcache/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ import (
"code.gitea.io/gitea/modules/setting"
)

// GetCacheControl returns a suitable "Cache-Control" header value
func GetCacheControl() string {
if !setting.IsProd() {
return "no-store"
// AddCacheControlToHeader adds suitable cache-control headers to response
func AddCacheControlToHeader(h http.Header, d time.Duration) {
if setting.IsProd() {
h.Set("Cache-Control", "private, max-age="+strconv.Itoa(int(d.Seconds())))
} else {
h.Set("Cache-Control", "no-store")
// to remind users they are using non-prod setting.
// some users may be confused by "Cache-Control: no-store" in their setup if they did wrong to `RUN_MODE` in `app.ini`.
h.Add("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
h.Add("X-Gitea-Debug", "CacheControl=not-prod")
}
return "private, max-age=" + strconv.FormatInt(int64(setting.StaticCacheTime.Seconds()), 10)
}

// generateETag generates an ETag based on size, filename and file modification time
Expand All @@ -32,7 +37,7 @@ func generateETag(fi os.FileInfo) string {

// HandleTimeCache handles time-based caching for a HTTP request
func HandleTimeCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (handled bool) {
w.Header().Set("Cache-Control", GetCacheControl())
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)

ifModifiedSince := req.Header.Get("If-Modified-Since")
if ifModifiedSince != "" {
Expand Down Expand Up @@ -63,7 +68,7 @@ func HandleGenericETagCache(req *http.Request, w http.ResponseWriter, etag strin
return true
}
}
w.Header().Set("Cache-Control", GetCacheControl())
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
return false
}

Expand Down
Loading