mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
107 lines
3.6 KiB
Go
107 lines
3.6 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
"github.com/hanzoai/git/modules/avatar"
|
|
"github.com/hanzoai/git/modules/log"
|
|
"github.com/hanzoai/git/modules/storage"
|
|
)
|
|
|
|
// UploadAvatar saves custom avatar for repository.
|
|
// FIXME: split uploads to different subdirs in case we have massive number of repos.
|
|
func UploadAvatar(ctx context.Context, repo *repo_model.Repository, data []byte) error {
|
|
avatarData, err := avatar.ProcessAvatarImage(data)
|
|
if err != nil {
|
|
return fmt.Errorf("UploadAvatar: failed to process repo avatar image: %w", err)
|
|
}
|
|
|
|
newAvatar := avatar.HashAvatar(repo.ID, data)
|
|
if repo.Avatar == newAvatar { // upload the same picture
|
|
return nil
|
|
}
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
oldAvatarPath := repo.CustomAvatarRelativePath()
|
|
|
|
// Users can upload the same image to other repo - prefix it with ID
|
|
// Then repo will be removed - only it avatar file will be removed
|
|
repo.Avatar = newAvatar
|
|
if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "avatar"); err != nil {
|
|
return fmt.Errorf("UploadAvatar: failed to update repository avatar: %w", err)
|
|
}
|
|
|
|
if err := storage.SaveFrom(storage.RepoAvatars, repo.CustomAvatarRelativePath(), func(w io.Writer) error {
|
|
_, err := w.Write(avatarData)
|
|
return err
|
|
}); err != nil {
|
|
return fmt.Errorf("UploadAvatar: failed to save repo avatar %s: %w", newAvatar, err)
|
|
}
|
|
|
|
if len(oldAvatarPath) > 0 {
|
|
if err := storage.RepoAvatars.Delete(oldAvatarPath); err != nil {
|
|
return fmt.Errorf("UploadAvatar: failed to remove old repo avatar %s: %w", oldAvatarPath, err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// DeleteAvatar deletes the repos's custom avatar.
|
|
func DeleteAvatar(ctx context.Context, repo *repo_model.Repository) error {
|
|
// Avatar not exists
|
|
if len(repo.Avatar) == 0 {
|
|
return nil
|
|
}
|
|
|
|
avatarPath := repo.CustomAvatarRelativePath()
|
|
log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath)
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
repo.Avatar = ""
|
|
if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "avatar"); err != nil {
|
|
return fmt.Errorf("DeleteAvatar: Update repository avatar: %w", err)
|
|
}
|
|
|
|
if err := storage.RepoAvatars.Delete(avatarPath); err != nil {
|
|
return fmt.Errorf("DeleteAvatar: Failed to remove %s: %w", avatarPath, err)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// RemoveRandomAvatars removes the randomly generated avatars that were created for repositories
|
|
func RemoveRandomAvatars(ctx context.Context) error {
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, repository *repo_model.Repository) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return db.ErrCancelledf("before random avatars removed for %s", repository.FullName())
|
|
default:
|
|
}
|
|
stringifiedID := strconv.FormatInt(repository.ID, 10)
|
|
if repository.Avatar == stringifiedID {
|
|
return DeleteAvatar(ctx, repository)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// generateAvatar generates the avatar from a template repository
|
|
func generateAvatar(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
|
|
// generate a new different hash, whatever the "hash data" is, it doesn't matter
|
|
generateRepo.Avatar = avatar.HashAvatar(generateRepo.ID, []byte("new-avatar"))
|
|
if _, err := storage.Copy(storage.RepoAvatars, generateRepo.CustomAvatarRelativePath(), storage.RepoAvatars, templateRepo.CustomAvatarRelativePath()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return repo_model.UpdateRepositoryColsNoAutoTime(ctx, generateRepo, "avatar")
|
|
}
|