From fa08ff5d897e554e8e87197f8e52b50d19e33208 Mon Sep 17 00:00:00 2001 From: Giuseppe Lo Presti Date: Mon, 13 Sep 2021 09:48:38 +0200 Subject: [PATCH] WIP: enhancements for https://github.com/cs3org/cs3apis/pull/145 --- changelog/unreleased/appreg-mimetypes.md | 4 ++ go.mod | 1 + .../storageprovider/storageprovider.go | 3 +- .../http/services/appprovider/appprovider.go | 46 ++++++++----------- 4 files changed, 26 insertions(+), 28 deletions(-) create mode 100644 changelog/unreleased/appreg-mimetypes.md diff --git a/changelog/unreleased/appreg-mimetypes.md b/changelog/unreleased/appreg-mimetypes.md new file mode 100644 index 00000000000..1ff56969b8f --- /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/123456 diff --git a/go.mod b/go.mod index 25949f9a7d8..a0f929abc2d 100644 --- a/go.mod +++ b/go.mod @@ -82,4 +82,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/grpc/services/storageprovider/storageprovider.go b/internal/grpc/services/storageprovider/storageprovider.go index c6e6d7f0606..4545fa8da0e 100644 --- a/internal/grpc/services/storageprovider/storageprovider.go +++ b/internal/grpc/services/storageprovider/storageprovider.go @@ -93,7 +93,8 @@ func (c *config) init() { c.AvailableXS = map[string]uint32{"md5": 100, "unset": 1000} } if c.MimeTypes == nil || len(c.MimeTypes) == 0 { - c.MimeTypes = map[string]string{".zmd": "application/compressed-markdown"} + c.MimeTypes = map[string]string{".zmd": "application/compressed-markdown", + ".epd": "application/etherpad"} } } diff --git a/internal/http/services/appprovider/appprovider.go b/internal/http/services/appprovider/appprovider.go index 22c0a3e775f..bebeadde342 100644 --- a/internal/http/services/appprovider/appprovider.go +++ b/internal/http/services/appprovider/appprovider.go @@ -124,16 +124,16 @@ func (s *svc) handleList(w http.ResponseWriter, r *http.Request) { return } + apps := listRes.Apps + filterAppsByUserAgent(apps, r.UserAgent()) mimeTypes := listRes.MimeTypes - filterAppsByUserAgent(mimeTypes, r.UserAgent()) + res := mapMerge(apps, mimeTypes) - filterMimeTypes(mimeTypes) - - if mimeTypes == nil { - mimeTypes = make(map[string]*appregistry.AppProviderList) // ensure array empty object instead of null in json + if res == nil { + res = make(map[string]*appregistry.AppProviderList) // ensure array empty object instead of null in json } - js, err := json.Marshal(map[string]interface{}{"mime-types": mimeTypes}) + 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 +189,31 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) { } } -func filterMimeTypes(mimeTypes map[string]*appregistry.AppProviderList) { +func filterAppsByUserAgent(mimeTypes map[string]*appregistry.AppProviderList, userAgent string) { + ua := ua.Parse(userAgent) + for m, providers := range mimeTypes { apps := []*appregistry.ProviderInfo{} for _, p := range providers.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) - } + mimeTypes[m] = &appregistry.AppProviderList{AppProviders: apps} } } -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) - } +func mapMerge(ms ...map[string]string) map[string][]string { + res := map[string][]string{} + for _, m := range ms { + for k, v := range m { + res[k] = append(res[k], v) } - mimeTypes[m] = &appregistry.AppProviderList{AppProviders: apps} } + return res } func (s *svc) getStatInfo(ctx context.Context, fileID string, client gateway.GatewayAPIClient) (*provider.ResourceInfo, ocmd.APIErrorCode, error) {