Add simple icons and option to turn dark mode off

This commit is contained in:
Florian Hoss 2024-09-30 21:14:47 +02:00
parent 808cbdf115
commit de22e5598a
Signed by: flohoss
GPG key ID: 5BA0B454E498DF62
5 changed files with 72 additions and 40 deletions

View file

@ -2,12 +2,16 @@ package components
import "gitlab.unjx.de/flohoss/godash/services"
func displayDark(app services.Application) bool {
return !app.IgnoreDark && app.IconLight != ""
}
templ Application(application services.Application) {
<a href={ templ.URL(application.URL) } class="flex items-center hover-effect">
<div class={ "w-8", "h-8", "flex", templ.KV("dark:hidden", application.IconLight != ""), "items-center" }>
<div class={ "w-8", "h-8", "flex", templ.KV("dark:hidden", displayDark(application)), "items-center" }>
@templ.Raw(application.Icon)
</div>
if application.IconLight != "" {
if displayDark(application) {
<div class={ "w-8", "h-8", "hidden", "dark:flex", "items-center" }>
@templ.Raw(application.IconLight)
</div>

View file

@ -10,10 +10,10 @@ func SetupRoutes(router *http.ServeMux, sse *sse.Server, appHandler *AppHandler,
router.Handle("GET /sse", authHandler.AuthMiddleware(http.HandlerFunc(sse.ServeHTTP)))
fsAssets := http.FileServer(http.Dir("assets"))
router.Handle("GET /assets/", http.StripPrefix("/assets/", fsAssets))
router.Handle("GET /assets/", authHandler.AuthMiddleware(http.StripPrefix("/assets/", fsAssets)))
icons := http.FileServer(http.Dir("storage/icons"))
router.Handle("GET /icons/", http.StripPrefix("/icons/", icons))
router.Handle("GET /icons/", authHandler.AuthMiddleware(http.StripPrefix("/icons/", icons)))
router.HandleFunc("GET /logout", authHandler.handleLogout)
router.HandleFunc("GET /callback", authHandler.handleCallback)

View file

@ -10,21 +10,21 @@ import (
"strings"
)
func DownloadSelfHostedIcon(ext, title, filePath string) ([]byte, error) {
resp, err := http.Get("https://cdn.jsdelivr.net/gh/selfhst/icons/" + strings.TrimPrefix(ext, ".") + "/" + title)
func DownloadSelfHostedIcon(url, title, filePath string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to get icon: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get icon, status: %d", resp.StatusCode)
return nil, fmt.Errorf("failed to get icon, status: %d, url: %s", resp.StatusCode, url)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read icon: %w", err)
}
data = replaceClassNames(data, strings.TrimSuffix(title, ext))
data = insertWidthHeight(data)
data = replaceClassNames(data, title)
// data = insertWidthHeight(data)
err = os.WriteFile(filePath, data, fs.FileMode(0640))
if err != nil {
return nil, fmt.Errorf("failed to write icon: %w", err)
@ -51,6 +51,7 @@ func insertWidthHeight(svgContent []byte) []byte {
func replaceClassNames(svgContent []byte, title string) []byte {
// Regular expression to match either class="st0" or .st0
classRegex := regexp.MustCompile(`(class="|\.)([a-z]{2}\d)`)
title = strings.TrimSuffix(title, ".svg")
newSVGContent := classRegex.ReplaceAllFunc(svgContent, func(match []byte) []byte {
groups := classRegex.FindSubmatch(match)

View file

@ -90,39 +90,65 @@ func (bs *BookmarkService) replaceIconStrings() {
slog.Error("icon must be an svg file")
continue
}
var data, lightData []byte
var err error
if strings.HasPrefix(bookmark.Icon, "shi/") {
title := strings.Replace(bookmark.Icon, "shi/", "", 1)
if title == "" {
slog.Error("icon title is empty")
data, lightData, err = downloadIcons(handleSelfHostedIcons(bookmark.Icon, ext))
if err != nil {
slog.Error(err.Error())
continue
}
data, err := os.ReadFile(iconsFolder + title)
if err != nil {
slog.Debug("icon not found, downloading...", "title", title)
data, err = media.DownloadSelfHostedIcon(ext, title, iconsFolder+title)
}
if strings.HasPrefix(bookmark.Icon, "si/") {
data, lightData, err = downloadIcons(handleSimpleIcons(bookmark.Icon, ext))
if err != nil {
slog.Error(err.Error())
continue
}
}
lightTitle := strings.Replace(title, ".svg", "-light.svg", 1)
lightData, err := os.ReadFile(iconsFolder + lightTitle)
if err != nil {
slog.Debug("light-icon not found, downloading...", "title", title)
lightData, err = media.DownloadSelfHostedIcon(ext, lightTitle, iconsFolder+lightTitle)
if err != nil {
slog.Warn(err.Error())
}
}
if data == nil {
slog.Error("icon data is null")
continue
}
bs.bookmarks.Applications[i].Entries[j].Icon = string(data)
bs.bookmarks.Applications[i].Entries[j].IconLight = string(lightData)
}
}
}
func downloadIcons(title, url, lightTitle, lightUrl string) ([]byte, []byte, error) {
data, err := downloadIcon(title, url)
if err != nil {
return nil, nil, err
}
lightData, _ := downloadIcon(lightTitle, lightUrl)
return data, lightData, nil
}
func downloadIcon(title, url string) ([]byte, error) {
filePath := iconsFolder + title
data, err := os.ReadFile(filePath)
if err != nil {
data, err = media.DownloadSelfHostedIcon(url, title, filePath)
if err != nil {
return nil, err
}
}
return data, nil
}
func handleSelfHostedIcons(icon, ext string) (string, string, string, string) {
ext = strings.TrimPrefix(ext, ".")
title := strings.Replace(icon, "shi/", "", 1)
url := "https://cdn.jsdelivr.net/gh/selfhst/icons/" + ext + "/" + title
lightTitle := strings.Replace(title, ".svg", "-light.svg", 1)
lightUrl := "https://cdn.jsdelivr.net/gh/selfhst/icons/" + ext + "/" + lightTitle
return title, url, lightTitle, lightUrl
}
func handleSimpleIcons(icon, ext string) (string, string, string, string) {
title := strings.Replace(icon, "si/", "", 1)
url := "https://cdn.simpleicons.org/" + strings.TrimSuffix(title, ext)
lightTitle := strings.Replace(title, ".svg", "-light.svg", 1)
lightUrl := "https://cdn.simpleicons.org/" + strings.TrimSuffix(title, ext) + "/white"
return title, url, lightTitle, lightUrl
}
func (bs *BookmarkService) parseBookmarks() {

View file

@ -25,4 +25,5 @@ type Application struct {
Icon string
URL string
IconLight string
IgnoreDark bool `yaml:"ignore_dark"`
}