From ec81d49a9042afa158cfc66235d5ae96a34e5126 Mon Sep 17 00:00:00 2001 From: libsgh Date: Mon, 7 Mar 2022 17:30:00 +0800 Subject: [PATCH] :bug:fix theme error --- boot/boot.go | 9 ++++----- control/admin.go | 2 +- control/index.go | 7 +++---- control/middleware/pwd.go | 19 ++++++++++++++++++- util/common.go | 8 +++++++- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/boot/boot.go b/boot/boot.go index cbda806e..5c6ee21a 100644 --- a/boot/boot.go +++ b/boot/boot.go @@ -231,21 +231,20 @@ func InitStaticBox(r *gin.Engine, fs embed.FS) { } func Templates(fs embed.FS) *template.Template { - themes := [6]string{"mdui", "mdui-light", "mdui-dark", "classic", "bootstrap", "materialdesign"} + themes := [3]string{"mdui", "classic", "bootstrap"} tmpl := template.New("") templatesFileNames := []string{"base", "appearance", "common", "disk", "hide", "login", "pwd", "safety", "view", "bypass", "cache", "webdav"} addTemplatesFromFolder("admin", tmpl, fs, templatesFileNames) for _, theme := range themes { - tmpName := strings.Join([]string{"templates/pan/", "/index.html"}, theme) - tmpFile := strings.ReplaceAll(tmpName, "-dark", "") - tmpFile = strings.ReplaceAll(tmpFile, "-light", "") + theme = util.GetCurrentTheme(theme) + tmpFile := strings.Join([]string{"templates/pan/", "/index.html"}, theme) dataBuf, _ := fs.ReadFile(tmpFile) data := string(dataBuf) if util.FileExist("./templates/" + tmpFile) { s, _ := ioutil.ReadFile("./templates/" + tmpFile) data = string(s) } - tmpl.New(tmpName).Funcs(template.FuncMap{ + tmpl.New(tmpFile).Funcs(template.FuncMap{ "unescaped": unescaped, "contains": strings.Contains, "iconclass": iconclass, diff --git a/control/admin.go b/control/admin.go index 7242be1b..2f0e0913 100644 --- a/control/admin.go +++ b/control/admin.go @@ -20,7 +20,7 @@ func AdminIndex(c *gin.Context) { //admin config managent func ConfigManagent(c *gin.Context) { - middleware.ThemeCheck(c) + middleware.AdminThemeCheck(c) theme := c.GetString("theme") fullPath := c.Request.URL.Path adminModule := strings.Split(fullPath, "/")[2] diff --git a/control/index.go b/control/index.go index db992b80..847f78fb 100644 --- a/control/index.go +++ b/control/index.go @@ -16,7 +16,8 @@ import ( func index(c *gin.Context) { var fns []module.FileNode var isFile bool - tmpFile := strings.Join([]string{"templates/pan/", "/index.html"}, module.GloablConfig.Theme) + tmpFile := strings.Join([]string{"templates/pan/", "/index.html"}, util.GetCurrentTheme(module.GloablConfig.Theme)) + fmt.Println(tmpFile) account, _ := c.Get("account") sortColumn := c.GetString("sort_column") sortOrder := c.GetString("sort_order") @@ -150,9 +151,7 @@ func DataRroxy(ac module.Account, downUrl, fileName string, c *gin.Context) { } func view(tmpFile *string, viewType string) { - theme := strings.ReplaceAll(module.GloablConfig.Theme, "-dark", "") - theme = strings.ReplaceAll(theme, "-light", "") - *tmpFile = fmt.Sprintf("templates/pan/%s/view-%s.html", theme, viewType) + *tmpFile = fmt.Sprintf("templates/pan/%s/view-%s.html", util.GetCurrentTheme(module.GloablConfig.Theme), viewType) } func ShortRedirect(c *gin.Context) { diff --git a/control/middleware/pwd.go b/control/middleware/pwd.go index dbebd1ed..523a6430 100644 --- a/control/middleware/pwd.go +++ b/control/middleware/pwd.go @@ -81,12 +81,29 @@ func VerifyPwd(pwd, path, cookiepwd string) (bool, string) { func ThemeCheck(c *gin.Context) { theme, err := c.Request.Cookie("theme") if err != nil { - c.Set("theme", "mdui") + c.Set("theme", module.GloablConfig.Theme) } else { c.Set("theme", theme.Value) } } +func AdminThemeCheck(c *gin.Context) { + theme, err := c.Request.Cookie("theme") + if err != nil { + if strings.HasPrefix(module.GloablConfig.Theme, "mdui") { + c.Set("theme", module.GloablConfig.Theme) + } else { + c.Set("theme", "mdui") + } + } else { + if strings.HasPrefix(theme.Value, "mdui") { + c.Set("theme", theme.Value) + } else { + c.Set("theme", "mdui") + } + } +} + func LayoutCheck(c *gin.Context) { layout, err := c.Request.Cookie("layout") if err != nil { diff --git a/util/common.go b/util/common.go index 17b86aff..57da90c8 100644 --- a/util/common.go +++ b/util/common.go @@ -195,7 +195,7 @@ func GetIcon(isFolder bool, fileType string) string { fileKey = "exe" } } - return iconMap[config.Theme].(KV)[fileKey].(string) + return iconMap[GetCurrentTheme(config.Theme)].(KV)[fileKey].(string) } func GetBetweenStr(str, start, end string) string { n := strings.Index(str, start) @@ -734,3 +734,9 @@ func Md5Params(params map[string]string) string { h.Write([]byte(signStr)) return hex.EncodeToString(h.Sum(nil)) } +func GetCurrentTheme(theme string) string { + if strings.HasPrefix(theme, "mdui") { + return "mdui" + } + return theme +}