Skip to content

Commit

Permalink
Boiler plate code to implement the /app/new HTTP endpoint
Browse files Browse the repository at this point in the history
This includes the GRPC gateway, the appprovider and its drivers
  • Loading branch information
glpatcern committed Sep 20, 2021
1 parent 0edd9c8 commit f156562
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 5 deletions.
6 changes: 5 additions & 1 deletion changelog/unreleased/appreg-mimetypes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Enhancement: handle mimetypes and their friendly names
Enhancement: UI improvements for the AppProviders

Mime types and their friendly names are now handled in
the /app/list HTTP endpoint, and an additional /app/new
endpoint is made available to create new files for apps.

https://github.com/cs3org/cs3apis/pull/145
https://github.com/cs3org/reva/pull/2067
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, req.Template)
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.CreateFileForAppResponse{
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
45 changes: 45 additions & 0 deletions internal/grpc/services/gateway/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,51 @@ import (
"google.golang.org/grpc/metadata"
)

func (s *svc) CreateFileForApp(ctx context.Context, req *providerpb.CreateFileForAppRequest) (*providerpb.CreateFileForAppResponse, error) {
_, 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: take the GetDefaultProviderForMimeType for the given file extension
// provider, err := s.getDefaultAppProvider(ctx, req.Ref.path)
// 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")
// }
res := &providerpb.CreateFileForAppResponse{}

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
45 changes: 45 additions & 0 deletions internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"unicode/utf8"

appprovider "github.com/cs3org/go-cs3apis/cs3/app/provider/v1beta1"
appregistry "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
Expand Down Expand Up @@ -98,6 +99,8 @@ func (s *svc) Handler() http.Handler {
head, r.URL.Path = router.ShiftPath(r.URL.Path)

switch head {
case "new":
s.handleNew(w, r)
case "list":
s.handleList(w, r)
case "open":
Expand All @@ -106,6 +109,48 @@ func (s *svc) Handler() http.Handler {
})
}

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 := appprovider.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 creating resource", err)
return
}
if createRes.Status.Code != rpc.Code_CODE_OK {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error creating resource", status.NewErrorFromCode(createRes.Status.Code, "appprovider"))
return
}

js, err := json.Marshal(createRes.ResourceInfo)
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 (s *svc) handleList(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
client, err := pool.GetGatewayServiceClient(s.conf.GatewaySvc)
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, template 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)
}
16 changes: 14 additions & 2 deletions pkg/app/provider/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ package demo
import (
"context"
"fmt"

"github.com/cs3org/reva/pkg/app"
"path"

appprovider "github.com/cs3org/go-cs3apis/cs3/app/provider/v1beta1"
appregistry "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/app"
"github.com/cs3org/reva/pkg/app/provider/registry"
uuid "github.com/google/uuid"
"github.com/mitchellh/mapstructure"
)

Expand All @@ -39,6 +40,17 @@ type demoProvider struct {
iframeUIProvider string
}

func (p *demoProvider) CreateFile(ctx context.Context, ref *provider.Reference, filename string, template string) (*provider.ResourceInfo, error) {
return &provider.ResourceInfo{
Id: &provider.ResourceId{
StorageId: "/",
OpaqueId: uuid.New().String(),
},
Type: provider.ResourceType_RESOURCE_TYPE_FILE,
Path: path.Join(ref.GetPath(), filename),
}, 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
14 changes: 13 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,18 @@ func New(m map[string]interface{}) (app.Provider, error) {
}, nil
}

func (p *wopiProvider) CreateFile(ctx context.Context, ref *provider.Reference, filename string, template string) (*provider.ResourceInfo, error) {
if template != "" {
// TODO create a file out of the given template
return nil, errtypes.NotSupported("Creating documents from templates is not yet supported")
}
// TODO call webdav PUT
return &provider.ResourceInfo{
Type: provider.ResourceType_RESOURCE_TYPE_FILE,
Path: path.Join(ref.GetPath(), filename),
}, 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 f156562

Please sign in to comment.