filebrowser/search/search.go
Henrique Dias ea434ffc04 🧹: gofmt
Former-commit-id: 9548d370497e13d436abbc670cf40d360badab9f [formerly f29ba9c4dcf9962c3a5045629526296cd2b43ad8] [formerly 3f89c094c1b3330c8d6f1b2379d6309567d12210 [formerly 92fda0070c]]
Former-commit-id: 4ad64211019e0236f0eaa6e041a8793e899954fe [formerly 6bf71ffba205b563ff6cd0c7487d9a328c757686]
Former-commit-id: 4c7afe072fc3e2a225ca5a7dbf3f3beb66b44058
2019-01-05 23:01:16 +00:00

63 lines
1.1 KiB
Go

package search
import (
"os"
"strings"
"github.com/filebrowser/filebrowser/v2/rules"
"github.com/spf13/afero"
)
type searchOptions struct {
CaseSensitive bool
Conditions []condition
Terms []string
}
// Search searches for a query in a fs.
func Search(fs afero.Fs, scope, query string, checker rules.Checker, found func(path string, f os.FileInfo) error) error {
search := parseSearch(query)
return afero.Walk(fs, scope, func(path string, f os.FileInfo, err error) error {
path = strings.TrimPrefix(path, "/")
path = strings.Replace(path, "\\", "/", -1)
if !checker.Check(path) {
return nil
}
if !search.CaseSensitive {
path = strings.ToLower(path)
}
if !search.CaseSensitive {
path = strings.ToLower(path)
}
if len(search.Conditions) > 0 {
match := false
for _, t := range search.Conditions {
if t(path) {
match = true
break
}
}
if !match {
return nil
}
}
if len(search.Terms) > 0 {
for _, term := range search.Terms {
if strings.Contains(path, term) {
return found(strings.TrimPrefix(path, scope), f)
}
}
}
return nil
})
}