package server import ( "encoding/json" "os" "path/filepath" "strings" ) type ThemeOption struct { ID string `json:"id"` Label string `json:"label"` CSS string `json:"css"` } func builtinThemeOptions() []ThemeOption { return []ThemeOption{ {ID: "dark", Label: "Dark", CSS: "/static/css/theme/dark.css"}, {ID: "light", Label: "Light", CSS: "/static/css/theme/light.css"}, {ID: "monokai", Label: "Monokai", CSS: "/static/css/theme/monokai.css"}, {ID: "solarized-dark", Label: "Solarized Dark", CSS: "/static/css/theme/solarized-dark.css"}, {ID: "solarized-light", Label: "Solarized Light", CSS: "/static/css/theme/solarized-light.css"}, {ID: "terminal-green", Label: "Terminal Green", CSS: "/static/css/theme/terminal-green.css"}, {ID: "amber-crt", Label: "Amber CRT", CSS: "/static/css/theme/amber-crt.css"}, {ID: "lain-dark", Label: "Lain Dark", CSS: "/static/css/theme/lain-dark.css"}, {ID: "nanoha-dark", Label: "Nanoha Dark", CSS: "/static/css/theme/nanoha-dark.css"}, } } func externalThemeOptions() []ThemeOption { path := "/data/themes/themes.json" b, err := os.ReadFile(path) if err != nil { return nil } var items []ThemeOption if err := json.Unmarshal(b, &items); err != nil { return nil } var out []ThemeOption for _, item := range items { item.ID = strings.TrimSpace(item.ID) item.Label = strings.TrimSpace(item.Label) item.CSS = strings.TrimSpace(item.CSS) if item.ID == "" || item.Label == "" || item.CSS == "" { continue } if strings.Contains(item.ID, "/") || strings.Contains(item.ID, "..") { continue } if !strings.HasPrefix(item.CSS, "/") { item.CSS = "/-/theme/" + filepath.Base(item.CSS) } out = append(out, item) } return out } func themeOptions() []ThemeOption { items := builtinThemeOptions() items = append(items, externalThemeOptions()...) return items } func findTheme(id string) (ThemeOption, bool) { for _, opt := range themeOptions() { if opt.ID == id { return opt, true } } return ThemeOption{}, false } func isAllowedTheme(theme string) bool { _, ok := findTheme(theme) return ok } func (s *Server) getTheme() string { theme, err := s.store.GetConfig("theme") if err != nil || theme == "" { return "dark" } if !isAllowedTheme(theme) { return "dark" } return theme } func (s *Server) getThemeCSS() string { opt, ok := findTheme(s.getTheme()) if !ok { return "/static/css/theme/dark.css" } return opt.CSS }