From f06f2f585aee655b04f6fe28f5a6d0c48aadf81f Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 21 Feb 2023 18:29:31 -0500 Subject: [PATCH] GC: OneDrive/SharePoint WebURL Fix (#2596) ## Description Changes: - Use of `ptr` package to ensure safe dereferencing of pointers - Update to query to ensure that `sharepointIds` are part of the response - Alternate method created to obtain originating sharepoint site url ## Does this PR need a docs update or release note? - [x] :no_entry: No ## Type of change - [x] :sunflower: Feature - [x] :bug: Bugfix ## Issue(s) * closes #1747 ## Test Plan - [x] :muscle: Manual --- src/internal/connector/onedrive/drive.go | 1 + src/internal/connector/onedrive/item.go | 70 +++++++++++++++++------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/internal/connector/onedrive/drive.go b/src/internal/connector/onedrive/drive.go index f95b826e1..07c83b7f1 100644 --- a/src/internal/connector/onedrive/drive.go +++ b/src/internal/connector/onedrive/drive.go @@ -179,6 +179,7 @@ func defaultItemPager( "package", "parentReference", "root", + "sharepointIds", "size", "deleted", }, diff --git a/src/internal/connector/onedrive/item.go b/src/internal/connector/onedrive/item.go index 9ce1188ca..c962e167d 100644 --- a/src/internal/connector/onedrive/item.go +++ b/src/internal/connector/onedrive/item.go @@ -14,6 +14,7 @@ import ( "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/pkg/errors" + "github.com/alcionai/corso/src/internal/common/ptr" "github.com/alcionai/corso/src/internal/connector/graph" "github.com/alcionai/corso/src/internal/connector/support" "github.com/alcionai/corso/src/internal/connector/uploadsession" @@ -194,9 +195,9 @@ func oneDriveItemInfo(di models.DriveItemable, itemSize int64) *details.OneDrive return &details.OneDriveInfo{ ItemType: details.OneDriveItem, - ItemName: *di.GetName(), - Created: *di.GetCreatedDateTime(), - Modified: *di.GetLastModifiedDateTime(), + ItemName: ptr.Val(di.GetName()), + Created: ptr.Val(di.GetCreatedDateTime()), + Modified: ptr.Val(di.GetLastModifiedDateTime()), DriveName: parent, Size: itemSize, Owner: email, @@ -280,36 +281,32 @@ func sharePointItemInfo(di models.DriveItemable, itemSize int64) *details.ShareP // TODO: we rely on this info for details/restore lookups, // so if it's nil we have an issue, and will need an alternative // way to source the data. + gsi := di.GetSharepointIds() if gsi != nil { - if gsi.GetSiteId() != nil { - id = *gsi.GetSiteId() - } + id = ptr.Val(gsi.GetSiteId()) + url = ptr.Val(gsi.GetSiteUrl()) - if gsi.GetSiteUrl() != nil { - url = *gsi.GetSiteUrl() + if len(url) == 0 { + url = constructWebURL(di.GetAdditionalData()) } } if reference != nil { - parent = *reference.GetDriveId() + parent = ptr.Val(reference.GetDriveId()) + temp := ptr.Val(reference.GetName()) + temp = strings.TrimSpace(temp) - if reference.GetName() != nil { - // EndPoint is not always populated from external apps - temp := *reference.GetName() - temp = strings.TrimSpace(temp) - - if temp != "" { - parent = temp - } + if temp != "" { + parent = temp } } return &details.SharePointInfo{ ItemType: details.OneDriveItem, - ItemName: *di.GetName(), - Created: *di.GetCreatedDateTime(), - Modified: *di.GetLastModifiedDateTime(), + ItemName: ptr.Val(di.GetName()), + Created: ptr.Val(di.GetCreatedDateTime()), + Modified: ptr.Val(di.GetLastModifiedDateTime()), DriveName: parent, Size: itemSize, Owner: id, @@ -344,3 +341,36 @@ func driveItemWriter( return uploadsession.NewWriter(itemID, url, itemSize), nil } + +// constructWebURL helper function for recreating the webURL +// for the originating SharePoint site. Uses additional data map +// from a models.DriveItemable that possesses a downloadURL within the map. +// Returns "" if map nil or key is not present. +func constructWebURL(adtl map[string]any) string { + var ( + desiredKey = "@microsoft.graph.downloadUrl" + sep = `/_layouts` + url string + ) + + if adtl == nil { + return url + } + + r := adtl[desiredKey] + point, ok := r.(*string) + + if !ok { + return url + } + + value := ptr.Val(point) + if len(value) == 0 { + return url + } + + temp := strings.Split(value, sep) + url = temp[0] + + return url +}