From f4142badffc0d3bb5ea838073eb3ecbf6472de3a Mon Sep 17 00:00:00 2001 From: Jealous Date: Fri, 31 Jan 2025 01:42:31 +0800 Subject: [PATCH] feat(index): support regular expressions for ignored paths --- internal/search/build.go | 20 ++++++++++++++------ internal/search/util.go | 3 ++- server/handles/index.go | 6 ++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/internal/search/build.go b/internal/search/build.go index 2888c1f4..a2407efc 100644 --- a/internal/search/build.go +++ b/internal/search/build.go @@ -4,6 +4,7 @@ import ( "context" "path" "path/filepath" + "regexp" "strings" "sync" "sync/atomic" @@ -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) @@ -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) { return filepath.SkipDir } } @@ -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) diff --git a/internal/search/util.go b/internal/search/util.go index 8d03b740..e644fe06 100644 --- a/internal/search/util.go +++ b/internal/search/util.go @@ -1,6 +1,7 @@ package search import ( + "regexp" "strings" "github.com/alist-org/alist/v3/drivers/alist_v3" @@ -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 } } diff --git a/server/handles/index.go b/server/handles/index.go index 5610688d..2dff976b 100644 --- a/server/handles/index.go +++ b/server/handles/index.go @@ -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" @@ -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) } @@ -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) }