mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
749d3ea3fc
Former-commit-id: dcc6bd82b3d3a89637a1032aad1a25d0b1f80046 [formerly 8784cd37bf58e81cdbe1bcec00e6f16b58efc915] [formerly 9e25850e063ae0825de337d5d5f29cee601b9040 [formerly 8b1d36dfb9
]]
Former-commit-id: 04a38bea2d141093570d9289d0d0a056a136fe8a [formerly 5995538504889e698aa6cd35b7da40c38b5d5ddf]
Former-commit-id: 8c81a0b060167e1a2983a99bc87b380838ac07dc
117 lines
2.4 KiB
Go
117 lines
2.4 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 = string(c.User.FileSystem) + 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.User.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
|
|
}
|