chore: optimize get settings

refactor/fs
Noah Hsu 2022-07-12 14:03:03 +08:00
parent 4ac312fd07
commit fbc858b43c
4 changed files with 52 additions and 12 deletions

View File

@ -71,10 +71,10 @@ func initialSettings() {
{Key: conf.ApiUrl, Value: "", Type: conf.TypeString, Group: model.SITE},
{Key: conf.BasePath, Value: "", Type: conf.TypeString, Group: model.SITE},
{Key: conf.SiteTitle, Value: "AList", Type: conf.TypeString, Group: model.SITE},
{Key: conf.SiteLogo, Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.SITE},
{Key: conf.Favicon, Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.SITE},
{Key: conf.Announcement, Value: "https://github.com/alist-org/alist", Type: conf.TypeString, Group: model.SITE},
// style settings
{Key: conf.Logo, Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.STYLE},
{Key: conf.Favicon, Value: "https://cdn.jsdelivr.net/gh/alist-org/logo@main/logo.svg", Type: conf.TypeString, Group: model.STYLE},
{Key: conf.IconColor, Value: "#1890ff", Type: conf.TypeString, Group: model.STYLE},
// preview settings
{Key: conf.TextTypes, Value: "txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp,tsx,vtt,srt,ass,rs,lrc", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},

View File

@ -13,7 +13,7 @@ const (
ApiUrl = "api_url"
BasePath = "base_path"
SiteTitle = "site_title"
SiteLogo = "site_logo"
Logo = "logo"
Favicon = "favicon"
Announcement = "announcement"
IconColor = "icon_color"

View File

@ -54,6 +54,14 @@ func GetSettingItemByKey(key string) (*model.SettingItem, error) {
return &settingItem, nil
}
func GetSettingItemInKeys(keys []string) ([]model.SettingItem, error) {
var settingItem []model.SettingItem
if err := db.Where(fmt.Sprintf("%s in ?", columnName("key")), keys).Find(&settingItem).Error; err != nil {
return nil, errors.WithStack(err)
}
return settingItem, nil
}
func GetPublicSettingItems() ([]model.SettingItem, error) {
var settingItems []model.SettingItem
if err := db.Where(fmt.Sprintf("%s in ?", columnName("flag")), []int{model.PUBLIC, model.READONLY}).Find(&settingItems).Error; err != nil {
@ -70,6 +78,14 @@ func GetSettingItemsByGroup(group int) ([]model.SettingItem, error) {
return settingItems, nil
}
func GetSettingItemsInGroups(groups []int) ([]model.SettingItem, error) {
var settingItems []model.SettingItem
if err := db.Where(fmt.Sprintf("%s in ?", columnName("group")), groups).Find(&settingItems).Error; err != nil {
return nil, errors.WithStack(err)
}
return settingItems, nil
}
func SaveSettingItems(items []model.SettingItem) error {
settingsMap = nil
return errors.WithStack(db.Save(items).Error)

View File

@ -9,6 +9,7 @@ import (
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"strconv"
"strings"
)
func ResetToken(c *gin.Context) {
@ -24,12 +25,22 @@ func ResetToken(c *gin.Context) {
func GetSetting(c *gin.Context) {
key := c.Query("key")
item, err := db.GetSettingItemByKey(key)
if err != nil {
common.ErrorResp(c, err, 400)
return
keys := c.Query("keys")
if key != "" {
item, err := db.GetSettingItemByKey(key)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
common.SuccessResp(c, item)
} else {
items, err := db.GetSettingItemInKeys(strings.Split(keys, ","))
if err != nil {
common.ErrorResp(c, err, 400)
return
}
common.SuccessResp(c, items)
}
common.SuccessResp(c, item)
}
func SaveSettings(c *gin.Context) {
@ -47,15 +58,28 @@ func SaveSettings(c *gin.Context) {
func ListSettings(c *gin.Context) {
groupStr := c.Query("group")
groupsStr := c.Query("groups")
var settings []model.SettingItem
var err error
if groupStr == "" {
if groupsStr == "" && groupStr == "" {
settings, err = db.GetSettingItems()
} else {
group, err := strconv.Atoi(groupStr)
if err == nil {
settings, err = db.GetSettingItemsByGroup(group)
var groupStrings []string
if groupsStr != "" {
groupStrings = strings.Split(groupsStr, ",")
} else {
groupStrings = append(groupStrings, groupStr)
}
var groups []int
for _, str := range groupStrings {
group, err := strconv.Atoi(str)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
groups = append(groups, group)
}
settings, err = db.GetSettingItemsInGroups(groups)
}
if err != nil {
common.ErrorResp(c, err, 400)