Skip to content

Commit

Permalink
Refactoring HTTP service
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Aug 9, 2021
1 parent c3fb416 commit d667d61
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 70 deletions.
16 changes: 2 additions & 14 deletions cmd/reva/open-in-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
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/pkg/utils"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func openInAppCommand() *command {
}
path := cmd.Args()[0]

vm := getViewMode(*viewMode)
vm := utils.GetViewMode(*viewMode)

client, err := getClient()
if err != nil {
Expand Down Expand Up @@ -92,16 +93,3 @@ func openInAppCommand() *command {
}
return cmd
}

func getViewMode(viewMode string) gateway.OpenInAppRequest_ViewMode {
switch viewMode {
case "view":
return gateway.OpenInAppRequest_VIEW_MODE_VIEW_ONLY
case "read":
return gateway.OpenInAppRequest_VIEW_MODE_READ_ONLY
case "write":
return gateway.OpenInAppRequest_VIEW_MODE_READ_WRITE
default:
return gateway.OpenInAppRequest_VIEW_MODE_INVALID
}
}
84 changes: 28 additions & 56 deletions internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import (
"encoding/base64"
"encoding/json"
"net/http"
"net/url"
"strings"
"time"
"unicode/utf8"

appregistry "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1"
Expand All @@ -39,6 +37,7 @@ import (
"github.com/cs3org/reva/pkg/rhttp/global"
"github.com/cs3org/reva/pkg/rhttp/router"
"github.com/cs3org/reva/pkg/sharedconf"
"github.com/cs3org/reva/pkg/utils"
ua "github.com/mileusna/useragent"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
Expand Down Expand Up @@ -112,30 +111,6 @@ func (s *svc) Handler() http.Handler {
})
}

// WopiResponse holds the various fields to be returned for a wopi open call
type WopiResponse struct {
WopiClientURL string `json:"wopiclienturl"`
AccessToken string `json:"accesstoken"`
AccessTokenTTL int64 `json:"accesstokenttl"`
}

func filterAppsByUserAgent(mimeTypes map[string]*appregistry.AppProviderList, userAgent string) {
ua := ua.Parse(userAgent)
if ua.Desktop {
return
}

for m, providers := range mimeTypes {
apps := []*appregistry.ProviderInfo{}
for _, p := range providers.AppProviders {
if !p.DesktopOnly {
apps = append(apps, p)
}
}
mimeTypes[m] = &appregistry.AppProviderList{AppProviders: apps}
}
}

func (s *svc) handleList(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := appctx.GetLogger(ctx)
Expand Down Expand Up @@ -182,15 +157,16 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
return
}

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

openReq := gateway.OpenInAppRequest{
Ref: &provider.Reference{ResourceId: info.Id},
ViewMode: getViewMode(info),
App: r.URL.Query().Get("app"),
ViewMode: getViewMode(info, r.URL.Query().Get("view_mode")),
App: r.URL.Query().Get("app_name"),
}
openRes, err := client.OpenInApp(ctx, &openReq)
if err != nil {
Expand All @@ -202,32 +178,7 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
return
}

u, err := url.Parse(openRes.AppUrl.AppUrl)
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error parsing app URL", err)
return
}
q := u.Query()

// remove access token from query parameters
accessToken := q.Get("access_token")
q.Del("access_token")

// more options used by oC 10:
// &lang=en-GB
// &closebutton=1
// &revisionhistory=1
// &title=Hello.odt
u.RawQuery = q.Encode()

js, err := json.Marshal(
WopiResponse{
WopiClientURL: u.String(),
AccessToken: accessToken,
// https://wopi.readthedocs.io/projects/wopirest/en/latest/concepts.html#term-access-token-ttl
AccessTokenTTL: time.Now().Add(time.Second*time.Duration(s.conf.AccessTokenTTL)).UnixNano() / 1e6,
},
)
js, err := json.Marshal(openRes.AppUrl)
if err != nil {
ocmd.WriteError(w, r, ocmd.APIErrorServerError, "error marshalling JSON response", err)
return
Expand All @@ -240,6 +191,23 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
}
}

func filterAppsByUserAgent(mimeTypes map[string]*appregistry.AppProviderList, userAgent string) {
ua := ua.Parse(userAgent)
if ua.Desktop {
return
}

for m, providers := range mimeTypes {
apps := []*appregistry.ProviderInfo{}
for _, p := range providers.AppProviders {
if !p.DesktopOnly {
apps = append(apps, p)
}
}
mimeTypes[m] = &appregistry.AppProviderList{AppProviders: apps}
}
}

func (s *svc) getStatInfo(ctx context.Context, fileID string, client gateway.GatewayAPIClient) (*provider.ResourceInfo, ocmd.APIErrorCode, error) {
if fileID == "" {
return nil, ocmd.APIErrorInvalidParameter, errors.New("fileID parameter missing in request")
Expand Down Expand Up @@ -276,7 +244,11 @@ func (s *svc) getStatInfo(ctx context.Context, fileID string, client gateway.Gat
return statRes.Info, ocmd.APIErrorCode(""), nil
}

func getViewMode(res *provider.ResourceInfo) gateway.OpenInAppRequest_ViewMode {
func getViewMode(res *provider.ResourceInfo, vm string) gateway.OpenInAppRequest_ViewMode {
if vm != "" {
return utils.GetViewMode(vm)
}

var viewMode gateway.OpenInAppRequest_ViewMode
canEdit := res.PermissionSet.InitiateFileUpload
canView := res.PermissionSet.InitiateFileDownload
Expand Down
15 changes: 15 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"strings"
"time"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
grouppb "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
Expand Down Expand Up @@ -271,3 +272,17 @@ func UserTypeToString(accountType userpb.UserType) string {
}
return t
}

// GetViewMode converts a human-readable string to a view mode for opening a resource in an app.
func GetViewMode(viewMode string) gateway.OpenInAppRequest_ViewMode {
switch viewMode {
case "view":
return gateway.OpenInAppRequest_VIEW_MODE_VIEW_ONLY
case "read":
return gateway.OpenInAppRequest_VIEW_MODE_READ_ONLY
case "write":
return gateway.OpenInAppRequest_VIEW_MODE_READ_WRITE
default:
return gateway.OpenInAppRequest_VIEW_MODE_INVALID
}
}

0 comments on commit d667d61

Please sign in to comment.