Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tags #3555

Merged
merged 2 commits into from
Dec 14, 2022
Merged

Tags #3555

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Tags

Base functionality for tagging files

https://github.com/cs3org/reva/pull/3555
15 changes: 14 additions & 1 deletion internal/http/services/owncloud/ocdav/propfind/propfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ func (p *Handler) getSpaceResourceInfos(ctx context.Context, w http.ResponseWrit

if res.Status.Code != rpc.Code_CODE_OK {
log.Debug().Interface("status", res.Status).Msg("List Container not ok, skipping")
w.WriteHeader(http.StatusInternalServerError)
return nil, false
}
for _, info := range res.Infos {
Expand Down Expand Up @@ -830,7 +831,7 @@ func requiresExplicitFetching(n *xml.Name) bool {
}
case net.NsOwncloud:
switch n.Local {
case "favorite", "share-types", "checksums", "size":
case "favorite", "share-types", "checksums", "size", "tags":
return true
default:
return false
Expand Down Expand Up @@ -1087,6 +1088,10 @@ func mdToPropResponse(ctx context.Context, pf *XML, md *provider.ResourceInfo, p
appendToOK(prop.Raw("oc:checksums", checksums.String()))
}

if k := md.GetArbitraryMetadata().GetMetadata(); k != nil {
propstatOK.Prop = append(propstatOK.Prop, prop.Raw("oc:tags", k["tags"]))
}

// ls do not report any properties as missing by default
if ls == nil {
// favorites from arbitrary metadata
Expand Down Expand Up @@ -1354,6 +1359,10 @@ func mdToPropResponse(ctx context.Context, pf *XML, md *provider.ResourceInfo, p
appendToNotFound(prop.NotFound("oc:signature-auth"))
}
}
case "tags":
if k := md.GetArbitraryMetadata().GetMetadata(); k != nil {
propstatOK.Prop = append(propstatOK.Prop, prop.Raw("oc:tags", k["tags"]))
}
case "name":
appendToOK(prop.Escaped("oc:name", md.Name))
case "shareid":
Expand Down Expand Up @@ -1582,6 +1591,10 @@ func metadataKeyOf(n *xml.Name) string {
if n.Local == "share-types" {
return "share-types"
}

if n.Local == "tags" {
return "tags"
}
}
return fmt.Sprintf("%s/%s", n.Space, n.Local)
}
Expand Down
1 change: 1 addition & 0 deletions internal/http/services/owncloud/ocs/data/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ type CapabilitiesFiles struct {
Undelete ocsBool `json:"undelete" xml:"undelete"`
Versioning ocsBool `json:"versioning" xml:"versioning"`
Favorites ocsBool `json:"favorites" xml:"favorites"`
Tags ocsBool `json:"tags" xml:"tags"`
BlacklistedFiles []string `json:"blacklisted_files" xml:"blacklisted_files>element" mapstructure:"blacklisted_files"`
TusSupport *CapabilitiesFilesTusSupport `json:"tus_support" xml:"tus_support" mapstructure:"tus_support"`
Archivers []*CapabilitiesArchiver `json:"archivers" xml:"archivers" mapstructure:"archivers"`
Expand Down
56 changes: 56 additions & 0 deletions pkg/events/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2018-2022 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package events

import (
"encoding/json"

user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
)

// TagsAdded is emitted when a Tag has been added
type TagsAdded struct {
SpaceOwner *user.UserId
Tags string
Ref *provider.Reference
Executant *user.UserId
}

// Unmarshal to fulfill umarshaller interface
func (TagsAdded) Unmarshal(v []byte) (interface{}, error) {
e := TagsAdded{}
err := json.Unmarshal(v, &e)
return e, err
}

// TagsRemoved is emitted when a Tag has been added
type TagsRemoved struct {
SpaceOwner *user.UserId
Tags string
Ref *provider.Reference
Executant *user.UserId
}

// Unmarshal to fulfill umarshaller interface
func (TagsRemoved) Unmarshal(v []byte) (interface{}, error) {
e := TagsRemoved{}
err := json.Unmarshal(v, &e)
return e, err
}
112 changes: 112 additions & 0 deletions pkg/tags/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2018-2022 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package tags

import (
"strings"
)

var (
// character used to separate tags in lists
_tagsep = ","
// maximum number of tags
_maxtags = 100
)

// Tags is a helper struct for merging, deleting and deduplicating the tags while preserving the order
type Tags struct {
sep string
maxtags int

t []string
exists map[string]bool
numtags int
}

// FromList creates a Tags struct from a list of tags
func FromList(s string) *Tags {
t := &Tags{sep: _tagsep, maxtags: _maxtags, exists: make(map[string]bool)}
t.t = t.addTags(s)
return t
}

// AddList appends a list of new tags and returns true if at least one was appended
func (t *Tags) AddList(s string) bool {
tags := t.addTags(s)
t.t = append(tags, t.t...)
return len(tags) > 0
}

// RemoveList removes a list of tags and returns true if at least one was removed
func (t *Tags) RemoveList(s string) bool {
var removed bool
for _, tag := range strings.Split(s, t.sep) {
if !t.exists[tag] {
continue
}

for i, tt := range t.t {
if tt == tag {
t.t = append(t.t[:i], t.t[i+1:]...)
break
}
}

delete(t.exists, tag)
removed = true
}
return removed
}

// AsList returns the tags converted to a list
func (t *Tags) AsList() string {
return strings.Join(t.t, t.sep)
}

// AsSlice returns the tags as slice of strings
func (t *Tags) AsSlice() []string {
return t.t
}

// adds the tags and returns a list of added tags
func (t *Tags) addTags(s string) []string {
added := make([]string, 0)
for _, tag := range strings.Split(s, t.sep) {
if tag == "" {
// ignore empty tags
continue
}

if t.exists[tag] {
// tag is already existing
continue
}

if t.numtags >= t.maxtags {
// max number of tags reached. We return silently without warning anyone
break
}

added = append(added, tag)
t.exists[tag] = true
t.numtags++
}

return added
}
Loading