gitforge/tests/integration/doctor_avatars_test.go
Antonin Delpeuch 0a57672544 feat: serve downsized versions of avatars (#11242)
Fixes #2325.

This introduces a way to download downsized versions of the user and repository avatars:
* `/avatars/123abcd` still serves the full-size avatar
* `/avatars/123abcd?size=64` serves it at size 64x64 px

Those downsized versions are computed on demand when requested for the first time and cached. The caching is done in a storage location configurable in the instance settings, just like the storage locations for the full-sized avatars are. The sizes of the downsized images are restricted to a fixed set of sizes, so that the cache doesn't grow too big. The caching and resizing logic is exposed in a way that could potentially be reused for other types of images (such as user uploads in issue discussions).

Luckily, the Go templates already specify in many places which size those avatars should be rendered, even if this information was only used for external avatar providers (such as Gravatar) until now.

The range of sizes requested by the HTML templates is rather wide: the table below lists all the sizes I could find, and the corresponding size served by the backend with the logic I implemented. The scaling factor of 2 was already used for requesting resized external avatars, and likely exists to make sure that users with display scaling enabled get a sharper picture.

| Size requested in the template | After scaling (x2)  | Size of the image served |
|---------|---------|---------|
| 256 px |  512 px | original (512 px) |
| 140 px | 280 px | original (512 px) |
| 48 px | 96 px | 128 px |
| 40 px | 80 px | 128 px |
| 32 px | 64 px | 64 px |
| 28 px | 56 px | 64 px |
| 24 px | 48 px | 64 px |
| 20 px | 40 px | 64 px |

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11242
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
2026-05-16 12:04:05 +02:00

101 lines
3 KiB
Go

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"bytes"
"fmt"
"image"
"image/png"
"testing"
cmd "forgejo.org/cmd"
"forgejo.org/models/db"
"forgejo.org/models/repo"
"forgejo.org/models/unittest"
"forgejo.org/models/user"
"forgejo.org/modules/setting"
"forgejo.org/modules/storage"
"forgejo.org/modules/test"
"forgejo.org/tests"
"github.com/stretchr/testify/require"
)
func TestPrecomputeUserAvatars(t *testing.T) {
defer tests.PrepareTestEnv(t, 1)()
var err error
// make the maximum uncached image size small, so that our test image is bigger than that
defer test.MockVariableValue(&setting.Avatar.MaxOriginSize, 3)()
ctx := db.DefaultContext
u := unittest.AssertExistsAndLoadBean(t, &user.User{ID: 2})
// generate an avatar for this user
myImage := image.NewRGBA(image.Rect(0, 0, 1024, 1024))
var buff bytes.Buffer
png.Encode(&buff, myImage)
// store the avatar, but only the original (not the resized versions)
avatarPath := "some_id"
storage.Avatars.Save(avatarPath, bytes.NewReader(buff.Bytes()), -1)
u.UseCustomAvatar = true
u.Avatar = avatarPath
err = user.UpdateUserCols(ctx, u, "use_custom_avatar", "avatar")
require.NoError(t, err)
// run the doctor command
err = cmd.RunAvatarResize(ctx, true, false)
require.NoError(t, err)
// the resized version of the avatar is now stored in the cache
_, err = storage.Avatars.Stat(fmt.Sprintf("resized/64/%s", avatarPath))
require.NoError(t, err)
}
func TestPrecomputeRepoAvatars(t *testing.T) {
defer tests.PrepareTestEnv(t, 1)()
var err error
// make the maximum uncached image size small, so that our test image is bigger than that
defer test.MockVariableValue(&setting.Avatar.MaxOriginSize, 3)()
ctx := db.DefaultContext
u := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 2})
// generate an avatar for this repo
myImage := image.NewRGBA(image.Rect(0, 0, 1024, 1024))
var buff bytes.Buffer
png.Encode(&buff, myImage)
// store the avatar, but only the original (not the resized versions)
avatarPath := "some_id"
storage.RepoAvatars.Save(avatarPath, bytes.NewReader(buff.Bytes()), -1)
u.Avatar = avatarPath
err = repo.UpdateRepositoryCols(ctx, u, "avatar")
require.NoError(t, err)
// make sure there is no resized version of the avatar in the storage yet
storage.RepoAvatars.Delete(fmt.Sprintf("resized/64/%s", avatarPath))
_, err = storage.RepoAvatars.Stat(fmt.Sprintf("resized/64/%s", avatarPath))
require.Error(t, err)
// run the doctor command
err = cmd.RunAvatarResize(ctx, false, true)
require.NoError(t, err)
// the resized version of the avatar is now stored in the cache
_, err = storage.RepoAvatars.Stat(fmt.Sprintf("resized/64/%s", avatarPath))
require.NoError(t, err)
}
func TestPrecomputeAvatarsWithoutArgument(t *testing.T) {
defer tests.PrepareTestEnv(t, 1)()
var err error
ctx := db.DefaultContext
// run the doctor command
err = cmd.RunAvatarResize(ctx, false, false)
// there is an error because we didn't specify which avatars to resize
require.Error(t, err)
}