From 4c0086624991c76a530a9f7c1149966471952764 Mon Sep 17 00:00:00 2001 From: Xhofe Date: Tue, 28 Dec 2021 16:38:56 +0800 Subject: [PATCH] :sparkles: grouping settings --- bootstrap/setting.go | 106 ++++++++++++++++------------------ model/setting.go | 36 ++++++++++-- server/controllers/setting.go | 15 ++++- 3 files changed, 95 insertions(+), 62 deletions(-) diff --git a/bootstrap/setting.go b/bootstrap/setting.go index 4c802412..b3bfbf8a 100644 --- a/bootstrap/setting.go +++ b/bootstrap/setting.go @@ -10,16 +10,8 @@ import ( func InitSettings() { log.Infof("init settings...") - version := model.SettingItem{ - Key: "version", - Value: conf.GitTag, - Description: "version", - Type: "string", - Group: model.CONST, - Version: conf.GitTag, - } - err := model.SaveSetting(version) + err := model.SaveSetting(model.Version) if err != nil { log.Fatalf("failed write setting: %s", err.Error()) } @@ -30,153 +22,155 @@ func InitSettings() { Value: "Alist", Description: "title", Type: "string", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "password", Value: "alist", Description: "password", Type: "string", - Group: model.PRIVATE, + Access: model.PRIVATE, + Group: model.BACK, }, { Key: "logo", Value: "https://store.heytapimage.com/cdo-portal/feedback/202112/05/1542f45f86b8609495b69c5380753135.png", Description: "logo", Type: "string", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "favicon", Value: "https://store.heytapimage.com/cdo-portal/feedback/202112/05/1542f45f86b8609495b69c5380753135.png", Description: "favicon", Type: "string", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "icon color", Value: "#1890ff", Description: "icon's color", Type: "string", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "text types", Value: strings.Join(conf.TextTypes, ","), Type: "string", Description: "text type extensions", + Group: model.FRONT, }, { Key: "hide readme file", Value: "true", Type: "bool", Description: "hide readme file? ", + Group: model.FRONT, }, { Key: "music cover", Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png", Description: "music cover image", Type: "string", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "site beian", Description: "chinese beian info", Type: "string", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "home readme url", Description: "when have multiple, the readme file to show", Type: "string", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "markdown theme", Value: "vuepress", Description: "default | github | vuepress", - Group: model.PUBLIC, + Access: model.PUBLIC, Type: "select", Values: "default,github,vuepress", + Group: model.FRONT, }, { - Key: "autoplay video", - Value: "false", - Type: "bool", - Group: model.PUBLIC, + Key: "autoplay video", + Value: "false", + Type: "bool", + Access: model.PUBLIC, + Group: model.FRONT, }, { - Key: "autoplay audio", - Value: "false", - Type: "bool", - Group: model.PUBLIC, + Key: "autoplay audio", + Value: "false", + Type: "bool", + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "check parent folder", Value: "false", Type: "bool", Description: "check parent folder password", - Group: model.PRIVATE, + Access: model.PRIVATE, + Group: model.BACK, }, { - Key: "customize head", - Value: ``, + Key: "customize head", + Value: "", Type: "text", Description: "Customize head, placed at the beginning of the head", - Group: model.PRIVATE, + Access: model.PRIVATE, + Group: model.FRONT, }, { Key: "customize body", Value: "", Type: "text", Description: "Customize script, placed at the end of the body", - Group: model.PRIVATE, + Access: model.PRIVATE, + Group: model.FRONT, }, { Key: "animation", Value: "true", Type: "bool", Description: "when there are a lot of files, the animation will freeze when opening", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.FRONT, }, { Key: "check down link", Value: "false", Type: "bool", Description: "check down link password, your link will be 'https://alist.com/d/filename?pw=xxx'", - Group: model.PUBLIC, + Access: model.PUBLIC, + Group: model.BACK, }, { Key: "WebDAV username", Value: "alist", Description: "WebDAV username", Type: "string", - Group: model.PRIVATE, + Access: model.PRIVATE, + Group: model.BACK, }, { Key: "WebDAV password", Value: "alist", Description: "WebDAV password", Type: "string", - Group: model.PRIVATE, + Access: model.PRIVATE, + Group: model.BACK, }, } for i, _ := range settings { @@ -193,8 +187,10 @@ func InitSettings() { log.Fatal("can't get setting: %s", err.Error()) } } else { - o.Version = conf.GitTag - err = model.SaveSetting(*o) + //o.Version = conf.GitTag + //err = model.SaveSetting(*o) + v.Value = o.Value + err = model.SaveSetting(v) if err != nil { log.Fatalf("failed write setting: %s", err.Error()) } diff --git a/model/setting.go b/model/setting.go index 671e1339..e817a61c 100644 --- a/model/setting.go +++ b/model/setting.go @@ -13,16 +13,33 @@ const ( CONST ) +const ( + FRONT = iota + BACK + OTHER +) + type SettingItem struct { Key string `json:"key" gorm:"primaryKey" binding:"required"` Value string `json:"value"` Description string `json:"description"` Type string `json:"type"` Group int `json:"group"` + Access int `json:"access"` Values string `json:"values"` Version string `json:"version"` } +var Version = SettingItem{ + Key: "version", + Value: conf.GitTag, + Description: "version", + Type: "string", + Access: CONST, + Version: conf.GitTag, + Group: OTHER, +} + func SaveSettings(items []SettingItem) error { return conf.DB.Save(items).Error } @@ -31,20 +48,29 @@ func SaveSetting(item SettingItem) error { return conf.DB.Save(item).Error } -func GetSettingsPublic() (*[]SettingItem, error) { +func GetSettingsPublic() ([]SettingItem, error) { var items []SettingItem - if err := conf.DB.Where("`group` <> ?", 1).Find(&items).Error; err != nil { + if err := conf.DB.Where("`access` <> ?", 1).Find(&items).Error; err != nil { return nil, err } - return &items, nil + return items, nil } -func GetSettings() (*[]SettingItem, error) { +func GetSettingsByGroup(group int) ([]SettingItem, error) { + var items []SettingItem + if err := conf.DB.Where("`group` = ?", group).Find(&items).Error; err != nil { + return nil, err + } + items = append([]SettingItem{Version}, items...) + return items, nil +} + +func GetSettings() ([]SettingItem, error) { var items []SettingItem if err := conf.DB.Find(&items).Error; err != nil { return nil, err } - return &items, nil + return items, nil } func DeleteSetting(key string) error { diff --git a/server/controllers/setting.go b/server/controllers/setting.go index d048ee59..19b2fe17 100644 --- a/server/controllers/setting.go +++ b/server/controllers/setting.go @@ -5,6 +5,7 @@ import ( "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/server/common" "github.com/gin-gonic/gin" + "strconv" ) func SaveSettings(c *gin.Context) { @@ -22,7 +23,17 @@ func SaveSettings(c *gin.Context) { } func GetSettings(c *gin.Context) { - settings, err := model.GetSettings() + groupStr := c.Query("group") + var settings []model.SettingItem + var err error + if groupStr == "" { + settings, err = model.GetSettings() + } else { + group, err := strconv.Atoi(groupStr) + if err == nil { + settings, err = model.GetSettingsByGroup(group) + } + } if err != nil { common.ErrorResp(c, err, 400) return @@ -36,7 +47,7 @@ func GetSettingsPublic(c *gin.Context) { common.ErrorResp(c, err, 400) return } - *settings = append(*settings, model.SettingItem{ + settings = append(settings, model.SettingItem{ Key: "no cors", Value: base.GetNoCors(), Description: "",