Skip to content

Commit

Permalink
chore: settings util
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 27, 2022
1 parent 005ded4 commit f01a81e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 105 deletions.
8 changes: 4 additions & 4 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -45,20 +45,20 @@ 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)
}
}
// 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)
}
Expand Down
96 changes: 0 additions & 96 deletions internal/db/setting.go

This file was deleted.

96 changes: 96 additions & 0 deletions internal/db/settingitem.go
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 22 additions & 0 deletions internal/setting/setting.go
Original file line number Diff line number Diff line change
@@ -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"
}
8 changes: 4 additions & 4 deletions server/controllers/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion server/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f01a81e

Please sign in to comment.