2022-11-28 05:45:25 +00:00
|
|
|
package handles
|
|
|
|
|
|
|
|
import (
|
2022-12-02 02:09:39 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
2022-11-28 05:45:25 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2022-12-18 11:51:20 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/op"
|
2022-11-28 05:45:25 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/search"
|
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-12-02 02:09:39 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-11-28 05:45:25 +00:00
|
|
|
)
|
|
|
|
|
2022-12-07 02:45:02 +00:00
|
|
|
type SearchReq struct {
|
|
|
|
model.SearchReq
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
2022-11-28 05:45:25 +00:00
|
|
|
type SearchResp struct {
|
|
|
|
model.SearchNode
|
|
|
|
Type int `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func Search(c *gin.Context) {
|
2022-12-06 09:28:39 +00:00
|
|
|
var (
|
2022-12-07 02:45:02 +00:00
|
|
|
req SearchReq
|
2022-12-06 09:28:39 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = c.ShouldBind(&req); err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user := c.MustGet("user").(*model.User)
|
|
|
|
req.Parent, err = user.JoinPath(req.Parent)
|
|
|
|
if err != nil {
|
2022-11-28 05:45:25 +00:00
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := req.Validate(); err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
2022-12-07 02:45:02 +00:00
|
|
|
nodes, total, err := search.Search(c, req.SearchReq)
|
2022-11-28 05:45:25 +00:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2022-12-06 09:28:39 +00:00
|
|
|
var filteredNodes []model.SearchNode
|
2022-12-02 02:09:39 +00:00
|
|
|
for _, node := range nodes {
|
|
|
|
if !strings.HasPrefix(node.Parent, user.BasePath) {
|
|
|
|
continue
|
|
|
|
}
|
2022-12-18 11:51:20 +00:00
|
|
|
meta, err := op.GetNearestMeta(node.Parent)
|
2022-12-02 02:09:39 +00:00
|
|
|
if err != nil && !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
|
|
|
continue
|
|
|
|
}
|
2022-12-07 02:45:02 +00:00
|
|
|
if !common.CanAccess(user, meta, path.Join(node.Parent, node.Name), req.Password) {
|
2022-12-02 02:09:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
filteredNodes = append(filteredNodes, node)
|
|
|
|
}
|
2022-11-28 05:45:25 +00:00
|
|
|
common.SuccessResp(c, common.PageResp{
|
2022-12-02 02:09:39 +00:00
|
|
|
Content: utils.MustSliceConvert(filteredNodes, nodeToSearchResp),
|
2022-11-28 05:45:25 +00:00
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeToSearchResp(node model.SearchNode) SearchResp {
|
|
|
|
return SearchResp{
|
|
|
|
SearchNode: node,
|
|
|
|
Type: utils.GetObjType(node.Name, node.IsDir),
|
|
|
|
}
|
|
|
|
}
|