Skip to content

Commit

Permalink
Add logic for choice of app in registry
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Jul 1, 2021
1 parent c6ece46 commit e32fde0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require (
go 1.16

replace (
github.com/cs3org/go-cs3apis => github.com/ishank011/go-cs3apis v0.0.0-20210628124432-b0cb570ff3ee
github.com/cs3org/go-cs3apis => github.com/ishank011/go-cs3apis v0.0.0-20210701122052-bb129518b297
github.com/eventials/go-tus => github.com/andrewmostello/go-tus v0.0.0-20200314041820-904a9904af9a
github.com/oleiade/reflections => github.com/oleiade/reflections v1.0.1
google.golang.org/grpc => google.golang.org/grpc v1.26.0 // temporary downgrade
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/inhies/go-bytesize v0.0.0-20201103132853-d0aed0d254f8/go.mod h1:KrtyD5PFj++GKkFS/7/RRrfnRhAMGQwy75GLCHWrCNs=
github.com/ishank011/go-cs3apis v0.0.0-20210628124432-b0cb570ff3ee h1:kAhkI0AB13WyGT1eAYmGK9f7bT9pNQLpNeQUurREAu8=
github.com/ishank011/go-cs3apis v0.0.0-20210628124432-b0cb570ff3ee/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/ishank011/go-cs3apis v0.0.0-20210701122052-bb129518b297 h1:v20yOPhlP4pPuoe1i1UNQzynZzSpf2c+j4zHHPM7620=
github.com/ishank011/go-cs3apis v0.0.0-20210701122052-bb129518b297/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
Expand Down
2 changes: 1 addition & 1 deletion internal/grpc/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func getProvider(c *config) (app.Provider, error) {
}

func (s *service) OpenInApp(ctx context.Context, req *providerpb.OpenInAppRequest) (*providerpb.OpenInAppResponse, error) {
appURL, err := s.provider.GetAppURL(ctx, req.ResourceInfo, req.ViewMode, req.App, req.AccessToken)
appURL, err := s.provider.GetAppURL(ctx, req.ResourceInfo, req.ViewMode, req.AccessToken)
if err != nil {
err := errors.Wrap(err, "appprovider: error calling GetAppURL")
res := &providerpb.OpenInAppResponse{
Expand Down
40 changes: 28 additions & 12 deletions internal/grpc/services/gateway/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (s *svc) openLocalResources(ctx context.Context, ri *storageprovider.Resour
}, nil
}

provider, err := s.findAppProvider(ctx, ri)
provider, err := s.findAppProvider(ctx, ri, app)
if err != nil {
err = errors.Wrap(err, "gateway: error calling findAppProvider")
var st *rpc.Status
Expand All @@ -208,7 +208,6 @@ func (s *svc) openLocalResources(ctx context.Context, ri *storageprovider.Resour
appProviderReq := &providerpb.OpenInAppRequest{
ResourceInfo: ri,
ViewMode: providerpb.OpenInAppRequest_ViewMode(vm),
App: app,
AccessToken: accessToken,
}

Expand All @@ -220,32 +219,49 @@ func (s *svc) openLocalResources(ctx context.Context, ri *storageprovider.Resour
return res, nil
}

func (s *svc) findAppProvider(ctx context.Context, ri *storageprovider.ResourceInfo) (*registry.ProviderInfo, error) {
func (s *svc) findAppProvider(ctx context.Context, ri *storageprovider.ResourceInfo, app string) (*registry.ProviderInfo, error) {
c, err := pool.GetAppRegistryClient(s.c.AppRegistryEndpoint)
if err != nil {
err = errors.Wrap(err, "gateway: error getting appregistry client")
return nil, err
}

if app == "" {
// We need to get the default provider in case app is not set
// If the default isn't set as well, we'll return the first provider which matches the mimetype
res, err := c.GetDefaultAppProviderForMimeType(ctx, &registry.GetDefaultAppProviderForMimeTypeRequest{
MimeType: ri.MimeType,
})
if err == nil && res.Status.Code == rpc.Code_CODE_OK && res.Provider != nil {
return res.Provider, nil
}
}

res, err := c.GetAppProviders(ctx, &registry.GetAppProvidersRequest{
ResourceInfo: ri,
})

if err != nil {
err = errors.Wrap(err, "gateway: error calling GetAppProviders")
return nil, err
}

// TODO(labkode): when sending an Open to the proxy we need to choose one
// provider from the list of available as the client
if res.Status.Code == rpc.Code_CODE_OK {
return res.Providers[0], nil
if res.Status.Code != rpc.Code_CODE_OK {
if res.Status.Code == rpc.Code_CODE_NOT_FOUND {
return nil, errtypes.NotFound("gateway: app provider not found for resource: " + ri.String())
}
return nil, errtypes.InternalError("gateway: error finding app providers")
}

if res.Status.Code == rpc.Code_CODE_NOT_FOUND {
return nil, errtypes.NotFound("gateway: app provider not found for resource: " + ri.String())
if app != "" {
for _, p := range res.Providers {
if p.Name == app {
return p, nil
}
}
return nil, errtypes.NotFound("gateway: app provider not found: " + app)
}

return nil, errtypes.InternalError("gateway: error finding a storage provider")
// As a fallback, return the first provider in the list
return res.Providers[0], nil
}

func getGRPCConfig(opaque *typespb.Opaque) (bool, bool) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ type Registry interface {
// Provider is the interface that application providers implement
// for providing the URL of the app which will serve the requested resource.
type Provider interface {
GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, app, token string) (string, error)
GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, token string) (string, error)
GetAppProviderInfo(ctx context.Context) (*registry.ProviderInfo, error)
}
2 changes: 1 addition & 1 deletion pkg/app/provider/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type demoProvider struct {
iframeUIProvider string
}

func (p *demoProvider) GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, app, token string) (string, error) {
func (p *demoProvider) GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, token string) (string, error) {
msg := fmt.Sprintf("<iframe src=%s/open/%s?view-mode=%s&access-token=%s />", p.iframeUIProvider, resource.Id.StorageId+":"+resource.Id.OpaqueId, viewMode.String(), token)
return msg, nil
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/app/provider/wopi/wopi.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,9 @@ func New(m map[string]interface{}) (app.Provider, error) {
}, nil
}

func (p *wopiProvider) GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, app string, token string) (string, error) {
func (p *wopiProvider) GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, token string) (string, error) {
log := appctx.GetLogger(ctx)

if app != "" && app != p.conf.AppName {
// Sanity check
return "", errors.New("AppProvider for " + p.conf.AppName + " cannot open in " + app)
}

ext := path.Ext(resource.Path)
wopiurl, err := url.Parse(p.conf.WopiURL)
if err != nil {
Expand All @@ -139,7 +134,7 @@ func (p *wopiProvider) GetAppURL(ctx context.Context, resource *provider.Resourc
q.Add("username", u.Username)
}

q.Add("appname", app)
q.Add("appname", p.conf.AppName)
q.Add("appurl", p.appURLs["edit"][ext])

if p.conf.AppIntURL != "" {
Expand Down

0 comments on commit e32fde0

Please sign in to comment.