mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 12:24:38 +03:00
Add exclude file from scan feature (#253)
* Added exclude file from scan feature * Abort exclusion instead of panicking when pattern isn't valid * Added UI configuration for exclude patterns * * cosmetic fixes * changed behavior of exclude function to continue and ignore invalide regex patterns * added some more tests (windows networks and continue after regex error)
This commit is contained in:
@@ -2,7 +2,9 @@ package manager
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -78,6 +80,7 @@ func (s *singleton) Scan(useFileMetadata bool) {
|
||||
return
|
||||
}
|
||||
|
||||
results, _ = excludeFiles(results, config.GetExcludes())
|
||||
total := len(results)
|
||||
logger.Infof("Starting scan of %d files. %d New files found", total, s.neededScan(results))
|
||||
|
||||
@@ -492,3 +495,46 @@ func (s *singleton) neededGenerate(scenes []*models.Scene, sprites, previews, ma
|
||||
}
|
||||
return &totals
|
||||
}
|
||||
|
||||
func excludeFiles(files []string, patterns []string) ([]string, int) {
|
||||
if patterns == nil {
|
||||
logger.Infof("No excludes in config.")
|
||||
return files, 0
|
||||
} else {
|
||||
var results []string
|
||||
var exclCount int
|
||||
var fileRegexps []*regexp.Regexp
|
||||
|
||||
for _, pattern := range patterns {
|
||||
reg, err := regexp.Compile(strings.ToLower(pattern))
|
||||
if err != nil {
|
||||
logger.Errorf("Exclude :%v", err)
|
||||
} else {
|
||||
fileRegexps = append(fileRegexps, reg)
|
||||
}
|
||||
}
|
||||
|
||||
if len(fileRegexps) == 0 {
|
||||
return files, 0
|
||||
}
|
||||
|
||||
for i := 0; i < len(files); i++ {
|
||||
match := false
|
||||
for _, regPattern := range fileRegexps {
|
||||
if regPattern.Match([]byte(strings.ToLower(files[i]))) {
|
||||
logger.Infof("File %s excluded from scan ", files[i])
|
||||
match = true
|
||||
exclCount++
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
//if pattern doesn't match add file to list
|
||||
if !match {
|
||||
results = append(results, files[i])
|
||||
}
|
||||
}
|
||||
logger.Infof("Excluded %d file(s) from scan ", exclCount)
|
||||
return results, exclCount
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user