Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override provider if was previously registered #2168

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog/unreleased/appregistry-register-once.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Bugfix: Override provider if was previously registered

Previously if an AppProvider registered himself two times, for example
after a failure, the mime types supported by the provider contained
multiple times the same provider.
Now this has been fixed, overriding the previous one.

https://github.com/cs3org/reva/pull/2168
39 changes: 29 additions & 10 deletions pkg/app/registry/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package static
import (
"context"
"fmt"
"reflect"
"strings"
"sync"

Expand Down Expand Up @@ -118,7 +117,20 @@ func New(m map[string]interface{}) (app.Registry, error) {
return &newManager, nil
}

// remove a provider from the provider list in a mime type
// it's a no-op if the provider is not in the list of providers in the mime type
func unregisterProvider(p *registrypb.ProviderInfo, mime *mimeTypeConfig) {
if index, in := getIndex(mime.apps, p); in {
// remove the provider from the list
mime.apps = append(mime.apps[:index], mime.apps[index+1:]...)
}
}

func registerProvider(p *registrypb.ProviderInfo, mime *mimeTypeConfig) {
// the app provider could be previously registered to the same mime type list
// so we will remove it
unregisterProvider(p, mime)

if providerIsDefaultForMimeType(p, mime) {
mime.apps = prependProvider(p, mime.apps)
} else {
Expand Down Expand Up @@ -161,14 +173,25 @@ func (m *manager) AddProvider(ctx context.Context, p *registrypb.ProviderInfo) e
m.Lock()
defer m.Unlock()

m.providers[p.Address] = p
// check if the provider was already registered
// if it's the case, we have to unregister it
// from all the old mime types
if oldP, ok := m.providers[p.Address]; ok {
oldMimeTypes := oldP.MimeTypes
for _, mimeName := range oldMimeTypes {
mimeIf, ok := m.mimetypes.Get(mimeName)
if !ok {
continue
}
mime := mimeIf.(*mimeTypeConfig)
unregisterProvider(p, mime)
}
}

// log := appctx.GetLogger(ctx)
m.providers[p.Address] = p

for _, mime := range p.MimeTypes {
if mimeTypeInterface, ok := m.mimetypes.Get(mime); ok {
// TODO (gdelmont): don't add to the list of apps an AppProvider
// that was already registered
mimeType := mimeTypeInterface.(*mimeTypeConfig)
registerProvider(p, mimeType)
} else {
Expand Down Expand Up @@ -288,11 +311,7 @@ func (m *manager) GetDefaultProviderForMimeType(ctx context.Context, mimeType st
}

func equalsProviderInfo(p1, p2 *registrypb.ProviderInfo) bool {
return p1.Address == p2.Address &&
p1.Name == p2.Name &&
reflect.DeepEqual(p1.MimeTypes, p2.MimeTypes) &&
p1.Description == p2.Description &&
p1.DesktopOnly == p2.DesktopOnly
return p1.Name == p2.Name
}

// check that all providers in the two lists are equals
Expand Down
151 changes: 151 additions & 0 deletions pkg/app/registry/static/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,157 @@ func TestAddProvider(t *testing.T) {
},
},
},
{
name: "register a provider already registered",
mimeTypes: []*mimeTypeConfig{
{
MimeType: "text/json",
Extension: "json",
Name: "JSON File",
Icon: "https://example.org/icons&file=json.png",
DefaultApp: "provider2",
},
},
initProviders: []*registrypb.ProviderInfo{
{
MimeTypes: []string{"text/json"},
Address: "ip-provider1",
Name: "provider1",
},
{
MimeTypes: []string{"text/json"},
Address: "ip-provider2",
Name: "provider2",
},
},
newProvider: &registrypb.ProviderInfo{
MimeTypes: []string{"text/json"},
Address: "ip-provider1",
Name: "provider1",
},
expectedProviders: map[string][]*registrypb.ProviderInfo{
"text/json": {
{
MimeTypes: []string{"text/json"},
Address: "ip-provider2",
Name: "provider2",
},
{
MimeTypes: []string{"text/json"},
Address: "ip-provider1",
Name: "provider1",
},
},
},
},
{
name: "register a provider already registered supporting more mime types",
mimeTypes: []*mimeTypeConfig{
{
MimeType: "text/json",
Extension: "json",
Name: "JSON File",
Icon: "https://example.org/icons&file=json.png",
DefaultApp: "provider2",
},
{
MimeType: "text/xml",
Extension: "xml",
Name: "XML File",
Icon: "https://example.org/icons&file=xml.png",
DefaultApp: "provider1",
},
},
initProviders: []*registrypb.ProviderInfo{
{
MimeTypes: []string{"text/json"},
Address: "ip-provider1",
Name: "provider1",
},
{
MimeTypes: []string{"text/json"},
Address: "ip-provider2",
Name: "provider2",
},
},
newProvider: &registrypb.ProviderInfo{
MimeTypes: []string{"text/json", "text/xml"},
Address: "ip-provider1",
Name: "provider1",
},
expectedProviders: map[string][]*registrypb.ProviderInfo{
"text/json": {
{
MimeTypes: []string{"text/json"},
Address: "ip-provider2",
Name: "provider2",
},
{
MimeTypes: []string{"text/json", "text/xml"},
Address: "ip-provider1",
Name: "provider1",
},
},
"text/xml": {
{
MimeTypes: []string{"text/json", "text/xml"},
Address: "ip-provider1",
Name: "provider1",
},
},
},
},
{
name: "register a provider already registered supporting less mime types",
mimeTypes: []*mimeTypeConfig{
{
MimeType: "text/json",
Extension: "json",
Name: "JSON File",
Icon: "https://example.org/icons&file=json.png",
DefaultApp: "provider2",
},
{
MimeType: "text/xml",
Extension: "xml",
Name: "XML File",
Icon: "https://example.org/icons&file=xml.png",
DefaultApp: "provider1",
},
},
initProviders: []*registrypb.ProviderInfo{
{
MimeTypes: []string{"text/json", "text/xml"},
Address: "ip-provider1",
Name: "provider1",
},
{
MimeTypes: []string{"text/json"},
Address: "ip-provider2",
Name: "provider2",
},
},
newProvider: &registrypb.ProviderInfo{
MimeTypes: []string{"text/json"},
Address: "ip-provider1",
Name: "provider1",
},
expectedProviders: map[string][]*registrypb.ProviderInfo{
"text/json": {
{
MimeTypes: []string{"text/json"},
Address: "ip-provider2",
Name: "provider2",
},
{
MimeTypes: []string{"text/json"},
Address: "ip-provider1",
Name: "provider1",
},
},
"text/xml": {},
},
},
}

for _, tt := range testCases {
Expand Down