Skip to content

Commit

Permalink
Expose extra properties for mimetypes in the /app/list HTTP endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Sep 17, 2021
1 parent df33e69 commit 0b33ceb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 42 deletions.
4 changes: 4 additions & 0 deletions changelog/unreleased/appreg-mimetypes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Enhancement: handle mimetypes and their friendly names

https://github.com/cs3org/cs3apis/pull/145
https://github.com/cs3org/reva/pull/2067
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ replace (
github.com/eventials/go-tus => github.com/andrewmostello/go-tus v0.0.0-20200314041820-904a9904af9a
github.com/oleiade/reflections => github.com/oleiade/reflections v1.0.1
google.golang.org/grpc => google.golang.org/grpc v1.26.0 // temporary downgrade
github.com/cs3org/go-cs3apis => /home/lopresti/CS3API/build/go-cs3apis
)
48 changes: 13 additions & 35 deletions internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,9 @@ func (s *svc) handleList(w http.ResponseWriter, r *http.Request) {
return
}

mimeTypes := listRes.MimeTypes
filterAppsByUserAgent(mimeTypes, r.UserAgent())

filterMimeTypes(mimeTypes)

if mimeTypes == nil {
mimeTypes = make(map[string]*appregistry.AppProviderList) // ensure array empty object instead of null in json
}

js, err := json.Marshal(map[string]interface{}{"mime-types": mimeTypes})
res := listRes.MimeTypes
filterAppsByUserAgent(res, 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)
return
Expand Down Expand Up @@ -189,39 +182,24 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
}
}

func filterMimeTypes(mimeTypes map[string]*appregistry.AppProviderList) {
for m, providers := range mimeTypes {
func filterAppsByUserAgent(mimeTypes *appregistry.MimeTypeList, userAgent string) {
ua := ua.Parse(userAgent)
res := []*appregistry.MimeTypeInfo{}
for _, m := range mimeTypes.MimeTypes {
apps := []*appregistry.ProviderInfo{}
for _, p := range providers.AppProviders {
for _, p := range m.AppProviders {
p.Address = "" // address is internal only and not needed in the client
// apps are called by name, so if it has no name you cannot call it. Therefore we should not advertise it.
if p.Name != "" {
// apps are called by name, so if it has no name it cannot be called and should not be advertised
// also filter Desktop-only apps if ua is not Desktop
if p.Name != "" && (ua.Desktop || !p.DesktopOnly) {
apps = append(apps, p)
}
}
if len(apps) > 0 {
mimeTypes[m] = &appregistry.AppProviderList{AppProviders: apps}
} else {
delete(mimeTypes, m)
}
}
}

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)
}
res = append(res, m)
}
mimeTypes[m] = &appregistry.AppProviderList{AppProviders: apps}
}
mimeTypes.MimeTypes = res
}

func (s *svc) getStatInfo(ctx context.Context, fileID string, client gateway.GatewayAPIClient) (*provider.ResourceInfo, ocmd.APIErrorCode, 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 @@ -31,7 +31,7 @@ import (
type Registry interface {
FindProviders(ctx context.Context, mimeType string) ([]*registry.ProviderInfo, error)
ListProviders(ctx context.Context) ([]*registry.ProviderInfo, error)
ListSupportedMimeTypes(ctx context.Context) (map[string]*registry.AppProviderList, error)
ListSupportedMimeTypes(ctx context.Context) (*registry.MimeTypeList, error)
AddProvider(ctx context.Context, p *registry.ProviderInfo) error
GetDefaultProviderForMimeType(ctx context.Context, mimeType string) (*registry.ProviderInfo, error)
SetDefaultProviderForMimeType(ctx context.Context, mimeType string, p *registry.ProviderInfo) error
Expand Down
20 changes: 14 additions & 6 deletions pkg/app/registry/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,31 @@ func (b *reg) ListProviders(ctx context.Context) ([]*registrypb.ProviderInfo, er
return providers, nil
}

func (b *reg) ListSupportedMimeTypes(ctx context.Context) (map[string]*registrypb.AppProviderList, error) {
func (b *reg) ListSupportedMimeTypes(ctx context.Context) (*registrypb.MimeTypeList, error) {
b.RLock()
defer b.RUnlock()

mimeTypes := make(map[string]*registrypb.AppProviderList)
res := registrypb.MimeTypeList{MimeTypes: []*registrypb.MimeTypeInfo{}}
mtmap := make(map[string]*registrypb.MimeTypeInfo)
for _, p := range b.providers {
t := *p
t.MimeTypes = nil
for _, m := range p.MimeTypes {
if _, ok := mimeTypes[m]; ok {
mimeTypes[m].AppProviders = append(mimeTypes[m].AppProviders, &t)
if _, ok := mtmap[m]; ok {
mtmap[m].AppProviders = append(mtmap[m].AppProviders, &t)
} else {
mimeTypes[m] = &registrypb.AppProviderList{AppProviders: []*registrypb.ProviderInfo{&t}}
mtmap[m] = &registrypb.MimeTypeInfo{
AppProviders: []*registrypb.ProviderInfo{&t},
Ext: "", // TODO fetch from config
Name: "",
Description: "",
Icon: "",
}
res.MimeTypes = append(res.MimeTypes, mtmap[m])
}
}
}
return mimeTypes, nil
return &res, nil
}

func (b *reg) SetDefaultProviderForMimeType(ctx context.Context, mimeType string, p *registrypb.ProviderInfo) error {
Expand Down

0 comments on commit 0b33ceb

Please sign in to comment.