refactoring hexagonal and clean architecture
This commit is contained in:
+83
-605
@@ -1,192 +1,85 @@
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use axum::{
|
||||
routing::{get, post, put, delete},
|
||||
Router,
|
||||
extract::{State, Query, Path},
|
||||
http::StatusCode,
|
||||
Json,
|
||||
response::IntoResponse,
|
||||
};
|
||||
use tower_http::{
|
||||
compression::CompressionLayer,
|
||||
trace::TraceLayer,
|
||||
};
|
||||
use serde_json::json;
|
||||
use crate::common::config::AppConfig;
|
||||
use crate::common::di::AppState;
|
||||
use crate::interfaces::middleware::auth::CurrentUserId;
|
||||
|
||||
use crate::interfaces::middleware::cache::{HttpCache, start_cache_cleanup_task};
|
||||
|
||||
use crate::application::services::folder_service::FolderService;
|
||||
use crate::application::services::file_service::FileService;
|
||||
use crate::application::services::i18n_application_service::I18nApplicationService;
|
||||
use crate::application::services::batch_operations::BatchOperationService;
|
||||
use crate::application::ports::trash_ports::TrashUseCase;
|
||||
use crate::application::ports::inbound::SearchUseCase;
|
||||
use crate::application::ports::share_ports::ShareUseCase;
|
||||
use crate::application::ports::favorites_ports::FavoritesUseCase;
|
||||
use crate::application::ports::recent_ports::RecentItemsUseCase;
|
||||
|
||||
use crate::interfaces::api::handlers::folder_handler::FolderHandler;
|
||||
use crate::interfaces::api::handlers::file_handler::FileHandler;
|
||||
use crate::interfaces::api::handlers::i18n_handler::I18nHandler;
|
||||
use crate::interfaces::api::handlers::chunked_upload_handler::ChunkedUploadHandler;
|
||||
// Eliminamos la importación de ShareHandler ya que ahora usamos directamente el servicio
|
||||
use crate::interfaces::api::handlers::trash_handler;
|
||||
use crate::interfaces::api::handlers::batch_handler::{
|
||||
self, BatchHandlerState
|
||||
};
|
||||
use crate::application::dtos::pagination::PaginationRequestDto;
|
||||
|
||||
/// Creates API routes for the application
|
||||
pub fn create_api_routes(
|
||||
folder_service: Arc<FolderService>,
|
||||
file_service: Arc<FileService>,
|
||||
i18n_service: Option<Arc<I18nApplicationService>>,
|
||||
trash_service: Option<Arc<dyn TrashUseCase>>,
|
||||
search_service: Option<Arc<dyn SearchUseCase>>,
|
||||
share_service: Option<Arc<dyn ShareUseCase>>,
|
||||
favorites_service: Option<Arc<dyn FavoritesUseCase>>,
|
||||
recent_service: Option<Arc<dyn RecentItemsUseCase>>,
|
||||
) -> Router<crate::common::di::AppState> {
|
||||
// Create a simplified AppState for the trash view
|
||||
// Setup required components for repository construction
|
||||
let path_service = Arc::new(crate::infrastructure::services::path_service::PathService::new(std::path::PathBuf::from("./storage")));
|
||||
let storage_mediator = Arc::new(crate::application::services::storage_mediator::FileSystemStorageMediator::new_stub());
|
||||
let id_mapping_service = Arc::new(crate::infrastructure::services::id_mapping_service::IdMappingService::dummy());
|
||||
let path_resolver = Arc::new(crate::infrastructure::repositories::file_path_resolver::FilePathResolver::new(
|
||||
path_service.clone(),
|
||||
storage_mediator.clone(),
|
||||
id_mapping_service.clone()
|
||||
));
|
||||
let metadata_cache = Arc::new(crate::infrastructure::services::file_metadata_cache::FileMetadataCache::new(
|
||||
crate::common::config::AppConfig::default(),
|
||||
1000 // Default max entries
|
||||
));
|
||||
|
||||
// Create file and folder repositories
|
||||
let file_repository = Arc::new(crate::infrastructure::repositories::file_fs_repository::FileFsRepository::new(
|
||||
std::path::PathBuf::from("./storage"),
|
||||
storage_mediator.clone(),
|
||||
id_mapping_service.clone(),
|
||||
path_service.clone(),
|
||||
metadata_cache.clone(),
|
||||
));
|
||||
|
||||
let folder_repository = Arc::new(crate::infrastructure::repositories::folder_fs_repository::FolderFsRepository::new(
|
||||
std::path::PathBuf::from("./storage"),
|
||||
storage_mediator.clone(),
|
||||
id_mapping_service.clone(),
|
||||
path_service.clone(),
|
||||
));
|
||||
|
||||
// Create concrete id_mapping_service for optimizer
|
||||
let id_mapping_service_concrete = Arc::new(crate::infrastructure::services::id_mapping_service::IdMappingService::dummy());
|
||||
let id_mapping_optimizer = Arc::new(crate::infrastructure::services::id_mapping_optimizer::IdMappingOptimizer::new(id_mapping_service_concrete.clone()));
|
||||
|
||||
// Create dummy thumbnail service for routes
|
||||
let thumbnail_service = Arc::new(
|
||||
crate::infrastructure::services::thumbnail_service::ThumbnailService::new(
|
||||
&std::path::PathBuf::from("./storage"),
|
||||
100,
|
||||
10 * 1024 * 1024,
|
||||
)
|
||||
);
|
||||
|
||||
// Create dummy write-behind cache for routes
|
||||
let write_behind_cache = crate::infrastructure::services::write_behind_cache::WriteBehindCache::new();
|
||||
|
||||
// Create dummy chunked upload service for routes
|
||||
let chunked_upload_service = Arc::new(
|
||||
crate::infrastructure::services::chunked_upload_service::ChunkedUploadService::new(
|
||||
std::path::PathBuf::from("./storage/.uploads")
|
||||
)
|
||||
);
|
||||
|
||||
// Create dummy image transcode service for routes
|
||||
let image_transcode_service = Arc::new(
|
||||
crate::infrastructure::services::image_transcode_service::ImageTranscodeService::new(
|
||||
&std::path::PathBuf::from("./storage"),
|
||||
100,
|
||||
10 * 1024 * 1024,
|
||||
)
|
||||
);
|
||||
|
||||
// Create dummy dedup service for routes
|
||||
let dedup_service = Arc::new(
|
||||
crate::infrastructure::services::dedup_service::DedupService::new(
|
||||
&std::path::PathBuf::from("./storage")
|
||||
)
|
||||
);
|
||||
|
||||
let mut app_state = crate::common::di::AppState {
|
||||
core: crate::common::di::CoreServices {
|
||||
path_service: path_service.clone(),
|
||||
cache_manager: Arc::new(crate::infrastructure::services::cache_manager::StorageCacheManager::default()),
|
||||
file_content_cache: Arc::new(crate::infrastructure::services::file_content_cache::FileContentCache::default()),
|
||||
id_mapping_service: id_mapping_service.clone(),
|
||||
file_id_mapping_service: id_mapping_service_concrete.clone(),
|
||||
id_mapping_optimizer: id_mapping_optimizer.clone(),
|
||||
thumbnail_service: thumbnail_service.clone(),
|
||||
write_behind_cache: write_behind_cache.clone(),
|
||||
chunked_upload_service: chunked_upload_service.clone(),
|
||||
image_transcode_service: image_transcode_service.clone(),
|
||||
dedup_service: dedup_service.clone(),
|
||||
config: crate::common::config::AppConfig::default(),
|
||||
},
|
||||
repositories: crate::common::di::RepositoryServices {
|
||||
folder_repository: folder_repository.clone(),
|
||||
file_repository: file_repository.clone(),
|
||||
file_read_repository: Arc::new(crate::infrastructure::repositories::FileFsReadRepository::default_stub()),
|
||||
file_write_repository: Arc::new(crate::infrastructure::repositories::FileFsWriteRepository::default_stub()),
|
||||
i18n_repository: Arc::new(crate::infrastructure::services::file_system_i18n_service::FileSystemI18nService::dummy()),
|
||||
storage_mediator: storage_mediator.clone(),
|
||||
metadata_manager: Arc::new(crate::infrastructure::repositories::FileMetadataManager::default()),
|
||||
path_resolver: path_resolver.clone(),
|
||||
metadata_cache: metadata_cache.clone(),
|
||||
trash_repository: None, // This is OK to be None since we use the trash_service directly
|
||||
},
|
||||
storage_usage_service: None,
|
||||
applications: crate::common::di::ApplicationServices {
|
||||
folder_service_concrete: folder_service.clone(),
|
||||
file_service_concrete: file_service.clone(),
|
||||
folder_service: folder_service.clone(),
|
||||
file_service: file_service.clone(),
|
||||
file_upload_service: Arc::new(crate::application::services::file_upload_service::FileUploadService::new(
|
||||
Arc::new(crate::infrastructure::repositories::FileFsWriteRepository::default_stub())
|
||||
)),
|
||||
file_retrieval_service: Arc::new(crate::application::services::file_retrieval_service::FileRetrievalService::new(
|
||||
Arc::new(crate::infrastructure::repositories::FileFsReadRepository::default_stub())
|
||||
)),
|
||||
file_management_service: Arc::new(crate::application::services::file_management_service::FileManagementService::new(
|
||||
Arc::new(crate::infrastructure::repositories::FileFsWriteRepository::default_stub())
|
||||
)),
|
||||
file_use_case_factory: Arc::new(crate::application::services::file_use_case_factory::AppFileUseCaseFactory::new(
|
||||
Arc::new(crate::infrastructure::repositories::FileFsReadRepository::default_stub()),
|
||||
Arc::new(crate::infrastructure::repositories::FileFsWriteRepository::default_stub())
|
||||
)),
|
||||
i18n_service: i18n_service.clone().unwrap_or_else(||
|
||||
Arc::new(crate::application::services::i18n_application_service::I18nApplicationService::dummy())
|
||||
),
|
||||
trash_service: trash_service.clone(), // Include the trash service here too for consistency
|
||||
search_service: search_service.clone(), // Include the search service
|
||||
share_service: share_service.clone(), // Include the share service
|
||||
favorites_service: favorites_service.clone(), // Include the favorites service
|
||||
recent_service: recent_service.clone(), // Include the recent service
|
||||
},
|
||||
db_pool: None,
|
||||
auth_service: None,
|
||||
trash_service: trash_service.clone(), // This is the important part - include the trash service
|
||||
share_service: share_service.clone(), // Include the share service for routes
|
||||
favorites_service: favorites_service.clone(), // Include the favorites service for routes
|
||||
recent_service: recent_service.clone(), // Include the recent service for routes
|
||||
calendar_service: None, // Adding missing field
|
||||
contact_service: None // Adding missing field
|
||||
};
|
||||
/// Creates public API routes that should NOT require authentication.
|
||||
///
|
||||
/// Currently this includes:
|
||||
/// - `/s/{token}` — public access to shared items via share link
|
||||
/// - `/s/{token}/verify` — password verification for protected share links
|
||||
/// - `/i18n/*` — internationalization/translation endpoints
|
||||
pub fn create_public_api_routes(app_state: &AppState) -> Router<AppState> {
|
||||
let share_service = app_state.share_service.clone();
|
||||
let i18n_service = Some(app_state.applications.i18n_service.clone());
|
||||
|
||||
let mut router = Router::new();
|
||||
|
||||
// Public share access routes — no auth required
|
||||
if let Some(share_service) = share_service {
|
||||
use crate::interfaces::api::handlers::share_handler;
|
||||
|
||||
let public_share_router = Router::new()
|
||||
.route("/{token}", get(share_handler::access_shared_item))
|
||||
.route("/{token}/verify", post(share_handler::verify_shared_item_password))
|
||||
.with_state(share_service);
|
||||
|
||||
router = router.nest("/s", public_share_router);
|
||||
}
|
||||
|
||||
// i18n routes — no auth required (localization should be available before login)
|
||||
if let Some(i18n_service) = i18n_service {
|
||||
let i18n_router = Router::new()
|
||||
.route("/locales", get(I18nHandler::get_locales))
|
||||
.route("/translate", get(I18nHandler::translate))
|
||||
.route("/locales/{locale_code}", get(I18nHandler::get_translations_by_locale))
|
||||
.with_state(i18n_service);
|
||||
|
||||
router = router.nest("/i18n", i18n_router);
|
||||
}
|
||||
|
||||
router
|
||||
}
|
||||
|
||||
/// Creates protected API routes for the application.
|
||||
///
|
||||
/// These routes require authentication when auth is enabled.
|
||||
/// Receives the fully-assembled `AppState` and extracts all needed services
|
||||
/// from it, avoiding a long parameter list.
|
||||
pub fn create_api_routes(app_state: &AppState) -> Router<AppState> {
|
||||
// Extract services from the pre-built AppState
|
||||
let folder_service = app_state.applications.folder_service_concrete.clone();
|
||||
let file_retrieval_service = app_state.applications.file_retrieval_service.clone();
|
||||
let file_management_service = app_state.applications.file_management_service.clone();
|
||||
let trash_service = app_state.trash_service.clone();
|
||||
let search_service = app_state.applications.search_service.clone();
|
||||
let share_service = app_state.share_service.clone();
|
||||
let favorites_service = app_state.favorites_service.clone();
|
||||
let recent_service = app_state.recent_service.clone();
|
||||
|
||||
// Inicializar el servicio de operaciones por lotes
|
||||
let batch_service = Arc::new(BatchOperationService::default(
|
||||
file_service.clone(),
|
||||
file_retrieval_service.clone(),
|
||||
file_management_service.clone(),
|
||||
folder_service.clone()
|
||||
));
|
||||
|
||||
@@ -209,33 +102,11 @@ pub fn create_api_routes(
|
||||
// Create the basic folders router with service operations
|
||||
let folders_basic_router = Router::new()
|
||||
.route("/", post(FolderHandler::create_folder))
|
||||
.route("/", get(|State(service): State<Arc<FolderService>>| async move {
|
||||
// No parent ID means list root folders
|
||||
FolderHandler::list_folders(State(service), None).await
|
||||
}))
|
||||
.route("/paginated", get(|
|
||||
State(service): State<Arc<FolderService>>,
|
||||
pagination: Query<PaginationRequestDto>
|
||||
| async move {
|
||||
// Paginación para carpetas raíz (sin parent)
|
||||
FolderHandler::list_folders_paginated(State(service), pagination, None).await
|
||||
}))
|
||||
.route("/", get(FolderHandler::list_root_folders))
|
||||
.route("/paginated", get(FolderHandler::list_root_folders_paginated))
|
||||
.route("/{id}", get(FolderHandler::get_folder))
|
||||
.route("/{id}/contents", get(|
|
||||
State(service): State<Arc<FolderService>>,
|
||||
Path(id): Path<String>
|
||||
| async move {
|
||||
// Listar contenido de una carpeta por su ID
|
||||
FolderHandler::list_folders(State(service), Some(&id)).await
|
||||
}))
|
||||
.route("/{id}/contents/paginated", get(|
|
||||
State(service): State<Arc<FolderService>>,
|
||||
Path(id): Path<String>,
|
||||
pagination: Query<PaginationRequestDto>
|
||||
| async move {
|
||||
// Listar contenido paginado de una carpeta por su ID
|
||||
FolderHandler::list_folders_paginated(State(service), pagination, Some(&id)).await
|
||||
}))
|
||||
.route("/{id}/contents", get(FolderHandler::list_folder_contents))
|
||||
.route("/{id}/contents/paginated", get(FolderHandler::list_folder_contents_paginated))
|
||||
.route("/{id}/rename", put(FolderHandler::rename_folder))
|
||||
.route("/{id}/move", put(FolderHandler::move_folder))
|
||||
.with_state(folder_service.clone());
|
||||
@@ -245,144 +116,25 @@ pub fn create_api_routes(
|
||||
.route("/{id}/download", get(FolderHandler::download_folder_zip))
|
||||
.with_state(app_state.clone());
|
||||
|
||||
// Create folder operations that use trash separately
|
||||
// Create folder operations that use trash (requires full AppState)
|
||||
let folders_ops_router = Router::new()
|
||||
.route("/{id}", delete(|
|
||||
State(state): State<AppState>,
|
||||
CurrentUserId(user_id): CurrentUserId,
|
||||
Path(id): Path<String>
|
||||
| async move {
|
||||
// Try to use trash service if available
|
||||
if let Some(trash_service) = &state.trash_service {
|
||||
tracing::info!("Moving folder to trash: {}", id);
|
||||
|
||||
match trash_service.move_to_trash(&id, "folder", &user_id).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Folder successfully moved to trash: {}", id);
|
||||
return StatusCode::NO_CONTENT.into_response();
|
||||
},
|
||||
Err(err) => {
|
||||
tracing::warn!("Could not move folder to trash, falling back to permanent delete: {}", err);
|
||||
// Fall through to regular delete
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to permanent delete
|
||||
let folder_service = &state.applications.folder_service;
|
||||
match folder_service.delete_folder(&id).await {
|
||||
Ok(_) => StatusCode::NO_CONTENT.into_response(),
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response()
|
||||
}
|
||||
}));
|
||||
.route("/{id}", delete(FolderHandler::delete_folder_with_trash));
|
||||
|
||||
// Merge the routers
|
||||
let folders_router = folders_basic_router.merge(folders_ops_router).merge(folder_zip_router);
|
||||
|
||||
// Create file routes for basic operations and trash-enabled delete
|
||||
let basic_file_router = Router::new()
|
||||
.route("/", get(|
|
||||
State(state): State<AppState>,
|
||||
axum::extract::Query(params): axum::extract::Query<std::collections::HashMap<String, String>>,
|
||||
| async move {
|
||||
// Get folder_id from query parameter if present
|
||||
let folder_id = params.get("folder_id").map(|id| id.as_str());
|
||||
tracing::info!("API: Listando archivos con folder_id: {:?}", folder_id);
|
||||
let service = &state.applications.file_service_concrete;
|
||||
match service.list_files(folder_id).await {
|
||||
Ok(files) => {
|
||||
tracing::info!("Found {} files", files.len());
|
||||
(StatusCode::OK, Json(files)).into_response()
|
||||
},
|
||||
Err(err) => {
|
||||
tracing::error!("Error listing files: {}", err);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": format!("Error listing files: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
}))
|
||||
.route("/upload", post(|
|
||||
State(state): State<AppState>,
|
||||
multipart: axum::extract::Multipart,
|
||||
| async move {
|
||||
use crate::infrastructure::services::thumbnail_service::ThumbnailService;
|
||||
|
||||
// Use the new upload handler with write-behind cache
|
||||
let response = FileHandler::upload_file_with_cache(
|
||||
State(state.clone()),
|
||||
multipart
|
||||
).await;
|
||||
|
||||
// Try to extract file info for thumbnail generation
|
||||
if let Ok(body_bytes) = axum::body::to_bytes(response.into_response().into_body(), 10 * 1024).await {
|
||||
if let Ok(file_info) = serde_json::from_slice::<serde_json::Value>(&body_bytes) {
|
||||
if let (Some(file_id), Some(mime_type), Some(file_path_str)) = (
|
||||
file_info.get("id").and_then(|v| v.as_str()),
|
||||
file_info.get("mime_type").and_then(|v| v.as_str()),
|
||||
file_info.get("path").and_then(|v| v.as_str())
|
||||
) {
|
||||
// Generate thumbnails for images in background
|
||||
if ThumbnailService::is_supported_image(mime_type) {
|
||||
let file_id = file_id.to_string();
|
||||
let file_path_rel = file_path_str.to_string();
|
||||
let thumbnail_service = state.core.thumbnail_service.clone();
|
||||
let path_service = state.core.path_service.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let file_path = path_service.get_root_path().join(&file_path_rel);
|
||||
tracing::info!("🖼️ Generating thumbnails for: {}", file_id);
|
||||
thumbnail_service.generate_all_sizes_background(file_id, file_path);
|
||||
});
|
||||
}
|
||||
|
||||
// Return the response
|
||||
return axum::http::Response::builder()
|
||||
.status(axum::http::StatusCode::CREATED)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.header(axum::http::header::CACHE_CONTROL, "no-cache, no-store, must-revalidate")
|
||||
.body(axum::body::Body::from(body_bytes))
|
||||
.unwrap()
|
||||
.into_response();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for errors
|
||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, "Upload processing error").into_response()
|
||||
}))
|
||||
.route("/", get(FileHandler::list_files_query))
|
||||
.route("/upload", post(FileHandler::upload_file_with_thumbnails))
|
||||
.route("/{id}", get(FileHandler::download_file))
|
||||
.route("/{id}/thumbnail/{size}", get(FileHandler::get_thumbnail))
|
||||
.with_state(app_state.clone());
|
||||
|
||||
// Let's create a router for file operations with trash support
|
||||
// File operations with trash support
|
||||
let file_operations_router = Router::new()
|
||||
// CRITICAL FIX: Ensure file deletion route correctly calls FileHandler::delete_file
|
||||
// Uses the correct URL pattern
|
||||
.route("/{id}", delete(|
|
||||
State(state): State<AppState>,
|
||||
CurrentUserId(user_id): CurrentUserId,
|
||||
Path(id): Path<String>
|
||||
| async move {
|
||||
tracing::info!("File delete route called explicitly for ID: {}", id);
|
||||
FileHandler::delete_file(State(state), CurrentUserId(user_id), Path(id)).await
|
||||
}))
|
||||
.route("/{id}/move", put(|
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
Json(payload): Json<serde_json::Value>,
|
||||
| async move {
|
||||
// Simplified move implementation just to get it working
|
||||
let folder_id = payload.get("folder_id")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string());
|
||||
|
||||
let file_service = &state.applications.file_service;
|
||||
match file_service.move_file(&id, folder_id).await {
|
||||
Ok(file_dto) => (StatusCode::OK, Json(file_dto)).into_response(),
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response()
|
||||
}
|
||||
}));
|
||||
.route("/{id}", delete(FileHandler::delete_file))
|
||||
.route("/{id}/move", put(FileHandler::move_file_simple));
|
||||
|
||||
// Merge the routers
|
||||
let files_router = basic_file_router.merge(file_operations_router);
|
||||
@@ -418,7 +170,7 @@ pub fn create_api_routes(
|
||||
|
||||
// Implementaciones directas de handlers para compartir, sin depender de ShareHandler
|
||||
|
||||
// Create routes for shared resources if the service is available
|
||||
// Create routes for shared resources management (requires auth)
|
||||
let share_router = if let Some(share_service) = share_service.clone() {
|
||||
use crate::interfaces::api::handlers::share_handler;
|
||||
|
||||
@@ -432,18 +184,6 @@ pub fn create_api_routes(
|
||||
} else {
|
||||
Router::new()
|
||||
};
|
||||
|
||||
// Public route for accessing shared links
|
||||
let public_share_router = if let Some(share_service) = share_service.clone() {
|
||||
use crate::interfaces::api::handlers::share_handler;
|
||||
|
||||
Router::new()
|
||||
.route("/{token}", get(share_handler::access_shared_item))
|
||||
.route("/{token}/verify", post(share_handler::verify_shared_item_password))
|
||||
.with_state(share_service.clone())
|
||||
} else {
|
||||
Router::new()
|
||||
};
|
||||
|
||||
// Create a router without the i18n routes
|
||||
// Create routes for favorites if the service is available
|
||||
@@ -500,236 +240,21 @@ pub fn create_api_routes(
|
||||
.nest("/batch", batch_router)
|
||||
.nest("/search", search_router)
|
||||
.nest("/shares", share_router)
|
||||
.nest("/s", public_share_router)
|
||||
.nest("/favorites", favorites_router)
|
||||
.nest("/recent", recent_router)
|
||||
;
|
||||
|
||||
// Store the share service in app_state for future use
|
||||
if let Some(share_service) = share_service.clone() {
|
||||
app_state.share_service = Some(share_service);
|
||||
}
|
||||
|
||||
// Re-enable trash routes to make the trash view work
|
||||
if let Some(_trash_service_ref) = trash_service.clone() {
|
||||
tracing::info!("Setting up trash routes for trash view");
|
||||
|
||||
// Create a router for trash specific endpoints that handles the auth requirements
|
||||
// Implement all trash operations needed by the frontend
|
||||
let trash_router = Router::new()
|
||||
// Get all trash items
|
||||
.route("/", get(|
|
||||
State(state): State<AppState>,
|
||||
CurrentUserId(user_id): CurrentUserId,
|
||||
Query(params): Query<HashMap<String, String>>
|
||||
| async move {
|
||||
tracing::info!("Getting trash items");
|
||||
// Use a valid UUID for the default user or from query params
|
||||
let effective_user = params.get("userId")
|
||||
.cloned()
|
||||
.unwrap_or(user_id);
|
||||
|
||||
tracing::info!("Using user ID: {}", effective_user);
|
||||
// Get the trash service directly
|
||||
if let Some(trash_service) = &state.trash_service {
|
||||
// Get trash items for default user
|
||||
match trash_service.get_trash_items(&effective_user).await {
|
||||
Ok(items) => {
|
||||
tracing::info!("Found {} items in trash", items.len());
|
||||
let response_data = serde_json::json!(items);
|
||||
tracing::info!("Response data: {:?}", response_data);
|
||||
(StatusCode::OK, Json(response_data)).into_response()
|
||||
},
|
||||
Err(err) => {
|
||||
tracing::error!("Error getting trash items: {}", err);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(json!({
|
||||
"error": format!("Error getting trash items: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Trash service not available");
|
||||
(StatusCode::NOT_IMPLEMENTED, Json(json!({
|
||||
"error": "Trash feature is not enabled"
|
||||
}))).into_response()
|
||||
}
|
||||
}))
|
||||
// Move file to trash
|
||||
.route("/files/{id}", delete(|
|
||||
State(state): State<AppState>,
|
||||
CurrentUserId(user_id): CurrentUserId,
|
||||
Path(id): Path<String>
|
||||
| async move {
|
||||
tracing::info!("Moving file to trash: {}", id);
|
||||
|
||||
if let Some(trash_service) = &state.trash_service {
|
||||
match trash_service.move_to_trash(&id, "file", &user_id).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("File moved to trash successfully");
|
||||
(StatusCode::OK, Json(json!({
|
||||
"success": true,
|
||||
"message": "File moved to trash successfully"
|
||||
}))).into_response()
|
||||
},
|
||||
Err(err) => {
|
||||
tracing::error!("Error moving file to trash: {}", err);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(json!({
|
||||
"error": format!("Error moving file to trash: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Trash service not available");
|
||||
(StatusCode::NOT_IMPLEMENTED, Json(json!({
|
||||
"error": "Trash feature is not enabled"
|
||||
}))).into_response()
|
||||
}
|
||||
}))
|
||||
// Move folder to trash
|
||||
.route("/folders/{id}", delete(|
|
||||
State(state): State<AppState>,
|
||||
CurrentUserId(user_id): CurrentUserId,
|
||||
Path(id): Path<String>
|
||||
| async move {
|
||||
tracing::info!("Moving folder to trash: {}", id);
|
||||
|
||||
if let Some(trash_service) = &state.trash_service {
|
||||
match trash_service.move_to_trash(&id, "folder", &user_id).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Folder moved to trash successfully");
|
||||
(StatusCode::OK, Json(json!({
|
||||
"success": true,
|
||||
"message": "Folder moved to trash successfully"
|
||||
}))).into_response()
|
||||
},
|
||||
Err(err) => {
|
||||
tracing::error!("Error moving folder to trash: {}", err);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(json!({
|
||||
"error": format!("Error moving folder to trash: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Trash service not available");
|
||||
(StatusCode::NOT_IMPLEMENTED, Json(json!({
|
||||
"error": "Trash feature is not enabled"
|
||||
}))).into_response()
|
||||
}
|
||||
}))
|
||||
// Restore item from trash
|
||||
.route("/{id}/restore", post(|
|
||||
State(state): State<AppState>,
|
||||
CurrentUserId(user_id): CurrentUserId,
|
||||
Path(id): Path<String>
|
||||
| async move {
|
||||
tracing::info!("Restoring item from trash: {}", id);
|
||||
|
||||
if let Some(trash_service) = &state.trash_service {
|
||||
match trash_service.restore_item(&id, &user_id).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Item restored from trash successfully");
|
||||
(StatusCode::OK, Json(json!({
|
||||
"success": true,
|
||||
"message": "Item restored from trash successfully"
|
||||
}))).into_response()
|
||||
},
|
||||
Err(err) => {
|
||||
let err_str = format!("{}", err);
|
||||
// Check if the error is due to item not being found
|
||||
if err_str.contains("not found") || err_str.contains("NotFound") {
|
||||
tracing::warn!("Item not found in trash, but reporting success: {}", id);
|
||||
// Return success even if the item is not found
|
||||
return (StatusCode::OK, Json(json!({
|
||||
"success": true,
|
||||
"message": "Item restored (or was already removed from trash)"
|
||||
}))).into_response();
|
||||
}
|
||||
|
||||
tracing::error!("Error restoring item from trash: {}", err);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(json!({
|
||||
"error": format!("Error restoring item from trash: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Trash service not available");
|
||||
(StatusCode::NOT_IMPLEMENTED, Json(json!({
|
||||
"error": "Trash feature is not enabled"
|
||||
}))).into_response()
|
||||
}
|
||||
}))
|
||||
// Permanently delete an item from trash
|
||||
.route("/{id}", delete(|
|
||||
State(state): State<AppState>,
|
||||
CurrentUserId(user_id): CurrentUserId,
|
||||
Path(id): Path<String>
|
||||
| async move {
|
||||
tracing::info!("Permanently deleting item from trash: {}", id);
|
||||
|
||||
if let Some(trash_service) = &state.trash_service {
|
||||
match trash_service.delete_permanently(&id, &user_id).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Item permanently deleted successfully");
|
||||
(StatusCode::OK, Json(json!({
|
||||
"success": true,
|
||||
"message": "Item permanently deleted"
|
||||
}))).into_response()
|
||||
},
|
||||
Err(err) => {
|
||||
let err_str = format!("{}", err);
|
||||
// Check if the error is due to item not being found
|
||||
if err_str.contains("not found") || err_str.contains("NotFound") {
|
||||
tracing::warn!("Item not found in trash, but reporting success: {}", id);
|
||||
// Return success even if the item is not found
|
||||
return (StatusCode::OK, Json(json!({
|
||||
"success": true,
|
||||
"message": "Item deleted (or was already removed from trash)"
|
||||
}))).into_response();
|
||||
}
|
||||
|
||||
tracing::error!("Error permanently deleting item: {}", err);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(json!({
|
||||
"error": format!("Error permanently deleting item: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Trash service not available");
|
||||
(StatusCode::NOT_IMPLEMENTED, Json(json!({
|
||||
"error": "Trash feature is not enabled"
|
||||
}))).into_response()
|
||||
}
|
||||
}))
|
||||
// Empty trash
|
||||
.route("/empty", delete(|
|
||||
State(state): State<AppState>,
|
||||
CurrentUserId(user_id): CurrentUserId,
|
||||
| async move {
|
||||
tracing::info!("Emptying trash");
|
||||
|
||||
if let Some(trash_service) = &state.trash_service {
|
||||
match trash_service.empty_trash(&user_id).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Trash emptied successfully");
|
||||
(StatusCode::OK, Json(json!({
|
||||
"success": true,
|
||||
"message": "Trash emptied successfully"
|
||||
}))).into_response()
|
||||
},
|
||||
Err(err) => {
|
||||
tracing::error!("Error emptying trash: {}", err);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(json!({
|
||||
"error": format!("Error emptying trash: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Trash service not available");
|
||||
(StatusCode::NOT_IMPLEMENTED, Json(json!({
|
||||
"error": "Trash feature is not enabled"
|
||||
}))).into_response()
|
||||
}
|
||||
}))
|
||||
.route("/", get(trash_handler::get_trash_items))
|
||||
.route("/files/{id}", delete(trash_handler::move_file_to_trash))
|
||||
.route("/folders/{id}", delete(trash_handler::move_folder_to_trash))
|
||||
.route("/{id}/restore", post(trash_handler::restore_from_trash))
|
||||
.route("/{id}", delete(trash_handler::delete_permanently))
|
||||
.route("/empty", delete(trash_handler::empty_trash))
|
||||
.with_state(app_state.clone());
|
||||
|
||||
router = router.nest("/trash", trash_router);
|
||||
@@ -737,66 +262,19 @@ pub fn create_api_routes(
|
||||
tracing::warn!("Trash service not available - trash view will not work");
|
||||
}
|
||||
|
||||
// Add i18n routes if the service is provided
|
||||
if let Some(i18n_service) = i18n_service {
|
||||
let i18n_router = Router::new()
|
||||
.route("/locales", get(I18nHandler::get_locales))
|
||||
.route("/translate", get(I18nHandler::translate))
|
||||
.route("/locales/{locale_code}", get(|
|
||||
State(service): State<Arc<I18nApplicationService>>,
|
||||
axum::extract::Path(locale_code): axum::extract::Path<String>,
|
||||
| async move {
|
||||
I18nHandler::get_translations(State(service), locale_code).await
|
||||
}))
|
||||
.with_state(i18n_service);
|
||||
|
||||
router = router.nest("/i18n", i18n_router);
|
||||
}
|
||||
|
||||
// Get the app configuration
|
||||
let _config = AppConfig::from_env();
|
||||
|
||||
// For now, just use the router as is - we'll properly implement the auth middleware later
|
||||
// when all implementation details are fixed
|
||||
let router = router;
|
||||
|
||||
// Apply compression and tracing layers
|
||||
// Note: We've removed the direct trash endpoints due to handler type compatibility issues
|
||||
// These will need to be implemented directly in main.rs or by modifying the file/folder handlers
|
||||
if trash_service.is_some() {
|
||||
tracing::info!("Trash service is available - trash view is functional");
|
||||
}
|
||||
|
||||
// Add WebDAV routes if needed
|
||||
let webdav_enabled = true; // In production, you'd read this from a config
|
||||
let router = if webdav_enabled {
|
||||
// Add WebDAV routes
|
||||
{
|
||||
use crate::interfaces::api::handlers::webdav_handler;
|
||||
router.merge(webdav_handler::webdav_routes())
|
||||
} else {
|
||||
router
|
||||
};
|
||||
router = router.merge(webdav_handler::webdav_routes());
|
||||
}
|
||||
|
||||
// Add CalDAV routes if needed
|
||||
let caldav_enabled = true; // In production, you'd read this from a config
|
||||
let router = if caldav_enabled {
|
||||
// Add CalDAV routes
|
||||
{
|
||||
use crate::interfaces::api::handlers::caldav_handler;
|
||||
router.nest("/caldav", caldav_handler::caldav_routes())
|
||||
} else {
|
||||
router
|
||||
};
|
||||
|
||||
// Add CardDAV routes if needed
|
||||
let carddav_enabled = true; // In production, you'd read this from a config
|
||||
let router = if carddav_enabled {
|
||||
// Note: We'll implement carddav_handler in the next phase
|
||||
router
|
||||
} else {
|
||||
router
|
||||
};
|
||||
router = router.nest("/caldav", caldav_handler::caldav_routes());
|
||||
}
|
||||
|
||||
router
|
||||
.layer(CompressionLayer::new())
|
||||
.layer(TraceLayer::new_for_http())
|
||||
// HTTP caching is disabled temporarily due to compatibility issues
|
||||
// .layer(HttpCacheLayer::new(http_cache.clone()).with_max_age(folders_ttl))
|
||||
}
|
||||
Reference in New Issue
Block a user