Refactor avatar package, support default avatar fallback#36788
Refactor avatar package, support default avatar fallback#36788wxiaoguang merged 3 commits intogo-gitea:mainfrom
Conversation
wxiaoguang
commented
Mar 1, 2026
- Fix Avatars broken (404) after turning on offline mode #34715
4fbd40b to
0384531
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #34715 where avatars return 404 errors when offline mode is enabled (because Gravatar URLs can't be fetched). The core fix is to return a randomly-generated identicon image instead of a 404 when a requested avatar file doesn't exist in storage. The PR also refactors the avatar serving infrastructure, simplifies the identicon.New() and RandomImage*() APIs by removing error returns, and consolidates HTTP cache helper functions into clearer public/private variants.
Changes:
- When a user/repo avatar is not found in storage, instead of returning HTTP 404, the server now generates and serves a deterministic random identicon (seeded by the avatar path) with proper public cache-control headers.
- The
HandleGenericETagCache/HandleGenericETagTimeCachefunctions are replaced byHandleGenericETagPublicCache/HandleGenericETagPrivateCache, which now always require alastModified *time.Timeparameter, and are used consistently across file/LFS/attachment serving endpoints. - The
RandomImageSize,RandomImage, andidenticon.New()APIs are simplified to never return errors (the identicon package now clamps size values instead of returning an error for out-of-range inputs).
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
routers/web/base.go |
Core fix: serves a fallback random avatar instead of 404 when avatar is not found |
modules/avatar/avatar.go |
Simplifies RandomImageSize/RandomImage to return image.Image (no error) |
modules/avatar/identicon/identicon.go |
New() no longer returns error; clamps size; uses default dark colors if none provided |
modules/avatar/avatar_test.go |
Removes old error-checking tests; adds benchmark |
modules/httpcache/httpcache.go |
Removes HandleGenericETagCache; adds HandleGenericETagPublicCache and HandleGenericETagPrivateCache |
modules/httpcache/httpcache_test.go |
Rewrites test to use table-driven approach for new private cache function |
modules/assetfs/embed.go |
Exports GetExecutableModTime for use in avatar fallback handler |
models/user/avatar.go |
Simplifies error handling with new infallible RandomImage API |
models/repo/avatar.go |
Simplifies error handling; fixes error message capitalization |
routers/common/serve.go |
Uses HandleGenericETagPrivateCache instead of removed function |
routers/web/repo/download.go |
Uses HandleGenericETagPrivateCache for blob/LFS caching |
routers/web/repo/attachment.go |
Uses HandleGenericETagPrivateCache for attachment caching |
routers/api/v1/repo/file.go |
Uses HandleGenericETagPrivateCache for raw file/LFS API caching |
tests/integration/user_avatar_test.go |
Extends test to verify avatar content, redirect behavior, and the new fallback response |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
d81e3d2 to
0d4117f
Compare
* giteaofficial/main: (21 commits) Enable docker layer caching for `dry-run` and `nightly` container builds (go-gitea#36738) Add admin badge to navbar avatar (go-gitea#36790) WorkflowDispatch api optionally return runid (go-gitea#36706) upgrade minimatch (go-gitea#36760) Add `never` option to `PUBLIC_URL_DETECTION` configuration (go-gitea#36785) Refactor avatar package, support default avatar fallback (go-gitea#36788) Mark unused&immature activitypub as "not implemented" (go-gitea#36789) Add “Copy Source” to markup comment menu (go-gitea#36726) Update Nix flake (go-gitea#36787) Implements OIDC RP-Initiated Logout (go-gitea#36724) Fix README symlink resolution in subdirectories like .github (go-gitea#36775) [skip ci] Updated translations via Crowdin Correct spelling (go-gitea#36783) refactor: replace legacy tw-flex utility classes with flex-text-block/inline (go-gitea#36778) Fix `no-content` message not rendering after comment edit (go-gitea#36733) Fix typos and grammar in English locale (go-gitea#36751) Move Fomantic dropdown CSS to custom module (go-gitea#36530) Use "Enable Gravatar" but not "Disable" (go-gitea#36771) feat: add branch_count to repository API (go-gitea#35351) (go-gitea#36743) Deprecate RenderWithErr (go-gitea#36769) ...