2021-12-10 10:27:50 +09:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-05-20 23:08:52 +09:00
|
|
|
"context"
|
2021-12-10 10:27:50 +09:00
|
|
|
"fmt"
|
|
|
|
"image/png"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/modules/avatar"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CustomAvatarRelativePath returns repository custom avatar file path.
|
|
|
|
func (repo *Repository) CustomAvatarRelativePath() string {
|
|
|
|
return repo.Avatar
|
|
|
|
}
|
|
|
|
|
|
|
|
// RelAvatarLink returns a relative link to the repository's avatar.
|
|
|
|
func (repo *Repository) RelAvatarLink() string {
|
2022-05-20 23:08:52 +09:00
|
|
|
return repo.relAvatarLink(db.DefaultContext)
|
2021-12-10 10:27:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateRandomAvatar generates a random avatar for repository.
|
2022-05-20 23:08:52 +09:00
|
|
|
func generateRandomAvatar(ctx context.Context, repo *Repository) error {
|
2021-12-10 10:27:50 +09:00
|
|
|
idToString := fmt.Sprintf("%d", repo.ID)
|
|
|
|
|
|
|
|
seed := idToString
|
|
|
|
img, err := avatar.RandomImage([]byte(seed))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("RandomImage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.Avatar = idToString
|
|
|
|
|
|
|
|
if err := storage.SaveFrom(storage.RepoAvatars, repo.CustomAvatarRelativePath(), func(w io.Writer) error {
|
|
|
|
if err := png.Encode(w, img); err != nil {
|
|
|
|
log.Error("Encode: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("Failed to create dir %s: %v", repo.CustomAvatarRelativePath(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("New random avatar created for repository: %d", repo.ID)
|
|
|
|
|
2022-05-20 23:08:52 +09:00
|
|
|
if _, err := db.GetEngine(ctx).ID(repo.ID).Cols("avatar").NoAutoTime().Update(repo); err != nil {
|
2021-12-10 10:27:50 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:08:52 +09:00
|
|
|
func (repo *Repository) relAvatarLink(ctx context.Context) string {
|
2021-12-10 10:27:50 +09:00
|
|
|
// If no avatar - path is empty
|
|
|
|
avatarPath := repo.CustomAvatarRelativePath()
|
|
|
|
if len(avatarPath) == 0 {
|
|
|
|
switch mode := setting.RepoAvatar.Fallback; mode {
|
|
|
|
case "image":
|
|
|
|
return setting.RepoAvatar.FallbackImage
|
|
|
|
case "random":
|
2022-05-20 23:08:52 +09:00
|
|
|
if err := generateRandomAvatar(ctx, repo); err != nil {
|
2021-12-10 10:27:50 +09:00
|
|
|
log.Error("generateRandomAvatar: %v", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// default behaviour: do not display avatar
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return setting.AppSubURL + "/repo-avatars/" + url.PathEscape(repo.Avatar)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AvatarLink returns a link to the repository's avatar.
|
|
|
|
func (repo *Repository) AvatarLink() string {
|
2022-05-20 23:08:52 +09:00
|
|
|
return repo.avatarLink(db.DefaultContext)
|
2021-12-10 10:27:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// avatarLink returns user avatar absolute link.
|
2022-05-20 23:08:52 +09:00
|
|
|
func (repo *Repository) avatarLink(ctx context.Context) string {
|
|
|
|
link := repo.relAvatarLink(ctx)
|
2021-12-10 10:27:50 +09:00
|
|
|
// we only prepend our AppURL to our known (relative, internal) avatar link to get an absolute URL
|
|
|
|
if strings.HasPrefix(link, "/") && !strings.HasPrefix(link, "//") {
|
|
|
|
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL)[1:]
|
|
|
|
}
|
|
|
|
// otherwise, return the link as it is
|
|
|
|
return link
|
|
|
|
}
|