code.gitea.io/gitea@v1.21.7/routers/web/user/avatar.go (about) 1 // Copyright 2019 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package user 5 6 import ( 7 "strings" 8 "time" 9 10 "code.gitea.io/gitea/models/avatars" 11 user_model "code.gitea.io/gitea/models/user" 12 "code.gitea.io/gitea/modules/context" 13 "code.gitea.io/gitea/modules/httpcache" 14 ) 15 16 func cacheableRedirect(ctx *context.Context, location string) { 17 // here we should not use `setting.StaticCacheTime`, it is pretty long (default: 6 hours) 18 // we must make sure the redirection cache time is short enough, otherwise a user won't see the updated avatar in 6 hours 19 // it's OK to make the cache time short, it is only a redirection, and doesn't cost much to make a new request 20 httpcache.SetCacheControlInHeader(ctx.Resp.Header(), 5*time.Minute) 21 ctx.Redirect(location) 22 } 23 24 // AvatarByUserName redirect browser to user avatar of requested size 25 func AvatarByUserName(ctx *context.Context) { 26 userName := ctx.Params(":username") 27 size := int(ctx.ParamsInt64(":size")) 28 29 var user *user_model.User 30 if strings.ToLower(userName) != "ghost" { 31 var err error 32 if user, err = user_model.GetUserByName(ctx, userName); err != nil { 33 if user_model.IsErrUserNotExist(err) { 34 ctx.NotFound("GetUserByName", err) 35 return 36 } 37 ctx.ServerError("Invalid user: "+userName, err) 38 return 39 } 40 } else { 41 user = user_model.NewGhostUser() 42 } 43 44 cacheableRedirect(ctx, user.AvatarLinkWithSize(ctx, size)) 45 } 46 47 // AvatarByEmailHash redirects the browser to the email avatar link 48 func AvatarByEmailHash(ctx *context.Context) { 49 hash := ctx.Params(":hash") 50 email, err := avatars.GetEmailForHash(hash) 51 if err != nil { 52 ctx.ServerError("invalid avatar hash: "+hash, err) 53 return 54 } 55 size := ctx.FormInt("size") 56 cacheableRedirect(ctx, avatars.GenerateEmailAvatarFinalLink(ctx, email, size)) 57 }