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
pull/8608/merge
千石 2025-08-25 19:46:24 +08:00 committed by GitHub
parent d7723c378f
commit 3319f6ea6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 27 additions and 16 deletions

View File

@ -43,28 +43,39 @@ func Search(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
nodes, total, err := search.Search(c, req.SearchReq)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
var filteredNodes []model.SearchNode
for _, node := range nodes {
if !strings.HasPrefix(node.Parent, user.BasePath) {
continue
var (
filteredNodes []model.SearchNode
)
for len(filteredNodes) < req.PerPage {
nodes, _, err := search.Search(c, req.SearchReq)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
meta, err := op.GetNearestMeta(node.Parent)
if err != nil && !errors.Is(errors.Cause(err), errs.MetaNotFound) {
continue
if len(nodes) == 0 {
break
}
if !common.CanAccessWithRoles(user, meta, path.Join(node.Parent, node.Name), req.Password) {
continue
for _, node := range nodes {
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{
Content: utils.MustSliceConvert(filteredNodes, nodeToSearchResp),
Total: total,
Total: int64(len(filteredNodes)),
})
}