From f01a81ee9c5e102aa92baf66961d308f7785665d Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Mon, 27 Jun 2022 17:25:19 +0800 Subject: [PATCH] chore: settings util --- internal/bootstrap/data/setting.go | 8 +-- internal/db/setting.go | 96 ------------------------------ internal/db/settingitem.go | 96 ++++++++++++++++++++++++++++++ internal/setting/setting.go | 22 +++++++ server/controllers/setting.go | 8 +-- server/middlewares/auth.go | 2 +- 6 files changed, 127 insertions(+), 105 deletions(-) delete mode 100644 internal/db/setting.go create mode 100644 internal/db/settingitem.go create mode 100644 internal/setting/setting.go diff --git a/internal/bootstrap/data/setting.go b/internal/bootstrap/data/setting.go index a68bb5bb3aa..5af8b3c82dd 100644 --- a/internal/bootstrap/data/setting.go +++ b/internal/bootstrap/data/setting.go @@ -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) } diff --git a/internal/db/setting.go b/internal/db/setting.go deleted file mode 100644 index 750e0a96e95..00000000000 --- a/internal/db/setting.go +++ /dev/null @@ -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) -} diff --git a/internal/db/settingitem.go b/internal/db/settingitem.go new file mode 100644 index 00000000000..853b9ce59f8 --- /dev/null +++ b/internal/db/settingitem.go @@ -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) +} diff --git a/internal/setting/setting.go b/internal/setting/setting.go new file mode 100644 index 00000000000..df0f44c5220 --- /dev/null +++ b/internal/setting/setting.go @@ -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" +} diff --git a/server/controllers/setting.go b/server/controllers/setting.go index 6e59bddd517..705214fbb26 100644 --- a/server/controllers/setting.go +++ b/server/controllers/setting.go @@ -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 } diff --git a/server/middlewares/auth.go b/server/middlewares/auth.go index 9ae3623524b..50a93298971 100644 --- a/server/middlewares/auth.go +++ b/server/middlewares/auth.go @@ -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()