Skip to content
Open
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
40 changes: 38 additions & 2 deletions internal/glance/config-fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,38 @@ type customIconField struct {
AutoInvert bool
}

/*
I'm sure there are better & more optimized ways to do this,
but I have little to no experience with Go.

I couldn't find an unpkg version for homarr and selfhst,
though there might be other alternatives ¯\_(ツ)_/¯
*/
var activeCDN string

func detectCDN() string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return an error

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't return an error in my testing, but let me know if it did on your end.

client := &http.Client{Timeout: 5 * time.Second}

candidates := []string{
"https://cdn.jsdelivr.net",
"https://unpkg.com",
}

for _, base := range candidates {
resp, err := client.Head(base)
if err != nil {
continue
}
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return base
}
}

return "None"
}


func newCustomIconField(value string) customIconField {
const autoInvertPrefix = "auto-invert "
field := customIconField{}
Expand All @@ -143,6 +175,10 @@ func newCustomIconField(value string) customIconField {
value = strings.TrimPrefix(value, autoInvertPrefix)
}

if activeCDN == "" {
activeCDN = detectCDN()
}

prefix, icon, found := strings.Cut(value, ":")
if !found {
field.URL = template.URL(value)
Expand All @@ -161,13 +197,13 @@ func newCustomIconField(value string) customIconField {

switch prefix {
case "si":
field.URL = template.URL(activeCDN + "/simple-icons@latest/icons/" + basename + ".svg")
field.AutoInvert = true
field.URL = template.URL("https://cdn.jsdelivr.net/npm/simple-icons@latest/icons/" + basename + ".svg")
case "di":
field.URL = template.URL("https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/" + ext + "/" + basename + "." + ext)
case "mdi":
field.URL = template.URL(activeCDN + "/@mdi/svg@latest/svg/" + basename + ".svg")
field.AutoInvert = true
field.URL = template.URL("https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/" + basename + ".svg")
case "sh":
field.URL = template.URL("https://cdn.jsdelivr.net/gh/selfhst/icons/" + ext + "/" + basename + "." + ext)
default:
Expand Down