mirror of
https://github.com/AlistGo/alist.git
synced 2025-11-25 03:15:10 +08:00
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 retrieval
This commit is contained in:
@@ -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)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user