Handle local files as well

This commit is contained in:
Florian Hoss 2024-09-30 21:44:32 +02:00
parent de22e5598a
commit c31c6243da
Signed by: flohoss
GPG key ID: 5BA0B454E498DF62

View file

@ -99,13 +99,18 @@ func (bs *BookmarkService) replaceIconStrings() {
continue continue
} }
} } else if strings.HasPrefix(bookmark.Icon, "si/") {
if strings.HasPrefix(bookmark.Icon, "si/") {
data, lightData, err = downloadIcons(handleSimpleIcons(bookmark.Icon, ext)) data, lightData, err = downloadIcons(handleSimpleIcons(bookmark.Icon, ext))
if err != nil { if err != nil {
slog.Error(err.Error()) slog.Error(err.Error())
continue continue
} }
} else {
data, lightData, err = handleLocalIcons(bookmark.Icon, ext)
if err != nil {
slog.Error(err.Error())
continue
}
} }
bs.bookmarks.Applications[i].Entries[j].Icon = string(data) bs.bookmarks.Applications[i].Entries[j].Icon = string(data)
bs.bookmarks.Applications[i].Entries[j].IconLight = string(lightData) bs.bookmarks.Applications[i].Entries[j].IconLight = string(lightData)
@ -138,7 +143,7 @@ func handleSelfHostedIcons(icon, ext string) (string, string, string, string) {
ext = strings.TrimPrefix(ext, ".") ext = strings.TrimPrefix(ext, ".")
title := strings.Replace(icon, "shi/", "", 1) title := strings.Replace(icon, "shi/", "", 1)
url := "https://cdn.jsdelivr.net/gh/selfhst/icons/" + ext + "/" + title url := "https://cdn.jsdelivr.net/gh/selfhst/icons/" + ext + "/" + title
lightTitle := strings.Replace(title, ".svg", "-light.svg", 1) lightTitle := strings.Replace(title, ext, "-light.svg", 1)
lightUrl := "https://cdn.jsdelivr.net/gh/selfhst/icons/" + ext + "/" + lightTitle lightUrl := "https://cdn.jsdelivr.net/gh/selfhst/icons/" + ext + "/" + lightTitle
return title, url, lightTitle, lightUrl return title, url, lightTitle, lightUrl
} }
@ -146,11 +151,21 @@ func handleSelfHostedIcons(icon, ext string) (string, string, string, string) {
func handleSimpleIcons(icon, ext string) (string, string, string, string) { func handleSimpleIcons(icon, ext string) (string, string, string, string) {
title := strings.Replace(icon, "si/", "", 1) title := strings.Replace(icon, "si/", "", 1)
url := "https://cdn.simpleicons.org/" + strings.TrimSuffix(title, ext) url := "https://cdn.simpleicons.org/" + strings.TrimSuffix(title, ext)
lightTitle := strings.Replace(title, ".svg", "-light.svg", 1) lightTitle := strings.Replace(title, ext, "-light.svg", 1)
lightUrl := "https://cdn.simpleicons.org/" + strings.TrimSuffix(title, ext) + "/white" lightUrl := "https://cdn.simpleicons.org/" + strings.TrimSuffix(title, ext) + "/white"
return title, url, lightTitle, lightUrl return title, url, lightTitle, lightUrl
} }
func handleLocalIcons(title, ext string) ([]byte, []byte, error) {
data, err := os.ReadFile(iconsFolder + title)
if err != nil {
return nil, nil, err
}
lightTitle := strings.Replace(title, ext, "-light.svg", 1)
lightData, _ := os.ReadFile(iconsFolder + lightTitle)
return data, lightData, err
}
func (bs *BookmarkService) parseBookmarks() { func (bs *BookmarkService) parseBookmarks() {
byteValue := bs.readBookmarksFile() byteValue := bs.readBookmarksFile()
err := yaml.Unmarshal(byteValue, &bs.bookmarks) err := yaml.Unmarshal(byteValue, &bs.bookmarks)