2019-09-26 18:21:23 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-09-26 18:21:23 +02:00
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-06 07:25:46 +08:00
|
|
|
"time"
|
2019-09-26 18:21:23 +02:00
|
|
|
|
2021-10-06 07:25:46 +08:00
|
|
|
"code.gitea.io/gitea/models/avatars"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-09-06 23:05:44 +02:00
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2019-09-26 18:21:23 +02:00
|
|
|
)
|
|
|
|
|
|
2021-09-06 23:05:44 +02:00
|
|
|
func cacheableRedirect(ctx *context.Context, location string) {
|
2021-10-06 07:25:46 +08:00
|
|
|
// here we should not use `setting.StaticCacheTime`, it is pretty long (default: 6 hours)
|
|
|
|
|
// we must make sure the redirection cache time is short enough, otherwise a user won't see the updated avatar in 6 hours
|
|
|
|
|
// it's OK to make the cache time short, it is only a redirection, and doesn't cost much to make a new request
|
2025-03-13 07:04:50 +08:00
|
|
|
httpcache.SetCacheControlInHeader(ctx.Resp.Header(), &httpcache.CacheControlOptions{MaxAge: 5 * time.Minute})
|
2021-09-06 23:05:44 +02:00
|
|
|
ctx.Redirect(location)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-30 07:33:50 +08:00
|
|
|
// AvatarByUsernameSize redirect browser to user avatar of requested size
|
|
|
|
|
func AvatarByUsernameSize(ctx *context.Context) {
|
|
|
|
|
username := ctx.PathParam("username")
|
|
|
|
|
user := user_model.GetSystemUserByName(username)
|
|
|
|
|
if user == nil {
|
2021-10-06 07:25:46 +08:00
|
|
|
var err error
|
2025-01-30 07:33:50 +08:00
|
|
|
if user, err = user_model.GetUserByName(ctx, username); err != nil {
|
|
|
|
|
ctx.NotFoundOrServerError("GetUserByName", user_model.IsErrUserNotExist, err)
|
2019-12-29 15:24:50 +01:00
|
|
|
return
|
2019-09-26 18:21:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-10-04 01:21:26 +08:00
|
|
|
cacheableRedirect(ctx, user.AvatarLinkWithSize(ctx, ctx.PathParamInt("size")))
|
2019-09-26 18:21:23 +02:00
|
|
|
}
|
2020-03-27 12:34:39 +00:00
|
|
|
|
2021-10-06 07:25:46 +08:00
|
|
|
// AvatarByEmailHash redirects the browser to the email avatar link
|
2020-03-27 12:34:39 +00:00
|
|
|
func AvatarByEmailHash(ctx *context.Context) {
|
2024-12-24 21:47:45 +08:00
|
|
|
hash := ctx.PathParam("hash")
|
2023-10-14 10:37:24 +02:00
|
|
|
email, err := avatars.GetEmailForHash(ctx, hash)
|
2020-03-27 12:34:39 +00:00
|
|
|
if err != nil {
|
2021-10-06 07:25:46 +08:00
|
|
|
ctx.ServerError("invalid avatar hash: "+hash, err)
|
2020-03-27 12:34:39 +00:00
|
|
|
return
|
|
|
|
|
}
|
2021-07-29 09:42:15 +08:00
|
|
|
size := ctx.FormInt("size")
|
2023-02-15 21:37:34 +08:00
|
|
|
cacheableRedirect(ctx, avatars.GenerateEmailAvatarFinalLink(ctx, email, size))
|
2020-03-27 12:34:39 +00:00
|
|
|
}
|