2019-01-05 22:44:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2023-08-12 16:30:41 +00:00
|
|
|
"net/http"
|
2023-10-18 15:32:22 +00:00
|
|
|
"strings"
|
2023-08-12 22:21:31 +00:00
|
|
|
|
2023-12-01 23:47:00 +00:00
|
|
|
"github.com/gtsteffaniak/filebrowser/files"
|
|
|
|
"github.com/gtsteffaniak/filebrowser/settings"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var searchHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
|
|
response := []map[string]interface{}{}
|
|
|
|
query := r.URL.Query().Get("query")
|
2023-08-17 21:46:49 +00:00
|
|
|
// Retrieve the User-Agent and X-Auth headers from the request
|
|
|
|
sessionId := r.Header.Get("SessionId")
|
2023-10-18 15:32:22 +00:00
|
|
|
userScope := r.Header.Get("UserScope")
|
2023-12-01 23:47:00 +00:00
|
|
|
index := files.GetIndex(settings.Config.Server.Root)
|
2023-10-18 15:32:22 +00:00
|
|
|
combinedScope := strings.TrimPrefix(userScope+r.URL.Path, ".")
|
|
|
|
results, fileTypes := index.Search(query, combinedScope, sessionId)
|
2023-09-30 14:18:21 +00:00
|
|
|
for _, path := range results {
|
2023-07-13 02:23:29 +00:00
|
|
|
responseObj := map[string]interface{}{
|
2023-08-12 16:30:41 +00:00
|
|
|
"path": path,
|
2023-08-12 22:21:31 +00:00
|
|
|
"dir": true,
|
2023-07-13 02:23:29 +00:00
|
|
|
}
|
2023-08-12 22:21:31 +00:00
|
|
|
if _, ok := fileTypes[path]; ok {
|
|
|
|
responseObj["dir"] = false
|
|
|
|
for filterType, value := range fileTypes[path] {
|
|
|
|
if value {
|
|
|
|
responseObj[filterType] = value
|
|
|
|
}
|
2023-07-17 05:17:52 +00:00
|
|
|
}
|
2023-07-13 02:23:29 +00:00
|
|
|
}
|
2023-08-12 16:30:41 +00:00
|
|
|
response = append(response, responseObj)
|
2023-06-15 15:30:49 +00:00
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
return renderJSON(w, r, response)
|
2023-08-12 16:30:41 +00:00
|
|
|
})
|