mirror of https://github.com/Xhofe/alist
feat(search): Optimized search result filtering and paging logic (#9287)
- Introduced the `filteredNodes` list to optimize the node filtering process - Filtered results based on the page limit during paging - Modified search logic to ensure nodes are within the user's base path - Added access permission checks for node metadata - Adjusted paging logic to avoid redundant node retrievalpull/8608/merge
parent
d7723c378f
commit
3319f6ea6a
|
@ -43,28 +43,39 @@ func Search(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
nodes, total, err := search.Search(c, req.SearchReq)
|
var (
|
||||||
if err != nil {
|
filteredNodes []model.SearchNode
|
||||||
common.ErrorResp(c, err, 500)
|
)
|
||||||
return
|
for len(filteredNodes) < req.PerPage {
|
||||||
}
|
nodes, _, err := search.Search(c, req.SearchReq)
|
||||||
var filteredNodes []model.SearchNode
|
if err != nil {
|
||||||
for _, node := range nodes {
|
common.ErrorResp(c, err, 500)
|
||||||
if !strings.HasPrefix(node.Parent, user.BasePath) {
|
return
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
meta, err := op.GetNearestMeta(node.Parent)
|
if len(nodes) == 0 {
|
||||||
if err != nil && !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
break
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
if !common.CanAccessWithRoles(user, meta, path.Join(node.Parent, node.Name), req.Password) {
|
for _, node := range nodes {
|
||||||
continue
|
if !strings.HasPrefix(node.Parent, user.BasePath) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
meta, err := op.GetNearestMeta(node.Parent)
|
||||||
|
if err != nil && !errors.Is(errors.Cause(err), errs.MetaNotFound) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !common.CanAccessWithRoles(user, meta, path.Join(node.Parent, node.Name), req.Password) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
filteredNodes = append(filteredNodes, node)
|
||||||
|
if len(filteredNodes) >= req.PerPage {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
filteredNodes = append(filteredNodes, node)
|
req.Page++
|
||||||
}
|
}
|
||||||
common.SuccessResp(c, common.PageResp{
|
common.SuccessResp(c, common.PageResp{
|
||||||
Content: utils.MustSliceConvert(filteredNodes, nodeToSearchResp),
|
Content: utils.MustSliceConvert(filteredNodes, nodeToSearchResp),
|
||||||
Total: total,
|
Total: int64(len(filteredNodes)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue