2020-10-14 08:07:51 -05:00
|
|
|
// Copyright 2020 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 models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"image/png"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2020-10-14 08:07:51 -05:00
|
|
|
"code.gitea.io/gitea/modules/avatar"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RemoveRandomAvatars removes the randomly generated avatars that were created for repositories
|
|
|
|
func RemoveRandomAvatars(ctx context.Context) error {
|
2021-09-23 10:45:36 -05:00
|
|
|
return db.GetEngine(db.DefaultContext).
|
2020-10-14 08:07:51 -05:00
|
|
|
Where("id > 0").BufferSize(setting.Database.IterateBufferSize).
|
2021-12-09 20:27:50 -05:00
|
|
|
Iterate(new(repo_model.Repository),
|
2020-10-14 08:07:51 -05:00
|
|
|
func(idx int, bean interface{}) error {
|
2021-12-09 20:27:50 -05:00
|
|
|
repository := bean.(*repo_model.Repository)
|
2020-10-14 08:07:51 -05:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-10 00:13:16 -05:00
|
|
|
return db.ErrCancelledf("before random avatars removed for %s", repository.FullName())
|
2020-10-14 08:07:51 -05:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
stringifiedID := strconv.FormatInt(repository.ID, 10)
|
|
|
|
if repository.Avatar == stringifiedID {
|
2021-12-09 20:27:50 -05:00
|
|
|
return DeleteRepoAvatar(repository)
|
2020-10-14 08:07:51 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
// UploadRepoAvatar saves custom avatar for repository.
|
2020-10-14 08:07:51 -05:00
|
|
|
// FIXME: split uploads to different subdirs in case we have massive number of repos.
|
2021-12-09 20:27:50 -05:00
|
|
|
func UploadRepoAvatar(repo *repo_model.Repository, data []byte) error {
|
2020-10-14 08:07:51 -05:00
|
|
|
m, err := avatar.Prepare(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newAvatar := fmt.Sprintf("%d-%x", repo.ID, md5.Sum(data))
|
|
|
|
if repo.Avatar == newAvatar { // upload the same picture
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 10:41:00 -05:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2020-10-14 08:07:51 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 10:41:00 -05:00
|
|
|
defer committer.Close()
|
2020-10-14 08:07:51 -05:00
|
|
|
|
|
|
|
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
|
2021-11-21 10:41:00 -05:00
|
|
|
if _, err := db.GetEngine(ctx).ID(repo.ID).Cols("avatar").Update(repo); err != nil {
|
2020-10-14 08:07:51 -05:00
|
|
|
return fmt.Errorf("UploadAvatar: Update repository avatar: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := storage.SaveFrom(storage.RepoAvatars, repo.CustomAvatarRelativePath(), func(w io.Writer) error {
|
|
|
|
if err := png.Encode(w, *m); err != nil {
|
|
|
|
log.Error("Encode: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("UploadAvatar %s failed: Failed to remove old repo avatar %s: %v", repo.RepoPath(), 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: %v", oldAvatarPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 10:41:00 -05:00
|
|
|
return committer.Commit()
|
2020-10-14 08:07:51 -05:00
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
// DeleteRepoAvatar deletes the repos's custom avatar.
|
|
|
|
func DeleteRepoAvatar(repo *repo_model.Repository) error {
|
2020-10-14 08:07:51 -05:00
|
|
|
// Avatar not exists
|
|
|
|
if len(repo.Avatar) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
avatarPath := repo.CustomAvatarRelativePath()
|
|
|
|
log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath)
|
|
|
|
|
2021-11-21 10:41:00 -05:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2020-10-14 08:07:51 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 10:41:00 -05:00
|
|
|
defer committer.Close()
|
2020-10-14 08:07:51 -05:00
|
|
|
|
|
|
|
repo.Avatar = ""
|
2021-11-21 10:41:00 -05:00
|
|
|
if _, err := db.GetEngine(ctx).ID(repo.ID).Cols("avatar").Update(repo); err != nil {
|
2020-10-14 08:07:51 -05:00
|
|
|
return fmt.Errorf("DeleteAvatar: Update repository avatar: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := storage.RepoAvatars.Delete(avatarPath); err != nil {
|
|
|
|
return fmt.Errorf("DeleteAvatar: Failed to remove %s: %v", avatarPath, err)
|
|
|
|
}
|
|
|
|
|
2021-11-21 10:41:00 -05:00
|
|
|
return committer.Commit()
|
2020-10-14 08:07:51 -05:00
|
|
|
}
|