filebrowser/backend/index/conditions.go

123 lines
2.4 KiB
Go
Raw Normal View History

package index
import (
"regexp"
"strconv"
2023-08-05 14:58:06 +00:00
"strings"
)
var typeRegexp = regexp.MustCompile(`type:(\S+)`)
2023-06-18 15:04:31 +00:00
2023-07-04 23:55:15 +00:00
var documentTypes = []string{
".word",
".pdf",
".txt",
2023-07-13 02:23:29 +00:00
".doc",
".docx",
}
2023-07-04 23:55:15 +00:00
var compressedFile = []string{
".7z",
".rar",
".zip",
".tar",
".tar.gz",
".tar.xz",
}
2023-08-12 16:30:41 +00:00
type SearchOptions struct {
2023-08-12 19:41:59 +00:00
Conditions map[string]bool
LargerThan int
SmallerThan int
Terms []string
}
2023-08-12 16:30:41 +00:00
func ParseSearch(value string) *SearchOptions {
opts := &SearchOptions{
2023-08-05 14:58:06 +00:00
Conditions: map[string]bool{
2023-07-04 23:55:15 +00:00
"exact": strings.Contains(value, "case:exact"),
},
2023-08-05 14:58:06 +00:00
Terms: []string{},
}
// removes the options from the value
2023-07-04 23:55:15 +00:00
value = strings.Replace(value, "case:exact", "", -1)
value = strings.TrimSpace(value)
types := typeRegexp.FindAllStringSubmatch(value, -1)
2023-07-04 23:55:15 +00:00
for _, filterType := range types {
if len(filterType) == 1 {
continue
}
filter := filterType[1]
switch filter {
2023-08-05 14:58:06 +00:00
case "image":
opts.Conditions["image"] = true
case "audio", "music":
opts.Conditions["audio"] = true
case "video":
opts.Conditions["video"] = true
case "doc":
opts.Conditions["doc"] = true
case "archive":
opts.Conditions["archive"] = true
case "folder":
opts.Conditions["dir"] = true
case "file":
opts.Conditions["dir"] = false
}
if len(filter) < 8 {
continue
}
2023-08-12 19:41:59 +00:00
if strings.HasPrefix(filter, "largerThan=") {
opts.Conditions["larger"] = true
2023-08-12 19:41:59 +00:00
size := strings.TrimPrefix(filter, "largerThan=")
opts.LargerThan = updateSize(size)
}
2023-08-12 19:41:59 +00:00
if strings.HasPrefix(filter, "smallerThan=") {
opts.Conditions["larger"] = true
size := strings.TrimPrefix(filter, "smallerThan=")
opts.SmallerThan = updateSize(size)
}
}
if len(types) > 0 {
2023-08-12 16:30:41 +00:00
// Remove the fields from the search value
value = typeRegexp.ReplaceAllString(value, "")
}
if value == "" {
return opts
}
// if the value starts with " and finishes what that character, we will
// only search for that term
if value[0] == '"' && value[len(value)-1] == '"' {
unique := strings.TrimPrefix(value, "\"")
unique = strings.TrimSuffix(unique, "\"")
opts.Terms = []string{unique}
return opts
}
2023-08-05 14:58:06 +00:00
value = strings.TrimSpace(value)
opts.Terms = strings.Split(value, "|")
return opts
}
func toInt(str string) int {
val, err := strconv.Atoi(str)
if err != nil {
return 0
}
return val
}
func updateSize(given string) int {
size := toInt(given)
if size == 0 {
return 100
} else {
return size
}
2023-08-05 14:58:06 +00:00
}