Skip to content

Commit

Permalink
Moved boiler-plate code to the gateway service
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Sep 21, 2021
1 parent f156562 commit b3220f5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 108 deletions.
18 changes: 0 additions & 18 deletions internal/grpc/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,6 @@ 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: 0 additions & 45 deletions internal/grpc/services/gateway/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,51 +42,6 @@ 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
105 changes: 86 additions & 19 deletions internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@ import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"path"
"strconv"
"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"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/internal/http/services/datagateway"
"github.com/cs3org/reva/internal/http/services/ocmd"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/pkg/rhttp"
"github.com/cs3org/reva/pkg/rhttp/global"
"github.com/cs3org/reva/pkg/rhttp/router"
"github.com/cs3org/reva/pkg/sharedconf"
Expand All @@ -42,6 +48,7 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func init() {
Expand Down Expand Up @@ -118,27 +125,87 @@ func (s *svc) handleNew(w http.ResponseWriter, r *http.Request) {
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)
if r.URL.Query().Get("template") != "" {
// TODO in the future we want to create a file out of the given template
ocmd.WriteError(w, r, ocmd.APIErrorInvalidParameter, "Template not implemented",
errtypes.NotSupported("Templates are not yet supported"))
return
}

createReq := appprovider.CreateFileForAppRequest{
Ref: &provider.Reference{ResourceId: info.Id},
Filename: r.URL.Query().Get("filename"),
target := r.URL.Query().Get("filename")
if target == "" {
ocmd.WriteError(w, r, ocmd.APIErrorInvalidParameter, "Missing filename",
errtypes.UserRequired("Missing filename"))
return
}
createRes, err := client.CreateFileForApp(ctx, &createReq)
// stat the container
_, ocmderr, err := statRef(ctx, provider.Reference{Path: r.URL.Query().Get("container")}, client)
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error creating resource", err)
log.Error().Err(err).Msg("error statting container")
ocmd.WriteError(w, r, ocmderr, "Container not found",
errtypes.NotFound("Container not found"))
return
}
// Create empty file via storageprovider: obtain the HTTP URL for a PUT
target = path.Join(r.URL.Query().Get("container"), target)
createReq := &provider.InitiateFileUploadRequest{
Ref: &provider.Reference{Path: target},
Opaque: &typespb.Opaque{
Map: map[string]*typespb.OpaqueEntry{
"Upload-Length": {
Decoder: "plain",
Value: []byte(strconv.FormatInt(0, 10)),
},
},
},
}
createRes, err := client.InitiateFileUpload(ctx, createReq)
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error calling InitiateFileUpload", 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)
// Do a HTTP PUT with an empty body
var ep, token string
for _, p := range createRes.Protocols {
if p.Protocol == "simple" {
ep, token = p.UploadEndpoint, p.Token
}
}
httpReq, err := rhttp.NewRequest(ctx, http.MethodPut, ep, nil)
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error executing PUT", err)
return
}

httpReq.Header.Set(datagateway.TokenTransportHeader, token)
httpRes, err := rhttp.GetHTTPClient().Do(httpReq)
if err != nil {
log.Error().Err(err).Msg("error doing PUT request to data service")
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error executing PUT", err)
return
}
defer httpRes.Body.Close()
if httpRes.StatusCode != http.StatusOK {
log.Error().Msg("PUT request to data server failed")
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error executing PUT",
errtypes.InternalError(fmt.Sprint(httpRes.StatusCode)))
return
}

// Stat created file and return its file id
statRes, ocmderr, err := statRef(ctx, provider.Reference{Path: target}, client)
if err != nil {
log.Error().Err(err).Msg("error statting created file")
ocmd.WriteError(w, r, ocmderr, "Created file not found",
errtypes.NotFound("Created file not found"))
return
}
js, err := json.Marshal(map[string]interface{}{"file_id": statRes.Id})
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error marshalling JSON response", err)
return
Expand Down Expand Up @@ -169,8 +236,7 @@ func (s *svc) handleList(w http.ResponseWriter, r *http.Request) {
return
}

res := listRes.MimeTypes
filterAppsByUserAgent(res, r.UserAgent())
res := filterAppsByUserAgent(listRes.MimeTypes, r.UserAgent())
js, err := json.Marshal(map[string]interface{}{"mime-types": res})
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error marshalling JSON response", err)
Expand Down Expand Up @@ -227,10 +293,10 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
}
}

func filterAppsByUserAgent(mimeTypes *appregistry.MimeTypeList, userAgent string) {
func filterAppsByUserAgent(mimeTypes []*appregistry.MimeTypeInfo, userAgent string) []*appregistry.MimeTypeInfo {
ua := ua.Parse(userAgent)
res := []*appregistry.MimeTypeInfo{}
for _, m := range mimeTypes.MimeTypes {
for _, m := range mimeTypes {
apps := []*appregistry.ProviderInfo{}
for _, p := range m.AppProviders {
p.Address = "" // address is internal only and not needed in the client
Expand All @@ -244,7 +310,7 @@ func filterAppsByUserAgent(mimeTypes *appregistry.MimeTypeList, userAgent string
res = append(res, m)
}
}
mimeTypes.MimeTypes = res
return res
}

func (s *svc) getStatInfo(ctx context.Context, fileID string, client gateway.GatewayAPIClient) (*provider.ResourceInfo, ocmd.APIErrorCode, error) {
Expand All @@ -266,9 +332,11 @@ func (s *svc) getStatInfo(ctx context.Context, fileID string, client gateway.Gat
OpaqueId: parts[1],
}

statReq := provider.StatRequest{
Ref: &provider.Reference{ResourceId: res},
}
return statRef(ctx, provider.Reference{ResourceId: res}, client)
}

func statRef(ctx context.Context, ref provider.Reference, client gateway.GatewayAPIClient) (*provider.ResourceInfo, ocmd.APIErrorCode, error) {
statReq := provider.StatRequest{Ref: &ref}
statRes, err := client.Stat(ctx, &statReq)
if err != nil {
return nil, ocmd.APIErrorServerError, err
Expand All @@ -279,7 +347,6 @@ func (s *svc) getStatInfo(ctx context.Context, fileID string, client gateway.Gat
if statRes.Info.Type != provider.ResourceType_RESOURCE_TYPE_FILE {
return nil, ocmd.APIErrorServerError, errors.New("unsupported resource type")
}

return statRes.Info, ocmd.APIErrorCode(""), nil
}

Expand Down
1 change: 0 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type Registry interface {
// Provider is the interface that application providers implement
// 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)
}
13 changes: 0 additions & 13 deletions pkg/app/provider/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ package demo
import (
"context"
"fmt"
"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 @@ -40,17 +38,6 @@ 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
12 changes: 0 additions & 12 deletions pkg/app/provider/wopi/wopi.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,6 @@ 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 b3220f5

Please sign in to comment.