mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-10 17:05:33 +02:00
* move error-related code for groups to its own file * update group avatar logic remove unused/duplicate logic * update `FindGroupsOptions.ToConds()` allow passing `-1` as the `ParentGroupID`, meaning "find matching groups regardless of the parent group id" * add `DedupeBy` function to container module this removes duplicate items from a slice using a custom function * add `SliceMap` util works like javascripts's `Array.prototoype.map`, taking in a slice and transforming each element with the provided function * add group service functions included so far: - avatar uploading/deletion - group deletion - group creation - group moving (including moving item inside a group) - group update - team management - add team - remove team - update team permissions - recalculating team access (in event of group move) - group searching (only used in frontend/web components for now)
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package group
|
|
|
|
import (
|
|
"code.gitea.io/gitea/models/db"
|
|
group_model "code.gitea.io/gitea/models/group"
|
|
"code.gitea.io/gitea/modules/avatar"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/storage"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// UploadAvatar saves custom icon for group.
|
|
func UploadAvatar(ctx context.Context, g *group_model.Group, data []byte) error {
|
|
avatarData, err := avatar.ProcessAvatarImage(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
|
|
g.Avatar = avatar.HashAvatar(g.ID, data)
|
|
if err = UpdateGroup(ctx, g, &UpdateOptions{}); err != nil {
|
|
return fmt.Errorf("updateGroup: %w", err)
|
|
}
|
|
|
|
if err = storage.SaveFrom(storage.Avatars, g.CustomAvatarRelativePath(), func(w io.Writer) error {
|
|
_, err = w.Write(avatarData)
|
|
return err
|
|
}); err != nil {
|
|
return fmt.Errorf("Failed to create dir %s: %w", g.CustomAvatarRelativePath(), err)
|
|
}
|
|
|
|
return committer.Commit()
|
|
}
|
|
|
|
// DeleteAvatar deletes the user's custom avatar.
|
|
func DeleteAvatar(ctx context.Context, g *group_model.Group) error {
|
|
aPath := g.CustomAvatarRelativePath()
|
|
log.Trace("DeleteAvatar[%d]: %s", g.ID, aPath)
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
hasAvatar := len(g.Avatar) > 0
|
|
g.Avatar = ""
|
|
if _, err := db.GetEngine(ctx).ID(g.ID).Cols("avatar, use_custom_avatar").Update(g); err != nil {
|
|
return fmt.Errorf("DeleteAvatar: %w", err)
|
|
}
|
|
|
|
if hasAvatar {
|
|
if err := storage.Avatars.Delete(aPath); err != nil {
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
return fmt.Errorf("failed to remove %s: %w", aPath, err)
|
|
}
|
|
log.Warn("Deleting avatar %s but it doesn't exist", aPath)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|