alist/server/handles/setting.go
foxxorcat 6024e8d832
refactor: split the db package hook and cache to the op package (#2747)
* refactor:separate the setting method from the db package to the op package and add the cache

* refactor:separate the meta method from the db package to the op package

* fix:setting not load database data

* refactor:separate the user method from the db package to the op package

* refactor:remove user JoinPath error

* fix:op package user cache

* refactor:fs package list method

* fix:tile virtual paths (close #2743)

* Revert "refactor:remove user JoinPath error"

This reverts commit 4e20daaf9e700da047000d4fd4900abbe05c3848.

* clean path directly may lead to unknown behavior

* fix: The path of the meta passed in must be prefix of reqPath

* chore: rename all virtualPath to mountPath

* fix: `getStoragesByPath` and `GetStorageVirtualFilesByPath`

is_sub_path:

/a/b isn't subpath of /a/bc

* fix: don't save setting if hook error

Co-authored-by: Noah Hsu <i@nn.ci>
2022-12-18 19:51:20 +08:00

106 lines
2.4 KiB
Go

package handles
import (
"strconv"
"strings"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/sign"
"github.com/alist-org/alist/v3/pkg/utils/random"
"github.com/alist-org/alist/v3/server/common"
"github.com/alist-org/alist/v3/server/static"
"github.com/gin-gonic/gin"
)
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 {
common.ErrorResp(c, err, 500)
return
}
sign.Instance()
common.SuccessResp(c, token)
}
func GetSetting(c *gin.Context) {
key := c.Query("key")
keys := c.Query("keys")
if key != "" {
item, err := op.GetSettingItemByKey(key)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
common.SuccessResp(c, item)
} else {
items, err := op.GetSettingItemInKeys(strings.Split(keys, ","))
if err != nil {
common.ErrorResp(c, err, 400)
return
}
common.SuccessResp(c, items)
}
}
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 {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
static.UpdateIndex()
}
}
func ListSettings(c *gin.Context) {
groupStr := c.Query("group")
groupsStr := c.Query("groups")
var settings []model.SettingItem
var err error
if groupsStr == "" && groupStr == "" {
settings, err = op.GetSettingItems()
} 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)
}
settings, err = op.GetSettingItemsInGroups(groups)
}
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 {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}
func PublicSettings(c *gin.Context) {
common.SuccessResp(c, op.GetPublicSettingsMap())
}