diff --git a/changelog/unreleased/appreg-mimetypes.md b/changelog/unreleased/appreg-mimetypes.md new file mode 100644 index 00000000000..0083f3c0780 --- /dev/null +++ b/changelog/unreleased/appreg-mimetypes.md @@ -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 diff --git a/go.mod b/go.mod index 580ceed97d8..e1f111d0c38 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/internal/http/services/appprovider/appprovider.go b/internal/http/services/appprovider/appprovider.go index 22c0a3e775f..a632f50d154 100644 --- a/internal/http/services/appprovider/appprovider.go +++ b/internal/http/services/appprovider/appprovider.go @@ -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 @@ -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) { diff --git a/pkg/app/app.go b/pkg/app/app.go index bb1319dc524..f99dcef71f9 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -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 diff --git a/pkg/app/registry/static/static.go b/pkg/app/registry/static/static.go index 6ddb63864ac..d95577ed7f5 100644 --- a/pkg/app/registry/static/static.go +++ b/pkg/app/registry/static/static.go @@ -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] = ®istrypb.AppProviderList{AppProviders: []*registrypb.ProviderInfo{&t}} + mtmap[m] = ®istrypb.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 {