big refactoring

This commit is contained in:
Dionisio
2026-02-03 17:59:04 +01:00
parent 52840e57df
commit 8f2b0a354c
46 changed files with 8505 additions and 1418 deletions
+93 -31
View File
@@ -2,7 +2,7 @@ use std::sync::Arc;
use axum::{
Router,
routing::{post, get, put},
extract::{State, Json, Extension},
extract::{State, Json},
http::{StatusCode, HeaderMap, header},
response::IntoResponse,
};
@@ -11,17 +11,25 @@ use crate::common::di::AppState;
use crate::application::dtos::user_dto::{
LoginDto, RegisterDto, UserDto, ChangePasswordDto, RefreshTokenDto, AuthResponseDto
};
use crate::interfaces::middleware::auth::CurrentUser;
use crate::interfaces::errors::AppError;
pub fn auth_routes() -> Router<Arc<AppState>> {
Router::new()
// Rutas que NO requieren autenticación
let public_routes = Router::new()
.route("/register", post(register))
.route("/login", post(login))
.route("/refresh", post(refresh_token))
.route("/status", get(get_system_status));
// Rutas que SÍ requieren autenticación - usamos route_layer para aplicar middleware
// El middleware usará el state que se pase con .with_state() desde main.rs
let protected_routes = Router::new()
.route("/me", get(get_current_user))
.route("/change-password", put(change_password))
.route("/logout", post(logout))
.route("/logout", post(logout));
// Combinar rutas públicas y protegidas
public_routes.merge(protected_routes)
}
async fn register(
@@ -265,69 +273,123 @@ async fn refresh_token(
async fn get_current_user(
State(state): State<Arc<AppState>>,
Extension(current_user): Extension<CurrentUser>,
headers: HeaderMap,
) -> Result<impl IntoResponse, AppError> {
// Normal process for all users
let auth_service = state.auth_service.as_ref()
.ok_or_else(|| AppError::internal_error("Servicio de autenticación no configurado"))?;
// Primero, intentamos actualizar las estadísticas de uso de almacenamiento
// Si existe el servicio de uso de almacenamiento
// Extraer y validar el token directamente
let token = headers
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.strip_prefix("Bearer "))
.ok_or_else(|| AppError::unauthorized("Token de autorización no encontrado"))?;
// Validar el token y obtener claims
let claims = auth_service.token_service.validate_token(token)
.map_err(|e| AppError::unauthorized(&format!("Token inválido: {}", e)))?;
let user_id = claims.sub;
// Primero, actualizar las estadísticas de uso de almacenamiento
// IMPORTANTE: Esperamos el cálculo para devolver datos actualizados
if let Some(storage_usage_service) = state.storage_usage_service.as_ref() {
// Actualizamos el uso de almacenamiento en segundo plano
// No bloqueamos la respuesta con esta actualización
let user_id = current_user.id.clone();
let storage_service = storage_usage_service.clone();
// Ejecutar asincronamente para no retrasar la respuesta
tokio::spawn(async move {
match storage_service.update_user_storage_usage(&user_id).await {
Ok(usage) => {
tracing::info!("Updated storage usage for user {}: {} bytes", user_id, usage);
},
Err(e) => {
tracing::warn!("Failed to update storage usage for user {}: {}", user_id, e);
}
// Calcular storage de forma síncrona (esperamos el resultado)
match storage_usage_service.update_user_storage_usage(&user_id).await {
Ok(usage) => {
tracing::info!("Updated storage usage for user {}: {} bytes", user_id, usage);
},
Err(e) => {
// Solo log de warning, no fallar la petición completa
tracing::warn!("Failed to update storage usage for user {}: {}", user_id, e);
}
});
}
}
// Obtener los datos del usuario (que puede tener valores de almacenamiento desactualizados)
let user = auth_service.auth_application_service.get_user_by_id(&current_user.id).await?;
// Ahora obtener los datos del usuario CON el almacenamiento actualizado
let user = auth_service.auth_application_service.get_user_by_id(&user_id).await?;
Ok((StatusCode::OK, Json(user)))
}
async fn change_password(
State(state): State<Arc<AppState>>,
Extension(current_user): Extension<CurrentUser>,
headers: HeaderMap,
Json(dto): Json<ChangePasswordDto>,
) -> Result<impl IntoResponse, AppError> {
let auth_service = state.auth_service.as_ref()
.ok_or_else(|| AppError::internal_error("Servicio de autenticación no configurado"))?;
auth_service.auth_application_service.change_password(&current_user.id, dto).await?;
// Extraer y validar el token directamente
let token = headers
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.strip_prefix("Bearer "))
.ok_or_else(|| AppError::unauthorized("Token de autorización no encontrado"))?;
// Validar el token y obtener claims
let claims = auth_service.token_service.validate_token(token)
.map_err(|e| AppError::unauthorized(&format!("Token inválido: {}", e)))?;
auth_service.auth_application_service.change_password(&claims.sub, dto).await?;
Ok(StatusCode::OK)
}
async fn logout(
State(state): State<Arc<AppState>>,
Extension(current_user): Extension<CurrentUser>,
headers: HeaderMap,
) -> Result<impl IntoResponse, AppError> {
let auth_service = state.auth_service.as_ref()
.ok_or_else(|| AppError::internal_error("Servicio de autenticación no configurado"))?;
// Extract refresh token from request
let refresh_token = headers
// Extraer y validar el token directamente
let token = headers
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.strip_prefix("Bearer "))
.ok_or_else(|| AppError::unauthorized("Token de refresco no encontrado"))?;
.ok_or_else(|| AppError::unauthorized("Token de autorización no encontrado"))?;
auth_service.auth_application_service.logout(&current_user.id, refresh_token).await?;
// Validar el token y obtener claims
let claims = auth_service.token_service.validate_token(token)
.map_err(|e| AppError::unauthorized(&format!("Token inválido: {}", e)))?;
// Use access token for logout (we don't have refresh token in headers)
auth_service.auth_application_service.logout(&claims.sub, token).await?;
Ok(StatusCode::OK)
}
/// Get system status - returns whether admin is configured
/// This is a public endpoint used to determine if setup is needed
#[derive(serde::Serialize)]
struct SystemStatus {
/// Whether the system has been set up with an admin
initialized: bool,
/// Number of admin users in the system
admin_count: i64,
/// Whether registration is allowed (only if admin exists)
registration_allowed: bool,
}
async fn get_system_status(
State(state): State<Arc<AppState>>,
) -> Result<impl IntoResponse, AppError> {
let auth_service = state.auth_service.as_ref()
.ok_or_else(|| AppError::internal_error("Servicio de autenticación no configurado"))?;
// Count admin users to determine if system is initialized
let admin_count = auth_service.auth_application_service.count_admin_users().await
.unwrap_or(0);
let status = SystemStatus {
initialized: admin_count > 0,
admin_count,
registration_allowed: admin_count > 0, // Only allow registration if admin exists
};
tracing::info!("System status check: initialized={}, admin_count={}", status.initialized, status.admin_count);
Ok((StatusCode::OK, Json(status)))
}
@@ -0,0 +1,307 @@
//! Chunked Upload Handler - TUS-like Protocol Endpoints
//!
//! Provides HTTP endpoints for resumable, parallel chunk uploads:
//! - POST /api/uploads → Create upload session
//! - PATCH /api/uploads/:id → Upload a chunk
//! - HEAD /api/uploads/:id → Get upload status
//! - POST /api/uploads/:id/complete → Assemble and finalize
//! - DELETE /api/uploads/:id → Cancel upload
use axum::{
extract::{Path, State, Query},
http::{StatusCode, header, HeaderMap},
response::{IntoResponse, Response},
Json,
};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use crate::common::di::AppState;
use crate::infrastructure::services::chunked_upload_service::DEFAULT_CHUNK_SIZE;
/// Request body for creating an upload session
#[derive(Debug, Deserialize)]
pub struct CreateUploadRequest {
pub filename: String,
pub folder_id: Option<String>,
pub content_type: Option<String>,
pub total_size: u64,
pub chunk_size: Option<usize>,
}
/// Query params for chunk upload
#[derive(Debug, Deserialize)]
pub struct ChunkUploadParams {
pub chunk_index: usize,
pub checksum: Option<String>,
}
/// Final response after completing upload
#[derive(Debug, Serialize)]
pub struct CompleteUploadResponse {
pub file_id: String,
pub filename: String,
pub size: u64,
pub path: String,
}
/// Chunked Upload Handler
pub struct ChunkedUploadHandler;
impl ChunkedUploadHandler {
/// POST /api/uploads - Create a new upload session
///
/// Request body:
/// ```json
/// {
/// "filename": "large-video.mp4",
/// "folder_id": "optional-folder-id",
/// "content_type": "video/mp4",
/// "total_size": 104857600,
/// "chunk_size": 5242880
/// }
/// ```
///
/// Response:
/// ```json
/// {
/// "upload_id": "uuid",
/// "chunk_size": 5242880,
/// "total_chunks": 20,
/// "expires_at": 86400
/// }
/// ```
pub async fn create_upload(
State(state): State<Arc<AppState>>,
Json(request): Json<CreateUploadRequest>,
) -> impl IntoResponse {
let chunked_service = &state.core.chunked_upload_service;
// Validate request
if request.filename.is_empty() {
return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
"error": "Filename is required"
}))).into_response();
}
if request.total_size == 0 {
return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
"error": "Total size must be greater than 0"
}))).into_response();
}
// Validate chunk size if provided
let chunk_size = request.chunk_size.unwrap_or(DEFAULT_CHUNK_SIZE);
if chunk_size < 1024 * 1024 {
return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
"error": "Chunk size must be at least 1MB"
}))).into_response();
}
let content_type = request.content_type
.unwrap_or_else(|| "application/octet-stream".to_string());
match chunked_service.create_session(
request.filename,
request.folder_id,
content_type,
request.total_size,
Some(chunk_size),
).await {
Ok(response) => {
(StatusCode::CREATED, Json(response)).into_response()
}
Err(e) => {
tracing::error!("Failed to create upload session: {}", e);
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
"error": e
}))).into_response()
}
}
}
/// PATCH /api/uploads/:upload_id - Upload a chunk
///
/// Query params:
/// - chunk_index: The index of the chunk (0-based)
/// - checksum: Optional MD5 checksum for verification
///
/// Body: Raw bytes of the chunk
pub async fn upload_chunk(
State(state): State<Arc<AppState>>,
Path(upload_id): Path<String>,
Query(params): Query<ChunkUploadParams>,
headers: HeaderMap,
body: Bytes,
) -> impl IntoResponse {
let chunked_service = &state.core.chunked_upload_service;
// Extract checksum from header or query param
let checksum = params.checksum.or_else(|| {
headers.get("Content-MD5")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string())
});
match chunked_service.upload_chunk(
&upload_id,
params.chunk_index,
body,
checksum,
).await {
Ok(response) => {
let mut resp = Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.header("Upload-Offset", response.bytes_received.to_string())
.header("Upload-Progress", format!("{:.2}", response.progress * 100.0));
if response.is_complete {
resp = resp.header("Upload-Complete", "true");
}
resp.body(axum::body::Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
.into_response()
}
Err(e) => {
let status = if e.contains("not found") {
StatusCode::NOT_FOUND
} else if e.contains("Invalid") || e.contains("already uploaded") {
StatusCode::BAD_REQUEST
} else if e.contains("Checksum") {
StatusCode::CONFLICT
} else {
StatusCode::INTERNAL_SERVER_ERROR
};
(status, Json(serde_json::json!({
"error": e
}))).into_response()
}
}
}
/// HEAD /api/uploads/:upload_id - Get upload status
///
/// Returns upload progress and pending chunks
pub async fn get_upload_status(
State(state): State<Arc<AppState>>,
Path(upload_id): Path<String>,
) -> impl IntoResponse {
let chunked_service = &state.core.chunked_upload_service;
match chunked_service.get_status(&upload_id).await {
Ok(status) => {
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.header("Upload-Offset", status.bytes_received.to_string())
.header("Upload-Length", status.total_size.to_string())
.header("Upload-Progress", format!("{:.2}", status.progress * 100.0))
.header("Upload-Chunks-Total", status.total_chunks.to_string())
.header("Upload-Chunks-Complete", status.completed_chunks.to_string())
.body(axum::body::Body::from(serde_json::to_string(&status).unwrap()))
.unwrap()
.into_response()
}
Err(e) => {
(StatusCode::NOT_FOUND, Json(serde_json::json!({
"error": e
}))).into_response()
}
}
}
/// POST /api/uploads/:upload_id/complete - Finalize upload
///
/// Assembles all chunks into the final file and creates the file record
pub async fn complete_upload(
State(state): State<Arc<AppState>>,
Path(upload_id): Path<String>,
) -> impl IntoResponse {
let chunked_service = &state.core.chunked_upload_service;
let file_service = &state.applications.file_service_concrete;
// Assemble chunks
let (assembled_path, filename, folder_id, content_type, total_size) =
match chunked_service.complete_upload(&upload_id).await {
Ok(result) => result,
Err(e) => {
let status = if e.contains("not found") {
StatusCode::NOT_FOUND
} else if e.contains("not complete") {
StatusCode::CONFLICT
} else {
StatusCode::INTERNAL_SERVER_ERROR
};
return (status, Json(serde_json::json!({
"error": e
}))).into_response();
}
};
// Read assembled file and create final file record
let file_data = match tokio::fs::read(&assembled_path).await {
Ok(data) => data,
Err(e) => {
tracing::error!("Failed to read assembled file: {}", e);
return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
"error": format!("Failed to read assembled file: {}", e)
}))).into_response();
}
};
// Upload via normal service (this handles path resolution, metadata, etc.)
match file_service.upload_file_from_bytes(
filename.clone(),
folder_id.clone(),
content_type,
file_data,
).await {
Ok(file) => {
// Cleanup session
let _ = chunked_service.finalize_upload(&upload_id).await;
tracing::info!(
"✅ CHUNKED UPLOAD COMPLETE: {} (ID: {}, {} bytes)",
filename, file.id, total_size
);
(StatusCode::CREATED, Json(CompleteUploadResponse {
file_id: file.id,
filename: file.name,
size: total_size,
path: file.path,
})).into_response()
}
Err(e) => {
tracing::error!("Failed to create file from assembled upload: {:?}", e);
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
"error": format!("Failed to create file: {:?}", e)
}))).into_response()
}
}
}
/// DELETE /api/uploads/:upload_id - Cancel upload
///
/// Cancels an in-progress upload and cleans up temp files
pub async fn cancel_upload(
State(state): State<Arc<AppState>>,
Path(upload_id): Path<String>,
) -> impl IntoResponse {
let chunked_service = &state.core.chunked_upload_service;
match chunked_service.cancel_upload(&upload_id).await {
Ok(_) => StatusCode::NO_CONTENT.into_response(),
Err(e) => {
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
"error": e
}))).into_response()
}
}
}
}
@@ -0,0 +1,428 @@
use axum::{
extract::{Path, State, Multipart},
http::{StatusCode, header, Response},
response::IntoResponse,
body::Body,
};
use bytes::Bytes;
use serde::Serialize;
use crate::common::di::AppState;
use crate::infrastructure::services::dedup_service::DedupResult;
/// Global application state for dependency injection
type GlobalState = AppState;
/// Response for hash check endpoint
#[derive(Debug, Serialize)]
pub struct HashCheckResponse {
/// Whether a blob with this hash already exists
pub exists: bool,
/// The SHA-256 hash that was checked
pub hash: String,
/// If exists, the size of the existing blob
#[serde(skip_serializing_if = "Option::is_none")]
pub existing_size: Option<u64>,
/// If exists, the number of references to this blob
#[serde(skip_serializing_if = "Option::is_none")]
pub ref_count: Option<u32>,
}
/// Response for upload with dedup endpoint
#[derive(Debug, Serialize)]
pub struct DedupUploadResponse {
/// Whether this was a new file or an existing one
pub is_new: bool,
/// The SHA-256 hash of the content
pub hash: String,
/// The size of the content in bytes
pub size: u64,
/// Bytes saved by deduplication (0 if new file)
pub bytes_saved: u64,
/// Current reference count for this blob
pub ref_count: u32,
}
/// Response for dedup stats endpoint
#[derive(Debug, Serialize)]
pub struct StatsResponse {
/// Total number of unique blobs stored
pub unique_blobs: u64,
/// Total number of references (files pointing to blobs)
pub total_references: u64,
/// Total bytes saved by deduplication
pub bytes_saved: u64,
/// Total logical bytes (what users think they have)
pub total_logical_bytes: u64,
/// Total physical bytes (actual disk usage)
pub total_physical_bytes: u64,
/// Deduplication ratio (logical / physical)
pub dedup_ratio: f64,
/// Percentage of storage saved
pub savings_percentage: f64,
}
/// Handler for deduplication-related endpoints
///
/// Provides endpoints for:
/// - Checking if content already exists (by hash)
/// - Uploading files with automatic deduplication
/// - Getting deduplication statistics
pub struct DedupHandler;
impl DedupHandler {
/// Check if a blob with the given hash already exists
///
/// This endpoint allows clients to check if uploading a file is necessary
/// by pre-computing the hash client-side and checking against the server.
///
/// GET /api/dedup/check/{hash}
pub async fn check_hash(
State(state): State<GlobalState>,
Path(hash): Path<String>,
) -> impl IntoResponse {
let dedup = &state.core.dedup_service;
// Validate hash format (SHA-256 = 64 hex chars)
if hash.len() != 64 || !hash.chars().all(|c| c.is_ascii_hexdigit()) {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(r#"{"error": "Invalid hash format. Expected SHA-256 (64 hex characters)"}"#))
.unwrap()
.into_response();
}
match dedup.get_blob_metadata(&hash).await {
Some(metadata) => {
let response = HashCheckResponse {
exists: true,
hash,
existing_size: Some(metadata.size),
ref_count: Some(metadata.ref_count),
};
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
.into_response()
}
None => {
let response = HashCheckResponse {
exists: false,
hash,
existing_size: None,
ref_count: None,
};
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
.into_response()
}
}
}
/// Upload content with automatic deduplication
///
/// This endpoint calculates the SHA-256 hash of the uploaded content
/// and either creates a new blob or increments the reference count
/// of an existing blob.
///
/// POST /api/dedup/upload
///
/// Returns information about whether the content was new or deduplicated.
pub async fn upload_with_dedup(
State(state): State<GlobalState>,
mut multipart: Multipart,
) -> impl IntoResponse {
let dedup = &state.core.dedup_service;
// Process multipart form
while let Some(field) = multipart.next_field().await.unwrap_or(None) {
let name = field.name().unwrap_or("").to_string();
if name == "file" {
let content_type = field.content_type()
.unwrap_or("application/octet-stream")
.to_string();
// Collect all chunks
let mut chunks: Vec<Bytes> = Vec::new();
let mut total_size: usize = 0;
let mut field = field;
while let Ok(Some(chunk)) = field.chunk().await {
total_size += chunk.len();
chunks.push(chunk);
}
if chunks.is_empty() {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(r#"{"error": "Empty file not allowed"}"#))
.unwrap()
.into_response();
}
// Combine chunks
let data: Vec<u8> = if chunks.len() == 1 {
chunks.into_iter().next().unwrap().to_vec()
} else {
let mut combined = Vec::with_capacity(total_size);
for chunk in chunks {
combined.extend_from_slice(&chunk);
}
combined
};
// Store with deduplication
match dedup.store_bytes(&data, Some(content_type)).await {
Ok(result) => {
let (is_new, bytes_saved) = match &result {
DedupResult::NewBlob { .. } => (true, 0),
DedupResult::ExistingBlob { saved_bytes, .. } => (false, *saved_bytes),
};
let metadata = dedup.get_blob_metadata(result.hash()).await;
let response = DedupUploadResponse {
is_new,
hash: result.hash().to_string(),
size: result.size(),
bytes_saved,
ref_count: metadata.map(|m| m.ref_count).unwrap_or(1),
};
tracing::info!(
"🔗 Dedup upload: hash={}, new={}, saved={}",
result.hash(),
is_new,
bytes_saved
);
return Response::builder()
.status(if is_new { StatusCode::CREATED } else { StatusCode::OK })
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
.into_response();
}
Err(e) => {
tracing::error!("❌ Dedup upload failed: {}", e);
return Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(format!(r#"{{"error": "Upload failed: {}"}}"#, e)))
.unwrap()
.into_response();
}
}
}
}
Response::builder()
.status(StatusCode::BAD_REQUEST)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(r#"{"error": "No file field found in multipart form"}"#))
.unwrap()
.into_response()
}
/// Get deduplication statistics
///
/// GET /api/dedup/stats
///
/// Returns comprehensive statistics about the deduplication system including:
/// - Number of unique blobs
/// - Total references
/// - Bytes saved
/// - Deduplication ratio
pub async fn get_stats(
State(state): State<GlobalState>,
) -> impl IntoResponse {
let dedup = &state.core.dedup_service;
let stats = dedup.get_stats().await;
// Calculate savings percentage
let savings_pct = if stats.total_bytes_referenced > 0 {
(stats.bytes_saved as f64 / stats.total_bytes_referenced as f64) * 100.0
} else {
0.0
};
let response = StatsResponse {
unique_blobs: stats.total_blobs,
total_references: stats.dedup_hits + stats.total_blobs, // Approximation
bytes_saved: stats.bytes_saved,
total_logical_bytes: stats.total_bytes_referenced,
total_physical_bytes: stats.total_bytes_stored,
dedup_ratio: stats.dedup_ratio,
savings_percentage: savings_pct,
};
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
.into_response()
}
/// Retrieve content by hash
///
/// GET /api/dedup/blob/{hash}
///
/// Returns the raw content of a blob identified by its SHA-256 hash.
/// Useful for retrieving deduplicated content.
pub async fn get_blob(
State(state): State<GlobalState>,
Path(hash): Path<String>,
) -> impl IntoResponse {
let dedup = &state.core.dedup_service;
// Validate hash format
if hash.len() != 64 || !hash.chars().all(|c| c.is_ascii_hexdigit()) {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(r#"{"error": "Invalid hash format"}"#))
.unwrap()
.into_response();
}
// Get metadata first for content-type
let metadata = dedup.get_blob_metadata(&hash).await;
let content_type = metadata
.as_ref()
.and_then(|m| m.content_type.clone())
.unwrap_or_else(|| "application/octet-stream".to_string());
match dedup.read_blob_bytes(&hash).await {
Ok(content) => {
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, content_type)
.header(header::CONTENT_LENGTH, content.len().to_string())
.header("X-Dedup-Hash", &hash)
.body(Body::from(content))
.unwrap()
.into_response()
}
Err(_) => {
Response::builder()
.status(StatusCode::NOT_FOUND)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(r#"{"error": "Blob not found"}"#))
.unwrap()
.into_response()
}
}
}
/// Remove a reference to a blob
///
/// DELETE /api/dedup/blob/{hash}
///
/// Decrements the reference count for a blob. If the reference count
/// reaches zero, the blob is deleted from storage.
pub async fn remove_reference(
State(state): State<GlobalState>,
Path(hash): Path<String>,
) -> impl IntoResponse {
let dedup = &state.core.dedup_service;
// Validate hash format
if hash.len() != 64 || !hash.chars().all(|c| c.is_ascii_hexdigit()) {
return Response::builder()
.status(StatusCode::BAD_REQUEST)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(r#"{"error": "Invalid hash format"}"#))
.unwrap()
.into_response();
}
match dedup.remove_reference(&hash).await {
Ok(deleted) => {
let message = if deleted {
format!(r#"{{"success": true, "deleted": true, "message": "Blob {} was deleted (ref_count reached 0)"}}"#, hash)
} else {
format!(r#"{{"success": true, "deleted": false, "message": "Reference removed from blob {}"}}"#, hash)
};
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(message))
.unwrap()
.into_response()
}
Err(e) => {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(format!(r#"{{"error": "{}"}}"#, e)))
.unwrap()
.into_response()
}
}
}
/// Force recalculation of statistics from disk
///
/// POST /api/dedup/recalculate
///
/// Verifies integrity and returns current statistics.
/// Useful for health checks and auditing.
pub async fn recalculate_stats(
State(state): State<GlobalState>,
) -> impl IntoResponse {
let dedup = &state.core.dedup_service;
// Verify integrity first
match dedup.verify_integrity().await {
Ok(issues) => {
if !issues.is_empty() {
tracing::warn!("Dedup integrity issues found: {:?}", issues);
}
}
Err(e) => {
return Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(format!(r#"{{"error": "Verification failed: {}"}}"#, e)))
.unwrap()
.into_response();
}
}
let stats = dedup.get_stats().await;
// Calculate savings percentage
let savings_pct = if stats.total_bytes_referenced > 0 {
(stats.bytes_saved as f64 / stats.total_bytes_referenced as f64) * 100.0
} else {
0.0
};
let response = StatsResponse {
unique_blobs: stats.total_blobs,
total_references: stats.dedup_hits + stats.total_blobs,
bytes_saved: stats.bytes_saved,
total_logical_bytes: stats.total_bytes_referenced,
total_physical_bytes: stats.total_bytes_stored,
dedup_ratio: stats.dedup_ratio,
savings_percentage: savings_pct,
};
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
.into_response()
}
}
File diff suppressed because it is too large Load Diff
+2
View File
@@ -10,6 +10,8 @@ pub mod favorites_handler;
pub mod recent_handler;
pub mod webdav_handler;
pub mod caldav_handler;
pub mod chunked_upload_handler;
pub mod dedup_handler;
/// Tipo de resultado para controladores de API
pub type ApiResult<T> = Result<T, (axum::http::StatusCode, String)>;
+116 -4
View File
@@ -31,6 +31,7 @@ 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::batch_handler::{
self, BatchHandlerState
@@ -83,13 +84,54 @@ pub fn create_api_routes(
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 {
@@ -239,13 +281,13 @@ pub fn create_api_routes(
// Create file routes for basic operations and trash-enabled delete
let basic_file_router = Router::new()
.route("/", get(|
State(service): State<Arc<FileService>>,
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);
// Pass the service directly to the handler
let service = &state.applications.file_service_concrete;
match service.list_files(folder_id).await {
Ok(files) => {
tracing::info!("Found {} files", files.len());
@@ -259,9 +301,58 @@ pub fn create_api_routes(
}
}
}))
.route("/upload", post(FileHandler::upload_file))
.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("/{id}", get(FileHandler::download_file))
.with_state(file_service.clone());
.route("/{id}/thumbnail/{size}", get(FileHandler::get_thumbnail))
.with_state(app_state.clone());
// Let's create a router for file operations with trash support
let file_operations_router = Router::new()
@@ -379,10 +470,31 @@ pub fn create_api_routes(
} else {
Router::new()
};
// Create routes for chunked uploads (large files >10MB)
let chunked_upload_router = Router::new()
.route("/", post(ChunkedUploadHandler::create_upload))
.route("/{upload_id}", axum::routing::patch(ChunkedUploadHandler::upload_chunk))
.route("/{upload_id}", axum::routing::head(ChunkedUploadHandler::get_upload_status))
.route("/{upload_id}/complete", post(ChunkedUploadHandler::complete_upload))
.route("/{upload_id}", delete(ChunkedUploadHandler::cancel_upload))
.with_state(Arc::new(app_state.clone()));
// Create routes for deduplication endpoints
let dedup_router = Router::new()
.route("/check/{hash}", get(super::handlers::dedup_handler::DedupHandler::check_hash))
.route("/upload", post(super::handlers::dedup_handler::DedupHandler::upload_with_dedup))
.route("/stats", get(super::handlers::dedup_handler::DedupHandler::get_stats))
.route("/blob/{hash}", get(super::handlers::dedup_handler::DedupHandler::get_blob))
.route("/blob/{hash}", delete(super::handlers::dedup_handler::DedupHandler::remove_reference))
.route("/recalculate", post(super::handlers::dedup_handler::DedupHandler::recalculate_stats))
.with_state(app_state.clone());
let mut router = Router::new()
.nest("/folders", folders_router)
.nest("/files", files_router)
.nest("/uploads", chunked_upload_router)
.nest("/dedup", dedup_router)
.nest("/batch", batch_router)
.nest("/search", search_router)
.nest("/shares", share_router)