filebrowser/search.go
Henrique Dias f6816c9a39 Major changes on API
Former-commit-id: 096988e1e40835be12cdbfc685e58c3a6faf7d54 [formerly c687ad9725e93f7ee34bcef0193bafb6643feb62] [formerly cf70ea15f14f4f849a8f449438ffdb8ab5b60f6e [formerly 1f7974de38]]
Former-commit-id: 85e35a88d9d46c2a6fe3598b902ad8ab86019c05 [formerly f38667fb170916541f62b14389b6296e8da2580e]
Former-commit-id: 5addfd36ad865a8644f659e5140453e1fdada62d
2017-07-02 17:40:52 +01:00

117 lines
2.3 KiB
Go

package filemanager
import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gorilla/websocket"
)
type searchOptions struct {
CaseInsensitive bool
Terms []string
}
func parseSearch(value string) *searchOptions {
opts := &searchOptions{
CaseInsensitive: strings.Contains(value, "case:insensitive"),
}
// removes the options from the value
value = strings.Replace(value, "case:insensitive", "", -1)
value = strings.Replace(value, "case:sensitive", "", -1)
value = strings.TrimSpace(value)
if opts.CaseInsensitive {
value = strings.ToLower(value)
}
// 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
}
opts.Terms = strings.Split(value, " ")
return opts
}
// search searches for a file or directory.
func search(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
// Upgrades the connection to a websocket and checks for errors.
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return 0, err
}
defer conn.Close()
var (
value string
search *searchOptions
message []byte
)
// Starts an infinite loop until a valid command is captured.
for {
_, message, err = conn.ReadMessage()
if err != nil {
return http.StatusInternalServerError, err
}
if len(message) != 0 {
value = string(message)
break
}
}
search = parseSearch(value)
scope := strings.TrimPrefix(r.URL.Path, "/")
scope = "/" + scope
scope = c.us.Scope + scope
scope = strings.Replace(scope, "\\", "/", -1)
scope = filepath.Clean(scope)
err = filepath.Walk(scope, func(path string, f os.FileInfo, err error) error {
if search.CaseInsensitive {
path = strings.ToLower(path)
}
path = strings.TrimPrefix(path, scope)
path = strings.TrimPrefix(path, "/")
path = strings.Replace(path, "\\", "/", -1)
is := false
for _, term := range search.Terms {
if is {
break
}
if strings.Contains(path, term) {
if !c.us.Allowed(path) {
return nil
}
is = true
}
}
if !is {
return nil
}
return conn.WriteMessage(websocket.TextMessage, []byte(path))
})
if err != nil {
return http.StatusInternalServerError, err
}
return 0, nil
}