From 5f3ff1f18f727b765ca353ad7eff9a37ab5e6b40 Mon Sep 17 00:00:00 2001 From: Michael Barz Date: Wed, 19 Jan 2022 12:30:23 +0100 Subject: [PATCH] merge master --- graph/pkg/service/v0/drives.go | 125 +++------------------------------ 1 file changed, 11 insertions(+), 114 deletions(-) diff --git a/graph/pkg/service/v0/drives.go b/graph/pkg/service/v0/drives.go index 4693a0d37c4..d54d6ebf950 100644 --- a/graph/pkg/service/v0/drives.go +++ b/graph/pkg/service/v0/drives.go @@ -7,7 +7,6 @@ import ( "math" "net/http" "net/url" - "path" "path/filepath" "strconv" "strings" @@ -157,70 +156,6 @@ func (g Graph) GetSingleDrive(w http.ResponseWriter, r *http.Request) { } } -// GetRootDriveChildren implements the Service interface. -func (g Graph) GetRootDriveChildren(w http.ResponseWriter, r *http.Request) { - g.logger.Info().Msg("Calling GetRootDriveChildren") - ctx := r.Context() - - client, err := g.GetClient() - if err != nil { - g.logger.Error().Err(err).Msg("could not get client") - errorcode.ServiceNotAvailable.Render(w, r, http.StatusInternalServerError, err.Error()) - return - } - - res, err := client.GetHome(ctx, &storageprovider.GetHomeRequest{}) - switch { - case err != nil: - g.logger.Error().Err(err).Msg("error sending get home grpc request") - errorcode.ServiceNotAvailable.Render(w, r, http.StatusInternalServerError, err.Error()) - return - case res.Status.Code != cs3rpc.Code_CODE_OK: - if res.Status.Code == cs3rpc.Code_CODE_NOT_FOUND { - errorcode.ItemNotFound.Render(w, r, http.StatusNotFound, res.Status.Message) - return - } - g.logger.Error().Err(err).Msg("error sending get home grpc request") - errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, res.Status.Message) - return - } - - lRes, err := client.ListContainer(ctx, &storageprovider.ListContainerRequest{ - Ref: &storageprovider.Reference{ - Path: res.Path, - }, - }) - switch { - case err != nil: - g.logger.Error().Err(err).Msg("error sending list container grpc request") - errorcode.ServiceNotAvailable.Render(w, r, http.StatusInternalServerError, err.Error()) - return - case res.Status.Code != cs3rpc.Code_CODE_OK: - if res.Status.Code == cs3rpc.Code_CODE_NOT_FOUND { - errorcode.ItemNotFound.Render(w, r, http.StatusNotFound, res.Status.Message) - return - } - if res.Status.Code == cs3rpc.Code_CODE_PERMISSION_DENIED { - // TODO check if we should return 404 to not disclose existing items - errorcode.AccessDenied.Render(w, r, http.StatusForbidden, res.Status.Message) - return - } - g.logger.Error().Err(err).Msg("error sending list container grpc request") - errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, res.Status.Message) - return - } - - files, err := formatDriveItems(lRes.Infos) - if err != nil { - g.logger.Error().Err(err).Msg("error encoding response as json") - errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) - return - } - - render.Status(r, http.StatusOK) - render.JSON(w, r, &listResponse{Value: files}) -} - // CreateDrive creates a storage drive (space). func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { us, ok := ctxpkg.ContextGetUser(r.Context()) @@ -439,18 +374,23 @@ func (g Graph) formatDrives(ctx context.Context, baseURL *url.URL, mds []*storag } // TODO this overwrites the quota that might already have been mapped in cs3StorageSpaceToDrive above ... move this into the cs3StorageSpaceToDrive method? res.Quota, err = g.getDriveQuota(ctx, space) + if err != nil { + return nil, err + } + responses = append(responses, res) + } + + return responses, nil +} + // ListStorageSpacesWithFilters List Storage Spaces using filters func (g Graph) ListStorageSpacesWithFilters(ctx context.Context, filters []*storageprovider.ListStorageSpacesRequest_Filter) (*storageprovider.ListStorageSpacesResponse, error) { - client, err := g.GetClient() - if err != nil { - g.logger.Err(err).Msg("error getting grpc client") - return nil, err - } + client := g.GetGatewayClient() permissions := make(map[string]struct{}, 1) s := sproto.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient) - _, err = s.GetPermissionByID(ctx, &sproto.GetPermissionByIDRequest{ + _, err := s.GetPermissionByID(ctx, &sproto.GetPermissionByIDRequest{ PermissionId: settingsSvc.ListAllSpacesPermissionID, }) @@ -475,49 +415,6 @@ func (g Graph) ListStorageSpacesWithFilters(ctx context.Context, filters []*stor return res, nil } -func cs3TimestampToTime(t *types.Timestamp) time.Time { - return time.Unix(int64(t.Seconds), int64(t.Nanos)) -} - -func cs3ResourceToDriveItem(res *storageprovider.ResourceInfo) (*libregraph.DriveItem, error) { - size := new(int64) - *size = int64(res.Size) // uint64 -> int :boom: - name := path.Base(res.Path) - - driveItem := &libregraph.DriveItem{ - Id: &res.Id.OpaqueId, - Name: &name, - ETag: &res.Etag, - Size: size, - } - if res.Mtime != nil { - lastModified := cs3TimestampToTime(res.Mtime) - driveItem.LastModifiedDateTime = &lastModified - } - if res.Type == storageprovider.ResourceType_RESOURCE_TYPE_FILE { - driveItem.File = &libregraph.OpenGraphFile{ // FIXME We cannot use libregraph.File here because the openapi codegenerator autodetects 'File' as a go type ... - MimeType: &res.MimeType, - } - } - if res.Type == storageprovider.ResourceType_RESOURCE_TYPE_CONTAINER { - driveItem.Folder = &libregraph.Folder{} - } - return driveItem, nil -} - -func formatDriveItems(mds []*storageprovider.ResourceInfo) ([]*libregraph.DriveItem, error) { - responses := make([]*libregraph.DriveItem, 0, len(mds)) - for i := range mds { - res, err := cs3ResourceToDriveItem(mds[i]) - if err != nil { - return nil, err - } - responses = append(responses, res) - } - - return responses, nil -} - func cs3StorageSpaceToDrive(baseURL *url.URL, space *storageprovider.StorageSpace) (*libregraph.Drive, error) { rootID := space.Root.StorageId + "!" + space.Root.OpaqueId if space.Root.StorageId == space.Root.OpaqueId {