From 7041549a163a9ca99bd139bbe95c903b6f520cca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 9 Dec 2021 14:59:25 +0000 Subject: [PATCH] fix listing drives MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- graph/pkg/service/v0/drives.go | 56 +++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/graph/pkg/service/v0/drives.go b/graph/pkg/service/v0/drives.go index aa370cbdfd..76469977fd 100644 --- a/graph/pkg/service/v0/drives.go +++ b/graph/pkg/service/v0/drives.go @@ -274,15 +274,20 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) { return } + root := &provider.ResourceId{} + identifierParts := strings.Split(req.FirstSegment.Identifier.Get(), "!") - if len(identifierParts) != 2 { + switch len(identifierParts) { + case 1: + root.StorageId, root.OpaqueId = identifierParts[0], identifierParts[0] + case 2: + root.StorageId, root.OpaqueId = identifierParts[0], identifierParts[1] + default: errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("invalid resource id: %v", req.FirstSegment.Identifier.Get())) w.WriteHeader(http.StatusInternalServerError) return } - storageID, opaqueID := identifierParts[0], identifierParts[1] - client, err := g.GetClient() if err != nil { errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) @@ -294,12 +299,9 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) { // the original storage space. StorageSpace: &provider.StorageSpace{ Id: &storageprovider.StorageSpaceId{ - OpaqueId: req.FirstSegment.Identifier.Get(), - }, - Root: &provider.ResourceId{ - StorageId: storageID, - OpaqueId: opaqueID, + OpaqueId: root.StorageId + "!" + root.OpaqueId, }, + Root: root, }, } @@ -386,21 +388,20 @@ func formatDriveItems(mds []*storageprovider.ResourceInfo) ([]*msgraph.DriveItem func cs3StorageSpaceToDrive(baseURL *url.URL, space *storageprovider.StorageSpace) (*msgraph.Drive, error) { rootID := space.Root.StorageId + "!" + space.Root.OpaqueId + if space.Root.StorageId == space.Root.OpaqueId { + // omit opaqueid + rootID = space.Root.StorageId + } + drive := &msgraph.Drive{ BaseItem: msgraph.BaseItem{ Entity: msgraph.Entity{ - Id: &space.Id.OpaqueId, + Id: &rootID, }, Name: &space.Name, //"createdDateTime": "string (timestamp)", // TODO read from StorageSpace ... needs Opaque for now //"description": "string", // TODO read from StorageSpace ... needs Opaque for now }, - Owner: &msgraph.IdentitySet{ - User: &msgraph.Identity{ - Id: &space.Owner.Id.OpaqueId, - // DisplayName: , TODO read and cache from users provider - }, - }, DriveType: &space.SpaceType, Root: &msgraph.DriveItem{ @@ -420,6 +421,15 @@ func cs3StorageSpaceToDrive(baseURL *url.URL, space *storageprovider.StorageSpac drive.Root.WebDavUrl = &webDavURL } + // TODO The public space has no owner ... should we even show it? + if space.Owner != nil && space.Owner.Id != nil { + drive.Owner = &msgraph.IdentitySet{ + User: &msgraph.Identity{ + Id: &space.Owner.Id.OpaqueId, + // DisplayName: , TODO read and cache from users provider + }, + } + } if space.Mtime != nil { lastModified := cs3TimestampToTime(space.Mtime) drive.BaseItem.LastModifiedDateTime = &lastModified @@ -447,22 +457,21 @@ func (g Graph) formatDrives(ctx context.Context, baseURL *url.URL, mds []*storag if err != nil { return nil, err } - qta, err := g.getDriveQuota(ctx, mds[i]) + res.Quota, err = g.getDriveQuota(ctx, mds[i]) if err != nil { return nil, err } - res.Quota = &qta responses = append(responses, res) } return responses, nil } -func (g Graph) getDriveQuota(ctx context.Context, space *storageprovider.StorageSpace) (msgraph.Quota, error) { +func (g Graph) getDriveQuota(ctx context.Context, space *storageprovider.StorageSpace) (*msgraph.Quota, error) { client, err := g.GetClient() if err != nil { g.logger.Error().Err(err).Msg("error creating grpc client") - return msgraph.Quota{}, err + return nil, nil } req := &gateway.GetQuotaRequest{ @@ -478,17 +487,20 @@ func (g Graph) getDriveQuota(ctx context.Context, space *storageprovider.Storage switch { case err != nil: g.logger.Error().Err(err).Msg("error sending get quota grpc request") - return msgraph.Quota{}, err + return nil, nil + case res.Status.Code == cs3rpc.Code_CODE_UNIMPLEMENTED: + // TODO well duh + return nil, nil case res.Status.Code != cs3rpc.Code_CODE_OK: g.logger.Error().Err(err).Msg("error sending sending get quota grpc request") - return msgraph.Quota{}, err + return nil, err } total := int64(res.TotalBytes) used := int64(res.UsedBytes) remaining := total - used - qta := msgraph.Quota{ + qta := &msgraph.Quota{ Remaining: &remaining, Total: &total, Used: &used,