Skip to content

Commit

Permalink
Backport from edge: added support for language locales in apps
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Oct 4, 2022
1 parent 9b39f5f commit 02bd43c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/lang-supp-in-app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: added support for language locales in apps

This is a backport of code in the edge branch

https://github.com/cs3org/reva/pull/3303
2 changes: 1 addition & 1 deletion internal/grpc/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,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.AccessToken)
appURL, err := s.provider.GetAppURL(ctx, req.ResourceInfo, req.ViewMode, req.AccessToken, "en-GB")
if err != nil {
res := &providerpb.OpenInAppResponse{
Status: status.NewInternal(ctx, errors.New("appprovider: error calling GetAppURL"), err.Error()),
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +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 {
GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, token string) (*appprovider.OpenInAppURL, error)
GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, token, language string) (*appprovider.OpenInAppURL, error)
GetAppProviderInfo(ctx context.Context) (*registry.ProviderInfo, error)
}
4 changes: 2 additions & 2 deletions pkg/app/provider/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type demoProvider struct {
iframeUIProvider string
}

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)
func (p *demoProvider) GetAppURL(ctx context.Context, resource *provider.ResourceInfo, viewMode appprovider.OpenInAppRequest_ViewMode, token, language string) (*appprovider.OpenInAppURL, error) {
url := fmt.Sprintf("<iframe src=%s/open/%s?view-mode=%s&access-token=%s&lang=%s />", p.iframeUIProvider, resource.Id.StorageId+":"+resource.Id.OpaqueId, viewMode.String(), token, language)
return &appprovider.OpenInAppURL{
AppUrl: url,
Method: "GET",
Expand Down
15 changes: 14 additions & 1 deletion pkg/app/provider/wopi/wopi.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func New(m map[string]interface{}) (app.Provider, error) {
}, nil
}

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

ext := path.Ext(resource.Path)
Expand Down Expand Up @@ -238,6 +238,19 @@ func (p *wopiProvider) GetAppURL(ctx context.Context, resource *provider.Resourc

appFullURL := result["app-url"].(string)

if language != "" {
url, err := url.Parse(appFullURL)
if err != nil {
return nil, err
}
urlQuery := url.Query()
urlQuery.Set("ui", language) // OnlyOffice
urlQuery.Set("lang", language) // Collabora
urlQuery.Set("UI_LLCC", language) // Office365
url.RawQuery = urlQuery.Encode()
appFullURL = url.String()
}

// Depending on whether wopi server returned any form parameters or not,
// we decide whether the request method is POST or GET
var formParams map[string]string
Expand Down
74 changes: 74 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"os/user"
"path"
"path/filepath"
Expand Down Expand Up @@ -348,7 +349,80 @@ func GetViewMode(viewMode string) gateway.OpenInAppRequest_ViewMode {
return gateway.OpenInAppRequest_VIEW_MODE_READ_ONLY
case "write":
return gateway.OpenInAppRequest_VIEW_MODE_READ_WRITE
case "preview":
return gateway.OpenInAppRequest_VIEW_MODE_PREVIEW
default:
return gateway.OpenInAppRequest_VIEW_MODE_INVALID
}
}

// AppendPlainToOpaque adds a new key value pair as a plain string on the given opaque and returns it
func AppendPlainToOpaque(o *types.Opaque, key, value string) *types.Opaque {
o = ensureOpaque(o)

o.Map[key] = &types.OpaqueEntry{
Decoder: "plain",
Value: []byte(value),
}
return o
}

// ReadPlainFromOpaque reads a plain string from the given opaque map
func ReadPlainFromOpaque(o *types.Opaque, key string) string {
if o.GetMap() == nil {
return ""
}
if e, ok := o.Map[key]; ok && e.Decoder == "plain" {
return string(e.Value)
}
return ""
}

// ExistsInOpaque returns true if the key exists in the opaque (ignoring the value)
func ExistsInOpaque(o *types.Opaque, key string) bool {
if o.GetMap() == nil {
return false
}

_, ok := o.Map[key]
return ok
}

// MergeOpaques will merge the opaques. If a key exists in both opaques
// the values from the first opaque will be taken
func MergeOpaques(o *types.Opaque, p *types.Opaque) *types.Opaque {
p = ensureOpaque(p)
for k, v := range o.GetMap() {
p.Map[k] = v
}
return p
}

// ensures the opaque is initialized
func ensureOpaque(o *types.Opaque) *types.Opaque {
if o == nil {
o = &types.Opaque{}
}
if o.Map == nil {
o.Map = map[string]*types.OpaqueEntry{}
}
return o
}

// RemoveItem removes the given item, its children and all empty parent folders
func RemoveItem(path string) error {
if err := os.RemoveAll(path); err != nil {
return err
}

for {
path = filepath.Dir(path)
if err := os.Remove(path); err != nil {
// remove will fail when the dir is not empty.
// We can exit in that case
return nil
}

}

}

0 comments on commit 02bd43c

Please sign in to comment.