github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/instance/sponsors.go (about) 1 package instance 2 3 import ( 4 "path/filepath" 5 6 "github.com/cozy/cozy-stack/pkg/config/config" 7 "github.com/go-viper/mapstructure/v2" 8 ) 9 10 type AppLogos struct { 11 Light []LogoItem `json:"light,omitempty"` 12 Dark []LogoItem `json:"dark,omitempty"` 13 } 14 type LogoItem struct { 15 Src string `json:"src"` 16 Alt string `json:"alt"` 17 Type string `json:"type,omitempty"` 18 } 19 20 func (i *Instance) GetContextWithSponsorships() map[string]interface{} { 21 context, ok := i.SettingsContext() 22 if !ok { 23 context = map[string]interface{}{} 24 } 25 if len(i.Sponsorships) == 0 { 26 return context 27 } 28 29 // Avoid changing the global config 30 clone := map[string]interface{}{} 31 for k, v := range context { 32 clone[k] = v 33 } 34 context = clone 35 var logos map[string]AppLogos 36 if err := mapstructure.Decode(context["logos"], &logos); err != nil || logos == nil { 37 logos = make(map[string]AppLogos) 38 } 39 context["logos"] = logos 40 41 contexts := config.GetConfig().Contexts 42 if contexts == nil { 43 return context 44 } 45 for _, sponsor := range i.Sponsorships { 46 if sponsorCtx, ok := contexts[sponsor].(map[string]interface{}); ok { 47 addHomeLogosForSponsor(context, sponsorCtx, sponsor) 48 } 49 } 50 return context 51 } 52 53 func addHomeLogosForSponsor(context, sponsorContext map[string]interface{}, sponsor string) { 54 sponsorLogos, ok := sponsorContext["logos"].(map[string]interface{}) 55 if !ok { 56 return 57 } 58 var newLogos AppLogos 59 if err := mapstructure.Decode(sponsorLogos["home"], &newLogos); err != nil { 60 return 61 } 62 63 contextLogos := context["logos"].(map[string]AppLogos) 64 homeLogos := contextLogos["home"] 65 66 for _, logo := range newLogos.Light { 67 if logo.Type == "main" { 68 continue 69 } 70 found := false 71 for _, item := range homeLogos.Light { 72 if filepath.Base(item.Src) == filepath.Base(logo.Src) { 73 found = true 74 } 75 } 76 if found { 77 continue 78 } 79 logo.Src = "/ext/" + sponsor + logo.Src 80 homeLogos.Light = append(homeLogos.Light, logo) 81 } 82 83 for _, logo := range newLogos.Dark { 84 if logo.Type == "main" { 85 continue 86 } 87 found := false 88 for _, item := range homeLogos.Dark { 89 if filepath.Base(item.Src) == filepath.Base(logo.Src) { 90 found = true 91 } 92 } 93 if found { 94 continue 95 } 96 logo.Src = "/ext/" + sponsor + logo.Src 97 homeLogos.Dark = append(homeLogos.Dark, logo) 98 } 99 100 contextLogos["home"] = homeLogos 101 }