Skip to content

Commit

Permalink
handle spaces relative paths in webdav responses
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Nov 13, 2023
1 parent db86b08 commit 3455a4a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
7 changes: 7 additions & 0 deletions internal/grpc/services/spacesregistry/spacesregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package spacesregistry

import (
"context"
"encoding/base64"
"errors"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
Expand Down Expand Up @@ -93,6 +94,12 @@ func (s *service) ListStorageSpaces(ctx context.Context, req *provider.ListStora
Status: status.NewInternal(ctx, err, "error listing storage spaces"),
}, nil
}

for _, s := range spaces {
s.Id = &provider.StorageSpaceId{
OpaqueId: base64.StdEncoding.EncodeToString([]byte(s.RootInfo.Path)),
}
}
return &provider.ListStorageSpacesResponse{
Status: status.NewOK(ctx),
StorageSpaces: spaces,
Expand Down
34 changes: 31 additions & 3 deletions internal/http/services/owncloud/ocdav/dav.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package ocdav

import (
"context"
"encoding/base64"
"net/http"
"path"
"path/filepath"
Expand All @@ -45,7 +46,7 @@ type DavHandler struct {
FilesHomeHandler *WebDavHandler
MetaHandler *MetaHandler
TrashbinHandler *TrashbinHandler
SpacesHandler *SpacesHandler
SpacesHandler *WebDavHandler
PublicFolderHandler *WebDavHandler
PublicFileHandler *PublicFileHandler
OCMSharesHandler *WebDavHandler
Expand All @@ -70,8 +71,8 @@ func (h *DavHandler) init(c *Config) error {
}
h.TrashbinHandler = new(TrashbinHandler)

h.SpacesHandler = new(SpacesHandler)
if err := h.SpacesHandler.init(c); err != nil {
h.SpacesHandler = new(WebDavHandler)
if err := h.SpacesHandler.init("", false); err != nil {
return err
}

Expand Down Expand Up @@ -176,6 +177,25 @@ func (h *DavHandler) Handler(s *svc) http.Handler {
case "spaces":
base := path.Join(ctx.Value(ctxKeyBaseURI).(string), "spaces")
ctx := context.WithValue(ctx, ctxKeyBaseURI, base)

// path is of type: space_id/relative/path/from/space
// the space_id is the base64 encode of the path where
// the space is located
spaceID, relativeSpacePath := router.ShiftPath(r.URL.Path)

spacePath, err := getSpacePath(spaceID)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

fullPath := filepath.Join(spacePath, relativeSpacePath)
r.URL.Path = fullPath

ctx = context.WithValue(ctx, ctxSpaceID, spaceID)
ctx = context.WithValue(ctx, ctxSpaceFullPath, fullPath)
ctx = context.WithValue(ctx, ctxSpacePath, spacePath)
ctx = context.WithValue(ctx, ctxSpaceRelativePath, relativeSpacePath)
r = r.WithContext(ctx)
h.SpacesHandler.Handler(s).ServeHTTP(w, r)
case "ocm":
Expand Down Expand Up @@ -323,6 +343,14 @@ func (h *DavHandler) Handler(s *svc) http.Handler {
})
}

func getSpacePath(spaceID string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(spaceID)
if err != nil {
return "", err
}
return string(decoded), nil
}

func getTokenStatInfo(ctx context.Context, client gatewayv1beta1.GatewayAPIClient, token string) (*provider.StatResponse, error) {
return client.Stat(ctx, &provider.StatRequest{Ref: &provider.Reference{Path: path.Join("/public", token)}})
}
Expand Down
4 changes: 4 additions & 0 deletions internal/http/services/owncloud/ocdav/ocdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ type ctxKey int
const (
ctxKeyBaseURI ctxKey = iota
ctxOCM10
ctxSpaceID
ctxSpacePath
ctxSpaceFullPath
ctxSpaceRelativePath
)

var (
Expand Down
22 changes: 21 additions & 1 deletion internal/http/services/owncloud/ocdav/propfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,20 @@ func supportLegacyOCMAccess(ctx context.Context, md *provider.ResourceInfo) {
}
}

func spaceHref(ctx context.Context, baseURI, fullPath string) string {
// in the context of spaces, the final URL will be baseURI + /<space_id>/relative/path/to/space
spacePath, ok := ctx.Value(ctxSpacePath).(string)
if !ok {
panic("space path expected to be in the context")
}
relativePath := strings.TrimPrefix(fullPath, spacePath)
spaceID, ok := ctx.Value(ctxSpaceID).(string)
if !ok {
panic("space id expected to be in the context")
}
return path.Join(baseURI, spaceID, relativePath)
}

// mdToPropResponse converts the CS3 metadata into a webdav PropResponse
// ns is the CS3 namespace that needs to be removed from the CS3 path before
// prefixing it with the baseURI.
Expand All @@ -524,7 +538,13 @@ func (s *svc) mdToPropResponse(ctx context.Context, pf *propfindXML, md *provide
baseURI := ctx.Value(ctxKeyBaseURI).(string)

supportLegacyOCMAccess(ctx, md)
ref := path.Join(baseURI, md.Path)

var ref string
if _, ok := ctx.Value(ctxSpaceID).(string); ok {
ref = spaceHref(ctx, baseURI, md.Path)
} else {
ref = path.Join(baseURI, md.Path)
}
if md.Type == provider.ResourceType_RESOURCE_TYPE_CONTAINER {
ref += "/"
}
Expand Down
4 changes: 2 additions & 2 deletions internal/http/services/owncloud/ocgraph/drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ func generateCs3Filters(request *godata.GoDataRequest) ([]*providerpb.ListStorag
func (s *svc) cs3StorageSpaceToDrive(user *userpb.User, space *providerpb.StorageSpace) *libregraph.Drive {
drive := &libregraph.Drive{
DriveAlias: libregraph.PtrString(space.RootInfo.Path),
Id: libregraph.PtrString("75403711-20d1-414f-aa17-df2fbb06833d$fad2ae4a-30ef-486d-95ab-d320e49f1722"),
Id: libregraph.PtrString(space.Id.OpaqueId),
Name: space.Name,
DriveType: libregraph.PtrString(space.SpaceType),
Root: &libregraph.DriveItem{
Id: libregraph.PtrString("75403711-20d1-414f-aa17-df2fbb06833d$fad2ae4a-30ef-486d-95ab-d320e49f1722"),
Id: libregraph.PtrString(space.Id.OpaqueId),
Permissions: cs3PermissionsToLibreGraph(user, space.RootInfo.PermissionSet),
},
}
Expand Down

0 comments on commit 3455a4a

Please sign in to comment.