mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Repository avatar fallback configuration (#7087)
* Only show repository avatar in list when one was selected Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds fallback configuration option for repository avatar Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Implements repository avatar fallback Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds admin task for deleting generated repository avatars Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Solve linting issues Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Save avatar before updating database * Linting * Update models/repo.go Co-Authored-By: zeripath <art27@cantab.net>
This commit is contained in:
		
				
					committed by
					
						
						Lunny Xiao
					
				
			
			
				
	
			
			
			
						parent
						
							356854fc5f
						
					
				
				
					commit
					8eba27c792
				
			@@ -2528,17 +2528,78 @@ func (repo *Repository) CustomAvatarPath() string {
 | 
			
		||||
	return filepath.Join(setting.RepositoryAvatarUploadPath, repo.Avatar)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RelAvatarLink returns a relative link to the user's avatar.
 | 
			
		||||
// The link a sub-URL to this site
 | 
			
		||||
// Since Gravatar support not needed here - just check for image path.
 | 
			
		||||
// GenerateRandomAvatar generates a random avatar for repository.
 | 
			
		||||
func (repo *Repository) GenerateRandomAvatar() error {
 | 
			
		||||
	return repo.generateRandomAvatar(x)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (repo *Repository) generateRandomAvatar(e Engine) error {
 | 
			
		||||
	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 = os.MkdirAll(filepath.Dir(repo.CustomAvatarPath()), os.ModePerm); err != nil {
 | 
			
		||||
		return fmt.Errorf("MkdirAll: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	fw, err := os.Create(repo.CustomAvatarPath())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("Create: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer fw.Close()
 | 
			
		||||
 | 
			
		||||
	if err = png.Encode(fw, img); err != nil {
 | 
			
		||||
		return fmt.Errorf("Encode: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	log.Info("New random avatar created for repository: %d", repo.ID)
 | 
			
		||||
 | 
			
		||||
	if _, err := e.ID(repo.ID).Cols("avatar").NoAutoTime().Update(repo); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RemoveRandomAvatars removes the randomly generated avatars that were created for repositories
 | 
			
		||||
func RemoveRandomAvatars() error {
 | 
			
		||||
	var (
 | 
			
		||||
		err error
 | 
			
		||||
	)
 | 
			
		||||
	err = x.
 | 
			
		||||
		Where("id > 0").BufferSize(setting.IterateBufferSize).
 | 
			
		||||
		Iterate(new(Repository),
 | 
			
		||||
			func(idx int, bean interface{}) error {
 | 
			
		||||
				repository := bean.(*Repository)
 | 
			
		||||
				stringifiedID := strconv.FormatInt(repository.ID, 10)
 | 
			
		||||
				if repository.Avatar == stringifiedID {
 | 
			
		||||
					return repository.DeleteAvatar()
 | 
			
		||||
				}
 | 
			
		||||
				return nil
 | 
			
		||||
			})
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RelAvatarLink returns a relative link to the repository's avatar.
 | 
			
		||||
func (repo *Repository) RelAvatarLink() string {
 | 
			
		||||
 | 
			
		||||
	// If no avatar - path is empty
 | 
			
		||||
	avatarPath := repo.CustomAvatarPath()
 | 
			
		||||
	if len(avatarPath) <= 0 {
 | 
			
		||||
		return ""
 | 
			
		||||
	}
 | 
			
		||||
	if !com.IsFile(avatarPath) {
 | 
			
		||||
		return ""
 | 
			
		||||
	if len(avatarPath) <= 0 || !com.IsFile(avatarPath) {
 | 
			
		||||
		switch mode := setting.RepositoryAvatarFallback; mode {
 | 
			
		||||
		case "image":
 | 
			
		||||
			return setting.RepositoryAvatarFallbackImage
 | 
			
		||||
		case "random":
 | 
			
		||||
			if err := repo.GenerateRandomAvatar(); err != nil {
 | 
			
		||||
				log.Error("GenerateRandomAvatar: %v", err)
 | 
			
		||||
			}
 | 
			
		||||
		default:
 | 
			
		||||
			// default behaviour: do not display avatar
 | 
			
		||||
			return ""
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return setting.AppSubURL + "/repo-avatars/" + repo.Avatar
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user