Files
Oxicloud/src/interfaces/api/handlers/search_handler.rs
T

239 lines
7.7 KiB
Rust
Raw Normal View History

2025-03-27 01:13:34 +01:00
use axum::{
2026-02-14 01:29:34 +01:00
extract::{Json, Query, State},
2025-03-27 01:13:34 +01:00
http::StatusCode,
2026-02-14 01:29:34 +01:00
response::IntoResponse,
2025-03-27 01:13:34 +01:00
};
use serde_json::json;
2026-02-14 01:29:34 +01:00
use tracing::{error, info};
2025-03-27 01:13:34 +01:00
use crate::application::dtos::search_dto::SearchCriteriaDto;
use crate::common::di::AppState;
/**
* Handler for search operations through the API.
2026-02-14 01:29:34 +01:00
*
* This handler exposes endpoints related to search functionality,
* allowing users to search for files and folders using various criteria.
2025-03-27 01:13:34 +01:00
*/
pub struct SearchHandler;
impl SearchHandler {
/**
* Performs a search based on the criteria provided as query parameters.
2026-02-14 01:29:34 +01:00
*
* This endpoint allows simple searches directly with URL parameters.
2026-02-14 01:29:34 +01:00
*
* @param state Application state with services
* @param query_params Search parameters as query string
* @return HTTP response with the search results
2025-03-27 01:13:34 +01:00
*/
pub async fn search_files_get(
State(state): State<AppState>,
Query(params): Query<SearchParams>,
) -> impl IntoResponse {
info!("API: File search with parameters: {:?}", params);
2026-02-14 01:29:34 +01:00
// Extract the search service or return error if not available
2025-03-27 01:13:34 +01:00
let search_service = match &state.applications.search_service {
Some(service) => service,
None => {
error!("Search service not available");
2025-03-27 01:13:34 +01:00
return (
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({
"error": "Search service is not available"
2026-02-14 01:29:34 +01:00
})),
)
.into_response();
2025-03-27 01:13:34 +01:00
}
};
2026-02-14 01:29:34 +01:00
// Convert search parameters to DTO
2025-03-27 01:13:34 +01:00
let search_criteria = SearchCriteriaDto {
name_contains: params.query,
2026-02-14 01:29:34 +01:00
file_types: params
.type_filter
.map(|t| t.split(',').map(|s| s.trim().to_string()).collect()),
2025-03-27 01:13:34 +01:00
created_after: params.created_after,
created_before: params.created_before,
modified_after: params.modified_after,
modified_before: params.modified_before,
min_size: params.min_size,
max_size: params.max_size,
folder_id: params.folder_id,
recursive: params.recursive.unwrap_or(true),
limit: params.limit.unwrap_or(100),
offset: params.offset.unwrap_or(0),
};
2026-02-14 01:29:34 +01:00
// Perform the search
2025-03-27 01:13:34 +01:00
match search_service.search(search_criteria).await {
Ok(results) => {
2026-02-14 01:29:34 +01:00
info!(
"Search completed, {} files and {} folders found",
results.files.len(),
results.folders.len()
);
2025-03-27 01:13:34 +01:00
(StatusCode::OK, Json(results)).into_response()
2026-02-14 01:29:34 +01:00
}
2025-03-27 01:13:34 +01:00
Err(err) => {
error!("Search error: {}", err);
2025-03-27 01:13:34 +01:00
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({
"error": format!("Search error: {}", err)
2026-02-14 01:29:34 +01:00
})),
)
.into_response()
2025-03-27 01:13:34 +01:00
}
}
}
2026-02-14 01:29:34 +01:00
2025-03-27 01:13:34 +01:00
/**
* Performs an advanced search based on a complete JSON criteria object.
2026-02-14 01:29:34 +01:00
*
* This endpoint allows more complex searches with all possible criteria
* provided in the request body.
2026-02-14 01:29:34 +01:00
*
* @param state Application state with services
* @param criteria Complete search criteria
* @return HTTP response with the search results
2025-03-27 01:13:34 +01:00
*/
pub async fn search_files_post(
State(state): State<AppState>,
Json(criteria): Json<SearchCriteriaDto>,
) -> impl IntoResponse {
info!("API: Advanced file search");
2026-02-14 01:29:34 +01:00
// Extract the search service or return error if not available
2025-03-27 01:13:34 +01:00
let search_service = match &state.applications.search_service {
Some(service) => service,
None => {
error!("Search service not available");
2025-03-27 01:13:34 +01:00
return (
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({
"error": "Search service is not available"
2026-02-14 01:29:34 +01:00
})),
)
.into_response();
2025-03-27 01:13:34 +01:00
}
};
2026-02-14 01:29:34 +01:00
// Perform the search
2025-03-27 01:13:34 +01:00
match search_service.search(criteria).await {
Ok(results) => {
2026-02-14 01:29:34 +01:00
info!(
"Search completed, {} files and {} folders found",
results.files.len(),
results.folders.len()
);
2025-03-27 01:13:34 +01:00
(StatusCode::OK, Json(results)).into_response()
2026-02-14 01:29:34 +01:00
}
2025-03-27 01:13:34 +01:00
Err(err) => {
error!("Search error: {}", err);
2025-03-27 01:13:34 +01:00
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({
"error": format!("Search error: {}", err)
2026-02-14 01:29:34 +01:00
})),
)
.into_response()
2025-03-27 01:13:34 +01:00
}
}
}
2026-02-14 01:29:34 +01:00
2025-03-27 01:13:34 +01:00
/**
* Clears the search results cache.
2026-02-14 01:29:34 +01:00
*
* This endpoint is useful for forcing fresh searches after significant
* changes in the file system.
2026-02-14 01:29:34 +01:00
*
* @param state Application state with services
* @return HTTP response indicating success or error
2025-03-27 01:13:34 +01:00
*/
2026-02-14 01:29:34 +01:00
pub async fn clear_search_cache(State(state): State<AppState>) -> impl IntoResponse {
info!("API: Clearing search cache");
2026-02-14 01:29:34 +01:00
// Extract the search service or return error if not available
2025-03-27 01:13:34 +01:00
let search_service = match &state.applications.search_service {
Some(service) => service,
None => {
error!("Search service not available");
2025-03-27 01:13:34 +01:00
return (
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({
"error": "Search service is not available"
2026-02-14 01:29:34 +01:00
})),
)
.into_response();
2025-03-27 01:13:34 +01:00
}
};
2026-02-14 01:29:34 +01:00
// Clear the cache
2025-03-27 01:13:34 +01:00
match search_service.clear_search_cache().await {
Ok(_) => {
info!("Search cache cleared successfully");
2025-03-27 01:13:34 +01:00
(
StatusCode::OK,
Json(json!({
"message": "Search cache cleared successfully"
2026-02-14 01:29:34 +01:00
})),
)
.into_response()
}
2025-03-27 01:13:34 +01:00
Err(err) => {
error!("Error clearing search cache: {}", err);
2025-03-27 01:13:34 +01:00
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({
"error": format!("Error clearing search cache: {}", err)
2026-02-14 01:29:34 +01:00
})),
)
.into_response()
2025-03-27 01:13:34 +01:00
}
}
}
}
/// Search parameters for the GET endpoint
2025-03-27 01:13:34 +01:00
#[derive(Debug, serde::Deserialize)]
pub struct SearchParams {
/// Text to search for in file and folder names
2025-03-27 01:13:34 +01:00
pub query: Option<String>,
2026-02-14 01:29:34 +01:00
/// Filter by file types (comma-separated extensions)
2025-03-27 01:13:34 +01:00
#[serde(rename = "type")]
pub type_filter: Option<String>,
2026-02-14 01:29:34 +01:00
/// Filter items created after this date (timestamp)
2025-03-27 01:13:34 +01:00
pub created_after: Option<u64>,
2026-02-14 01:29:34 +01:00
/// Filter items created before this date (timestamp)
2025-03-27 01:13:34 +01:00
pub created_before: Option<u64>,
2026-02-14 01:29:34 +01:00
/// Filter items modified after this date (timestamp)
2025-03-27 01:13:34 +01:00
pub modified_after: Option<u64>,
2026-02-14 01:29:34 +01:00
/// Filter items modified before this date (timestamp)
2025-03-27 01:13:34 +01:00
pub modified_before: Option<u64>,
2026-02-14 01:29:34 +01:00
/// Minimum size in bytes
2025-03-27 01:13:34 +01:00
pub min_size: Option<u64>,
2026-02-14 01:29:34 +01:00
/// Maximum size in bytes
2025-03-27 01:13:34 +01:00
pub max_size: Option<u64>,
2026-02-14 01:29:34 +01:00
/// Folder ID to limit the search scope
2025-03-27 01:13:34 +01:00
pub folder_id: Option<String>,
2026-02-14 01:29:34 +01:00
/// Recursive search in subfolders
2025-03-27 01:13:34 +01:00
pub recursive: Option<bool>,
2026-02-14 01:29:34 +01:00
/// Result limit for pagination
2025-03-27 01:13:34 +01:00
pub limit: Option<usize>,
2026-02-14 01:29:34 +01:00
/// Offset for pagination
2025-03-27 01:13:34 +01:00
pub offset: Option<usize>,
2026-02-14 01:29:34 +01:00
}