1
0
Fork 0
forked from forgejo/forgejo

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

Why this refactor

The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.

And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:

* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)

And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
This commit is contained in:
wxiaoguang 2021-10-06 07:25:46 +08:00 committed by GitHub
parent 48c2578bd8
commit f0ba87fda8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 274 additions and 300 deletions

View file

@ -11,7 +11,6 @@ import (
"mime/multipart"
"net/http"
"net/url"
"strings"
"testing"
"code.gitea.io/gitea/models"
@ -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.
})
}