Skip to content

Commit

Permalink
sharedWithMe boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Jul 19, 2024
1 parent 9fa81d6 commit af4c818
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/http/services/owncloud/ocgraph/drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ func getDrivesForShares(ctx context.Context, gw gateway.GatewayAPIClient) ([]*li
return spacesRes, nil
}

func libregraphShareID(shareID *collaborationv1beta1.ShareId) string {
return fmt.Sprintf("%s$%s!%s", shareJailID, shareJailID, shareID.OpaqueId)
}

func convertShareToSpace(share *gateway.SharedResourceInfo) *libregraph.Drive {
// the prefix of the remote_item.id and rootid
return &libregraph.Drive{
Id: libregraph.PtrString(fmt.Sprintf("%s$%s!%s", shareJailID, shareJailID, share.Share.Share.Id.OpaqueId)),
Id: libregraph.PtrString(libregraphShareID(share.Share.Share.Id)),
DriveType: libregraph.PtrString("mountpoint"),
DriveAlias: libregraph.PtrString(share.Share.Share.Id.OpaqueId), // this is not used, but must not be the same alias as the drive item
Name: filepath.Base(share.ResourceInfo.Path),
Expand Down
16 changes: 16 additions & 0 deletions internal/http/services/owncloud/ocgraph/ocgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ func (s *svc) Handler() http.Handler {
return
}
}
} else if head == "v1beta1" {
head, r.URL.Path = router.ShiftPath(r.URL.Path)
// https://demo.owncloud.com/graph/v1beta1/me/drive/sharedWithMe
switch head {

Check failure on line 95 in internal/http/services/owncloud/ocgraph/ocgraph.go

View workflow job for this annotation

GitHub Actions / lint

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case "me":
head, r.URL.Path = router.ShiftPath(r.URL.Path)
switch head {

Check failure on line 98 in internal/http/services/owncloud/ocgraph/ocgraph.go

View workflow job for this annotation

GitHub Actions / lint

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case "drive":
head, r.URL.Path = router.ShiftPath(r.URL.Path)
switch head {

Check failure on line 101 in internal/http/services/owncloud/ocgraph/ocgraph.go

View workflow job for this annotation

GitHub Actions / lint

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case "sharedWithMe":
s.getSharedWithMe(w, r)
return
}
}
}
}

w.WriteHeader(http.StatusNotFound)
Expand Down
107 changes: 107 additions & 0 deletions internal/http/services/owncloud/ocgraph/shares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2018-2024 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.

// This package implements the APIs defined in https://owncloud.dev/apis/http/graph/spaces/

package ocgraph

import (
"encoding/json"
"net/http"
"time"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"

collaborationv1beta1 "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
libregraph "github.com/owncloud/libre-graph-api-go"
)

func (s *svc) getSharedWithMe(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := appctx.GetLogger(ctx)

gw, err := s.getClient()
if err != nil {
log.Error().Err(err).Msg("error getting grpc client")
w.WriteHeader(http.StatusInternalServerError)
return
}

resShares, err := gw.ListExistingReceivedShares(ctx, &collaborationv1beta1.ListReceivedSharesRequest{})
if err != nil {
log.Error().Err(err).Msg("error getting received shares")
w.WriteHeader(http.StatusInternalServerError)
return
}

shares := make([]*libregraph.DriveItem, 0, len(resShares.Shares))
for _, s := range resShares.Shares {
shares = append(shares, cs3ReceivedShareToDriveItem(s))
}

if err := json.NewEncoder(w).Encode(map[string]any{
"value": shares,
}); err != nil {
log.Error().Err(err).Msg("error marshalling shares as json")
w.WriteHeader(http.StatusInternalServerError)
return
}
}

func cs3ReceivedShareToDriveItem(share *gateway.SharedResourceInfo) *libregraph.DriveItem {
return &libregraph.DriveItem{
UIHidden: libregraph.PtrBool(share.Share.Hidden),
ClientSynchronize: libregraph.PtrBool(true),
CreatedBy: &libregraph.IdentitySet{
User: &libregraph.Identity{
DisplayName: "", // TODO: understand if needed, in case needs to be resolved
Id: &share.Share.Share.Creator.OpaqueId,
},
},
ETag: &share.ResourceInfo.Etag,
File: &libregraph.OpenGraphFile{
MimeType: &share.ResourceInfo.MimeType,
},
Id: libregraph.PtrString(libregraphShareID(share.Share.Share.Id)),
LastModifiedDateTime: libregraph.PtrTime(time.Unix(int64(share.ResourceInfo.Mtime.Seconds), int64(share.ResourceInfo.Mtime.Nanos))),
Name: libregraph.PtrString(share.ResourceInfo.Name),
ParentReference: &libregraph.ItemReference{}, // TODO: do we have enough info?
RemoteItem: &libregraph.RemoteItem{
CreatedBy: &libregraph.IdentitySet{
User: &libregraph.Identity{
DisplayName: "", // TODO: understand if needed, in case needs to be resolved
Id: &share.Share.Share.Creator.OpaqueId,
},
},
ETag: &share.ResourceInfo.Etag,
File: &libregraph.OpenGraphFile{
MimeType: &share.ResourceInfo.MimeType,
},
Id: nil, // TODO: space id of the resource
LastModifiedDateTime: libregraph.PtrTime(time.Unix(int64(share.ResourceInfo.Mtime.Seconds), int64(share.ResourceInfo.Mtime.Nanos))),
Name: libregraph.PtrString(share.ResourceInfo.Name),
ParentReference: &libregraph.ItemReference{}, // TODO: space id of the resource
Permissions: []libregraph.Permission{
// TODO
},
Size: libregraph.PtrInt64(int64(share.ResourceInfo.Size)),
},
Size: libregraph.PtrInt64(int64(share.ResourceInfo.Size)),
}
}

0 comments on commit af4c818

Please sign in to comment.