@@ -124,16 +124,15 @@ func (s *svc) handleList(w http.ResponseWriter, r *http.Request) {
124
124
return
125
125
}
126
126
127
+ apps := listRes .Apps
128
+ filterAppsByUserAgent (apps , r .UserAgent ())
127
129
mimeTypes := listRes .MimeTypes
128
- filterAppsByUserAgent (mimeTypes , r .UserAgent ())
129
-
130
- filterMimeTypes (mimeTypes )
131
-
132
- if mimeTypes == nil {
133
- mimeTypes = make (map [string ]* appregistry.AppProviderList ) // ensure array empty object instead of null in json
130
+ res := mapMerge (apps , mimeTypes )
131
+ if res == nil {
132
+ res = make (map [string ]* appregistry.AppProviderList ) // ensure array empty object instead of null in json
134
133
}
135
134
136
- js , err := json .Marshal (map [string ]interface {}{"mime-types" : mimeTypes })
135
+ js , err := json .Marshal (map [string ]interface {}{"mime-types" : res })
137
136
if err != nil {
138
137
ocmd .WriteError (w , r , ocmd .APIErrorServerError , "error marshalling JSON response" , err )
139
138
return
@@ -189,39 +188,31 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
189
188
}
190
189
}
191
190
192
- func filterMimeTypes (mimeTypes map [string ]* appregistry.AppProviderList ) {
193
- for m , providers := range mimeTypes {
191
+ func filterAppsByUserAgent (appProviders map [string ]* appregistry.AppProviderList , userAgent string ) {
192
+ ua := ua .Parse (userAgent )
193
+
194
+ for m , providers := range appProviders {
194
195
apps := []* appregistry.ProviderInfo {}
195
196
for _ , p := range providers .AppProviders {
196
197
p .Address = "" // address is internal only and not needed in the client
197
- // apps are called by name, so if it has no name you cannot call it. Therefore we should not advertise it.
198
- if p .Name != "" {
198
+ // apps are called by name, so if it has no name it cannot be called and should not be advertised
199
+ // also filter Desktop-only apps if ua is not Desktop
200
+ if p .Name != "" && (ua .Desktop || ! p .DesktopOnly ) {
199
201
apps = append (apps , p )
200
202
}
201
203
}
202
- if len (apps ) > 0 {
203
- mimeTypes [m ] = & appregistry.AppProviderList {AppProviders : apps }
204
- } else {
205
- delete (mimeTypes , m )
206
- }
204
+ appProviders [m ] = & appregistry.AppProviderList {AppProviders : apps }
207
205
}
208
206
}
209
207
210
- func filterAppsByUserAgent (mimeTypes map [string ]* appregistry.AppProviderList , userAgent string ) {
211
- ua := ua .Parse (userAgent )
212
- if ua .Desktop {
213
- return
214
- }
215
-
216
- for m , providers := range mimeTypes {
217
- apps := []* appregistry.ProviderInfo {}
218
- for _ , p := range providers .AppProviders {
219
- if ! p .DesktopOnly {
220
- apps = append (apps , p )
221
- }
208
+ func mapMerge (ms ... map [string ]interface {}) map [string ]interface {} {
209
+ res := map [string ]interface {}
210
+ for _, m := range ms {
211
+ for k , v := range m {
212
+ res [k ] = append (res [k ], v )
222
213
}
223
- mimeTypes [m ] = & appregistry.AppProviderList {AppProviders : apps }
224
214
}
215
+ return res
225
216
}
226
217
227
218
func (s * svc ) getStatInfo (ctx context.Context , fileID string , client gateway.GatewayAPIClient ) (* provider.ResourceInfo , ocmd.APIErrorCode , error ) {
0 commit comments