diff --git a/services/collaboration/mocks/file_connector_service.go b/services/collaboration/mocks/file_connector_service.go index 126d29a474..9518dd36ae 100644 --- a/services/collaboration/mocks/file_connector_service.go +++ b/services/collaboration/mocks/file_connector_service.go @@ -447,31 +447,40 @@ func (_c *FileConnectorService_RefreshLock_Call) RunAndReturn(run func(context.C } // RenameFile provides a mock function with given fields: ctx, lockID, target -func (_m *FileConnectorService) RenameFile(ctx context.Context, lockID string, target string) (string, error) { +func (_m *FileConnectorService) RenameFile(ctx context.Context, lockID string, target string) (*connector.RenameResponse, string, error) { ret := _m.Called(ctx, lockID, target) if len(ret) == 0 { panic("no return value specified for RenameFile") } - var r0 string - var r1 error - if rf, ok := ret.Get(0).(func(context.Context, string, string) (string, error)); ok { + var r0 *connector.RenameResponse + var r1 string + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, string, string) (*connector.RenameResponse, string, error)); ok { return rf(ctx, lockID, target) } - if rf, ok := ret.Get(0).(func(context.Context, string, string) string); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, string) *connector.RenameResponse); ok { r0 = rf(ctx, lockID, target) } else { - r0 = ret.Get(0).(string) + if ret.Get(0) != nil { + r0 = ret.Get(0).(*connector.RenameResponse) + } } - if rf, ok := ret.Get(1).(func(context.Context, string, string) error); ok { + if rf, ok := ret.Get(1).(func(context.Context, string, string) string); ok { r1 = rf(ctx, lockID, target) } else { - r1 = ret.Error(1) + r1 = ret.Get(1).(string) } - return r0, r1 + if rf, ok := ret.Get(2).(func(context.Context, string, string) error); ok { + r2 = rf(ctx, lockID, target) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 } // FileConnectorService_RenameFile_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RenameFile' @@ -494,12 +503,12 @@ func (_c *FileConnectorService_RenameFile_Call) Run(run func(ctx context.Context return _c } -func (_c *FileConnectorService_RenameFile_Call) Return(_a0 string, _a1 error) *FileConnectorService_RenameFile_Call { - _c.Call.Return(_a0, _a1) +func (_c *FileConnectorService_RenameFile_Call) Return(_a0 *connector.RenameResponse, _a1 string, _a2 error) *FileConnectorService_RenameFile_Call { + _c.Call.Return(_a0, _a1, _a2) return _c } -func (_c *FileConnectorService_RenameFile_Call) RunAndReturn(run func(context.Context, string, string) (string, error)) *FileConnectorService_RenameFile_Call { +func (_c *FileConnectorService_RenameFile_Call) RunAndReturn(run func(context.Context, string, string) (*connector.RenameResponse, string, error)) *FileConnectorService_RenameFile_Call { _c.Call.Return(run) return _c } diff --git a/services/collaboration/pkg/connector/fileconnector.go b/services/collaboration/pkg/connector/fileconnector.go index 74617def72..bcd668fdd4 100644 --- a/services/collaboration/pkg/connector/fileconnector.go +++ b/services/collaboration/pkg/connector/fileconnector.go @@ -1164,23 +1164,11 @@ func (f *FileConnector) generatePrefix() string { return base64.RawURLEncoding.EncodeToString(bytes.TrimLeft(byteArray, "\x00")) } +// The adjustWopiReference should be called first so the file reference +// contains the resource id of the target file without the path +// (storage, opaque and space points directly to the file). The path component +// will be ignored func (f *FileConnector) generateWOPISrc(ctx context.Context, wopiContext middleware.WopiContext, logger zerolog.Logger) (*url.URL, error) { - statRes, err := f.gwc.Stat(ctx, &providerv1beta1.StatRequest{ - Ref: &wopiContext.FileReference, - }) - if err != nil { - logger.Error().Err(err).Msg("generateWOPISrc: stat failed") - return nil, err - } - - if statRes.GetStatus().GetCode() != rpcv1beta1.Code_CODE_OK { - logger.Error(). - Str("StatusCode", statRes.GetStatus().GetCode().String()). - Str("StatusMsg", statRes.GetStatus().GetMessage()). - Msg("generateWOPISrc: stat failed with unexpected status") - return nil, NewConnectorError(500, statRes.GetStatus().GetCode().String()+" "+statRes.GetStatus().GetMessage()) - } - // get the WOPI token for the new file accessToken, _, err := middleware.GenerateWopiToken(wopiContext, f.cfg) if err != nil { @@ -1189,8 +1177,9 @@ func (f *FileConnector) generateWOPISrc(ctx context.Context, wopiContext middlew } // get the reference + resourceId := wopiContext.FileReference.GetResourceId() c := sha256.New() - c.Write([]byte(statRes.GetInfo().GetId().GetStorageId() + "$" + statRes.GetInfo().GetId().GetSpaceId() + "!" + statRes.GetInfo().GetId().GetOpaqueId())) + c.Write([]byte(resourceId.GetStorageId() + "$" + resourceId.GetSpaceId() + "!" + resourceId.GetOpaqueId())) fileRef := hex.EncodeToString(c.Sum(nil)) // generate the URL for the WOPI app to access the new created file diff --git a/services/collaboration/pkg/connector/fileconnector_test.go b/services/collaboration/pkg/connector/fileconnector_test.go index da67a1ccf1..d6f88c5ad7 100644 --- a/services/collaboration/pkg/connector/fileconnector_test.go +++ b/services/collaboration/pkg/connector/fileconnector_test.go @@ -1307,9 +1307,10 @@ var _ = Describe("FileConnector", func() { Describe("RenameFile", func() { It("No valid context", func() { ctx := context.Background() - lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") + response, lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") Expect(err).To(HaveOccurred()) Expect(lockID).To(Equal("")) + Expect(response).To(BeNil()) }) It("Stat fails", func() { @@ -1320,10 +1321,11 @@ var _ = Describe("FileConnector", func() { Status: status.NewInternal(ctx, "something failed"), }, targetErr) - lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") + response, lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") Expect(err).To(HaveOccurred()) Expect(err).To(Equal(targetErr)) Expect(lockID).To(Equal("")) + Expect(response).To(BeNil()) }) It("Stat fails status not ok", func() { @@ -1333,11 +1335,12 @@ var _ = Describe("FileConnector", func() { Status: status.NewInternal(ctx, "something failed"), }, nil) - lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") + response, lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") Expect(err).To(HaveOccurred()) conErr := err.(*connector.ConnectorError) Expect(conErr.HttpCodeOut).To(Equal(500)) Expect(lockID).To(Equal("")) + Expect(response).To(BeNil()) }) It("Rename failed", func() { @@ -1358,10 +1361,11 @@ var _ = Describe("FileConnector", func() { Status: status.NewInternal(ctx, "something failed"), }, targetErr) - lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") + response, lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") Expect(err).To(HaveOccurred()) Expect(err).To(Equal(targetErr)) Expect(lockID).To(Equal("")) + Expect(response).To(BeNil()) }) It("Rename failed status not ok", func() { @@ -1381,11 +1385,12 @@ var _ = Describe("FileConnector", func() { Status: status.NewInternal(ctx, "something failed"), }, nil) - lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") + response, lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") Expect(err).To(HaveOccurred()) conErr := err.(*connector.ConnectorError) Expect(conErr.HttpCodeOut).To(Equal(500)) Expect(lockID).To(Equal("")) + Expect(response).To(BeNil()) }) It("Rename conflict", func() { @@ -1405,11 +1410,12 @@ var _ = Describe("FileConnector", func() { Status: status.NewLocked(ctx, "lock mismatch"), }, nil) - lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") + response, lockID, err := fc.RenameFile(ctx, "lockid", "newFile.doc") Expect(err).To(HaveOccurred()) conErr := err.(*connector.ConnectorError) Expect(conErr.HttpCodeOut).To(Equal(409)) Expect(lockID).To(Equal("zzz999")) + Expect(response).To(BeNil()) }) It("Rename already exists", func() { @@ -1448,9 +1454,10 @@ var _ = Describe("FileConnector", func() { Status: status.NewOK(ctx), }, nil).Once() - lockID, err := fc.RenameFile(ctx, "zzz999", "newFile.doc") + response, lockID, err := fc.RenameFile(ctx, "zzz999", "newFile.doc") Expect(err).To(Succeed()) Expect(lockID).To(Equal("")) + Expect(response.Name).To(MatchRegexp(`^[a-zA-Z0-9_-]+ newFile$`)) }) It("Success", func() { @@ -1477,9 +1484,10 @@ var _ = Describe("FileConnector", func() { Status: status.NewOK(ctx), }, nil).Once() - lockID, err := fc.RenameFile(ctx, "zzz999", "newFile.doc") + response, lockID, err := fc.RenameFile(ctx, "zzz999", "newFile.doc") Expect(err).To(Succeed()) Expect(lockID).To(Equal("")) + Expect(response.Name).To(Equal("newFile")) }) })