filebrowser/backend/files/search.go

178 lines
4.5 KiB
Go
Raw Normal View History

package files
2023-06-15 01:08:09 +00:00
import (
2023-08-12 16:30:41 +00:00
"math/rand"
2023-06-15 01:08:09 +00:00
"os"
"path/filepath"
2023-06-18 15:04:31 +00:00
"sort"
2023-06-15 01:08:09 +00:00
"strings"
2023-06-16 17:29:43 +00:00
"sync"
2023-06-15 01:08:09 +00:00
"time"
)
2023-06-16 17:29:43 +00:00
var (
sessionInProgress sync.Map
maxSearchResults = 100
2023-06-16 17:29:43 +00:00
)
2023-06-15 01:08:09 +00:00
func (si *Index) Search(search string, scope string, sourceSession string) ([]string, map[string]map[string]bool) {
if scope == "" {
scope = "/"
}
runningHash := generateRandomHash(4)
sessionInProgress.Store(sourceSession, runningHash) // Store the value in the sync.Map
2023-06-18 15:04:31 +00:00
searchOptions := ParseSearch(search)
2023-07-13 02:23:29 +00:00
fileListTypes := make(map[string]map[string]bool)
matching := []string{}
count := 0
2023-06-18 15:04:31 +00:00
for _, searchTerm := range searchOptions.Terms {
2023-07-04 23:55:15 +00:00
if searchTerm == "" {
continue
}
si.mu.Lock()
defer si.mu.Unlock()
for dirName, dir := range si.Directories {
isDir := true
files := strings.Split(dir.Files, ";")
value, found := sessionInProgress.Load(sourceSession)
if !found || value != runningHash {
return []string{}, map[string]map[string]bool{}
}
if count > maxSearchResults {
break
}
pathName := scopedPathNameFilter(dirName, scope, isDir)
if pathName == "" {
continue // path not matched
}
fileTypes := map[string]bool{}
matches, fileType := containsSearchTerm(dirName, searchTerm, *searchOptions, isDir, fileTypes)
if matches {
fileListTypes[pathName] = fileType
matching = append(matching, pathName)
count++
}
isDir = false
for _, file := range files {
if file == "" {
continue
}
2023-08-17 21:46:49 +00:00
value, found := sessionInProgress.Load(sourceSession)
if !found || value != runningHash {
return []string{}, map[string]map[string]bool{}
}
if count > maxSearchResults {
2023-08-17 21:46:49 +00:00
break
}
fullName := pathName + file
fileTypes := map[string]bool{}
matches, fileType := containsSearchTerm(fullName, searchTerm, *searchOptions, isDir, fileTypes)
if !matches {
continue
}
fileListTypes[fullName] = fileType
matching = append(matching, fullName)
count++
2023-07-13 02:23:29 +00:00
}
2023-06-15 01:08:09 +00:00
}
}
2023-06-16 17:29:43 +00:00
// Sort the strings based on the number of elements after splitting by "/"
sort.Slice(matching, func(i, j int) bool {
parts1 := strings.Split(matching[i], "/")
parts2 := strings.Split(matching[j], "/")
2023-06-16 17:29:43 +00:00
return len(parts1) < len(parts2)
})
return matching, fileListTypes
2023-06-15 01:08:09 +00:00
}
func scopedPathNameFilter(pathName string, scope string, isDir bool) string {
2023-08-17 21:46:49 +00:00
scope = strings.TrimPrefix(scope, "/")
pathName = strings.TrimPrefix(pathName, "/")
pathName = strings.TrimSuffix(pathName, "/")
if strings.HasPrefix(pathName, scope) {
pathName = strings.TrimPrefix(pathName, scope)
if isDir {
pathName = pathName + "/"
}
2023-08-17 21:46:49 +00:00
} else {
pathName = "" // return not matched
}
2023-08-17 21:46:49 +00:00
return pathName
}
func containsSearchTerm(pathName string, searchTerm string, options SearchOptions, isDir bool, fileTypes map[string]bool) (bool, map[string]bool) {
conditions := options.Conditions
2023-08-12 16:30:41 +00:00
path := getLastPathComponent(pathName)
// Convert to lowercase once
if !conditions["exact"] {
2023-08-12 19:41:59 +00:00
path = strings.ToLower(path)
searchTerm = strings.ToLower(searchTerm)
2023-08-12 16:30:41 +00:00
}
if strings.Contains(path, searchTerm) {
2023-08-12 16:30:41 +00:00
// Calculate fileSize only if needed
var fileSize int64
matchesAllConditions := true
2023-08-12 19:41:59 +00:00
extension := filepath.Ext(path)
for _, k := range AllFiletypeOptions {
if IsMatchingType(extension, k) {
fileTypes[k] = true
}
}
2023-08-12 16:30:41 +00:00
fileTypes["dir"] = isDir
for t, v := range conditions {
if t == "exact" {
continue
}
var matchesCondition bool
switch t {
2023-08-12 16:30:41 +00:00
case "larger":
if fileSize == 0 {
fileSize = getFileSize(pathName)
}
matchesCondition = fileSize > int64(options.LargerThan)*bytesInMegabyte
2023-08-12 16:30:41 +00:00
case "smaller":
if fileSize == 0 {
fileSize = getFileSize(pathName)
}
matchesCondition = fileSize < int64(options.SmallerThan)*bytesInMegabyte
2023-08-12 16:30:41 +00:00
default:
matchesCondition = v == fileTypes[t]
}
2023-08-12 16:30:41 +00:00
if !matchesCondition {
2023-07-31 22:20:14 +00:00
matchesAllConditions = false
}
}
2023-07-31 22:20:14 +00:00
return matchesAllConditions, fileTypes
}
2023-08-12 16:30:41 +00:00
// Clear variables and return
return false, map[string]bool{}
2023-07-13 02:23:29 +00:00
}
func getFileSize(filepath string) int64 {
2023-08-12 16:30:41 +00:00
fileInfo, err := os.Stat(rootPath + "/" + filepath)
if err != nil {
return 0
}
return fileInfo.Size()
}
2023-06-15 01:08:09 +00:00
func getLastPathComponent(path string) string {
// Use filepath.Base to extract the last component of the path
return filepath.Base(path)
2023-06-18 15:04:31 +00:00
}
func generateRandomHash(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
2023-09-25 01:03:09 +00:00
rand.New(rand.NewSource(time.Now().UnixNano()))
result := make([]byte, length)
for i := range result {
result[i] = charset[rand.Intn(len(charset))]
}
return string(result)
}