Skip to content

Commit

Permalink
WIP: enhancements for cs3org/cs3apis#145
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Sep 13, 2021
1 parent a1a5d61 commit fa08ff5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 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/123456
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 2 additions & 1 deletion internal/grpc/services/storageprovider/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
}

}
Expand Down
46 changes: 19 additions & 27 deletions internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit fa08ff5

Please sign in to comment.