Skip to content

Commit

Permalink
Adding the /app/new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Sep 17, 2021
1 parent 0b33ceb commit 61bff26
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
18 changes: 18 additions & 0 deletions internal/grpc/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ func getProvider(c *config) (app.Provider, error) {
return nil, errtypes.NotFound("driver not found: " + c.Driver)
}

func (s *service) CreateFileForApp(ctx context.Context, req *providerpb.CreateFileForAppRequest) (*providerpb.CreateFileForAppResponse, error) {
fileInfo, err := s.provider.CreateFile(ctx, req.Ref, req.Filename)
if err != nil {
err := errors.Wrap(err, "appprovider: error calling CreateFile")
res := &providerpb.CreateFileForAppResponse{
Status: status.NewInternal(ctx, err, "error creating file"),
}
return res, nil
}

res := &providerpb.OpenInAppResponse{
Status: status.NewOK(ctx),
ResourceInfo: fileInfo,
}
return res, nil

}

func (s *service) OpenInApp(ctx context.Context, req *providerpb.OpenInAppRequest) (*providerpb.OpenInAppResponse, error) {
appURL, err := s.provider.GetAppURL(ctx, req.ResourceInfo, req.ViewMode, req.AccessToken)
if err != nil {
Expand Down
44 changes: 44 additions & 0 deletions internal/grpc/services/gateway/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,50 @@ import (
"google.golang.org/grpc/metadata"
)

func (s *svc) CreateFileForApp(ctx context.Context, req *providerpb.CreateFileForAppRequest) (*providerpb.CreateFileForAppResponse, error) {
p, st := s.getPath(ctx, req.Ref)
if st.Code != rpc.Code_CODE_OK {
if st.Code == rpc.Code_CODE_NOT_FOUND {
return &providerpb.CreateFileForAppResponse{
Status: status.NewNotFound(ctx, "gateway: container resource not found:"+req.Ref.String()),
}, nil
}
return &providerpb.CreateFileForAppResponse{
Status: st,
}, nil
}

// TODO identify app provider, not like this
provider, err := s.findAppProvider(ctx, ri, app)
if err != nil {
err = errors.Wrap(err, "gateway: error calling findAppProvider")
var st *rpc.Status
if _, ok := err.(errtypes.IsNotFound); ok {
st = status.NewNotFound(ctx, "app provider not found")
} else {
st = status.NewInternal(ctx, err, "error searching for app provider")
}
return &providerpb.CreateFileForAppResponse{
Status: st,
}, nil
}

appProviderClient, err := pool.GetAppProviderClient(provider.Address)
if err != nil {
err = errors.Wrap(err, "gateway: error calling GetAppProviderClient")
return &providerpb.CreateFileForAppResponse{
Status: status.NewInternal(ctx, err, "error getting appprovider client"),
}, nil
}

res, err := appProviderClient.CreateFileForApp(ctx, req)
if err != nil {
return nil, errors.Wrap(err, "gateway: error calling CreateFileForApp")
}

return res, nil
}

func (s *svc) OpenInApp(ctx context.Context, req *gateway.OpenInAppRequest) (*providerpb.OpenInAppResponse, error) {
p, st := s.getPath(ctx, req.Ref)
if st.Code != rpc.Code_CODE_OK {
Expand Down
44 changes: 44 additions & 0 deletions internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func (s *svc) Handler() http.Handler {
s.handleList(w, r)
case "open":
s.handleOpen(w, r)
case "new":
s.handleNew(w, r)
}
})
}
Expand Down Expand Up @@ -182,6 +184,48 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
}
}

func (s *svc) handleNew(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

client, err := pool.GetGatewayServiceClient(s.conf.GatewaySvc)
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error getting grpc gateway client", err)
return
}

info, errCode, err := s.getStatInfo(ctx, r.URL.Query().Get("container"), client)
if err != nil {
ocmd.WriteError(w, r, errCode, "error statting container", err)
return
}

createReq := gateway.CreateFileForAppRequest{
Ref: &provider.Reference{ResourceId: info.Id},
Filename: r.URL.Query().Get("filename"),
}
createRes, err := client.CreateFileForApp(ctx, &createReq)
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error opening resource", err)
return
}
if createRes.Status.Code != rpc.Code_CODE_OK {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error opening resource information", status.NewErrorFromCode(openRes.Status.Code, "appprovider"))
return
}

js, err := json.Marshal(createRes.resourceInfo.info)
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error marshalling JSON response", err)
return
}

w.Header().Set("Content-Type", "application/json")
if _, err = w.Write(js); err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error writing JSON response", err)
return
}
}

func filterAppsByUserAgent(mimeTypes *appregistry.MimeTypeList, userAgent string) {
ua := ua.Parse(userAgent)
res := []*appregistry.MimeTypeInfo{}
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ type Registry interface {
}

// Provider is the interface that application providers implement
// for providing the URL of the app which will serve the requested resource.
// for interacting with external apps that serve the requested resource.
type Provider interface {
CreateFile(ctx context.Context, ref *provider.Reference, filename string) (*provider.ResourceInfo, error)
GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, token string) (*appprovider.OpenInAppURL, error)
GetAppProviderInfo(ctx context.Context) (*registry.ProviderInfo, error)
}
6 changes: 6 additions & 0 deletions pkg/app/provider/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ type demoProvider struct {
iframeUIProvider string
}

func (p *demoProvider) CreateFile(ctx context.Context, ref *provider.Reference, filename string) (*provider.ResourceInfo, error)
return &provider.ResourceInfo{
...
}, nil
}

func (p *demoProvider) GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, token string) (*appprovider.OpenInAppURL, error) {
url := 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 &appprovider.OpenInAppURL{
Expand Down
8 changes: 7 additions & 1 deletion pkg/app/provider/wopi/wopi.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package demo
package wopi

import (
"bytes"
Expand Down Expand Up @@ -117,6 +117,12 @@ func New(m map[string]interface{}) (app.Provider, error) {
}, nil
}

func (p *wopiProvider) CreateFile(ctx context.Context, ref *provider.Reference, filename string) (*provider.ResourceInfo, error)
// TODO call webdav PUT
return &provider.ResourceInfo{
}, nil
}

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

Expand Down

0 comments on commit 61bff26

Please sign in to comment.