filebrowser/backend/http/search.go

74 lines
2.6 KiB
Go
Raw Normal View History

package http
import (
2023-08-12 16:30:41 +00:00
"net/http"
"strings"
2023-08-12 22:21:31 +00:00
2024-12-17 00:01:55 +00:00
"github.com/gtsteffaniak/filebrowser/backend/files"
)
2024-11-21 00:15:30 +00:00
// searchHandler handles search requests for files based on the provided query.
//
// This endpoint processes a search query, retrieves relevant file paths, and
// returns a JSON response with the search results. The search is performed
// against the file index, which is built from the root directory specified in
// the server's configuration. The results are filtered based on the user's scope.
//
// The handler expects the following headers in the request:
// - SessionId: A unique identifier for the user's session.
// - UserScope: The scope of the user, which influences the search context.
//
// The request URL should include a query parameter named `query` that specifies
// the search terms to use. The response will include an array of searchResponse objects
// containing the path, type, and dir status.
//
// Example request:
//
// GET api/search?query=myfile
//
// Example response:
// [
//
// {
// "path": "/path/to/myfile.txt",
// "type": "text"
// },
// {
// "path": "/path/to/mydir/",
// "type": "directory"
// }
//
// ]
//
// @Summary Search Files
// @Description Searches for files matching the provided query. Returns file paths and metadata based on the user's session and scope.
// @Tags Search
// @Accept json
// @Produce json
// @Param query query string true "Search query"
// @Param scope query string false "path within user scope to search, for example '/first/second' to search within the second directory only"
// @Param SessionId header string false "User session ID, add unique value to prevent collisions"
2024-12-17 00:01:55 +00:00
// @Success 200 {array} files.SearchResult "List of search results"
2024-11-21 00:15:30 +00:00
// @Failure 400 {object} map[string]string "Bad Request"
// @Router /api/search [get]
func searchHandler(w http.ResponseWriter, r *http.Request, d *requestContext) (int, error) {
query := r.URL.Query().Get("query")
2025-01-05 19:05:33 +00:00
source := r.URL.Query().Get("source")
if source == "" {
source = "default"
}
2024-11-21 00:15:30 +00:00
searchScope := strings.TrimPrefix(r.URL.Query().Get("scope"), ".")
searchScope = strings.TrimPrefix(searchScope, "/")
2023-08-17 21:46:49 +00:00
// Retrieve the User-Agent and X-Auth headers from the request
sessionId := r.Header.Get("SessionId")
2025-01-05 19:05:33 +00:00
index := files.GetIndex(source)
2024-11-21 00:15:30 +00:00
userScope := strings.TrimPrefix(d.user.Scope, ".")
combinedScope := strings.TrimPrefix(userScope+"/"+searchScope, "/")
// Perform the search using the provided query and user scope
response := index.Search(query, combinedScope, sessionId)
// Set the Content-Type header to application/json
w.Header().Set("Content-Type", "application/json")
return renderJSON(w, r, response)
2024-11-21 00:15:30 +00:00
}