You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

avatar.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "context"
  7. "fmt"
  8. "image/png"
  9. "io"
  10. "net/url"
  11. "strings"
  12. "code.gitea.io/gitea/models/db"
  13. "code.gitea.io/gitea/modules/avatar"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/storage"
  17. )
  18. // CustomAvatarRelativePath returns repository custom avatar file path.
  19. func (repo *Repository) CustomAvatarRelativePath() string {
  20. return repo.Avatar
  21. }
  22. // RelAvatarLink returns a relative link to the repository's avatar.
  23. func (repo *Repository) RelAvatarLink() string {
  24. return repo.relAvatarLink(db.DefaultContext)
  25. }
  26. // generateRandomAvatar generates a random avatar for repository.
  27. func generateRandomAvatar(ctx context.Context, repo *Repository) error {
  28. idToString := fmt.Sprintf("%d", repo.ID)
  29. seed := idToString
  30. img, err := avatar.RandomImage([]byte(seed))
  31. if err != nil {
  32. return fmt.Errorf("RandomImage: %v", err)
  33. }
  34. repo.Avatar = idToString
  35. if err := storage.SaveFrom(storage.RepoAvatars, repo.CustomAvatarRelativePath(), func(w io.Writer) error {
  36. if err := png.Encode(w, img); err != nil {
  37. log.Error("Encode: %v", err)
  38. }
  39. return err
  40. }); err != nil {
  41. return fmt.Errorf("Failed to create dir %s: %v", repo.CustomAvatarRelativePath(), err)
  42. }
  43. log.Info("New random avatar created for repository: %d", repo.ID)
  44. if _, err := db.GetEngine(ctx).ID(repo.ID).Cols("avatar").NoAutoTime().Update(repo); err != nil {
  45. return err
  46. }
  47. return nil
  48. }
  49. func (repo *Repository) relAvatarLink(ctx context.Context) string {
  50. // If no avatar - path is empty
  51. avatarPath := repo.CustomAvatarRelativePath()
  52. if len(avatarPath) == 0 {
  53. switch mode := setting.RepoAvatar.Fallback; mode {
  54. case "image":
  55. return setting.RepoAvatar.FallbackImage
  56. case "random":
  57. if err := generateRandomAvatar(ctx, repo); err != nil {
  58. log.Error("generateRandomAvatar: %v", err)
  59. }
  60. default:
  61. // default behaviour: do not display avatar
  62. return ""
  63. }
  64. }
  65. return setting.AppSubURL + "/repo-avatars/" + url.PathEscape(repo.Avatar)
  66. }
  67. // AvatarLink returns a link to the repository's avatar.
  68. func (repo *Repository) AvatarLink() string {
  69. return repo.avatarLink(db.DefaultContext)
  70. }
  71. // avatarLink returns user avatar absolute link.
  72. func (repo *Repository) avatarLink(ctx context.Context) string {
  73. link := repo.relAvatarLink(ctx)
  74. // we only prepend our AppURL to our known (relative, internal) avatar link to get an absolute URL
  75. if strings.HasPrefix(link, "/") && !strings.HasPrefix(link, "//") {
  76. return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL)[1:]
  77. }
  78. // otherwise, return the link as it is
  79. return link
  80. }