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/db"
|
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
2022-11-28 05:45:25 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
type SearchResp struct {
|
|
|
|
model.SearchNode
|
|
|
|
Type int `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func Search(c *gin.Context) {
|
|
|
|
var req model.SearchReq
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := req.Validate(); err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nodes, total, err := search.Search(c, req)
|
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2022-12-02 02:09:39 +00:00
|
|
|
filteredNodes := []model.SearchNode{}
|
|
|
|
user := c.MustGet("user").(*model.User)
|
|
|
|
for _, node := range nodes {
|
|
|
|
if !strings.HasPrefix(node.Parent, user.BasePath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
meta, err := db.GetNearestMeta(node.Parent)
|
|
|
|
if err != nil && !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !common.CanAccess(user, meta, path.Join(node.Parent, node.Name), "") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// node.Parent = "/" + strings.Replace(node.Parent, user.BasePath, "", 1)
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|