alist/model/setting.go

64 lines
1.4 KiB
Go
Raw Normal View History

2021-10-26 14:28:37 +00:00
package model
2021-10-27 10:59:03 +00:00
import (
"github.com/Xhofe/alist/conf"
2021-11-12 14:56:30 +00:00
"strings"
2021-10-27 10:59:03 +00:00
)
const (
PUBLIC = iota
PRIVATE
CONST
)
2021-10-26 14:28:37 +00:00
type SettingItem struct {
2021-11-13 07:53:26 +00:00
Key string `json:"key" gorm:"primaryKey" binding:"required"`
2021-10-26 14:28:37 +00:00
Value string `json:"value"`
Description string `json:"description"`
2021-11-12 11:39:01 +00:00
Type string `json:"type"`
2021-10-29 16:35:29 +00:00
Group int `json:"group"`
2021-11-12 11:39:01 +00:00
Values string `json:"values"`
2021-10-26 14:28:37 +00:00
}
func SaveSettings(items []SettingItem) error {
return conf.DB.Save(items).Error
}
2021-11-04 05:23:17 +00:00
func SaveSetting(item SettingItem) error {
return conf.DB.Save(item).Error
}
2021-11-03 11:51:19 +00:00
func GetSettingsPublic() (*[]SettingItem, error) {
2021-10-26 14:28:37 +00:00
var items []SettingItem
2021-11-03 11:51:19 +00:00
if err := conf.DB.Where("`group` <> ?", 1).Find(&items).Error; err != nil {
2021-10-29 16:35:29 +00:00
return nil, err
}
return &items, nil
}
func GetSettings() (*[]SettingItem, error) {
var items []SettingItem
if err := conf.DB.Find(&items).Error; err != nil {
2021-10-26 14:28:37 +00:00
return nil, err
}
return &items, nil
}
2021-10-27 10:59:03 +00:00
func GetSettingByKey(key string) (*SettingItem, error) {
var items SettingItem
2021-11-07 06:04:54 +00:00
if err := conf.DB.Where("`key` = ?", key).First(&items).Error; err != nil {
2021-10-27 10:59:03 +00:00
return nil, err
}
return &items, nil
}
2021-11-12 14:56:30 +00:00
func LoadSettings() {
textTypes, err := GetSettingByKey("text types")
if err == nil {
conf.TextTypes = strings.Split(textTypes.Value, ",")
}
checkParent, err := GetSettingByKey("check parent folder")
if err == nil {
conf.CheckParent = checkParent.Value == "true"
}
}