filebrowser/handlers/search.go
Henrique Dias eb86ce27f6 improve search #88
Former-commit-id: 8cd36c3ce8d79bf256587107fbe31e9ae88c85b6 [formerly f685d2088f8c28b03a816a71851e321e371cf821] [formerly 13b9d953907545737051178a0ee26e60fe29d305 [formerly 1ff0c6cb68c9f1edc4b0dbcf77f910baa5966748]]
Former-commit-id: 81cd9b61592d8367b340ce52ade8ee0707f28ba9 [formerly d6d2074f965ff37d3342fce90c626b934c1f65c2]
Former-commit-id: 064840128e5a8788998905e6012bd7e29f553228
2017-05-27 08:29:58 +01:00

114 lines
2.4 KiB
Go

package handlers
import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gorilla/websocket"
"github.com/hacdias/caddy-filemanager/config"
)
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 ...
func Search(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (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.Replace(r.URL.Path, c.BaseURL, "", 1)
scope = strings.TrimPrefix(scope, "/")
scope = "/" + scope
scope = u.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.Replace(path, "\\", "/", -1)
for _, term := range search.Terms {
if strings.Contains(path, term) {
if !u.Allowed(path) {
return nil
}
path = strings.TrimPrefix(path, scope)
path = strings.TrimPrefix(path, "/")
err = conn.WriteMessage(websocket.TextMessage, []byte(path))
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}