11package data
22
33import (
4+ "sort"
45 "strconv"
56
67 "github.com/OpenListTeam/OpenList/v4/cmd/flags"
@@ -15,10 +16,16 @@ import (
1516 "gorm.io/gorm"
1617)
1718
18- var initialSettingItems []model.SettingItem
19-
2019func initSettings () {
21- InitialSettings ()
20+ initialSettingItems := InitialSettings ()
21+ isActive := func (key string ) bool {
22+ for _ , item := range initialSettingItems {
23+ if item .Key == key {
24+ return true
25+ }
26+ }
27+ return false
28+ }
2229 // check deprecated
2330 settings , err := op .GetSettingItems ()
2431 if err != nil {
@@ -35,13 +42,16 @@ func initSettings() {
3542 }
3643 settingMap [v .Key ] = & v
3744 }
45+ op .MigrationSettingItems = map [string ]op.MigrationValueItem {}
3846 // create or save setting
39- save := false
47+ var saveItems []model. SettingItem
4048 for i := range initialSettingItems {
4149 item := & initialSettingItems [i ]
4250 item .Index = uint (i )
43- if len (item .MigrationValue ) == 0 {
44- item .MigrationValue = item .Value
51+ migrationValue := item .MigrationValue
52+ if len (migrationValue ) > 0 {
53+ op .MigrationSettingItems [item .Key ] = op.MigrationValueItem {MigrationValue : item .MigrationValue , Value : item .Value }
54+ item .MigrationValue = ""
4555 }
4656 // err
4757 stored , ok := settingMap [item .Key ]
@@ -52,21 +62,21 @@ func initSettings() {
5262 continue
5363 }
5464 }
55- if stored != nil && item .Key != conf .VERSION && stored .Value != item .MigrationValue {
65+ if item .Key != conf .VERSION && stored != nil &&
66+ (len (migrationValue ) == 0 || stored .Value != migrationValue ) {
5667 item .Value = stored .Value
5768 }
5869 _ , err = op .HandleSettingItemHook (item )
5970 if err != nil {
6071 utils .Log .Errorf ("failed to execute hook on %s: %+v" , item .Key , err )
6172 continue
6273 }
63- // save
6474 if stored == nil || * item != * stored {
65- save = true
75+ saveItems = append ( saveItems , * item )
6676 }
6777 }
68- if save {
69- err = db .SaveSettingItems (initialSettingItems )
78+ if len ( saveItems ) > 0 {
79+ err = db .SaveSettingItems (saveItems )
7080 if err != nil {
7181 utils .Log .Fatalf ("failed save setting: %+v" , err )
7282 } else {
@@ -75,23 +85,14 @@ func initSettings() {
7585 }
7686}
7787
78- func isActive (key string ) bool {
79- for _ , item := range initialSettingItems {
80- if item .Key == key {
81- return true
82- }
83- }
84- return false
85- }
86-
8788func InitialSettings () []model.SettingItem {
8889 var token string
8990 if flags .Dev {
9091 token = "dev_token"
9192 } else {
9293 token = random .Token ()
9394 }
94- initialSettingItems = []model.SettingItem {
95+ initialSettingItems : = []model.SettingItem {
9596 // site settings
9697 {Key : conf .VERSION , Value : conf .Version , Type : conf .TypeString , Group : model .SITE , Flag : model .READONLY },
9798 //{Key: conf.ApiUrl, Value: "", Type: conf.TypeString, Group: model.SITE},
@@ -223,7 +224,12 @@ func InitialSettings() []model.SettingItem {
223224 {Key : conf .StreamMaxServerDownloadSpeed , Value : "-1" , Type : conf .TypeNumber , Group : model .TRAFFIC , Flag : model .PRIVATE },
224225 {Key : conf .StreamMaxServerUploadSpeed , Value : "-1" , Type : conf .TypeNumber , Group : model .TRAFFIC , Flag : model .PRIVATE },
225226 }
226- initialSettingItems = append (initialSettingItems , tool .Tools .Items ()... )
227+ additionalSettingItems := tool .Tools .Items ()
228+ // 固定顺序
229+ sort .Slice (additionalSettingItems , func (i , j int ) bool {
230+ return additionalSettingItems [i ].Key < additionalSettingItems [j ].Key
231+ })
232+ initialSettingItems = append (initialSettingItems , additionalSettingItems ... )
227233 if flags .Dev {
228234 initialSettingItems = append (initialSettingItems , []model.SettingItem {
229235 {Key : "test_deprecated" , Value : "test_value" , Type : conf .TypeString , Flag : model .DEPRECATED },
0 commit comments