grouping settings

pull/548/head
Xhofe 2021-12-28 16:38:56 +08:00
parent 04752f7473
commit 4c00866249
3 changed files with 95 additions and 62 deletions

View File

@ -10,16 +10,8 @@ import (
func InitSettings() { func InitSettings() {
log.Infof("init settings...") 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 { if err != nil {
log.Fatalf("failed write setting: %s", err.Error()) log.Fatalf("failed write setting: %s", err.Error())
} }
@ -30,153 +22,155 @@ func InitSettings() {
Value: "Alist", Value: "Alist",
Description: "title", Description: "title",
Type: "string", Type: "string",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "password", Key: "password",
Value: "alist", Value: "alist",
Description: "password", Description: "password",
Type: "string", Type: "string",
Group: model.PRIVATE, Access: model.PRIVATE,
Group: model.BACK,
}, },
{ {
Key: "logo", Key: "logo",
Value: "https://store.heytapimage.com/cdo-portal/feedback/202112/05/1542f45f86b8609495b69c5380753135.png", Value: "https://store.heytapimage.com/cdo-portal/feedback/202112/05/1542f45f86b8609495b69c5380753135.png",
Description: "logo", Description: "logo",
Type: "string", Type: "string",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "favicon", Key: "favicon",
Value: "https://store.heytapimage.com/cdo-portal/feedback/202112/05/1542f45f86b8609495b69c5380753135.png", Value: "https://store.heytapimage.com/cdo-portal/feedback/202112/05/1542f45f86b8609495b69c5380753135.png",
Description: "favicon", Description: "favicon",
Type: "string", Type: "string",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "icon color", Key: "icon color",
Value: "#1890ff", Value: "#1890ff",
Description: "icon's color", Description: "icon's color",
Type: "string", Type: "string",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "text types", Key: "text types",
Value: strings.Join(conf.TextTypes, ","), Value: strings.Join(conf.TextTypes, ","),
Type: "string", Type: "string",
Description: "text type extensions", Description: "text type extensions",
Group: model.FRONT,
}, },
{ {
Key: "hide readme file", Key: "hide readme file",
Value: "true", Value: "true",
Type: "bool", Type: "bool",
Description: "hide readme file? ", Description: "hide readme file? ",
Group: model.FRONT,
}, },
{ {
Key: "music cover", Key: "music cover",
Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png", Value: "https://store.heytapimage.com/cdo-portal/feedback/202110/30/d43c41c5d257c9bc36366e310374fb19.png",
Description: "music cover image", Description: "music cover image",
Type: "string", Type: "string",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "site beian", Key: "site beian",
Description: "chinese beian info", Description: "chinese beian info",
Type: "string", Type: "string",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "home readme url", Key: "home readme url",
Description: "when have multiple, the readme file to show", Description: "when have multiple, the readme file to show",
Type: "string", Type: "string",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "markdown theme", Key: "markdown theme",
Value: "vuepress", Value: "vuepress",
Description: "default | github | vuepress", Description: "default | github | vuepress",
Group: model.PUBLIC, Access: model.PUBLIC,
Type: "select", Type: "select",
Values: "default,github,vuepress", Values: "default,github,vuepress",
Group: model.FRONT,
}, },
{ {
Key: "autoplay video", Key: "autoplay video",
Value: "false", Value: "false",
Type: "bool", Type: "bool",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "autoplay audio", Key: "autoplay audio",
Value: "false", Value: "false",
Type: "bool", Type: "bool",
Group: model.PUBLIC, Access: model.PUBLIC,
Group: model.FRONT,
}, },
{ {
Key: "check parent folder", Key: "check parent folder",
Value: "false", Value: "false",
Type: "bool", Type: "bool",
Description: "check parent folder password", Description: "check parent folder password",
Group: model.PRIVATE, Access: model.PRIVATE,
Group: model.BACK,
}, },
{ {
Key: "customize head", Key: "customize head",
Value: `<style> Value: "",
.chakra-ui-light{
background-color: #edddfd;
}
.main-box {
border-radius: 15px !important;
box-shadow: unset !important;
}
.chakra-ui-light .main-box {
background-color: rgba(255,255,255,0.9) !important;
}
.chakra-ui-light .readme-box {
background-color: rgba(255,255,255,0.9) !important;
}
.readme-box {
border-radius: 15px !important;
box-shadow: unset !important;
}
</style>`,
Type: "text", Type: "text",
Description: "Customize head, placed at the beginning of the head", Description: "Customize head, placed at the beginning of the head",
Group: model.PRIVATE, Access: model.PRIVATE,
Group: model.FRONT,
}, },
{ {
Key: "customize body", Key: "customize body",
Value: "", Value: "",
Type: "text", Type: "text",
Description: "Customize script, placed at the end of the body", Description: "Customize script, placed at the end of the body",
Group: model.PRIVATE, Access: model.PRIVATE,
Group: model.FRONT,
}, },
{ {
Key: "animation", Key: "animation",
Value: "true", Value: "true",
Type: "bool", Type: "bool",
Description: "when there are a lot of files, the animation will freeze when opening", 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", Key: "check down link",
Value: "false", Value: "false",
Type: "bool", Type: "bool",
Description: "check down link password, your link will be 'https://alist.com/d/filename?pw=xxx'", 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", Key: "WebDAV username",
Value: "alist", Value: "alist",
Description: "WebDAV username", Description: "WebDAV username",
Type: "string", Type: "string",
Group: model.PRIVATE, Access: model.PRIVATE,
Group: model.BACK,
}, },
{ {
Key: "WebDAV password", Key: "WebDAV password",
Value: "alist", Value: "alist",
Description: "WebDAV password", Description: "WebDAV password",
Type: "string", Type: "string",
Group: model.PRIVATE, Access: model.PRIVATE,
Group: model.BACK,
}, },
} }
for i, _ := range settings { for i, _ := range settings {
@ -193,8 +187,10 @@ func InitSettings() {
log.Fatal("can't get setting: %s", err.Error()) log.Fatal("can't get setting: %s", err.Error())
} }
} else { } else {
o.Version = conf.GitTag //o.Version = conf.GitTag
err = model.SaveSetting(*o) //err = model.SaveSetting(*o)
v.Value = o.Value
err = model.SaveSetting(v)
if err != nil { if err != nil {
log.Fatalf("failed write setting: %s", err.Error()) log.Fatalf("failed write setting: %s", err.Error())
} }

View File

@ -13,16 +13,33 @@ const (
CONST CONST
) )
const (
FRONT = iota
BACK
OTHER
)
type SettingItem struct { type SettingItem struct {
Key string `json:"key" gorm:"primaryKey" binding:"required"` Key string `json:"key" gorm:"primaryKey" binding:"required"`
Value string `json:"value"` Value string `json:"value"`
Description string `json:"description"` Description string `json:"description"`
Type string `json:"type"` Type string `json:"type"`
Group int `json:"group"` Group int `json:"group"`
Access int `json:"access"`
Values string `json:"values"` Values string `json:"values"`
Version string `json:"version"` 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 { func SaveSettings(items []SettingItem) error {
return conf.DB.Save(items).Error return conf.DB.Save(items).Error
} }
@ -31,20 +48,29 @@ func SaveSetting(item SettingItem) error {
return conf.DB.Save(item).Error return conf.DB.Save(item).Error
} }
func GetSettingsPublic() (*[]SettingItem, error) { func GetSettingsPublic() ([]SettingItem, error) {
var items []SettingItem 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 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 var items []SettingItem
if err := conf.DB.Find(&items).Error; err != nil { if err := conf.DB.Find(&items).Error; err != nil {
return nil, err return nil, err
} }
return &items, nil return items, nil
} }
func DeleteSetting(key string) error { func DeleteSetting(key string) error {

View File

@ -5,6 +5,7 @@ import (
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common" "github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strconv"
) )
func SaveSettings(c *gin.Context) { func SaveSettings(c *gin.Context) {
@ -22,7 +23,17 @@ func SaveSettings(c *gin.Context) {
} }
func GetSettings(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 { if err != nil {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
@ -36,7 +47,7 @@ func GetSettingsPublic(c *gin.Context) {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return
} }
*settings = append(*settings, model.SettingItem{ settings = append(settings, model.SettingItem{
Key: "no cors", Key: "no cors",
Value: base.GetNoCors(), Value: base.GetNoCors(),
Description: "", Description: "",