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/db"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
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"
|
|
|
|
"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 := db.SaveSettingItem(item); err != nil {
|
|
|
|
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 := db.GetSettingItemByKey(key)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
common.SuccessResp(c, item)
|
|
|
|
} else {
|
|
|
|
items, err := db.GetSettingItemInKeys(strings.Split(keys, ","))
|
|
|
|
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
|
|
|
|
}
|
2022-06-27 09:25:19 +00:00
|
|
|
if err := db.SaveSettingItems(req); err != nil {
|
2022-06-27 09:06:10 +00:00
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
} else {
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 == "" {
|
2022-06-27 09:25:19 +00:00
|
|
|
settings, err = db.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
|
|
|
}
|
2022-07-12 06:03:03 +00:00
|
|
|
settings, err = db.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")
|
2022-06-27 09:25:19 +00:00
|
|
|
if err := db.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, db.GetPublicSettingsMap())
|
|
|
|
}
|