Skip to content

Commit

Permalink
feat: add wopi host and sharing urls
Browse files Browse the repository at this point in the history
  • Loading branch information
micbar committed Sep 26, 2024
1 parent f2260f0 commit 0067ce2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 22 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/wopi-host-urls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Add WOPI host URLs to the collaboration service

We added the WOPI host urls to create a better integration with WOPI clients. This allows the WOPI apps to display links to our sharing and versions panel in the UI.

https://github.com/owncloud/ocis/pull/10174
61 changes: 51 additions & 10 deletions services/collaboration/pkg/connector/fileconnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,14 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (*ConnectorResponse,
if err != nil {
return nil, err
}

fileRef := wopiContext.FileReference
// set path to empty string to get the full path from the stat
fileRef.Path = ""
statRes, err := gwc.Stat(ctx, &providerv1beta1.StatRequest{
Ref: wopiContext.FileReference,
Ref: fileRef,
})

if err != nil {
logger.Error().Err(err).Msg("CheckFileInfo: stat failed")
return nil, err
Expand Down Expand Up @@ -1175,18 +1180,16 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (*ConnectorResponse,
}

breadcrumbFolderName := path.Dir(statRes.Info.Path)
if breadcrumbFolderName == "." || breadcrumbFolderName == "" {
if breadcrumbFolderName == "." || breadcrumbFolderName == "" || breadcrumbFolderName == "/" {
breadcrumbFolderName = statRes.GetInfo().GetSpace().GetName()
}

ocisUrl, err := url.Parse(f.cfg.Commons.OcisURL)
ocisURL, err := url.Parse(f.cfg.Commons.OcisURL)
if err != nil {
return nil, err
}
breadcrumbFolderURL, viewAppUrl, editAppUrl := *ocisUrl, *ocisUrl, *ocisUrl
breadcrumbFolderURL.Path = path.Join(breadcrumbFolderURL.Path, "f", storagespace.FormatResourceID(statRes.GetInfo().GetId()))
viewAppUrl.Path = path.Join(viewAppUrl.Path, "external"+strings.ToLower(f.cfg.App.Name))
editAppUrl.Path = path.Join(editAppUrl.Path, "external"+strings.ToLower(f.cfg.App.Name))
privateLinkURL := *ocisURL
privateLinkURL.Path = path.Join(ocisURL.Path, "f", storagespace.FormatResourceID(statRes.GetInfo().GetId()))
// fileinfo map
infoMap := map[string]interface{}{
fileinfo.KeyOwnerID: hexEncodedOwnerId,
Expand All @@ -1196,10 +1199,12 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (*ConnectorResponse,
fileinfo.KeyBreadcrumbDocName: path.Base(statRes.GetInfo().GetPath()),
// to get the folder we actually need to do a GetPath() request
fileinfo.KeyBreadcrumbFolderName: breadcrumbFolderName,
fileinfo.KeyBreadcrumbFolderURL: breadcrumbFolderURL.String(),
fileinfo.KeyBreadcrumbFolderURL: privateLinkURL.String(),

//fileinfo.KeyHostViewURL: viewAppUrl.String(),
//fileinfo.KeyHostEditURL: editAppUrl.String(),
fileinfo.KeyHostViewURL: createHostUrl("view", ocisURL, f.cfg.App.Name, statRes.GetInfo()),
fileinfo.KeyHostEditURL: createHostUrl("write", ocisURL, f.cfg.App.Name, statRes.GetInfo()),
fileinfo.KeyFileSharingURL: createShareUrl(&privateLinkURL),
fileinfo.KeyFileVersionURL: createVersionsUrl(&privateLinkURL),

fileinfo.KeyEnableOwnerTermination: true, // only for collabora
fileinfo.KeySupportsExtendedLockLength: true,
Expand Down Expand Up @@ -1240,6 +1245,42 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (*ConnectorResponse,
return NewResponseSuccessBody(info), nil
}

func createHostUrl(mode string, ocisUrl *url.URL, appName string, info *providerv1beta1.ResourceInfo) string {
webUrl := createAppExternalURL(ocisUrl, appName, info)
addURLParams(webUrl, map[string]string{"view_mode": mode})
return webUrl.String()
}

func createShareUrl(ocisURL *url.URL) string {
shareURL := *ocisURL
addURLParams(&shareURL, map[string]string{"details": "sharing"})
return shareURL.String()
}

func createVersionsUrl(ocisURL *url.URL) string {
versionsURL := *ocisURL
addURLParams(&versionsURL, map[string]string{"details": "versions"})
return versionsURL.String()
}

func addURLParams(u *url.URL, params map[string]string) {
q := u.Query()
for k, v := range params {
q.Add(k, v)
}
u.RawQuery = q.Encode()
}

func createAppExternalURL(ocisURL *url.URL, appName string, info *providerv1beta1.ResourceInfo) *url.URL {
spaceAlias := utils.ReadPlainFromOpaque(info.GetSpace().GetOpaque(), "spaceAlias")
appExternalURL := *ocisURL
appExternalURL.Path = path.Join(ocisURL.Path, "external-"+strings.ToLower(appName), spaceAlias, info.GetPath())
q := ocisURL.Query()
q.Add("fileId", storagespace.FormatResourceID(info.GetId()))
appExternalURL.RawQuery = q.Encode()
return &appExternalURL
}

func (f *FileConnector) watermarkText(user *userv1beta1.User) string {
if user != nil {
return strings.TrimSpace(user.GetDisplayName() + " " + user.GetMail())
Expand Down
23 changes: 13 additions & 10 deletions services/collaboration/pkg/connector/fileconnector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var _ = Describe("FileConnector", func() {
},
App: config.App{
LockName: "testName_for_unittests", // Only the LockName is used
Name: "test",
},
Wopi: config.Wopi{
WopiSrc: "https://ocis.server.prv",
Expand Down Expand Up @@ -1694,16 +1695,14 @@ var _ = Describe("FileConnector", func() {
}, nil)

expectedFileInfo := &fileinfo.Microsoft{
OwnerID: "61616262636340637573746f6d496470", // hex of aabbcc@customIdp
Size: int64(998877),
Version: "v162738490",
BaseFileName: "test.txt",
BreadcrumbDocName: "test.txt",
BreadcrumbFolderName: "/path/to",
BreadcrumbFolderURL: "https://ocis.example.prv/f/storageid$spaceid%21opaqueid",
UserCanNotWriteRelative: false,
//HostViewURL: "http://test.ex.prv/view",
//HostEditURL: "http://test.ex.prv/edit",
OwnerID: "61616262636340637573746f6d496470", // hex of aabbcc@customIdp
Size: int64(998877),
Version: "v162738490",
BaseFileName: "test.txt",
BreadcrumbDocName: "test.txt",
BreadcrumbFolderName: "/path/to",
BreadcrumbFolderURL: "https://ocis.example.prv/f/storageid$spaceid%21opaqueid",
UserCanNotWriteRelative: false,
SupportsExtendedLockLength: true,
SupportsGetLock: true,
SupportsLocks: true,
Expand All @@ -1714,6 +1713,10 @@ var _ = Describe("FileConnector", func() {
UserCanRename: true,
UserID: "61646d696e40637573746f6d496470", // hex of admin@customIdp
UserFriendlyName: "Pet Shaft",
FileSharingURL: "https://ocis.example.prv/f/storageid$spaceid%21opaqueid?details=sharing",
FileVersionURL: "https://ocis.example.prv/f/storageid$spaceid%21opaqueid?details=versions",
HostEditURL: "https://ocis.example.prv/external-test/path/to/test.txt?fileId=storageid%24spaceid%21opaqueid&view_mode=write",
HostViewURL: "https://ocis.example.prv/external-test/path/to/test.txt?fileId=storageid%24spaceid%21opaqueid&view_mode=view",
}

response, err := fc.CheckFileInfo(ctx)
Expand Down
28 changes: 26 additions & 2 deletions tests/acceptance/features/apiCollaboration/checkFileInfo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ Feature: check file info with different wopi apps
"UserCanWrite",
"SupportsLocks",
"SupportsRename",
"SupportsUpdate"
"SupportsUpdate",
"HostEditUrl",
"FileSharingUrl",
"FileVersionUrl"
],
"properties": {
"BaseFileName": {
Expand Down Expand Up @@ -285,6 +288,15 @@ Feature: check file info with different wopi apps
},
"PostMessageOrigin": {
"type": "string"
},
"FileSharingUrl": {
"type": "string"
},
"HostEditUrl": {
"type": "string"
},
"FileVersionUrl": {
"type": "string"
}
}
}
Expand Down Expand Up @@ -315,7 +327,10 @@ Feature: check file info with different wopi apps
"UserCanWrite",
"SupportsLocks",
"SupportsRename",
"SupportsUpdate"
"SupportsUpdate",
"HostEditUrl",
"FileSharingUrl",
"FileVersionUrl"
],
"properties": {
"BaseFileName": {
Expand Down Expand Up @@ -365,6 +380,15 @@ Feature: check file info with different wopi apps
},
"PostMessageOrigin": {
"type": "string"
},
"FileSharingUrl": {
"type": "string"
},
"HostEditUrl": {
"type": "string"
},
"FileVersionUrl": {
"type": "string"
}
}
}
Expand Down

0 comments on commit 0067ce2

Please sign in to comment.