Skip to content

Commit 1d84cf0

Browse files
ffuranoFabrizio Furanoglpatcern
authored
Implement favourites for eos/grpc (#4863)
* Implement favourites for eos/grpc * Add changelog * Add changelog * Remove unused const --------- Co-authored-by: Fabrizio Furano <[email protected]> Co-authored-by: Giuseppe Lo Presti <[email protected]>
1 parent ce7d0f8 commit 1d84cf0

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

changelog/unreleased/fav-grpc.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Enhancement: Favourites for eos/grpc
2+
3+
https://github.com/cs3org/reva/pull/4863

pkg/eosclient/eosgrpc/eosgrpc.go

+67-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ import (
4747
)
4848

4949
const (
50-
versionPrefix = ".sys.v#."
50+
versionPrefix = ".sys.v#."
51+
favoritesKey = "http://owncloud.org/ns/favorite"
5152
)
5253

5354
const (
@@ -57,6 +58,29 @@ const (
5758
UserAttr
5859
)
5960

61+
func serializeAttribute(a *eosclient.Attribute) string {
62+
return fmt.Sprintf("%s.%s=%s", attrTypeToString(a.Type), a.Key, a.Val)
63+
}
64+
65+
func attrTypeToString(at eosclient.AttrType) string {
66+
switch at {
67+
case eosclient.SystemAttr:
68+
return "sys"
69+
case eosclient.UserAttr:
70+
return "user"
71+
default:
72+
return "invalid"
73+
}
74+
}
75+
76+
func isValidAttribute(a *eosclient.Attribute) bool {
77+
// validate that an attribute is correct.
78+
if (a.Type != eosclient.SystemAttr && a.Type != eosclient.UserAttr) || a.Key == "" {
79+
return false
80+
}
81+
return true
82+
}
83+
6084
// Options to configure the Client.
6185
type Options struct {
6286

@@ -483,6 +507,22 @@ func (c *Client) fixupACLs(ctx context.Context, auth eosclient.Authorization, in
483507

484508
// SetAttr sets an extended attributes on a path.
485509
func (c *Client) SetAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, errorIfExists, recursive bool, path, app string) error {
510+
if !isValidAttribute(attr) {
511+
return errors.New("eos: attr is invalid: " + serializeAttribute(attr))
512+
}
513+
514+
// Favorites need to be stored per user so handle these separately
515+
if attr.Type == eosclient.UserAttr && attr.Key == favoritesKey {
516+
info, err := c.GetFileInfoByPath(ctx, auth, path)
517+
if err != nil {
518+
return err
519+
}
520+
return c.handleFavAttr(ctx, auth, attr, recursive, path, info, true)
521+
}
522+
return c.setEOSAttr(ctx, auth, attr, errorIfExists, recursive, path, app)
523+
}
524+
525+
func (c *Client) setEOSAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, errorIfExists, recursive bool, path, app string) error {
486526
log := appctx.GetLogger(ctx)
487527
log.Info().Str("func", "SetAttr").Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("path", path).Msg("")
488528

@@ -531,6 +571,32 @@ func (c *Client) SetAttr(ctx context.Context, auth eosclient.Authorization, attr
531571
return err
532572
}
533573

574+
func (c *Client) handleFavAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, recursive bool, path string, info *eosclient.FileInfo, set bool) error {
575+
var err error
576+
u := appctx.ContextMustGetUser(ctx)
577+
if info == nil {
578+
info, err = c.GetFileInfoByPath(ctx, auth, path)
579+
if err != nil {
580+
return err
581+
}
582+
}
583+
favStr := info.Attrs[favoritesKey]
584+
favs, err := acl.Parse(favStr, acl.ShortTextForm)
585+
if err != nil {
586+
return err
587+
}
588+
if set {
589+
err = favs.SetEntry(acl.TypeUser, u.Id.OpaqueId, "1")
590+
if err != nil {
591+
return err
592+
}
593+
} else {
594+
favs.DeleteEntry(acl.TypeUser, u.Id.OpaqueId)
595+
}
596+
attr.Val = favs.Serialize()
597+
return c.setEOSAttr(ctx, auth, attr, false, recursive, path, "")
598+
}
599+
534600
// UnsetAttr unsets an extended attribute on a path.
535601
func (c *Client) UnsetAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, recursive bool, path, app string) error {
536602
log := appctx.GetLogger(ctx)

0 commit comments

Comments
 (0)