Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(index): support regular expressions for ignored paths #7909

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions internal/search/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"sync/atomic"
Expand All @@ -30,12 +31,21 @@ func Running() bool {
return Quit.Load() != nil
}

func BuildIndex(ctx context.Context, indexPaths, ignorePaths []string, maxDepth int, count bool) error {
func BuildIndex(ctx context.Context, indexPaths []string, maxDepth int, count bool) error {
var (
err error
objCount uint64 = 0
fi model.Obj
)
ignorePaths := conf.SlicesMap[conf.IgnorePaths]
ignoreRegs := make([]*regexp.Regexp, 0, len(ignorePaths))
for _, ignorePath := range ignorePaths {
if reg, err := regexp.Compile(ignorePath); err == nil {
ignoreRegs = append(ignoreRegs, reg)
} else {
return err
}
}
log.Infof("build index for: %+v", indexPaths)
log.Infof("ignore paths: %+v", ignorePaths)
quit := make(chan struct{}, 1)
Expand Down Expand Up @@ -152,8 +162,8 @@ func BuildIndex(ctx context.Context, indexPaths, ignorePaths []string, maxDepth
if !running.Load() {
return filepath.SkipDir
}
for _, avoidPath := range ignorePaths {
if strings.HasPrefix(indexPath, avoidPath) {
for _, ignoreReg := range ignoreRegs {
if ignoreReg.MatchString(indexPath) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉这里最好加个正则标识,以不影响之前的设置。
比如以 re_ 开头才当做正则处理

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原本的评估是改成正则后原来的路径依然在覆盖范围内,只是相同路径的子目录也会被覆盖进去,感觉影响不是太大,所以没做完全兼容。
如果期望100%跟原来一样的话,加一个『使用正则表达式配置忽略路径』的选项呢?

return filepath.SkipDir
}
}
Expand Down Expand Up @@ -254,9 +264,7 @@ func Update(parent string, objs []model.Obj) {
} else {
// build index if it's a folder
dir := path.Join(parent, objs[i].GetName())
err = BuildIndex(ctx,
[]string{dir},
conf.SlicesMap[conf.IgnorePaths],
err = BuildIndex(ctx, []string{dir},
setting.GetInt(conf.MaxIndexDepth, 20)-strings.Count(dir, "/"), false)
if err != nil {
log.Errorf("update search index error while build index: %+v", err)
Expand Down
3 changes: 2 additions & 1 deletion internal/search/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package search

import (
"regexp"
"strings"

"github.com/alist-org/alist/v3/drivers/alist_v3"
Expand Down Expand Up @@ -75,7 +76,7 @@ func updateIgnorePaths() {

func isIgnorePath(path string) bool {
for _, ignorePath := range conf.SlicesMap[conf.IgnorePaths] {
if strings.HasPrefix(path, ignorePath) {
if match, _ := regexp.MatchString(ignorePath, path); match {
return true
}
}
Expand Down
6 changes: 2 additions & 4 deletions server/handles/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handles

import (
"context"

"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/search"
Expand Down Expand Up @@ -31,7 +30,7 @@ func BuildIndex(c *gin.Context) {
return
}
err = search.BuildIndex(context.Background(), []string{"/"},
conf.SlicesMap[conf.IgnorePaths], setting.GetInt(conf.MaxIndexDepth, 20), true)
setting.GetInt(conf.MaxIndexDepth, 20), true)
if err != nil {
log.Errorf("build index error: %+v", err)
}
Expand Down Expand Up @@ -62,8 +61,7 @@ func UpdateIndex(c *gin.Context) {
return
}
}
err := search.BuildIndex(context.Background(), req.Paths,
conf.SlicesMap[conf.IgnorePaths], req.MaxDepth, false)
err := search.BuildIndex(context.Background(), req.Paths, req.MaxDepth, false)
if err != nil {
log.Errorf("update index error: %+v", err)
}
Expand Down
Loading