feat: default settings api (#716)

* feat: default settings api

* fix logic bug

* chore
This commit is contained in:
j2rong4cn
2025-07-14 23:41:34 +08:00
committed by GitHub
parent 750d4eb3f6
commit 2a4c546a8b
5 changed files with 95 additions and 45 deletions

View File

@ -1,6 +1,7 @@
package data
import (
"sort"
"strconv"
"github.com/OpenListTeam/OpenList/v4/cmd/flags"
@ -15,10 +16,16 @@ import (
"gorm.io/gorm"
)
var initialSettingItems []model.SettingItem
func initSettings() {
InitialSettings()
initialSettingItems := InitialSettings()
isActive := func(key string) bool {
for _, item := range initialSettingItems {
if item.Key == key {
return true
}
}
return false
}
// check deprecated
settings, err := op.GetSettingItems()
if err != nil {
@ -35,13 +42,16 @@ func initSettings() {
}
settingMap[v.Key] = &v
}
op.MigrationSettingItems = map[string]op.MigrationValueItem{}
// create or save setting
save := false
var saveItems []model.SettingItem
for i := range initialSettingItems {
item := &initialSettingItems[i]
item.Index = uint(i)
if len(item.MigrationValue) == 0 {
item.MigrationValue = item.Value
migrationValue := item.MigrationValue
if len(migrationValue) > 0 {
op.MigrationSettingItems[item.Key] = op.MigrationValueItem{MigrationValue: item.MigrationValue, Value: item.Value}
item.MigrationValue = ""
}
// err
stored, ok := settingMap[item.Key]
@ -52,7 +62,8 @@ func initSettings() {
continue
}
}
if stored != nil && item.Key != conf.VERSION && stored.Value != item.MigrationValue {
if item.Key != conf.VERSION && stored != nil &&
(len(migrationValue) == 0 || stored.Value != migrationValue) {
item.Value = stored.Value
}
_, err = op.HandleSettingItemHook(item)
@ -60,13 +71,12 @@ func initSettings() {
utils.Log.Errorf("failed to execute hook on %s: %+v", item.Key, err)
continue
}
// save
if stored == nil || *item != *stored {
save = true
saveItems = append(saveItems, *item)
}
}
if save {
err = db.SaveSettingItems(initialSettingItems)
if len(saveItems) > 0 {
err = db.SaveSettingItems(saveItems)
if err != nil {
utils.Log.Fatalf("failed save setting: %+v", err)
} else {
@ -75,15 +85,6 @@ func initSettings() {
}
}
func isActive(key string) bool {
for _, item := range initialSettingItems {
if item.Key == key {
return true
}
}
return false
}
func InitialSettings() []model.SettingItem {
var token string
if flags.Dev {
@ -91,7 +92,7 @@ func InitialSettings() []model.SettingItem {
} else {
token = random.Token()
}
initialSettingItems = []model.SettingItem{
initialSettingItems := []model.SettingItem{
// site settings
{Key: conf.VERSION, Value: conf.Version, Type: conf.TypeString, Group: model.SITE, Flag: model.READONLY},
//{Key: conf.ApiUrl, Value: "", Type: conf.TypeString, Group: model.SITE},
@ -223,7 +224,12 @@ func InitialSettings() []model.SettingItem {
{Key: conf.StreamMaxServerDownloadSpeed, Value: "-1", Type: conf.TypeNumber, Group: model.TRAFFIC, Flag: model.PRIVATE},
{Key: conf.StreamMaxServerUploadSpeed, Value: "-1", Type: conf.TypeNumber, Group: model.TRAFFIC, Flag: model.PRIVATE},
}
initialSettingItems = append(initialSettingItems, tool.Tools.Items()...)
additionalSettingItems := tool.Tools.Items()
// 固定顺序
sort.Slice(additionalSettingItems, func(i, j int) bool {
return additionalSettingItems[i].Key < additionalSettingItems[j].Key
})
initialSettingItems = append(initialSettingItems, additionalSettingItems...)
if flags.Dev {
initialSettingItems = append(initialSettingItems, []model.SettingItem{
{Key: "test_deprecated", Value: "test_value", Type: conf.TypeString, Flag: model.DEPRECATED},