2017-06-24 19:12:15 +08:00
|
|
|
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.
|
2017-06-27 21:26:12 +08:00
|
|
|
func search(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
2017-06-24 19:12:15 +08:00
|
|
|
// 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)
|
2017-06-27 17:45:06 +08:00
|
|
|
scope := strings.TrimPrefix(r.URL.Path, "/")
|
2017-06-24 19:12:15 +08:00
|
|
|
scope = "/" + scope
|
2017-06-27 21:26:12 +08:00
|
|
|
scope = c.us.scope + scope
|
2017-06-24 19:12:15 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-06-28 02:00:58 +08:00
|
|
|
path = strings.TrimPrefix(path, scope)
|
|
|
|
path = strings.TrimPrefix(path, "/")
|
2017-06-24 19:12:15 +08:00
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
|
|
|
is := false
|
|
|
|
|
|
|
|
for _, term := range search.Terms {
|
|
|
|
if is {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(path, term) {
|
2017-06-27 21:26:12 +08:00
|
|
|
if !c.us.Allowed(path) {
|
2017-06-24 19:12:15 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
is = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !is {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn.WriteMessage(websocket.TextMessage, []byte(path))
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2017-06-29 02:06:08 +08:00
|
|
|
return 0, nil
|
2017-06-24 19:12:15 +08:00
|
|
|
}
|