chore: settings util

refactor/fs
Noah Hsu 2022-06-27 17:25:19 +08:00
parent 005ded41c3
commit f01a81ee9c
6 changed files with 127 additions and 105 deletions

View File

@ -35,7 +35,7 @@ var initialSettingItems = []model.SettingItem{
func initSettings() {
// check deprecated
settings, err := db.GetSettings()
settings, err := db.GetSettingItems()
if err != nil {
log.Fatalf("failed get settings: %+v", err)
}
@ -45,7 +45,7 @@ func initSettings() {
}
}
if settings != nil && len(settings) > 0 {
err = db.SaveSettings(settings)
err = db.SaveSettingItems(settings)
if err != nil {
log.Fatalf("failed save settings: %+v", err)
}
@ -53,12 +53,12 @@ func initSettings() {
// insert new items
for i, _ := range initialSettingItems {
v := initialSettingItems[i]
_, err := db.GetSettingByKey(v.Key)
_, err := db.GetSettingItemByKey(v.Key)
if err == nil {
continue
}
if errors.Is(err, gorm.ErrRecordNotFound) {
err = db.SaveSetting(v)
err = db.SaveSettingItem(v)
if err != nil {
log.Fatalf("failed create setting: %+v", err)
}

View File

@ -1,96 +0,0 @@
package db
import (
"fmt"
"github.com/alist-org/alist/v3/internal/model"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var settingsMap map[string]string
var publicSettingsMap map[string]string
func GetPublicSettingsMap() map[string]string {
if settingsMap == nil {
publicSettingsMap = make(map[string]string)
settings, err := GetPublicSettings()
if err != nil {
log.Errorf("failed to get settings: %+v", err)
}
for _, setting := range settings {
publicSettingsMap[setting.Key] = setting.Value
}
}
return publicSettingsMap
}
func GetSettingsMap() map[string]string {
if settingsMap == nil {
settingsMap = make(map[string]string)
settings, err := GetSettings()
if err != nil {
log.Errorf("failed to get settings: %+v", err)
}
for _, setting := range settings {
settingsMap[setting.Key] = setting.Value
}
}
return settingsMap
}
func GetSettings() ([]model.SettingItem, error) {
var items []model.SettingItem
if err := db.Find(&items).Error; err != nil {
return nil, errors.WithStack(err)
}
return items, nil
}
func GetSettingByKey(key string) (*model.SettingItem, error) {
var item model.SettingItem
if err := db.Where(fmt.Sprintf("%s = ?", columnName("key")), key).First(&item).Error; err != nil {
return nil, errors.WithStack(err)
}
return &item, nil
}
func GetPublicSettings() ([]model.SettingItem, error) {
var items []model.SettingItem
if err := db.Where(fmt.Sprintf("%s in ?", columnName("flag")), []int{0, 2}).Find(&items).Error; err != nil {
return nil, err
}
return items, nil
}
func GetSettingsByGroup(group int) ([]model.SettingItem, error) {
var items []model.SettingItem
if err := db.Where(fmt.Sprintf("%s = ?", columnName("group")), group).Find(&items).Error; err != nil {
return nil, errors.WithStack(err)
}
return items, nil
}
func SaveSettings(items []model.SettingItem) error {
settingsMap = nil
return errors.WithStack(db.Save(items).Error)
}
func SaveSetting(item model.SettingItem) error {
settingsMap = nil
return errors.WithStack(db.Save(item).Error)
}
func DeleteSettingByKey(key string) error {
setting := model.SettingItem{
Key: key,
}
old, err := GetSettingByKey(key)
if err != nil {
return errors.WithMessage(err, "failed to get setting")
}
if !old.IsDeprecated() {
return errors.Errorf("setting [%s] is not deprecated", key)
}
settingsMap = nil
return errors.WithStack(db.Delete(&setting).Error)
}

View File

@ -0,0 +1,96 @@
package db
import (
"fmt"
"github.com/alist-org/alist/v3/internal/model"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var settingsMap map[string]string
var publicSettingsMap map[string]string
func GetPublicSettingsMap() map[string]string {
if settingsMap == nil {
publicSettingsMap = make(map[string]string)
settingItems, err := GetPublicSettingItems()
if err != nil {
log.Errorf("failed to get settingItems: %+v", err)
}
for _, settingItem := range settingItems {
publicSettingsMap[settingItem.Key] = settingItem.Value
}
}
return publicSettingsMap
}
func GetSettingsMap() map[string]string {
if settingsMap == nil {
settingsMap = make(map[string]string)
settingItems, err := GetSettingItems()
if err != nil {
log.Errorf("failed to get settingItems: %+v", err)
}
for _, settingItem := range settingItems {
settingsMap[settingItem.Key] = settingItem.Value
}
}
return settingsMap
}
func GetSettingItems() ([]model.SettingItem, error) {
var settingItems []model.SettingItem
if err := db.Find(&settingItems).Error; err != nil {
return nil, errors.WithStack(err)
}
return settingItems, nil
}
func GetSettingItemByKey(key string) (*model.SettingItem, error) {
var settingItem model.SettingItem
if err := db.Where(fmt.Sprintf("%s = ?", columnName("key")), key).First(&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{0, 2}).Find(&settingItems).Error; err != nil {
return nil, err
}
return settingItems, nil
}
func GetSettingItemsByGroup(group int) ([]model.SettingItem, error) {
var settingItems []model.SettingItem
if err := db.Where(fmt.Sprintf("%s = ?", columnName("group")), group).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)
}
func SaveSettingItem(item model.SettingItem) error {
settingsMap = nil
return errors.WithStack(db.Save(item).Error)
}
func DeleteSettingItemByKey(key string) error {
settingItem := model.SettingItem{
Key: key,
}
old, err := GetSettingItemByKey(key)
if err != nil {
return errors.WithMessage(err, "failed to get settingItem")
}
if !old.IsDeprecated() {
return errors.Errorf("setting [%s] is not deprecated", key)
}
settingsMap = nil
return errors.WithStack(db.Delete(&settingItem).Error)
}

View File

@ -0,0 +1,22 @@
package setting
import (
"github.com/alist-org/alist/v3/internal/db"
"strconv"
)
func GetByKey(key string) string {
return db.GetSettingsMap()[key]
}
func GetIntSetting(key string, defaultVal int) int {
i, err := strconv.Atoi(GetByKey(key))
if err != nil {
return defaultVal
}
return i
}
func IsTrue(key string) bool {
return GetByKey(key) == "true" || GetByKey(key) == "1"
}

View File

@ -14,7 +14,7 @@ func SaveSettings(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
if err := db.SaveSettings(req); err != nil {
if err := db.SaveSettingItems(req); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
@ -26,11 +26,11 @@ func ListSettings(c *gin.Context) {
var settings []model.SettingItem
var err error
if groupStr == "" {
settings, err = db.GetSettings()
settings, err = db.GetSettingItems()
} else {
group, err := strconv.Atoi(groupStr)
if err == nil {
settings, err = db.GetSettingsByGroup(group)
settings, err = db.GetSettingItemsByGroup(group)
}
}
if err != nil {
@ -42,7 +42,7 @@ func ListSettings(c *gin.Context) {
func DeleteSetting(c *gin.Context) {
key := c.Query("key")
if err := db.DeleteSettingByKey(key); err != nil {
if err := db.DeleteSettingItemByKey(key); err != nil {
common.ErrorResp(c, err, 500)
return
}

View File

@ -41,7 +41,7 @@ func Auth(c *gin.Context) {
func AuthAdmin(c *gin.Context) {
user := c.MustGet("user").(*model.User)
if !user.IsAdmin() {
common2.ErrorStrResp(c, "You are not an admin", 403)
common2.ErrorStrResp(c, "You are not an admin", 403, true)
c.Abort()
} else {
c.Next()