In many cases user avatar link should be an absolute URL with http host (#17420)

This commit is contained in:
wxiaoguang 2021-10-25 13:01:16 +08:00 committed by GitHub
parent 3676fafdac
commit 7c951fdd4a
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 3 deletions

@ -73,7 +73,7 @@ func TestUserAvatar(t *testing.T) {
user2 = db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org user2 = db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org
req = NewRequest(t, "GET", user2.AvatarLink()) req = NewRequest(t, "GET", user2.AvatarLinkWithSize(0))
_ = session.MakeRequest(t, req, http.StatusOK) _ = session.MakeRequest(t, req, http.StatusOK)
// 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. // 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.

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"image/png" "image/png"
"io" "io"
"strings"
"code.gitea.io/gitea/models/avatars" "code.gitea.io/gitea/models/avatars"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
@ -91,9 +92,13 @@ func (u *User) AvatarLinkWithSize(size int) string {
return avatars.GenerateEmailAvatarFastLink(u.AvatarEmail, size) return avatars.GenerateEmailAvatarFastLink(u.AvatarEmail, size)
} }
// AvatarLink returns a avatar link with default size // AvatarLink returns the full avatar link with http host
func (u *User) AvatarLink() string { func (u *User) AvatarLink() string {
return u.AvatarLinkWithSize(0) link := u.AvatarLinkWithSize(0)
if !strings.HasPrefix(link, "//") && !strings.Contains(link, "://") {
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL+"/")
}
return link
} }
// UploadAvatar saves custom avatar for user. // UploadAvatar saves custom avatar for user.

@ -21,6 +21,7 @@ func TestUser_ToUser(t *testing.T) {
apiUser := toUser(user1, true, true) apiUser := toUser(user1, true, true)
assert.True(t, apiUser.IsAdmin) assert.True(t, apiUser.IsAdmin)
assert.Contains(t, apiUser.AvatarURL, "://")
user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User)