alist/server/handles/setting.go

106 lines
2.4 KiB
Go
Raw Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-27 09:06:10 +00:00
import (
2022-08-03 06:26:59 +00:00
"strconv"
"strings"
2022-06-28 06:18:10 +00:00
"github.com/alist-org/alist/v3/internal/conf"
2022-06-27 09:06:10 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
2022-06-28 07:12:40 +00:00
"github.com/alist-org/alist/v3/internal/sign"
2022-06-28 06:18:10 +00:00
"github.com/alist-org/alist/v3/pkg/utils/random"
2022-06-27 09:06:10 +00:00
"github.com/alist-org/alist/v3/server/common"
2022-08-28 15:13:03 +00:00
"github.com/alist-org/alist/v3/server/static"
2022-06-27 09:06:10 +00:00
"github.com/gin-gonic/gin"
)
2022-06-28 06:18:10 +00:00
func ResetToken(c *gin.Context) {
token := random.Token()
item := model.SettingItem{Key: "token", Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE}
if err := op.SaveSettingItem(&item); err != nil {
2022-06-28 06:18:10 +00:00
common.ErrorResp(c, err, 500)
return
}
2022-06-28 07:12:40 +00:00
sign.Instance()
2022-06-28 06:18:10 +00:00
common.SuccessResp(c, token)
}
func GetSetting(c *gin.Context) {
key := c.Query("key")
2022-07-12 06:03:03 +00:00
keys := c.Query("keys")
if key != "" {
item, err := op.GetSettingItemByKey(key)
2022-07-12 06:03:03 +00:00
if err != nil {
common.ErrorResp(c, err, 400)
return
}
common.SuccessResp(c, item)
} else {
items, err := op.GetSettingItemInKeys(strings.Split(keys, ","))
2022-07-12 06:03:03 +00:00
if err != nil {
common.ErrorResp(c, err, 400)
return
}
common.SuccessResp(c, items)
2022-06-28 06:18:10 +00:00
}
}
2022-06-27 09:06:10 +00:00
func SaveSettings(c *gin.Context) {
var req []model.SettingItem
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
if err := op.SaveSettingItems(req); err != nil {
2022-06-27 09:06:10 +00:00
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
2022-08-28 15:13:03 +00:00
static.UpdateIndex()
2022-06-27 09:06:10 +00:00
}
}
func ListSettings(c *gin.Context) {
groupStr := c.Query("group")
2022-07-12 06:03:03 +00:00
groupsStr := c.Query("groups")
2022-06-27 09:06:10 +00:00
var settings []model.SettingItem
var err error
2022-07-12 06:03:03 +00:00
if groupsStr == "" && groupStr == "" {
settings, err = op.GetSettingItems()
2022-06-27 09:06:10 +00:00
} else {
2022-07-12 06:03:03 +00:00
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)
2022-06-27 09:06:10 +00:00
}
settings, err = op.GetSettingItemsInGroups(groups)
2022-06-27 09:06:10 +00:00
}
if err != nil {
common.ErrorResp(c, err, 400)
return
}
common.SuccessResp(c, settings)
}
func DeleteSetting(c *gin.Context) {
key := c.Query("key")
if err := op.DeleteSettingItemByKey(key); err != nil {
2022-06-27 09:06:10 +00:00
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}
func PublicSettings(c *gin.Context) {
common.SuccessResp(c, op.GetPublicSettingsMap())
2022-06-27 09:06:10 +00:00
}