2020-11-13 20:51:07 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-11-13 20:51:07 +08:00
|
|
|
|
2021-06-09 07:33:54 +08:00
|
|
|
package web
|
2020-11-13 20:51:07 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2026-03-01 21:32:35 +08:00
|
|
|
"image/png"
|
2020-11-13 20:51:07 +08:00
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-03-01 21:32:35 +08:00
|
|
|
"code.gitea.io/gitea/modules/assetfs"
|
|
|
|
|
"code.gitea.io/gitea/modules/avatar"
|
2020-11-17 23:44:52 +01:00
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
2020-11-13 20:51:07 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2023-03-08 20:17:39 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-01-20 19:41:25 +08:00
|
|
|
"code.gitea.io/gitea/modules/web/routing"
|
2020-11-13 20:51:07 +08:00
|
|
|
)
|
|
|
|
|
|
2025-03-13 07:04:50 +08:00
|
|
|
func avatarStorageHandler(storageSetting *setting.Storage, prefix string, objStore storage.ObjectStorage) http.HandlerFunc {
|
2022-03-22 21:02:26 +00:00
|
|
|
prefix = strings.Trim(prefix, "/")
|
2025-03-13 07:04:50 +08:00
|
|
|
funcInfo := routing.GetFuncInfo(avatarStorageHandler, prefix)
|
2026-03-01 21:32:35 +08:00
|
|
|
exeModTime := assetfs.GetExecutableModTime()
|
|
|
|
|
fallbackEtag := fmt.Sprintf(`"avatar-%s"`, exeModTime.Format("20060102150405"))
|
2020-11-13 20:51:07 +08:00
|
|
|
|
2026-03-01 21:32:35 +08:00
|
|
|
handleError := func(w http.ResponseWriter, req *http.Request, avatarPath string, err error) bool {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2020-11-17 23:44:52 +01:00
|
|
|
|
2026-03-01 21:32:35 +08:00
|
|
|
if errors.Is(err, os.ErrNotExist) || errors.Is(err, util.ErrNotExist) {
|
|
|
|
|
// if avatar doesn't exist, generate a random one and serve it with proper cache control headers
|
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
|
if !httpcache.HandleGenericETagPublicCache(req, w, fallbackEtag, &exeModTime) {
|
|
|
|
|
if req.Method == http.MethodGet {
|
|
|
|
|
img := avatar.RandomImageWithSize(96, []byte(avatarPath))
|
|
|
|
|
_ = png.Encode(w, img)
|
|
|
|
|
} // else: for HEAD request, just return the headers without body
|
2020-11-13 20:51:07 +08:00
|
|
|
}
|
2026-03-01 21:32:35 +08:00
|
|
|
} else {
|
|
|
|
|
// for internal errors, log the error and return 500
|
|
|
|
|
log.Error("Error when serving avatar %s: %s", req.URL.Path, err)
|
|
|
|
|
http.Error(w, "unable to serve avatar image", http.StatusInternalServerError)
|
2025-03-13 07:04:50 +08:00
|
|
|
}
|
2026-03-01 21:32:35 +08:00
|
|
|
return true
|
2020-11-13 20:51:07 +08:00
|
|
|
}
|
2023-10-06 13:23:14 +00:00
|
|
|
|
2025-03-13 07:04:50 +08:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2026-03-01 21:32:35 +08:00
|
|
|
defer routing.RecordFuncInfo(req.Context(), funcInfo)()
|
2023-10-06 13:23:14 +00:00
|
|
|
|
2026-03-01 21:32:35 +08:00
|
|
|
avatarPath, ok := strings.CutPrefix(req.URL.Path, "/"+prefix+"/")
|
|
|
|
|
if !ok {
|
|
|
|
|
http.Error(w, "invalid avatar path", http.StatusBadRequest)
|
2023-10-06 13:23:14 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-03-01 21:32:35 +08:00
|
|
|
avatarPath = util.PathJoinRelX(avatarPath)
|
|
|
|
|
if avatarPath == "" || avatarPath == "." {
|
|
|
|
|
http.Error(w, "not found", http.StatusNotFound)
|
2023-10-06 13:23:14 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 21:32:35 +08:00
|
|
|
if storageSetting.ServeDirect() {
|
|
|
|
|
// Old logic: no check for existence by Stat, so old code's "errors.Is(err, os.ErrNotExist)" didn't work.
|
|
|
|
|
// So in theory, it doesn't work with the non-existing avatar fallback, it just gets the URL and redirects to it.
|
|
|
|
|
// Checking "stat" requires one more request to the storage, which is inefficient.
|
|
|
|
|
// Workaround: disable "SERVE_DIRECT". Leave the problem to the future.
|
2026-03-22 05:26:13 +01:00
|
|
|
u, err := objStore.ServeDirectURL(avatarPath, path.Base(avatarPath), req.Method, nil)
|
2026-03-01 21:32:35 +08:00
|
|
|
if handleError(w, req, avatarPath, err) {
|
2023-10-06 13:23:14 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-03-01 21:32:35 +08:00
|
|
|
http.Redirect(w, req, u.String(), http.StatusTemporaryRedirect)
|
2023-10-06 13:23:14 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 21:32:35 +08:00
|
|
|
fr, err := objStore.Open(avatarPath)
|
|
|
|
|
if handleError(w, req, avatarPath, err) {
|
2023-10-06 13:23:14 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer fr.Close()
|
2025-03-13 07:04:50 +08:00
|
|
|
|
2026-03-01 21:32:35 +08:00
|
|
|
fi, err := fr.Stat()
|
|
|
|
|
if handleError(w, req, avatarPath, err) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 07:04:50 +08:00
|
|
|
httpcache.SetCacheControlInHeader(w.Header(), httpcache.CacheControlForPublicStatic())
|
2026-03-01 21:32:35 +08:00
|
|
|
http.ServeContent(w, req, path.Base(avatarPath), fi.ModTime(), fr)
|
2025-03-13 07:04:50 +08:00
|
|
|
}
|
2020-11-13 20:51:07 +08:00
|
|
|
}
|