mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-07-19 01:48:42 +08:00
feat: default settings api (#716)
* feat: default settings api * fix logic bug * chore
This commit is contained in:
@ -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},
|
||||
|
@ -1,6 +1,7 @@
|
||||
package op
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -153,42 +154,36 @@ func GetSettingItemsInGroups(groups []int) ([]model.SettingItem, error) {
|
||||
}
|
||||
|
||||
func SaveSettingItems(items []model.SettingItem) error {
|
||||
noHookItems := make([]model.SettingItem, 0)
|
||||
errs := make([]error, 0)
|
||||
for i := range items {
|
||||
if ok, err := HandleSettingItemHook(&items[i]); ok {
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
err = db.SaveSettingItem(&items[i])
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
noHookItems = append(noHookItems, items[i])
|
||||
item := &items[i]
|
||||
if it, ok := MigrationSettingItems[item.Key]; ok &&
|
||||
item.Value == it.MigrationValue {
|
||||
item.Value = it.Value
|
||||
}
|
||||
if ok, err := HandleSettingItemHook(item); ok && err != nil {
|
||||
return fmt.Errorf("failed to execute hook on %s: %+v", item.Key, err)
|
||||
}
|
||||
}
|
||||
if len(noHookItems) > 0 {
|
||||
err := db.SaveSettingItems(noHookItems)
|
||||
err := db.SaveSettingItems(items)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
return fmt.Errorf("failed save setting: %+v", err)
|
||||
}
|
||||
if len(errs) < len(items)-len(noHookItems)+1 {
|
||||
SettingCacheUpdate()
|
||||
}
|
||||
return utils.MergeErrors(errs...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveSettingItem(item *model.SettingItem) (err error) {
|
||||
if it, ok := MigrationSettingItems[item.Key]; ok &&
|
||||
item.Value == it.MigrationValue {
|
||||
item.Value = it.Value
|
||||
}
|
||||
// hook
|
||||
if _, err := HandleSettingItemHook(item); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to execute hook on %s: %+v", item.Key, err)
|
||||
}
|
||||
// update
|
||||
if err = db.SaveSettingItem(item); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed save setting on %s: %+v", item.Key, err)
|
||||
}
|
||||
SettingCacheUpdate()
|
||||
return nil
|
||||
@ -205,3 +200,9 @@ func DeleteSettingItemByKey(key string) error {
|
||||
SettingCacheUpdate()
|
||||
return db.DeleteSettingItemByKey(key)
|
||||
}
|
||||
|
||||
type MigrationValueItem struct {
|
||||
MigrationValue, Value string
|
||||
}
|
||||
|
||||
var MigrationSettingItems map[string]MigrationValueItem
|
||||
|
@ -73,7 +73,7 @@ func attachHeader(w http.ResponseWriter, file model.Obj, header http.Header) {
|
||||
func GetEtag(file model.Obj) string {
|
||||
hash := ""
|
||||
for _, v := range file.GetHash().Export() {
|
||||
if strings.Compare(v, hash) > 0 {
|
||||
if v > hash {
|
||||
hash = v
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package handles
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/data"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/conf"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
||||
"github.com/OpenListTeam/OpenList/v4/internal/op"
|
||||
@ -91,6 +93,46 @@ func ListSettings(c *gin.Context) {
|
||||
common.SuccessResp(c, settings)
|
||||
}
|
||||
|
||||
func DefaultSettings(c *gin.Context) {
|
||||
groupStr := c.Query("group")
|
||||
groupsStr := c.Query("groups")
|
||||
settings := data.InitialSettings()
|
||||
if groupsStr == "" && groupStr == "" {
|
||||
for i := range settings {
|
||||
(&settings[i]).Index = uint(i)
|
||||
}
|
||||
common.SuccessResp(c, settings)
|
||||
} else {
|
||||
var groupStrings []string
|
||||
if groupsStr != "" {
|
||||
groupStrings = strings.Split(groupsStr, ",")
|
||||
} else {
|
||||
groupStrings = append(groupStrings, groupStr)
|
||||
}
|
||||
var groups []int
|
||||
for _, str := range groupStrings {
|
||||
group, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
groups = append(groups, group)
|
||||
}
|
||||
sort.Ints(groups)
|
||||
var resultItems []model.SettingItem
|
||||
for _, group := range groups {
|
||||
for i := range settings {
|
||||
item := settings[i]
|
||||
if group == item.Group {
|
||||
item.Index = uint(i)
|
||||
resultItems = append(resultItems, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
common.SuccessResp(c, resultItems)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteSetting(c *gin.Context) {
|
||||
key := c.Query("key")
|
||||
if err := op.DeleteSettingItemByKey(key); err != nil {
|
||||
|
@ -140,6 +140,7 @@ func admin(g *gin.RouterGroup) {
|
||||
setting.GET("/list", handles.ListSettings)
|
||||
setting.POST("/save", handles.SaveSettings)
|
||||
setting.POST("/delete", handles.DeleteSetting)
|
||||
setting.POST("/default", handles.DefaultSettings)
|
||||
setting.POST("/reset_token", handles.ResetToken)
|
||||
setting.POST("/set_aria2", handles.SetAria2)
|
||||
setting.POST("/set_qbit", handles.SetQbittorrent)
|
||||
|
Reference in New Issue
Block a user