From 808cbdf11589e2d6bedeeead6749a4e2dd6ba032 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 30 Sep 2024 20:16:29 +0200 Subject: [PATCH] Use light and dark icons --- components/application.templ | 7 ++- handlers/routes.go | 4 +- main.go | 2 +- {internal => pkg}/logger/logger.go | 0 pkg/media/media.go | 72 ++++++++++++++++++++++++++++++ services/bookmark.services.go | 40 ++++++----------- services/bookmark.types.go | 8 ++-- 7 files changed, 98 insertions(+), 35 deletions(-) rename {internal => pkg}/logger/logger.go (100%) create mode 100644 pkg/media/media.go diff --git a/components/application.templ b/components/application.templ index fa920cc..b8eac23 100644 --- a/components/application.templ +++ b/components/application.templ @@ -4,9 +4,14 @@ import "gitlab.unjx.de/flohoss/godash/services" templ Application(application services.Application) { -
+
@templ.Raw(application.Icon)
+ if application.IconLight != "" { +
+ @templ.Raw(application.IconLight) +
+ }
{ application.Name }
} diff --git a/handlers/routes.go b/handlers/routes.go index f392140..cf04be2 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -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/", authHandler.AuthMiddleware(http.StripPrefix("/assets/", fsAssets))) + router.Handle("GET /assets/", http.StripPrefix("/assets/", fsAssets)) icons := http.FileServer(http.Dir("storage/icons")) - router.Handle("GET /icons/", authHandler.AuthMiddleware(http.StripPrefix("/icons/", icons))) + router.Handle("GET /icons/", http.StripPrefix("/icons/", icons)) router.HandleFunc("GET /logout", authHandler.handleLogout) router.HandleFunc("GET /callback", authHandler.handleCallback) diff --git a/main.go b/main.go index 1c927f9..ae51893 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( "gitlab.unjx.de/flohoss/godash/handlers" "gitlab.unjx.de/flohoss/godash/internal/env" - "gitlab.unjx.de/flohoss/godash/internal/logger" + "gitlab.unjx.de/flohoss/godash/pkg/logger" "gitlab.unjx.de/flohoss/godash/services" ) diff --git a/internal/logger/logger.go b/pkg/logger/logger.go similarity index 100% rename from internal/logger/logger.go rename to pkg/logger/logger.go diff --git a/pkg/media/media.go b/pkg/media/media.go new file mode 100644 index 0000000..69dfab5 --- /dev/null +++ b/pkg/media/media.go @@ -0,0 +1,72 @@ +package media + +import ( + "fmt" + "io" + "io/fs" + "net/http" + "os" + "regexp" + "strings" +) + +func DownloadSelfHostedIcon(ext, title, filePath string) ([]byte, error) { + resp, err := http.Get("https://cdn.jsdelivr.net/gh/selfhst/icons/" + strings.TrimPrefix(ext, ".") + "/" + title) + 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) + } + 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) + err = os.WriteFile(filePath, data, fs.FileMode(0640)) + if err != nil { + return nil, fmt.Errorf("failed to write icon: %w", err) + } + return data, nil +} + +func insertWidthHeight(svgContent []byte) []byte { + classRegex := regexp.MustCompile(`(?: