mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
b578e2196a
Former-commit-id: d9c9234e87c190d847572c1fe715cf745ba44b53 [formerly f8b5f600fe39be8b47aece870ff24756c5dcfc6d] [formerly ad1cbfd739888d4cc3a1db882a98251cac3b89cc [formerly bafe9f0ad7
]]
Former-commit-id: 493e4f3e4b42092201cfd1f99d8ee6cd3eb25dfc [formerly 520a9df6582492f24906f8f3f5c5e90cf5a02acd]
Former-commit-id: 17484d0fe391a82d5ee0dc990f4b1f039d305f4c
70 lines
1.4 KiB
Go
70 lines
1.4 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)
|
|
|
|
scope = strings.Replace(scope, "\\", "/", -1)
|
|
scope = strings.TrimPrefix(scope, "/")
|
|
scope = strings.TrimSuffix(scope, "/")
|
|
scope = "/" + scope + "/"
|
|
|
|
return afero.Walk(fs, scope, func(originalPath string, f os.FileInfo, err error) error {
|
|
originalPath = strings.Replace(originalPath, "\\", "/", -1)
|
|
originalPath = strings.TrimPrefix(originalPath, "/")
|
|
originalPath = "/" + originalPath
|
|
path := originalPath
|
|
|
|
if path == scope {
|
|
return nil
|
|
}
|
|
|
|
if !checker.Check(path) {
|
|
return nil
|
|
}
|
|
|
|
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(originalPath, scope), f)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|