Skip to content

Commit 733b13e

Browse files
committed
refactor(graph): use BaseGraphService.publicBaseURL consistently
drive.WebUrl, driveItem.WebUrl and the public share link WebUrl all derive from the same config value (graph.spaces.webdav_base), but only driveItem.WebUrl used the pre-parsed BaseGraphService.publicBaseURL. The other two re-parsed the config on every call. Add webURLForResource and webURLForPublicShareToken methods on BaseGraphService, and convert the cs3ResourceToDriveItem and formatDriveItems free functions into methods so they pick up logger and publicBaseURL from the receiver. This also aligns them with the surrounding code: BaseGraphService already exposes ~15 similar methods, so the two free functions were the odd ones out. Net: all three WebUrls are now constructed from a single pre-parsed URL, and the (g.logger, g.publicBaseURL) plumbing at 7 call sites disappears.
1 parent e7b2a88 commit 733b13e

6 files changed

Lines changed: 40 additions & 42 deletions

File tree

services/graph/pkg/service/v0/api_driveitem_permissions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func (s DriveItemPermissionsService) ListPermissions(ctx context.Context, itemID
407407

408408
driveItems := make(driveItemsByResourceID, 1)
409409
// we can use the statResponse to build the drive item before fetching the shares
410-
item, err := cs3ResourceToDriveItem(s.logger, s.publicBaseURL, statResponse.GetInfo())
410+
item, err := s.cs3ResourceToDriveItem(statResponse.GetInfo())
411411
if err != nil {
412412
return collectionOfPermissions, err
413413
}

services/graph/pkg/service/v0/base.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ type BaseGraphService struct {
5252
publicBaseURL *url.URL
5353
}
5454

55+
// webURLForResource returns the public web URL pointing at the given resource
56+
// (e.g. https://cloud.example.com/f/<resource-id>), using the pre-parsed
57+
// publicBaseURL held by the service.
58+
func (g BaseGraphService) webURLForResource(rid *storageprovider.ResourceId) *string {
59+
u := *g.publicBaseURL
60+
u.Path = path.Join(u.Path, "f", storagespace.FormatResourceID(rid))
61+
return libregraph.PtrString(u.String())
62+
}
63+
64+
// webURLForPublicShareToken returns the public web URL for a public share link
65+
// (e.g. https://cloud.example.com/s/<token>), using the pre-parsed
66+
// publicBaseURL held by the service.
67+
func (g BaseGraphService) webURLForPublicShareToken(token string) *string {
68+
u := *g.publicBaseURL
69+
u.Path = path.Join(u.Path, "s", token)
70+
return libregraph.PtrString(u.String())
71+
}
72+
5573
func (g BaseGraphService) getDriveItem(ctx context.Context, ref *storageprovider.Reference) (*libregraph.DriveItem, error) {
5674
gatewayClient, err := g.gatewaySelector.Next()
5775
if err != nil {
@@ -66,7 +84,7 @@ func (g BaseGraphService) getDriveItem(ctx context.Context, ref *storageprovider
6684
refStr, _ := storagespace.FormatReference(ref)
6785
return nil, fmt.Errorf("could not stat %s: %s", refStr, res.GetStatus().GetMessage())
6886
}
69-
return cs3ResourceToDriveItem(g.logger, g.publicBaseURL, res.GetInfo())
87+
return g.cs3ResourceToDriveItem(res.GetInfo())
7088
}
7189

7290
func (g BaseGraphService) CS3ReceivedSharesToDriveItems(ctx context.Context, receivedShares []*collaboration.ReceivedShare) ([]libregraph.DriveItem, error) {
@@ -217,14 +235,6 @@ func (g BaseGraphService) cs3SpacePermissionsToLibreGraph(ctx context.Context, s
217235
}
218236

219237
func (g BaseGraphService) libreGraphPermissionFromCS3PublicShare(createdLink *link.PublicShare) (*libregraph.Permission, error) {
220-
webURL, err := url.Parse(g.config.Spaces.WebDavBase)
221-
if err != nil {
222-
g.logger.Error().
223-
Err(err).
224-
Str("url", g.config.Spaces.WebDavBase).
225-
Msg("failed to parse webURL base url")
226-
return nil, err
227-
}
228238
lt, actions := linktype.SharingLinkTypeFromCS3Permissions(createdLink.GetPermissions())
229239
perm := libregraph.NewPermission()
230240
perm.Id = libregraph.PtrString(createdLink.GetId().GetOpaqueId())
@@ -235,8 +245,7 @@ func (g BaseGraphService) libreGraphPermissionFromCS3PublicShare(createdLink *li
235245
LibreGraphQuickLink: libregraph.PtrBool(createdLink.GetQuicklink()),
236246
}
237247
perm.LibreGraphPermissionsActions = actions
238-
webURL.Path = path.Join(webURL.Path, "s", createdLink.GetToken())
239-
perm.Link.SetWebUrl(webURL.String())
248+
perm.Link.SetWebUrl(*g.webURLForPublicShareToken(createdLink.GetToken()))
240249

241250
// set expiration date
242251
if createdLink.GetExpiration() != nil {

services/graph/pkg/service/v0/driveitems.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (g Graph) GetRootDriveChildren(w http.ResponseWriter, r *http.Request) {
204204
return
205205
}
206206

207-
files, err := formatDriveItems(g.logger, g.publicBaseURL, lRes.GetInfos())
207+
files, err := g.formatDriveItems(lRes.GetInfos())
208208
if err != nil {
209209
g.logger.Error().Err(err).Msg("error encoding response as json")
210210
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
@@ -269,7 +269,7 @@ func (g Graph) GetDriveItem(w http.ResponseWriter, r *http.Request) {
269269
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, res.GetStatus().GetMessage())
270270
return
271271
}
272-
driveItem, err := cs3ResourceToDriveItem(g.logger, g.publicBaseURL, res.GetInfo())
272+
driveItem, err := g.cs3ResourceToDriveItem(res.GetInfo())
273273
if err != nil {
274274
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
275275
return
@@ -337,7 +337,7 @@ func (g Graph) GetDriveItemChildren(w http.ResponseWriter, r *http.Request) {
337337
return
338338
}
339339

340-
files, err := formatDriveItems(g.logger, g.publicBaseURL, res.GetInfos())
340+
files, err := g.formatDriveItems(res.GetInfos())
341341
if err != nil {
342342
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
343343
return
@@ -385,10 +385,10 @@ func (g Graph) getRemoteItem(ctx context.Context, root *storageprovider.Resource
385385
return item, nil
386386
}
387387

388-
func formatDriveItems(logger *log.Logger, publicBaseURL *url.URL, mds []*storageprovider.ResourceInfo) ([]*libregraph.DriveItem, error) {
388+
func (g BaseGraphService) formatDriveItems(mds []*storageprovider.ResourceInfo) ([]*libregraph.DriveItem, error) {
389389
responses := make([]*libregraph.DriveItem, 0, len(mds))
390390
for i := range mds {
391-
res, err := cs3ResourceToDriveItem(logger, publicBaseURL, mds[i])
391+
res, err := g.cs3ResourceToDriveItem(mds[i])
392392
if err != nil {
393393
return nil, err
394394
}
@@ -402,19 +402,16 @@ func cs3TimestampToTime(t *types.Timestamp) time.Time {
402402
return time.Unix(int64(t.GetSeconds()), int64(t.GetNanos()))
403403
}
404404

405-
func cs3ResourceToDriveItem(logger *log.Logger, publicBaseURL *url.URL, res *storageprovider.ResourceInfo) (*libregraph.DriveItem, error) {
405+
func (g BaseGraphService) cs3ResourceToDriveItem(res *storageprovider.ResourceInfo) (*libregraph.DriveItem, error) {
406406
size := new(int64)
407407
*size = int64(res.GetSize()) // TODO lurking overflow: make size of libregraph drive item use uint64
408408

409409
driveItem := &libregraph.DriveItem{
410-
Id: libregraph.PtrString(storagespace.FormatResourceID(res.GetId())),
411-
Size: size,
410+
Id: libregraph.PtrString(storagespace.FormatResourceID(res.GetId())),
411+
Size: size,
412+
WebUrl: g.webURLForResource(res.GetId()),
412413
}
413414

414-
webURL := *publicBaseURL
415-
webURL.Path = path.Join(webURL.Path, "f", storagespace.FormatResourceID(res.GetId()))
416-
driveItem.WebUrl = libregraph.PtrString(webURL.String())
417-
418415
if name := path.Base(res.GetPath()); name != "" {
419416
driveItem.Name = &name
420417
}
@@ -453,10 +450,10 @@ func cs3ResourceToDriveItem(logger *log.Logger, publicBaseURL *url.URL, res *sto
453450
}
454451

455452
if res.GetArbitraryMetadata() != nil {
456-
driveItem.Audio = cs3ResourceToDriveItemAudioFacet(logger, res)
457-
driveItem.Image = cs3ResourceToDriveItemImageFacet(logger, res)
458-
driveItem.Location = cs3ResourceToDriveItemLocationFacet(logger, res)
459-
driveItem.Photo = cs3ResourceToDriveItemPhotoFacet(logger, res)
453+
driveItem.Audio = cs3ResourceToDriveItemAudioFacet(g.logger, res)
454+
driveItem.Image = cs3ResourceToDriveItemImageFacet(g.logger, res)
455+
driveItem.Location = cs3ResourceToDriveItemLocationFacet(g.logger, res)
456+
driveItem.Photo = cs3ResourceToDriveItemPhotoFacet(g.logger, res)
460457
}
461458

462459
return driveItem, nil

services/graph/pkg/service/v0/driveitems_weburl_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ func TestCS3ResourceToDriveItemPopulatesWebUrl(t *testing.T) {
2626
base, err := url.Parse("https://example.com")
2727
require.NoError(t, err)
2828

29-
item, err := cs3ResourceToDriveItem(&logger, base, res)
29+
g := BaseGraphService{logger: &logger, publicBaseURL: base}
30+
item, err := g.cs3ResourceToDriveItem(res)
3031
require.NoError(t, err)
3132
require.NotNil(t, item.WebUrl)
3233
assert.Equal(t, "https://example.com/f/storage-1$space-1%21item-1", *item.WebUrl)
@@ -36,7 +37,8 @@ func TestCS3ResourceToDriveItemPopulatesWebUrl(t *testing.T) {
3637
base, err := url.Parse("https://example.com/cloud")
3738
require.NoError(t, err)
3839

39-
item, err := cs3ResourceToDriveItem(&logger, base, res)
40+
g := BaseGraphService{logger: &logger, publicBaseURL: base}
41+
item, err := g.cs3ResourceToDriveItem(res)
4042
require.NoError(t, err)
4143
require.NotNil(t, item.WebUrl)
4244
assert.Equal(t, "https://example.com/cloud/f/storage-1$space-1%21item-1", *item.WebUrl)

services/graph/pkg/service/v0/drives.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -850,17 +850,7 @@ func (g Graph) cs3StorageSpaceToDrive(ctx context.Context, baseURL *url.URL, spa
850850
drive.Root.WebDavUrl = libregraph.PtrString(webDavURL.String())
851851
}
852852

853-
webURL, err := url.Parse(g.config.Spaces.WebDavBase)
854-
if err != nil {
855-
logger.Error().
856-
Err(err).
857-
Str("url", g.config.Spaces.WebDavBase).
858-
Msg("failed to parse webURL base url")
859-
return nil, err
860-
}
861-
862-
webURL.Path = path.Join(webURL.Path, "f", storagespace.FormatResourceID(spaceRid))
863-
drive.WebUrl = libregraph.PtrString(webURL.String())
853+
drive.WebUrl = g.webURLForResource(spaceRid)
864854

865855
if space.Owner != nil && space.Owner.Id != nil {
866856
drive.Owner = &libregraph.IdentitySet{

services/graph/pkg/service/v0/follow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (g Graph) FollowDriveItem(w http.ResponseWriter, r *http.Request) {
9494
}
9595
}
9696

97-
driveItem, err := cs3ResourceToDriveItem(g.logger, g.publicBaseURL, statRes.GetInfo())
97+
driveItem, err := g.cs3ResourceToDriveItem(statRes.GetInfo())
9898
if err != nil {
9999
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
100100
return

0 commit comments

Comments
 (0)