refactoring hexagonal and clean architecture

This commit is contained in:
Diocrafts
2026-02-08 13:40:23 +01:00
parent 3e4fb67c19
commit a82faa5eaf
101 changed files with 7433 additions and 9721 deletions
@@ -0,0 +1,139 @@
use std::path::PathBuf;
use std::sync::Arc;
use async_trait::async_trait;
use bytes::Bytes;
use futures::Stream;
use crate::application::ports::storage_ports::{FileReadPort, FileWritePort};
use crate::common::errors::DomainError;
use crate::domain::entities::file::File;
use crate::domain::services::path_service::StoragePath;
/// Composite que envuelve `Arc<dyn FileReadPort>` + `Arc<dyn FileWritePort>`
/// y delega cada método al port correspondiente.
///
/// Gracias al blanket impl `impl<T: FileReadPort + FileWritePort> FileStoragePort for T {}`
/// este tipo obtiene `FileStoragePort` automáticamente.
pub struct CompositeFileRepository {
read: Arc<dyn FileReadPort>,
write: Arc<dyn FileWritePort>,
}
impl CompositeFileRepository {
pub fn new(read: Arc<dyn FileReadPort>, write: Arc<dyn FileWritePort>) -> Self {
Self { read, write }
}
}
// ─────────────────────────────────────────────────────
// FileReadPort — delegate to self.read
// ─────────────────────────────────────────────────────
#[async_trait]
impl FileReadPort for CompositeFileRepository {
async fn get_file(&self, id: &str) -> Result<File, DomainError> {
self.read.get_file(id).await
}
async fn list_files(&self, folder_id: Option<&str>) -> Result<Vec<File>, DomainError> {
self.read.list_files(folder_id).await
}
async fn get_file_content(&self, id: &str) -> Result<Vec<u8>, DomainError> {
self.read.get_file_content(id).await
}
async fn get_file_stream(
&self,
id: &str,
) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError> {
self.read.get_file_stream(id).await
}
async fn get_file_range_stream(
&self,
id: &str,
start: u64,
end: Option<u64>,
) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError> {
self.read.get_file_range_stream(id, start, end).await
}
async fn get_file_mmap(&self, id: &str) -> Result<Bytes, DomainError> {
self.read.get_file_mmap(id).await
}
async fn get_file_path(&self, id: &str) -> Result<StoragePath, DomainError> {
self.read.get_file_path(id).await
}
async fn get_parent_folder_id(&self, path: &str) -> Result<String, DomainError> {
self.read.get_parent_folder_id(path).await
}
}
// ─────────────────────────────────────────────────────
// FileWritePort — delegate to self.write
// ─────────────────────────────────────────────────────
#[async_trait]
impl FileWritePort for CompositeFileRepository {
async fn save_file(
&self,
name: String,
folder_id: Option<String>,
content_type: String,
content: Vec<u8>,
) -> Result<File, DomainError> {
self.write.save_file(name, folder_id, content_type, content).await
}
async fn save_file_from_stream(
&self,
name: String,
folder_id: Option<String>,
content_type: String,
stream: std::pin::Pin<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>>,
) -> Result<File, DomainError> {
self.write.save_file_from_stream(name, folder_id, content_type, stream).await
}
async fn move_file(
&self,
file_id: &str,
target_folder_id: Option<String>,
) -> Result<File, DomainError> {
self.write.move_file(file_id, target_folder_id).await
}
async fn delete_file(&self, id: &str) -> Result<(), DomainError> {
self.write.delete_file(id).await
}
async fn update_file_content(&self, file_id: &str, content: Vec<u8>) -> Result<(), DomainError> {
self.write.update_file_content(file_id, content).await
}
async fn register_file_deferred(
&self,
name: String,
folder_id: Option<String>,
content_type: String,
size: u64,
) -> Result<(File, PathBuf), DomainError> {
self.write.register_file_deferred(name, folder_id, content_type, size).await
}
async fn move_to_trash(&self, file_id: &str) -> Result<(), DomainError> {
self.write.move_to_trash(file_id).await
}
async fn restore_from_trash(&self, file_id: &str, original_path: &str) -> Result<(), DomainError> {
self.write.restore_from_trash(file_id, original_path).await
}
async fn delete_file_permanently(&self, file_id: &str) -> Result<(), DomainError> {
self.write.delete_file_permanently(file_id).await
}
}
@@ -1,186 +1,367 @@
use std::path::PathBuf;
use std::sync::Arc;
use async_trait::async_trait;
use tokio::{fs, time};
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};
use futures::{Stream, StreamExt};
use bytes::Bytes;
use futures::Stream;
use tokio::task;
use mime_guess::from_path;
use crate::domain::entities::file::File;
use crate::application::ports::storage_ports::FileReadPort;
use crate::common::errors::DomainError;
use crate::domain::repositories::file_repository::FileRepositoryResult;
use crate::infrastructure::repositories::file_metadata_manager::{FileMetadataManager, MetadataError};
use crate::infrastructure::repositories::file_path_resolver::FilePathResolver;
use crate::domain::services::path_service::StoragePath;
use crate::infrastructure::repositories::repository_errors::{FileRepositoryResult, FileRepositoryError};
use crate::infrastructure::repositories::parallel_file_processor::ParallelFileProcessor;
use crate::application::ports::cache_ports::MetadataCachePort;
use crate::application::services::storage_mediator::StorageMediator;
use crate::infrastructure::services::path_service::PathService;
use crate::domain::services::path_service::StoragePath;
use crate::common::config::AppConfig;
/// Implementación de repositorio para operaciones de lectura de archivos
/// Implementación de repositorio para operaciones de **lectura** de archivos.
///
/// Implementa `FileReadPort`:
/// get_file, list_files, get_file_content, get_file_stream,
/// get_file_range_stream, get_file_mmap, get_file_path, get_parent_folder_id.
pub struct FileFsReadRepository {
metadata_manager: Arc<FileMetadataManager>,
path_resolver: Arc<FilePathResolver>,
root_path: PathBuf,
storage_mediator: Arc<dyn StorageMediator>,
id_mapping_service: Arc<dyn crate::application::ports::outbound::IdMappingPort>,
path_service: Arc<PathService>,
metadata_cache: Arc<dyn MetadataCachePort>,
config: AppConfig,
parallel_processor: Option<Arc<ParallelFileProcessor>>,
}
impl FileFsReadRepository {
/// Crea un nuevo repositorio de lectura de archivos
/// Constructor completo con todas las dependencias de infraestructura.
pub fn new(
_root_path: PathBuf,
metadata_manager: Arc<FileMetadataManager>,
path_resolver: Arc<FilePathResolver>,
_config: AppConfig,
_parallel_processor: Option<Arc<ParallelFileProcessor>>,
root_path: PathBuf,
storage_mediator: Arc<dyn StorageMediator>,
id_mapping_service: Arc<dyn crate::application::ports::outbound::IdMappingPort>,
path_service: Arc<PathService>,
metadata_cache: Arc<dyn MetadataCachePort>,
config: AppConfig,
parallel_processor: Option<Arc<ParallelFileProcessor>>,
) -> Self {
Self {
metadata_manager,
path_resolver,
root_path,
storage_mediator,
id_mapping_service,
path_service,
metadata_cache,
config,
parallel_processor,
}
}
/// Crea un stub para pruebas
/// Stub para pruebas (no realiza I/O real).
pub fn default_stub() -> Self {
Self {
metadata_manager: Arc::new(FileMetadataManager::default()),
path_resolver: Arc::new(FilePathResolver::default_stub()),
root_path: PathBuf::from("./storage"),
storage_mediator: Arc::new(
crate::application::services::storage_mediator::FileSystemStorageMediator::new_stub(),
),
id_mapping_service: Arc::new(crate::common::stubs::StubIdMappingPort),
path_service: Arc::new(PathService::new(PathBuf::from("./storage"))),
metadata_cache: Arc::new(
crate::infrastructure::services::file_metadata_cache::FileMetadataCache::default()
) as Arc<dyn MetadataCachePort>,
config: AppConfig::default(),
parallel_processor: None,
}
}
/// Crea una entidad de archivo a partir de metadatos
async fn create_file_entity(
&self,
id: String,
name: String,
storage_path: StoragePath,
size: u64,
mime_type: String,
folder_id: Option<String>,
created_at: Option<u64>,
modified_at: Option<u64>,
) -> FileRepositoryResult<File> {
// If timestamps are provided, use them; otherwise, let File::new create default timestamps
if let (Some(created), Some(modified)) = (created_at, modified_at) {
File::with_timestamps(
id,
name,
storage_path,
size,
mime_type,
folder_id,
created,
modified,
)
.map_err(|e| crate::domain::repositories::file_repository::FileRepositoryError::Other(e.to_string()))
} else {
File::new(
id,
name,
storage_path,
size,
mime_type,
folder_id,
)
.map_err(|e| crate::domain::repositories::file_repository::FileRepositoryError::Other(e.to_string()))
}
// ─── helpers internos ────────────────────────────────────
fn resolve_storage_path(&self, storage_path: &StoragePath) -> PathBuf {
self.path_service.resolve_path(storage_path)
}
/// Obtiene un archivo por su ID
async fn get_file_metadata_raw(&self, abs_path: &PathBuf) -> FileRepositoryResult<(u64, u64, u64)> {
// Cache first
if let Some(cached) = self.metadata_cache.get_metadata(abs_path).await {
if let (Some(s), Some(c), Some(m)) = (cached.size, cached.created_at, cached.modified_at) {
return Ok((s, c, m));
}
}
let metadata = time::timeout(self.config.timeouts.file_timeout(), fs::metadata(abs_path))
.await
.map_err(|_| FileRepositoryError::StorageError(format!("Timeout metadata: {}", abs_path.display())))?
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
let size = metadata.len();
let created_at = metadata.created()
.map(|t| t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs())
.unwrap_or(0);
let modified_at = metadata.modified()
.map(|t| t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs())
.unwrap_or(0);
let _ = self.metadata_cache.refresh_metadata(abs_path).await;
Ok((size, created_at, modified_at))
}
async fn get_file_by_id(&self, id: &str) -> FileRepositoryResult<File> {
// Obtener la ruta del archivo usando el resolver de rutas
let storage_path = self.path_resolver.get_path_by_id(id).await?;
// Verificar que el archivo existe físicamente
let abs_path = self.path_resolver.resolve_storage_path(&storage_path);
if !self.metadata_manager.file_exists(&abs_path).await
.map_err(|e| crate::domain::repositories::file_repository::FileRepositoryError::Other(e.to_string()))? {
return Err(crate::domain::repositories::file_repository::FileRepositoryError::NotFound(
format!("File {} not found at {}", id, storage_path.to_string())
let storage_path = self.id_mapping_service.get_path_by_id(id).await
.map_err(|e| FileRepositoryError::Other(e.to_string()))?;
let abs_path = self.resolve_storage_path(&storage_path);
if !abs_path.exists() || !abs_path.is_file() {
return Err(FileRepositoryError::NotFound(
format!("File {} not found at {}", id, storage_path.to_string()),
));
}
// Obtener metadatos del archivo
let (size, created_at, modified_at) = self.metadata_manager.get_file_metadata(&abs_path).await
.map_err(|e| match e {
MetadataError::IoError(io_err) => crate::domain::repositories::file_repository::FileRepositoryError::IoError(io_err),
MetadataError::Timeout(msg) => crate::domain::repositories::file_repository::FileRepositoryError::Timeout(msg),
MetadataError::Unavailable(msg) => crate::domain::repositories::file_repository::FileRepositoryError::NotFound(msg),
})?;
// Obtener nombre del archivo de la ruta
let name = match storage_path.file_name() {
Some(name) => name,
None => {
return Err(crate::domain::repositories::file_repository::FileRepositoryError::InvalidPath(
storage_path.to_string()
));
}
};
// Determinar ID de carpeta padre
let parent = storage_path.parent();
let folder_id: Option<String> = if parent.is_none() || parent.as_ref().unwrap().is_empty() {
None // Root folder
} else {
None // En implementación real, buscar ID de la carpeta padre
};
// Determinar tipo MIME
let mime_type = mime_guess::from_path(&abs_path)
.first_or_octet_stream()
.to_string();
// Crear entidad de archivo
let file = self.create_file_entity(
id.to_string(),
name,
storage_path,
size,
mime_type,
folder_id,
Some(created_at),
Some(modified_at),
).await?;
Ok(file)
let (size, created_at, modified_at) = self.get_file_metadata_raw(&abs_path).await?;
let name = storage_path
.file_name()
.ok_or_else(|| FileRepositoryError::InvalidPath(storage_path.to_string()))?;
let mime_type = from_path(&abs_path).first_or_octet_stream().to_string();
File::with_timestamps(
id.to_string(), name, storage_path, size, mime_type, None,
created_at, modified_at,
)
.map_err(|e| FileRepositoryError::Other(e.to_string()))
}
}
impl Clone for FileFsReadRepository {
fn clone(&self) -> Self {
Self {
root_path: self.root_path.clone(),
storage_mediator: self.storage_mediator.clone(),
id_mapping_service: self.id_mapping_service.clone(),
path_service: self.path_service.clone(),
metadata_cache: self.metadata_cache.clone(),
config: self.config.clone(),
parallel_processor: self.parallel_processor.clone(),
}
}
}
#[async_trait]
impl FileReadPort for FileFsReadRepository {
async fn get_file(&self, id: &str) -> Result<File, DomainError> {
self.get_file_by_id(id).await
.map_err(|e| match e {
crate::domain::repositories::file_repository::FileRepositoryError::NotFound(msg) => DomainError::not_found("File", msg),
crate::domain::repositories::file_repository::FileRepositoryError::IoError(io_err) => DomainError::internal_error("File", io_err.to_string()),
crate::domain::repositories::file_repository::FileRepositoryError::Timeout(msg) => DomainError::internal_error("File", msg),
_ => DomainError::internal_error("File", e.to_string()),
})
self.get_file_by_id(id).await.map_err(|e| match e {
FileRepositoryError::NotFound(msg) => DomainError::not_found("File", msg),
FileRepositoryError::StorageError(msg) => DomainError::internal_error("File", msg),
other => DomainError::internal_error("File", other.to_string()),
})
}
async fn list_files(&self, _folder_id: Option<&str>) -> Result<Vec<File>, DomainError> {
// Implementación real debe obtener la lista de archivos en una carpeta
// Por ahora, devolvemos lista vacía
Ok(Vec::new())
async fn list_files(&self, folder_id: Option<&str>) -> Result<Vec<File>, DomainError> {
let folder_storage_path = match folder_id {
Some(id) => {
match self.storage_mediator.get_folder_path(id).await {
Ok(path) => {
let lossy = path.to_string_lossy().to_string();
let folder_name = path.file_name()
.and_then(|f| f.to_str())
.unwrap_or(&lossy);
StoragePath::from_string(folder_name)
}
Err(_) => return Ok(Vec::new()),
}
}
None => StoragePath::root(),
};
let abs_folder_path = self.path_service.resolve_path(&folder_storage_path);
if !abs_folder_path.exists() || !abs_folder_path.is_dir() {
return Ok(Vec::new());
}
let mut files_result = Vec::new();
let mut entries = fs::read_dir(&abs_folder_path).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
while let Some(entry) = entries.next_entry().await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?
{
let path = entry.path();
if !path.is_file() { continue; }
let file_name = entry.file_name().to_string_lossy().to_string();
if file_name.starts_with('.') || file_name == "folder_ids.json" || file_name == "file_ids.json" {
continue;
}
let metadata = match fs::metadata(&path).await {
Ok(m) => m,
Err(_) => continue,
};
let file_storage_path = folder_storage_path.join(&file_name);
let id = match self.id_mapping_service.get_or_create_id(&file_storage_path).await {
Ok(id) => id,
Err(_) => continue,
};
let size = metadata.len();
let created_at = metadata.created()
.map(|t| t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs())
.unwrap_or(0);
let modified_at = metadata.modified()
.map(|t| t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs())
.unwrap_or(0);
let mime_type = from_path(&path).first_or_octet_stream().to_string();
match File::with_timestamps(id, file_name, file_storage_path, size, mime_type, folder_id.map(String::from), created_at, modified_at) {
Ok(file) => files_result.push(file),
Err(_) => continue,
}
}
// Persist any new ID mappings
let _ = self.id_mapping_service.save_changes().await;
Ok(files_result)
}
async fn get_file_content(&self, id: &str) -> Result<Vec<u8>, DomainError> {
// Primero obtenemos el archivo para verificar existencia
let file = self.get_file_by_id(id).await
.map_err(|e| match e {
crate::domain::repositories::file_repository::FileRepositoryError::NotFound(msg) => DomainError::not_found("File", msg),
crate::domain::repositories::file_repository::FileRepositoryError::IoError(io_err) => DomainError::internal_error("File", io_err.to_string()),
crate::domain::repositories::file_repository::FileRepositoryError::Timeout(msg) => DomainError::internal_error("File", msg),
_ => DomainError::internal_error("File", e.to_string()),
})?;
// Ruta absoluta del archivo
let _abs_path = self.path_resolver.resolve_storage_path(file.storage_path());
// Implementación real debe leer el contenido del archivo
// Por ahora, devolvemos un vector vacío
Ok(Vec::new())
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let abs_path = self.resolve_storage_path(file.storage_path());
let metadata = time::timeout(self.config.timeouts.file_timeout(), fs::metadata(&abs_path))
.await
.map_err(|_| DomainError::internal_error("File", format!("Timeout metadata: {}", abs_path.display())))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let file_size = metadata.len();
if !self.config.resources.can_load_in_memory(file_size) {
return Err(DomainError::internal_error("File",
format!("File too large for memory: {} MB", file_size / (1024 * 1024))));
}
// Parallel read for very large files
if self.config.resources.needs_parallel_processing(file_size, &self.config.concurrency) {
let content = if let Some(processor) = &self.parallel_processor {
processor.read_file_parallel(&abs_path).await
} else {
let processor = ParallelFileProcessor::new(self.config.clone());
processor.read_file_parallel(&abs_path).await
};
return content.map_err(|e| DomainError::internal_error("File", e.to_string()));
}
// spawn_blocking for large-ish files
if self.config.resources.is_large_file(file_size) {
let abs_clone = abs_path.clone();
let chunk_size = self.config.resources.chunk_size_bytes;
let content = task::spawn_blocking(move || -> std::io::Result<Vec<u8>> {
use std::io::{Read, BufReader};
let file = std::fs::File::open(&abs_clone)?;
let mut reader = BufReader::with_capacity(chunk_size, file);
let mut buf = Vec::with_capacity(file_size as usize);
reader.read_to_end(&mut buf)?;
Ok(buf)
}).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
return Ok(content);
}
// Small files — async read
time::timeout(self.config.timeouts.file_timeout(), fs::read(&abs_path))
.await
.map_err(|_| DomainError::internal_error("File", format!("Timeout reading: {}", abs_path.display())))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))
}
async fn get_file_stream(&self, _id: &str) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError> {
// Implementación real debe devolver un stream de bytes del archivo
// Por ahora, lanzamos un error
Err(DomainError::internal_error("File stream", "Stream functionality not yet implemented"))
async fn get_file_stream(
&self,
id: &str,
) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError> {
let file = self.get_file_by_id(id).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let abs_path = self.resolve_storage_path(file.storage_path());
let metadata = time::timeout(self.config.timeouts.file_timeout(), fs::metadata(&abs_path))
.await
.map_err(|_| DomainError::internal_error("File", "Timeout getting metadata"))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let file_size = metadata.len();
let is_large = self.config.resources.is_large_file(file_size);
let fh = time::timeout(self.config.timeouts.file_timeout(), TokioFile::open(&abs_path))
.await
.map_err(|_| DomainError::internal_error("File", "Timeout opening file"))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let chunk_size = if is_large { self.config.resources.chunk_size_bytes } else { 4096 };
let codec = BytesCodec::new();
let stream = FramedRead::with_capacity(fh, codec, chunk_size)
.map(|r| r.map(|bm| bm.freeze()));
Ok(Box::new(stream))
}
async fn get_file_range_stream(
&self,
id: &str,
start: u64,
end: Option<u64>,
) -> Result<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>, DomainError> {
use tokio::io::AsyncSeekExt;
let file = self.get_file_by_id(id).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let abs_path = self.resolve_storage_path(file.storage_path());
let metadata = time::timeout(self.config.timeouts.file_timeout(), fs::metadata(&abs_path))
.await
.map_err(|_| DomainError::internal_error("File", "Timeout"))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let file_size = metadata.len();
if start >= file_size {
return Err(DomainError::internal_error("File",
format!("Range start {} beyond file size {}", start, file_size)));
}
let actual_end = end.map(|e| e.min(file_size - 1)).unwrap_or(file_size - 1);
let range_length = actual_end - start + 1;
let mut fh = time::timeout(self.config.timeouts.file_timeout(), TokioFile::open(&abs_path))
.await
.map_err(|_| DomainError::internal_error("File", "Timeout opening file"))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
fh.seek(std::io::SeekFrom::Start(start)).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let chunk_size = if range_length > 1024 * 1024 { self.config.resources.chunk_size_bytes } else { 8192 };
use tokio::io::AsyncReadExt;
let limited = fh.take(range_length);
let codec = BytesCodec::new();
let stream = FramedRead::with_capacity(limited, codec, chunk_size)
.map(|r| r.map(|bm| bm.freeze()));
Ok(Box::new(stream))
}
async fn get_file_mmap(&self, id: &str) -> Result<Bytes, DomainError> {
use memmap2::Mmap;
let file = self.get_file_by_id(id).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let abs_path = self.resolve_storage_path(file.storage_path());
let path_clone = abs_path.clone();
task::spawn_blocking(move || -> Result<Bytes, DomainError> {
let fh = std::fs::File::open(&path_clone)
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let mmap = unsafe { Mmap::map(&fh) }
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
Ok(Bytes::copy_from_slice(&mmap[..]))
}).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?
}
async fn get_file_path(&self, id: &str) -> Result<StoragePath, DomainError> {
self.id_mapping_service.get_path_by_id(id).await
}
async fn get_parent_folder_id(&self, path: &str) -> Result<String, DomainError> {
let storage_path = StoragePath::from_string(path);
match storage_path.parent() {
Some(parent) if !parent.is_empty() => {
self.id_mapping_service.get_or_create_id(&parent).await
}
_ => Ok("root".to_string()),
}
}
}
File diff suppressed because it is too large Load Diff
@@ -1,368 +0,0 @@
use std::path::PathBuf;
use tokio::fs;
use tracing::{debug, error, instrument};
use crate::domain::repositories::file_repository::FileRepositoryResult;
use crate::infrastructure::repositories::file_fs_repository::FileFsRepository;
// This file contains the implementation of trash-related methods
// for the FileFsRepository file repository
// Implementation of trash methods for the file repository
impl FileFsRepository {
// Gets the complete path to the trash directory
fn get_trash_dir(&self) -> PathBuf {
let trash_dir = self.get_root_path().join(".trash").join("files");
debug!("Base trash directory: {}", trash_dir.display());
trash_dir
}
// Gets the trash directory path for a specific user (if provided)
fn get_user_trash_dir(&self, user_id: Option<&str>) -> PathBuf {
let base_trash_dir = self.get_trash_dir();
if let Some(uid) = user_id {
let user_trash_dir = base_trash_dir.join(uid);
debug!("User-specific trash directory: {}", user_trash_dir.display());
user_trash_dir
} else {
// No user ID provided - this should not happen in production
tracing::warn!("No user_id provided for trash directory, this indicates a bug");
let default_dir = base_trash_dir.join("unknown-user");
debug!("Fallback user trash directory: {}", default_dir.display());
default_dir
}
}
// Creates a unique path in the trash for the file
async fn create_trash_file_path(&self, file_id: &str) -> FileRepositoryResult<PathBuf> {
debug!("Creating trash file path for file ID: {}", file_id);
// Get the trash directory for the default user
let user_trash_dir = self.get_user_trash_dir(None);
// Ensure the user's trash directory exists
debug!("Ensuring user trash directory exists: {}", user_trash_dir.display());
if !user_trash_dir.exists() {
debug!("Creating user trash directory: {}", user_trash_dir.display());
fs::create_dir_all(&user_trash_dir).await
.map_err(|e| {
error!("Failed to create user trash directory: {}", e);
FileRepositoryError::IoError(e)
})?;
debug!("User trash directory created successfully");
} else {
debug!("User trash directory already exists");
}
// Create a unique path for the file in the trash
let trash_file_path = user_trash_dir.join(file_id);
debug!("Trash file path: {}", trash_file_path.display());
Ok(trash_file_path)
}
}
// Implementation of the public methods of the FileRepository trait related to trash
// Note: The FileRepository trait implementation has been moved to file_fs_repository.rs
// to avoid duplicate implementations
// Implementation of internal methods for trash functionality
impl FileFsRepository {
/// Helper method that will be used for trash functionality
pub(crate) async fn _trash_move_to_trash(&self, file_id: &str) -> FileRepositoryResult<()> {
debug!("Moving file to trash: {}", file_id);
// Get the physical path of the file
// We create an independent method to access the ID mapping service
debug!("Getting file path with ID: {}", file_id);
let file_path = match self.id_mapping_service().get_file_path(file_id).await {
Ok(path) => {
debug!("File path obtained: {}", path.display());
path
},
Err(e) => {
error!("Error getting file path {}: {:?}", file_id, e);
return Err(FileRepositoryError::IdMappingError(format!("Failed to get file path: {}", e)));
}
};
// Verify that the file exists
debug!("Verifying that the file exists: {}", file_path.display());
if !self.file_exists(&file_path).await? {
error!("File not found at the specified path: {}", file_path.display());
return Err(FileRepositoryError::NotFound(format!("File not found: {}", file_id)));
}
debug!("File found, continuing with the operation");
// Create directory in trash if it doesn't exist
debug!("Creating path for file in trash");
let trash_file_path = self.create_trash_file_path(file_id).await?;
debug!("Path in trash: {}", trash_file_path.display());
// Physically move the file to trash (doesn't update mappings)
debug!("Physically moving file to trash: {} -> {}", file_path.display(), trash_file_path.display());
match fs::rename(&file_path, &trash_file_path).await {
Ok(_) => {
debug!("File successfully moved to trash: {} -> {}", file_path.display(), trash_file_path.display());
// Invalidate the cache for the original file
debug!("Invalidating cache for: {}", file_path.display());
self.metadata_cache().invalidate(&file_path).await;
// Update the mapping to the new path in trash
debug!("Updating ID mapping to new path in trash");
if let Err(e) = self.id_mapping_service().update_file_path(file_id, &trash_file_path).await {
error!("Error updating file mapping in trash: {}", e);
return Err(FileRepositoryError::MappingError(format!("Failed to update mapping: {}", e)));
}
debug!("Mapping successfully updated");
debug!("Move to trash operation completed successfully for file: {}", file_id);
Ok(())
},
Err(e) => {
error!("Error moving file to trash: {} -> {}: {}",
file_path.display(), trash_file_path.display(), e);
Err(FileRepositoryError::IoError(e))
}
}
}
/// Restores a file from trash to its original location
#[instrument(skip(self))]
pub(crate) async fn _trash_restore_from_trash(&self, file_id: &str, original_path: &str) -> FileRepositoryResult<()> {
debug!("Restoring file {} to {}", file_id, original_path);
// Try to get the current path from the ID mapping service
let current_path_result = self.id_mapping_service().get_file_path(file_id).await;
match current_path_result {
Ok(current_path) => {
debug!("Current path in trash: {}", current_path.display());
// Check if the file exists in the trash
let file_exists = match fs::metadata(&current_path).await {
Ok(_) => {
debug!("File exists in trash");
true
},
Err(e) => {
debug!("File does not exist in trash: {} - {}", current_path.display(), e);
false
}
};
if !file_exists {
error!("The file does not physically exist in the trash: {}", current_path.display());
return Err(FileRepositoryError::NotFound(format!("File not found in trash: {}", file_id)));
}
// Parse the original path to a PathBuf
let original_path_buf = PathBuf::from(original_path);
debug!("Original path for restoration: {}", original_path_buf.display());
// Check if a file already exists at the destination
let target_exists = fs::metadata(&original_path_buf).await.is_ok();
if target_exists {
debug!("A file already exists at the destination path, generating alternative path");
// Generate a unique path by adding a suffix
// Extract filename and extension
let file_name = original_path_buf.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|| "restored_file".to_string());
let parent_dir = original_path_buf.parent()
.unwrap_or_else(|| std::path::Path::new(""));
let (stem, ext) = if let Some(dot_pos) = file_name.rfind('.') {
(file_name[..dot_pos].to_string(), file_name[dot_pos..].to_string())
} else {
(file_name, "".to_string())
};
// Create a new name with a timestamp
let timestamp = chrono::Utc::now().timestamp();
let new_name = format!("{}_{}{}", stem, timestamp, ext);
// Create the alternative path
let alternative_path = parent_dir.join(new_name);
debug!("Alternative path for restoration: {}", alternative_path.display());
// Ensure the parent directory exists
if let Some(parent) = alternative_path.parent() {
if !parent.exists() {
debug!("Creating parent directory for restoration: {}", parent.display());
match fs::create_dir_all(parent).await {
Ok(_) => debug!("Parent directory created successfully"),
Err(e) => {
error!("Error creating parent directory: {} - {}", parent.display(), e);
return Err(FileRepositoryError::IoError(e));
}
}
}
}
// Move the file from trash to the alternative location
debug!("Moving file from trash to alternative location: {} -> {}",
current_path.display(), alternative_path.display());
match fs::rename(&current_path, &alternative_path).await {
Ok(_) => {
debug!("File successfully restored to alternative location");
// Invalidate cache entries
debug!("Invalidating cache for file in trash");
self.metadata_cache().invalidate(&current_path).await;
// Update the ID mapping
debug!("Updating ID mapping to new location");
if let Err(e) = self.id_mapping_service().update_file_path(file_id, &alternative_path).await {
error!("Error updating mapping of restored file: {}", e);
return Err(FileRepositoryError::MappingError(
format!("Failed to update mapping: {}", e)
));
}
debug!("Restoration to alternative location completed successfully");
Ok(())
},
Err(e) => {
error!("Error restoring file to alternative location: {}", e);
Err(FileRepositoryError::IoError(e))
}
}
} else {
// Ensure the parent directory exists
if let Some(parent) = original_path_buf.parent() {
if !parent.exists() {
debug!("Creating parent directory for restoration: {}", parent.display());
match fs::create_dir_all(parent).await {
Ok(_) => debug!("Parent directory created successfully"),
Err(e) => {
error!("Error creating parent directory: {} - {}", parent.display(), e);
return Err(FileRepositoryError::IoError(e));
}
}
}
}
// Move the file from trash to its original location
debug!("Moving file from trash to original location: {} -> {}",
current_path.display(), original_path_buf.display());
match fs::rename(&current_path, &original_path_buf).await {
Ok(_) => {
debug!("File successfully restored to original location");
// Invalidate cache entries
debug!("Invalidating cache for file in trash");
self.metadata_cache().invalidate(&current_path).await;
// Update the ID mapping
debug!("Updating ID mapping to original location");
if let Err(e) = self.id_mapping_service().update_file_path(file_id, &original_path_buf).await {
error!("Error updating mapping of restored file: {}", e);
return Err(FileRepositoryError::MappingError(
format!("Failed to update mapping: {}", e)
));
}
debug!("Restoration to original location completed successfully");
Ok(())
},
Err(e) => {
error!("Error restoring file to original location: {}", e);
Err(FileRepositoryError::IoError(e))
}
}
}
},
Err(e) => {
error!("Error getting current path of file {}: {:?}", file_id, e);
// Check if the error is because the ID was not found
if format!("{}", e).contains("not found") {
debug!("ID not found in mapping, file no longer exists in trash");
return Err(FileRepositoryError::NotFound(format!("File not found in trash: {}", file_id)));
}
return Err(FileRepositoryError::IdMappingError(
format!("Failed to get file path: {}", e)
));
}
}
}
/// Permanently deletes a file (used by trash)
#[instrument(skip(self))]
pub(crate) async fn _trash_delete_file_permanently(&self, file_id: &str) -> FileRepositoryResult<()> {
debug!("Permanently deleting file: {}", file_id);
// Get the file path using the ID mapping service
let file_path_result = self.id_mapping_service().get_file_path(file_id).await;
match file_path_result {
Ok(file_path) => {
debug!("Found path for file: {} -> {}", file_id, file_path.display());
// Check if the file physically exists before attempting to delete
let file_exists = fs::metadata(&file_path).await.is_ok();
if file_exists {
debug!("File exists physically, deleting: {}", file_path.display());
// Delete the file physically
if let Err(e) = fs::remove_file(&file_path).await {
error!("Error permanently deleting file: {} - {}", file_path.display(), e);
// Don't report error if the file already doesn't exist
if e.kind() != std::io::ErrorKind::NotFound {
return Err(FileRepositoryError::IoError(e));
}
} else {
debug!("File physically deleted successfully");
}
// Invalidate cache for this file
debug!("Invalidating cache for file: {}", file_path.display());
self.metadata_cache().invalidate(&file_path).await;
} else {
debug!("File does not exist physically, only cleaning mappings: {}", file_path.display());
}
// Always remove the ID mapping regardless of whether the file exists
debug!("Removing ID mapping: {}", file_id);
match self.id_mapping_service().remove_id(file_id).await {
Ok(_) => debug!("ID mapping successfully removed"),
Err(e) => {
error!("Error removing file mapping: {}", e);
// Only return error for critical mapping errors, otherwise continue
if format!("{}", e).contains("not found") {
debug!("ID mapping not found, ignoring this error for deletion");
} else {
return Err(FileRepositoryError::MappingError(format!("Failed to remove mapping: {}", e)));
}
}
};
debug!("File permanently deleted successfully: {}", file_id);
Ok(())
},
Err(e) => {
// This could happen if the file is already deleted or wasn't properly indexed
error!("Error getting file path {}: {:?}", file_id, e);
// Check if the error is because the ID was not found
if format!("{}", e).contains("not found") {
debug!("ID not found in mapping, considering deletion successful: {}", file_id);
// In this case, we consider the file already deleted
return Ok(());
}
return Err(FileRepositoryError::IdMappingError(format!("Failed to get file path: {}", e)));
}
}
}
}
// Re-exports needed for the compiler
use crate::domain::repositories::file_repository::FileRepositoryError;
@@ -1,103 +1,223 @@
use std::path::PathBuf;
use std::sync::Arc;
use async_trait::async_trait;
use tokio::{fs, time};
use tokio::fs::File as TokioFile;
use tokio::io::AsyncWriteExt;
use futures::{Stream, StreamExt};
use bytes::Bytes;
use mime_guess::from_path;
use tokio::task;
use crate::domain::entities::file::File;
use crate::application::ports::storage_ports::FileWritePort;
use crate::common::errors::DomainError;
use crate::domain::repositories::file_repository::FileRepositoryResult;
use crate::infrastructure::repositories::file_metadata_manager::{FileMetadataManager, MetadataError};
use crate::infrastructure::repositories::file_path_resolver::FilePathResolver;
use crate::domain::services::path_service::StoragePath;
use crate::infrastructure::repositories::parallel_file_processor::ParallelFileProcessor;
use crate::common::config::AppConfig;
use crate::application::services::storage_mediator::StorageMediator;
use crate::infrastructure::repositories::repository_errors::{FileRepositoryResult, FileRepositoryError};
use crate::infrastructure::services::file_system_utils::FileSystemUtils;
use crate::application::ports::cache_ports::MetadataCachePort;
use crate::infrastructure::repositories::parallel_file_processor::ParallelFileProcessor;
use crate::application::services::storage_mediator::StorageMediator;
use crate::infrastructure::services::path_service::PathService;
use crate::domain::services::path_service::StoragePath;
use crate::common::config::AppConfig;
/// Implementación de repositorio para operaciones de escritura de archivos
/// Implementación de repositorio para operaciones de **escritura** de archivos.
///
/// Implementa `FileWritePort`:
/// save_file, save_file_from_stream, move_file, delete_file,
/// update_file_content, register_file_deferred.
pub struct FileFsWriteRepository {
metadata_manager: Arc<FileMetadataManager>,
path_resolver: Arc<FilePathResolver>,
root_path: PathBuf,
storage_mediator: Arc<dyn StorageMediator>,
id_mapping_service: Arc<dyn crate::application::ports::outbound::IdMappingPort>,
path_service: Arc<PathService>,
metadata_cache: Arc<dyn MetadataCachePort>,
config: AppConfig,
parallel_processor: Option<Arc<ParallelFileProcessor>>,
}
impl FileFsWriteRepository {
/// Crea un nuevo repositorio de escritura de archivos
/// Constructor completo con todas las dependencias.
pub fn new(
_root_path: PathBuf,
metadata_manager: Arc<FileMetadataManager>,
path_resolver: Arc<FilePathResolver>,
_storage_mediator: Arc<dyn StorageMediator>,
root_path: PathBuf,
storage_mediator: Arc<dyn StorageMediator>,
id_mapping_service: Arc<dyn crate::application::ports::outbound::IdMappingPort>,
path_service: Arc<PathService>,
metadata_cache: Arc<dyn MetadataCachePort>,
config: AppConfig,
_parallel_processor: Option<Arc<ParallelFileProcessor>>,
parallel_processor: Option<Arc<ParallelFileProcessor>>,
) -> Self {
Self {
metadata_manager,
path_resolver,
config,
}
Self { root_path, storage_mediator, id_mapping_service, path_service, metadata_cache, config, parallel_processor }
}
/// Crea un stub para pruebas
/// Stub para pruebas (no realiza I/O real).
pub fn default_stub() -> Self {
Self {
metadata_manager: Arc::new(FileMetadataManager::default()),
path_resolver: Arc::new(FilePathResolver::default_stub()),
root_path: PathBuf::from("./storage"),
storage_mediator: Arc::new(
crate::application::services::storage_mediator::FileSystemStorageMediator::new_stub(),
),
id_mapping_service: Arc::new(crate::common::stubs::StubIdMappingPort),
path_service: Arc::new(PathService::new(PathBuf::from("./storage"))),
metadata_cache: Arc::new(
crate::infrastructure::services::file_metadata_cache::FileMetadataCache::default()
) as Arc<dyn MetadataCachePort>,
config: AppConfig::default(),
parallel_processor: None,
}
}
/// Crea directorios padres si es necesario, con sincronización
// ─── helpers ─────────────────────────────────────────────
fn resolve_storage_path(&self, storage_path: &StoragePath) -> PathBuf {
self.path_service.resolve_path(storage_path)
}
async fn ensure_parent_directory(&self, abs_path: &PathBuf) -> FileRepositoryResult<()> {
if let Some(parent) = abs_path.parent() {
tokio::time::timeout(
time::timeout(
self.config.timeouts.dir_timeout(),
FileSystemUtils::create_dir_with_sync(parent)
FileSystemUtils::create_dir_with_sync(parent),
).await
.map_err(|_| crate::domain::repositories::file_repository::FileRepositoryError::Timeout(
format!("Timeout creating parent directory: {}", parent.display())
))?
.map_err(crate::domain::repositories::file_repository::FileRepositoryError::IoError)?;
.map_err(|_| FileRepositoryError::StorageError(format!("Timeout creating dir: {}", parent.display())))?
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
}
Ok(())
}
/// Crea una entidad de archivo a partir de metadatos
async fn create_file_entity(
&self,
id: String,
name: String,
storage_path: StoragePath,
size: u64,
mime_type: String,
folder_id: Option<String>,
created_at: Option<u64>,
modified_at: Option<u64>,
) -> FileRepositoryResult<File> {
// If timestamps are provided, use them; otherwise, let File::new create default timestamps
if let (Some(created), Some(modified)) = (created_at, modified_at) {
File::with_timestamps(
id,
name,
storage_path,
size,
mime_type,
folder_id,
created,
modified,
)
.map_err(|e| crate::domain::repositories::file_repository::FileRepositoryError::Other(e.to_string()))
} else {
File::new(
id,
name,
storage_path,
size,
mime_type,
folder_id,
)
.map_err(|e| crate::domain::repositories::file_repository::FileRepositoryError::Other(e.to_string()))
async fn file_exists_at_storage_path(&self, storage_path: &StoragePath) -> FileRepositoryResult<bool> {
let abs = self.resolve_storage_path(storage_path);
if let Some(is_file) = self.metadata_cache.is_file(&abs).await {
return Ok(is_file);
}
match time::timeout(self.config.timeouts.file_timeout(), fs::metadata(&abs)).await {
Ok(Ok(m)) => {
let _ = self.metadata_cache.refresh_metadata(&abs).await;
Ok(m.is_file())
}
Ok(Err(_)) => Ok(false),
Err(_) => Err(FileRepositoryError::StorageError(format!("Timeout: {}", abs.display()))),
}
}
async fn get_file_metadata_raw(&self, abs_path: &PathBuf) -> FileRepositoryResult<(u64, u64, u64)> {
if let Some(cached) = self.metadata_cache.get_metadata(abs_path).await {
if let (Some(s), Some(c), Some(m)) = (cached.size, cached.created_at, cached.modified_at) {
return Ok((s, c, m));
}
}
let meta = time::timeout(self.config.timeouts.file_timeout(), fs::metadata(abs_path))
.await
.map_err(|_| FileRepositoryError::StorageError(format!("Timeout: {}", abs_path.display())))?
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
let s = meta.len();
let c = meta.created().map(|t| t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs()).unwrap_or(0);
let m = meta.modified().map(|t| t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs()).unwrap_or(0);
let _ = self.metadata_cache.refresh_metadata(abs_path).await;
Ok((s, c, m))
}
/// Resolve folder to StoragePath
async fn resolve_folder_path(&self, folder_id: &Option<String>) -> StoragePath {
match folder_id {
Some(id) => match self.storage_mediator.get_folder_path(id).await {
Ok(path) => {
let lossy = path.to_string_lossy().to_string();
let folder_name = path.file_name().and_then(|f| f.to_str()).unwrap_or(&lossy);
StoragePath::from_string(folder_name)
}
Err(_) => StoragePath::root(),
},
None => StoragePath::root(),
}
}
/// Generate unique file path avoiding name collisions.
async fn unique_file_path(
&self,
folder_path: &StoragePath,
name: &str,
) -> FileRepositoryResult<(StoragePath, String)> {
let mut file_path = folder_path.join(name);
let mut actual_name = name.to_string();
let mut counter = 1;
while self.file_exists_at_storage_path(&file_path).await? {
let (stem, ext) = if let Some(dot) = name.rfind('.') {
(name[..dot].to_string(), name[dot..].to_string())
} else {
(name.to_string(), String::new())
};
actual_name = format!("{}_{}{}", stem, counter, ext);
file_path = folder_path.join(&actual_name);
counter += 1;
}
Ok((file_path, actual_name))
}
async fn delete_file_non_blocking(&self, abs_path: PathBuf) -> FileRepositoryResult<()> {
let file_size = match fs::metadata(&abs_path).await {
Ok(m) => m.len(),
Err(_) => 0,
};
if self.config.resources.is_large_file(file_size) {
task::spawn_blocking(move || { let _ = std::fs::remove_file(&abs_path); })
.await
.map_err(|e| FileRepositoryError::Other(e.to_string()))?;
} else {
time::timeout(self.config.timeouts.file_timeout(), fs::remove_file(&abs_path))
.await
.map_err(|_| FileRepositoryError::StorageError("Timeout deleting file".into()))?
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
}
Ok(())
}
/// Persist ID mapping with retry + verification.
async fn persist_id_mapping(&self, id: &str, expected_path: &str) -> FileRepositoryResult<()> {
for attempt in 1..=3 {
match self.id_mapping_service.save_changes().await {
Ok(_) => {
if let Ok(verified) = self.id_mapping_service.get_path_by_id(id).await {
if verified.to_string() == expected_path {
return Ok(());
}
}
if attempt == 3 {
return Err(FileRepositoryError::Other("Failed to verify ID mapping after 3 attempts".into()));
}
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
Err(e) if attempt < 3 => {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
tracing::warn!("ID mapping save retry {}: {}", attempt, e);
}
Err(e) => return Err(FileRepositoryError::Other(format!("Save ID mapping failed: {}", e))),
}
}
Ok(())
}
}
impl Clone for FileFsWriteRepository {
fn clone(&self) -> Self {
Self {
root_path: self.root_path.clone(),
storage_mediator: self.storage_mediator.clone(),
id_mapping_service: self.id_mapping_service.clone(),
path_service: self.path_service.clone(),
metadata_cache: self.metadata_cache.clone(),
config: self.config.clone(),
parallel_processor: self.parallel_processor.clone(),
}
}
}
fn map_repo_err(e: FileRepositoryError) -> DomainError {
match e {
FileRepositoryError::NotFound(m) => DomainError::not_found("File", m),
FileRepositoryError::AlreadyExists(m) => DomainError::already_exists("File", m),
FileRepositoryError::StorageError(m) => DomainError::internal_error("File", m),
other => DomainError::internal_error("File", other.to_string()),
}
}
@@ -110,103 +230,288 @@ impl FileWritePort for FileFsWriteRepository {
content_type: String,
content: Vec<u8>,
) -> Result<File, DomainError> {
// Generate a unique ID for the file
let file_id = uuid::Uuid::new_v4().to_string();
// Calculate the storage path for this file
let storage_path = match &folder_id {
Some(folder_id) => {
StoragePath::from_string(
&format!("/{}/{}", folder_id, name)
)
},
None => {
StoragePath::from_string(
&format!("/{}", name)
)
let folder_path = self.resolve_folder_path(&folder_id).await;
let (file_storage_path, actual_name) = self.unique_file_path(&folder_path, &name).await.map_err(map_repo_err)?;
let abs_path = self.resolve_storage_path(&file_storage_path);
self.ensure_parent_directory(&abs_path).await.map_err(map_repo_err)?;
let content_size = content.len() as u64;
// Write strategy based on file size
if self.config.resources.needs_parallel_processing(content_size, &self.config.concurrency) {
if let Some(proc) = &self.parallel_processor {
proc.write_file_parallel(&abs_path, &content).await.map_err(map_repo_err)?;
} else {
let proc = ParallelFileProcessor::new(self.config.clone());
proc.write_file_parallel(&abs_path, &content).await.map_err(map_repo_err)?;
}
};
// Resolve the absolute path on disk
let abs_path = self.path_resolver.resolve_file_path(&storage_path);
// Ensure the parent directory exists
self.ensure_parent_directory(&abs_path).await
.map_err(|e| DomainError::internal_error("File system", e.to_string()))?;
// Write the file to disk using atomic write with fsync
tokio::time::timeout(
self.config.timeouts.file_write_timeout(),
FileSystemUtils::atomic_write(&abs_path, &content)
).await
.map_err(|_| DomainError::internal_error(
"File write",
format!("Timeout writing file: {}", abs_path.display())
))?
.map_err(|e| DomainError::internal_error(
"File system",
format!("Error writing file: {} - {}", abs_path.display(), e)
))?;
// Create and return a File entity
let size = content.len() as u64;
let file = self.create_file_entity(
file_id,
name,
storage_path,
size,
content_type,
folder_id,
None,
None,
).await
.map_err(|e| DomainError::internal_error("File entity creation", e.to_string()))?;
// Save metadata
self.metadata_manager.update_file_metadata(&file)
.await
.map_err(|e| match e {
MetadataError::IoError(e) => DomainError::internal_error("File metadata", e.to_string()),
MetadataError::Timeout(msg) => DomainError::internal_error("File metadata", msg),
MetadataError::Unavailable(msg) => DomainError::not_found("File metadata", msg)
})?;
tracing::info!("File saved successfully: {} (ID: {})", file.name(), file.id());
} else if content_size > self.config.resources.large_file_threshold_mb * 1024 * 1024 {
let mut fh = time::timeout(self.config.timeouts.file_timeout(), TokioFile::create(&abs_path))
.await
.map_err(|_| DomainError::internal_error("File", "Timeout creating file"))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let chunk_size = self.config.resources.chunk_size_bytes;
for chunk in content.chunks(chunk_size) {
fh.write_all(chunk).await.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
}
fh.flush().await.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
} else {
let mut fh = time::timeout(self.config.timeouts.file_timeout(), TokioFile::create(&abs_path))
.await
.map_err(|_| DomainError::internal_error("File", "Timeout creating file"))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
fh.write_all(&content).await.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
fh.flush().await.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
}
let (size, created_at, modified_at) = self.get_file_metadata_raw(&abs_path).await.map_err(map_repo_err)?;
let mime = if content_type.is_empty() { from_path(&abs_path).first_or_octet_stream().to_string() } else { content_type };
let id = self.id_mapping_service.get_or_create_id(&file_storage_path).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let path_string = file_storage_path.to_string();
let file = File::with_timestamps(id.clone(), actual_name, file_storage_path, size, mime, folder_id, created_at, modified_at)
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
self.persist_id_mapping(&id, &path_string).await.map_err(map_repo_err)?;
if let Some(parent) = abs_path.parent() {
self.metadata_cache.invalidate_directory(parent).await;
}
Ok(file)
}
async fn move_file(&self, _file_id: &str, _target_folder_id: Option<String>) -> Result<File, DomainError> {
// Implementación real debe mover el archivo a otra carpeta
// Por ahora, devolvemos un error
Err(DomainError::internal_error("File move", "Move functionality not yet implemented"))
async fn save_file_from_stream(
&self,
name: String,
folder_id: Option<String>,
content_type: String,
mut stream: std::pin::Pin<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>>,
) -> Result<File, DomainError> {
let folder_path = self.resolve_folder_path(&folder_id).await;
let (file_storage_path, actual_name) = self.unique_file_path(&folder_path, &name).await.map_err(map_repo_err)?;
let abs_path = self.resolve_storage_path(&file_storage_path);
self.ensure_parent_directory(&abs_path).await.map_err(map_repo_err)?;
let temp_path = abs_path.with_extension("tmp.upload");
let mut fh = time::timeout(self.config.timeouts.file_timeout(), TokioFile::create(&temp_path))
.await
.map_err(|_| DomainError::internal_error("File", "Timeout creating temp file"))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let mut total_bytes: u64 = 0;
while let Some(chunk_result) = stream.next().await {
let chunk = chunk_result.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
fh.write_all(&chunk).await.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
total_bytes += chunk.len() as u64;
}
fh.flush().await.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
fh.sync_all().await.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
drop(fh);
// Atomic rename
fs::rename(&temp_path, &abs_path).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let (size, created_at, modified_at) = self.get_file_metadata_raw(&abs_path).await.map_err(map_repo_err)?;
let mime = if content_type.is_empty() { from_path(&abs_path).first_or_octet_stream().to_string() } else { content_type };
let id = self.id_mapping_service.get_or_create_id(&file_storage_path).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let path_string = file_storage_path.to_string();
let log_name = actual_name.clone();
let file = File::with_timestamps(id.clone(), actual_name, file_storage_path, size, mime, folder_id, created_at, modified_at)
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
self.persist_id_mapping(&id, &path_string).await.map_err(map_repo_err)?;
if let Some(parent) = abs_path.parent() {
self.metadata_cache.invalidate_directory(parent).await;
}
tracing::info!("✅ STREAMING UPLOAD COMPLETE: {} ({} bytes)", log_name, total_bytes);
Ok(file)
}
async fn delete_file(&self, _id: &str) -> Result<(), DomainError> {
// Por ahora, devolvemos OK simulando éxito
// En una implementación real, buscaríamos el archivo por ID y lo eliminaríamos
tracing::info!("File deletion simulated successfully");
async fn move_file(
&self,
file_id: &str,
target_folder_id: Option<String>,
) -> Result<File, DomainError> {
// Get original file
let original_path = self.id_mapping_service.get_path_by_id(file_id).await?;
let old_abs = self.resolve_storage_path(&original_path);
if !old_abs.exists() || !old_abs.is_file() {
return Err(DomainError::not_found("File", file_id.to_string()));
}
let (size, created_at, modified_at) = self.get_file_metadata_raw(&old_abs).await.map_err(map_repo_err)?;
let name = original_path.file_name()
.ok_or_else(|| DomainError::internal_error("File", "Invalid path"))?;
let mime = from_path(&old_abs).first_or_octet_stream().to_string();
// Build target path
let target_folder_path = self.resolve_folder_path(&target_folder_id).await;
let new_storage_path = target_folder_path.join(&name);
if self.file_exists_at_storage_path(&new_storage_path).await.map_err(map_repo_err)? {
return Err(DomainError::already_exists("File",
format!("File already exists at {}", new_storage_path.to_string())));
}
let new_abs = self.resolve_storage_path(&new_storage_path);
self.ensure_parent_directory(&new_abs).await.map_err(map_repo_err)?;
// Rename
time::timeout(
self.config.timeouts.file_timeout(),
FileSystemUtils::rename_with_sync(&old_abs, &new_abs),
).await
.map_err(|_| DomainError::internal_error("File", "Timeout moving file"))?
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
// Update mapping
self.id_mapping_service.update_path(file_id, &new_storage_path).await?;
let _ = self.id_mapping_service.save_changes().await;
File::with_timestamps(file_id.to_string(), name, new_storage_path, size, mime, target_folder_id, created_at, modified_at)
.map_err(|e| DomainError::internal_error("File", e.to_string()))
}
async fn delete_file(&self, id: &str) -> Result<(), DomainError> {
let storage_path = self.id_mapping_service.get_path_by_id(id).await?;
let abs_path = self.resolve_storage_path(&storage_path);
self.metadata_cache.invalidate(&abs_path).await;
if let Some(parent) = abs_path.parent() {
self.metadata_cache.invalidate_directory(parent).await;
}
self.delete_file_non_blocking(abs_path).await.map_err(map_repo_err)?;
Ok(())
}
async fn get_folder_details(&self, folder_id: &str) -> Result<File, DomainError> {
// Fetch the folder information from the metadata manager
match self.metadata_manager.get_folder_by_id(folder_id).await {
Ok(folder) => Ok(folder),
Err(err) => {
tracing::warn!("Error getting folder details for ID {}: {}", folder_id, err);
Err(DomainError::not_found("Folder", folder_id.to_string()))
}
}
async fn update_file_content(&self, file_id: &str, content: Vec<u8>) -> Result<(), DomainError> {
let storage_path = self.id_mapping_service.get_path_by_id(file_id).await?;
let physical_path = self.resolve_storage_path(&storage_path);
FileSystemUtils::atomic_write(&physical_path, &content)
.await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
// Refresh cache
let _ = self.metadata_cache.refresh_metadata(&physical_path).await;
Ok(())
}
async fn get_folder_path_str(&self, folder_id: &str) -> Result<String, DomainError> {
// Fetch the folder information
let folder = self.get_folder_details(folder_id).await?;
// Convert StoragePath to string
let path_str = folder.storage_path().to_string();
tracing::debug!("Resolved folder path for ID {}: {}", folder_id, path_str);
Ok(path_str)
async fn register_file_deferred(
&self,
name: String,
folder_id: Option<String>,
content_type: String,
size: u64,
) -> Result<(File, PathBuf), DomainError> {
let folder_path = self.resolve_folder_path(&folder_id).await;
let (file_storage_path, actual_name) = self.unique_file_path(&folder_path, &name).await.map_err(map_repo_err)?;
let abs_path = self.resolve_storage_path(&file_storage_path);
self.ensure_parent_directory(&abs_path).await.map_err(map_repo_err)?;
let mime = if content_type.is_empty() {
from_path(&abs_path).first_or_octet_stream().to_string()
} else {
content_type
};
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let id = self.id_mapping_service.get_or_create_id(&file_storage_path).await
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
let _ = self.id_mapping_service.save_changes().await;
let file = File::with_timestamps(id.clone(), actual_name, file_storage_path, size, mime, folder_id, now, now)
.map_err(|e| DomainError::internal_error("File", e.to_string()))?;
tracing::debug!("⚡ Registered deferred file: {} -> {:?}", id, abs_path);
Ok((file, abs_path))
}
async fn move_to_trash(&self, file_id: &str) -> Result<(), DomainError> {
// Get the file's current path
let storage_path = self.id_mapping_service.get_path_by_id(file_id).await?;
let abs_path = self.resolve_storage_path(&storage_path);
if !abs_path.exists() || !abs_path.is_file() {
return Err(DomainError::not_found("File", file_id.to_string()));
}
// Create trash directory
let trash_dir = self.root_path.join(".trash").join("files");
fs::create_dir_all(&trash_dir).await
.map_err(|e| DomainError::internal_error("File", format!("Failed to create trash dir: {}", e)))?;
// Move file to trash
let trash_path = trash_dir.join(file_id);
fs::rename(&abs_path, &trash_path).await
.map_err(|e| DomainError::internal_error("File", format!("Failed to move file to trash: {}", e)))?;
// Update mapping to trash location
let trash_storage_path = StoragePath::from_string(&format!(".trash/files/{}", file_id));
self.id_mapping_service.update_path(file_id, &trash_storage_path).await?;
let _ = self.id_mapping_service.save_changes().await;
// Invalidate cache
self.metadata_cache.invalidate(&abs_path).await;
if let Some(parent) = abs_path.parent() {
self.metadata_cache.invalidate_directory(parent).await;
}
tracing::debug!("File moved to trash: {} -> {}", file_id, trash_path.display());
Ok(())
}
async fn restore_from_trash(&self, file_id: &str, original_path: &str) -> Result<(), DomainError> {
// Get current path (should be in trash)
let current_storage_path = self.id_mapping_service.get_path_by_id(file_id).await?;
let current_abs_path = self.resolve_storage_path(&current_storage_path);
if !current_abs_path.exists() {
return Err(DomainError::not_found("File", format!("File {} not found in trash", file_id)));
}
// Ensure parent directory exists for original location
let original_storage_path = StoragePath::from_string(original_path);
let original_abs_path = self.resolve_storage_path(&original_storage_path);
if let Some(parent) = original_abs_path.parent() {
fs::create_dir_all(parent).await
.map_err(|e| DomainError::internal_error("File", format!("Failed to create parent dir: {}", e)))?;
}
// Move file back to original location
fs::rename(&current_abs_path, &original_abs_path).await
.map_err(|e| DomainError::internal_error("File", format!("Failed to restore file: {}", e)))?;
// Update mapping back to original path
self.id_mapping_service.update_path(file_id, &original_storage_path).await?;
let _ = self.id_mapping_service.save_changes().await;
tracing::debug!("File restored from trash: {} -> {}", file_id, original_abs_path.display());
Ok(())
}
async fn delete_file_permanently(&self, file_id: &str) -> Result<(), DomainError> {
// Get current path (could be in trash or original location)
let storage_path = self.id_mapping_service.get_path_by_id(file_id).await?;
let abs_path = self.resolve_storage_path(&storage_path);
// Delete the physical file if it exists
if abs_path.exists() {
self.delete_file_non_blocking(abs_path.clone()).await.map_err(map_repo_err)?;
}
// Remove ID mapping
self.id_mapping_service.remove_id(file_id).await?;
let _ = self.id_mapping_service.save_changes().await;
// Invalidate cache
self.metadata_cache.invalidate(&abs_path).await;
tracing::debug!("File permanently deleted: {}", file_id);
Ok(())
}
}
@@ -1,223 +0,0 @@
use std::path::PathBuf;
use std::sync::Arc;
use tokio::time;
use tokio::fs;
use std::time::Duration;
use crate::infrastructure::services::file_metadata_cache::{FileMetadataCache, CacheEntryType, FileMetadata};
use crate::domain::entities::file::File;
use crate::domain::services::path_service::StoragePath;
use crate::common::config::AppConfig;
use crate::common::errors::DomainError;
/// Gestor de metadatos de archivos que encapsula la lógica de caché
pub struct FileMetadataManager {
metadata_cache: Arc<FileMetadataCache>,
config: AppConfig,
}
#[derive(Debug, thiserror::Error)]
pub enum MetadataError {
#[error("Error de E/S al acceder a los metadatos: {0}")]
IoError(#[from] std::io::Error),
#[error("Timeout al acceder a los metadatos: {0}")]
Timeout(String),
#[error("Metadatos no disponibles: {0}")]
Unavailable(String),
}
impl From<MetadataError> for DomainError {
fn from(err: MetadataError) -> Self {
match err {
MetadataError::IoError(e) => DomainError::internal_error("FileMetadata", e.to_string()),
MetadataError::Timeout(msg) => DomainError::internal_error("FileMetadata", msg),
MetadataError::Unavailable(msg) => DomainError::not_found("FileMetadata", msg),
}
}
}
impl FileMetadataManager {
/// Crea un nuevo gestor de metadatos
pub fn new(metadata_cache: Arc<FileMetadataCache>, config: AppConfig) -> Self {
Self {
metadata_cache,
config,
}
}
/// Crea un gestor por defecto para pruebas
pub fn default() -> Self {
Self {
metadata_cache: Arc::new(FileMetadataCache::default()),
config: AppConfig::default(),
}
}
/// Comprueba si un archivo existe en la ruta especificada con caché
pub async fn file_exists(&self, abs_path: &PathBuf) -> Result<bool, MetadataError> {
// Intentar obtener del caché avanzado primero
if let Some(is_file) = self.metadata_cache.is_file(&abs_path).await {
tracing::debug!("Metadata cache hit for existence check: {} - path: {}", is_file, abs_path.display());
return Ok(is_file);
}
// Si no está en caché, verificar directamente y actualizar caché
tracing::debug!("Metadata cache miss for existence check: {}", abs_path.display());
// Utilizar timeout para evitar bloqueo
match time::timeout(
self.config.timeouts.file_timeout(),
fs::metadata(&abs_path)
).await {
Ok(Ok(metadata)) => {
let is_file = metadata.is_file();
// Actualizar la caché con información fresca
if let Err(e) = self.metadata_cache.refresh_metadata(&abs_path).await {
tracing::warn!("Failed to update cache for {}: {}", abs_path.display(), e);
}
if is_file {
tracing::debug!("File exists and is accessible: {}", abs_path.display());
Ok(true)
} else {
tracing::warn!("Path exists but is not a file: {}", abs_path.display());
Ok(false)
}
},
Ok(Err(e)) => {
tracing::warn!("File check failed: {} - {}", abs_path.display(), e);
// Añadir a caché como no existente
let entry_type = CacheEntryType::Unknown;
let file_metadata = FileMetadata::new(
abs_path.clone(),
false,
entry_type,
None,
None,
None,
None,
Duration::from_millis(self.config.timeouts.file_operation_ms),
);
self.metadata_cache.update_cache(file_metadata).await;
Ok(false)
},
Err(_) => {
tracing::warn!("Timeout checking file metadata: {}", abs_path.display());
Err(MetadataError::Timeout(format!("Timeout checking file: {}", abs_path.display())))
}
}
}
/// Obtiene metadatos de archivo (tamaño, fechas creación/modificación) con caché
pub async fn get_file_metadata(&self, abs_path: &PathBuf) -> Result<(u64, u64, u64), MetadataError> {
// Intentar obtener de caché primero
if let Some(cached_metadata) = self.metadata_cache.get_metadata(abs_path).await {
if let (Some(size), Some(created_at), Some(modified_at)) =
(cached_metadata.size, cached_metadata.created_at, cached_metadata.modified_at) {
tracing::debug!("Using cached metadata for: {}", abs_path.display());
return Ok((size, created_at, modified_at));
}
}
// Si no está en caché o metadatos incompletos, cargar desde sistema de archivos
let metadata = match time::timeout(
self.config.timeouts.file_timeout(),
fs::metadata(&abs_path)
).await {
Ok(Ok(metadata)) => metadata,
Ok(Err(e)) => return Err(MetadataError::IoError(e)),
Err(_) => return Err(MetadataError::Timeout(
format!("Timeout getting metadata for: {}", abs_path.display())
)),
};
let size = metadata.len();
// Get creation timestamp
let created_at = metadata.created()
.map(|time| time.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs())
.unwrap_or_else(|_| 0);
// Get modification timestamp
let modified_at = metadata.modified()
.map(|time| time.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs())
.unwrap_or_else(|_| 0);
// Actualizar caché si es posible
if let Err(e) = self.metadata_cache.refresh_metadata(abs_path).await {
tracing::warn!("Failed to update metadata cache for {}: {}", abs_path.display(), e);
}
Ok((size, created_at, modified_at))
}
/// Invalida la entrada de caché para un archivo
pub async fn invalidate(&self, abs_path: &PathBuf) {
self.metadata_cache.invalidate(abs_path).await;
}
/// Invalida la entrada de caché para un directorio y su contenido
pub async fn invalidate_directory(&self, dir_path: &PathBuf) {
self.metadata_cache.invalidate_directory(dir_path).await;
}
/// Actualiza los metadatos de un archivo en la caché
pub async fn update_file_metadata(&self, file: &crate::domain::entities::file::File) -> Result<(), MetadataError> {
// Crear una ruta absoluta para el archivo
let abs_path = PathBuf::from(format!("{}/{}", self.config.storage_path.display(), file.storage_path().to_string()));
// Crear un objeto FileMetadata
let metadata = FileMetadataCache::create_metadata_from_file(file, abs_path.clone());
// Actualizar la caché
self.metadata_cache.update_cache(metadata).await;
Ok(())
}
/// Obtiene información de una carpeta por ID
pub async fn get_folder_by_id(&self, folder_id: &str) -> Result<File, MetadataError> {
// Implementación simplificada que solo busca en la caché de metadatos
// pero que devuelve una estructura mínima para el servicio de uso de almacenamiento
// En una implementación real, se consultaría un índice persistente
// Para esta implementación básica, usaremos un método simplificado
// Crear un objeto StoragePath mínimo
let storage_path = StoragePath::from_string(&format!("/{}", folder_id));
// Creamos una carpeta con información mínima
// Esta implementación es un placeholder - en una situación real
// consultaríamos el mapa folder_id -> folder_metadata en el sistema
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
// Verificamos si el nombre contiene información del usuario
let folder_name = if folder_id.contains('-') {
// Asumimos un formato UUID v4, intentamos usar "Mi Carpeta - username" como nombre
format!("Mi Carpeta - usuario")
} else {
// Si no, usamos el ID como nombre
folder_id.to_string()
};
let folder = File::new_folder(
folder_id.to_string(),
folder_name,
storage_path,
None, // parent_id
now, // created_at
now, // updated_at
)
.map_err(|e| MetadataError::Unavailable(format!("Error creating folder entity: {}", e)))?;
Ok(folder)
}
}
@@ -1,132 +0,0 @@
use std::path::PathBuf;
use std::sync::Arc;
use async_trait::async_trait;
use crate::domain::services::path_service::StoragePath;
use crate::infrastructure::services::path_service::PathService;
use crate::application::services::storage_mediator::StorageMediator;
// use crate::application::ports::outbound::IdMappingPort;
use crate::domain::repositories::file_repository::FileRepositoryError;
use crate::common::errors::DomainError;
use crate::application::ports::storage_ports::FilePathResolutionPort;
/// Resuelve rutas de archivos y gestiona el mapeo de IDs a rutas
pub struct FilePathResolver {
path_service: Arc<PathService>,
storage_mediator: Arc<dyn StorageMediator>,
id_mapping_service: Arc<dyn crate::application::ports::outbound::IdMappingPort>,
}
impl FilePathResolver {
/// Crea un nuevo resolver de rutas
pub fn new(
path_service: Arc<PathService>,
storage_mediator: Arc<dyn StorageMediator>,
id_mapping_service: Arc<dyn crate::application::ports::outbound::IdMappingPort>,
) -> Self {
Self {
path_service,
storage_mediator,
id_mapping_service,
}
}
/// Crea un resolver de rutas de prueba
pub fn default_stub() -> Self {
let path_service = Arc::new(PathService::new(PathBuf::from("./storage")));
// Create dummy implementation of IdMappingPort
struct DummyIdMappingService;
#[async_trait::async_trait]
impl crate::application::ports::outbound::IdMappingPort for DummyIdMappingService {
async fn get_or_create_id(&self, _path: &StoragePath) -> Result<String, DomainError> {
Ok("dummy-id".to_string())
}
async fn get_path_by_id(&self, _id: &str) -> Result<StoragePath, DomainError> {
Ok(StoragePath::from_string("/"))
}
async fn update_path(&self, _id: &str, _new_path: &StoragePath) -> Result<(), DomainError> {
Ok(())
}
async fn remove_id(&self, _id: &str) -> Result<(), DomainError> {
Ok(())
}
async fn save_changes(&self) -> Result<(), DomainError> {
Ok(())
}
}
Self {
path_service: path_service.clone(),
storage_mediator: Arc::new(crate::application::services::storage_mediator::FileSystemStorageMediator::new_stub()),
id_mapping_service: Arc::new(DummyIdMappingService) as Arc<dyn crate::application::ports::outbound::IdMappingPort>,
}
}
/// Resuelve una ruta de dominio a una ruta física absoluta
pub fn resolve_storage_path(&self, storage_path: &StoragePath) -> PathBuf {
self.path_service.resolve_path(storage_path)
}
/// Resuelve la ruta de un archivo (alias para resolve_storage_path)
pub fn resolve_file_path(&self, storage_path: &StoragePath) -> PathBuf {
self.resolve_storage_path(storage_path)
}
/// Resuelve una ruta PathBuf a una ruta física absoluta (legacy)
pub fn resolve_legacy_path(&self, relative_path: &std::path::Path) -> PathBuf {
self.storage_mediator.resolve_path(relative_path)
}
/// Obtiene la ruta de un archivo por su ID
pub async fn get_path_by_id(&self, id: &str) -> Result<StoragePath, FileRepositoryError> {
self.id_mapping_service.get_path_by_id(id).await
.map_err(|e| FileRepositoryError::IdMappingError(e.to_string()))
}
/// Actualiza la ruta para un ID existente
pub async fn update_path(&self, id: &str, storage_path: &StoragePath) -> Result<(), FileRepositoryError> {
self.id_mapping_service.update_path(id, storage_path).await
.map_err(|e| FileRepositoryError::IdMappingError(e.to_string()))
}
/// Obtiene o crea un ID para una ruta
pub async fn get_or_create_id(&self, storage_path: &StoragePath) -> Result<String, FileRepositoryError> {
self.id_mapping_service.get_or_create_id(storage_path).await
.map_err(|e| FileRepositoryError::IdMappingError(e.to_string()))
}
/// Elimina un ID del mapeo
pub async fn remove_id(&self, id: &str) -> Result<(), FileRepositoryError> {
self.id_mapping_service.remove_id(id).await
.map_err(|e| FileRepositoryError::IdMappingError(e.to_string()))
}
/// Guarda cambios pendientes
pub async fn save_changes(&self) -> Result<(), FileRepositoryError> {
self.id_mapping_service.save_changes().await
.map_err(|e| FileRepositoryError::IdMappingError(e.to_string()))
}
}
// Implementación de FilePathResolutionPort
#[async_trait]
impl FilePathResolutionPort for FilePathResolver {
async fn get_file_path(&self, id: &str) -> Result<StoragePath, DomainError> {
self.get_path_by_id(id).await
.map_err(|e| match e {
FileRepositoryError::NotFound(id) => DomainError::not_found("File", id),
FileRepositoryError::IoError(e) => DomainError::internal_error("FilePath", e.to_string()),
FileRepositoryError::Timeout(msg) => DomainError::internal_error("FilePath", msg),
_ => DomainError::internal_error("FilePath", e.to_string()),
})
}
fn resolve_path(&self, storage_path: &StoragePath) -> PathBuf {
self.resolve_storage_path(storage_path)
}
}
@@ -4,18 +4,17 @@ use std::time::Duration;
use async_trait::async_trait;
use tokio::fs;
use tokio::time::timeout;
use tracing::instrument;
use crate::domain::entities::folder::{Folder, FolderError};
use crate::domain::repositories::folder_repository::{
FolderRepository, FolderRepositoryError, FolderRepositoryResult
use crate::infrastructure::repositories::repository_errors::{
FolderRepositoryError, FolderRepositoryResult
};
use crate::domain::services::path_service::StoragePath;
use crate::infrastructure::services::path_service::PathService;
// use crate::application::ports::outbound::IdMappingPort;
use crate::infrastructure::services::id_mapping_service::{IdMappingService, IdMappingError};
use crate::application::services::storage_mediator::StorageMediator;
use crate::application::ports::outbound::FolderStoragePort;
use crate::domain::repositories::folder_repository::FolderRepository;
use crate::common::errors::DomainError;
// To be able to use streams in the list_folders function
@@ -88,7 +87,7 @@ impl FolderFsRepository {
match read_dir_result {
Ok(result) => {
let mut entries = result.map_err(FolderRepositoryError::IoError)?;
let mut entries = result.map_err(|e| FolderRepositoryError::StorageError(e.to_string()))?;
let mut count = 0;
// Count entries manually
@@ -111,20 +110,22 @@ impl FolderFsRepository {
self.path_service.resolve_path(storage_path)
}
/// Resolves a legacy PathBuf to an absolute filesystem path
fn resolve_legacy_path(&self, relative_path: &std::path::Path) -> PathBuf {
self.storage_mediator.resolve_path(relative_path)
}
/// Returns a reference to the ID mapping service
pub fn id_mapping_service(&self) -> &Arc<dyn crate::application::ports::outbound::IdMappingPort> {
&self.id_mapping_service
}
/// Gets the storage path for a folder by its ID (internal helper)
async fn _get_folder_storage_path(&self, id: &str) -> FolderRepositoryResult<StoragePath> {
let storage_path = self.id_mapping_service.get_path_by_id(id).await
.map_err(FolderRepositoryError::from)?;
Ok(storage_path)
}
/// Gets a folder path from the ID mapping service
pub async fn get_mapped_folder_path(&self, folder_id: &str) -> FolderRepositoryResult<String> {
let storage_path = self.id_mapping_service.get_path_by_id(folder_id).await
.map_err(|e| FolderRepositoryError::MappingError(format!("Failed to get folder path: {}", e)))?;
.map_err(|e| FolderRepositoryError::StorageError(format!("Failed to get folder path: {}", e)))?;
Ok(storage_path.to_string())
}
@@ -132,13 +133,13 @@ impl FolderFsRepository {
pub async fn update_mapped_folder_path(&self, folder_id: &str, new_path: &PathBuf) -> FolderRepositoryResult<()> {
let storage_path = StoragePath::from_string(&new_path.to_string_lossy().to_string());
self.id_mapping_service.update_path(folder_id, &storage_path).await
.map_err(|e| FolderRepositoryError::MappingError(format!("Failed to update folder path: {}", e)))
.map_err(|e| FolderRepositoryError::StorageError(format!("Failed to update folder path: {}", e)))
}
/// Removes a folder ID from the ID mapping service
pub async fn remove_mapped_folder_id(&self, folder_id: &str) -> FolderRepositoryResult<()> {
self.id_mapping_service.remove_id(folder_id).await
.map_err(|e| FolderRepositoryError::MappingError(format!("Failed to remove folder ID: {}", e)))
.map_err(|e| FolderRepositoryError::StorageError(format!("Failed to remove folder ID: {}", e)))
}
/// Checks if a folder exists at a given storage path
@@ -199,7 +200,7 @@ impl FolderFsRepository {
/// Extracts folder metadata from a physical path
async fn get_folder_metadata(&self, abs_path: &PathBuf) -> FolderRepositoryResult<(u64, u64)> {
let metadata = fs::metadata(&abs_path).await
.map_err(FolderRepositoryError::IoError)?;
.map_err(|e| FolderRepositoryError::StorageError(e.to_string()))?;
// Get creation timestamp
let created_at = metadata.created()
@@ -220,43 +221,9 @@ impl From<IdMappingError> for FolderRepositoryError {
fn from(err: IdMappingError) -> Self {
match err {
IdMappingError::NotFound(id) => FolderRepositoryError::NotFound(id),
IdMappingError::IoError(e) => FolderRepositoryError::IoError(e),
IdMappingError::Timeout(msg) => FolderRepositoryError::Other(format!("Timeout: {}", msg)),
_ => FolderRepositoryError::MappingError(err.to_string()),
}
}
}
// Convert FolderRepositoryError to DomainError
impl From<FolderRepositoryError> for DomainError {
fn from(err: FolderRepositoryError) -> Self {
match err {
FolderRepositoryError::NotFound(id) => {
DomainError::not_found("Folder", id)
},
FolderRepositoryError::AlreadyExists(path) => {
DomainError::already_exists("Folder", path)
},
FolderRepositoryError::InvalidPath(path) => {
DomainError::validation_error(format!("Invalid path: {}", path))
},
FolderRepositoryError::IoError(e) => {
DomainError::internal_error("Folder", format!("IO error: {}", e))
.with_source(e)
},
FolderRepositoryError::ValidationError(msg) => {
DomainError::validation_error(msg)
},
FolderRepositoryError::MappingError(msg) => {
DomainError::internal_error("Folder", format!("Mapping error: {}", msg))
},
FolderRepositoryError::Other(msg) => {
DomainError::internal_error("Folder", msg)
},
FolderRepositoryError::OperationNotSupported(msg) => {
DomainError::operation_not_supported("Folder", msg)
},
FolderRepositoryError::DomainError(e) => e,
IdMappingError::IoError(e) => FolderRepositoryError::StorageError(e.to_string()),
IdMappingError::Timeout(msg) => FolderRepositoryError::StorageError(format!("Timeout: {}", msg)),
_ => FolderRepositoryError::StorageError(err.to_string()),
}
}
}
@@ -274,88 +241,20 @@ impl Clone for FolderFsRepository {
}
}
#[async_trait]
impl FolderStoragePort for FolderFsRepository {
async fn create_folder(&self, name: String, parent_id: Option<String>) -> Result<Folder, DomainError> {
FolderRepository::create_folder(self, name, parent_id).await.map_err(DomainError::from)
}
async fn get_folder(&self, id: &str) -> Result<Folder, DomainError> {
FolderRepository::get_folder_by_id(self, id).await.map_err(DomainError::from)
}
async fn get_folder_by_path(&self, storage_path: &StoragePath) -> Result<Folder, DomainError> {
FolderRepository::get_folder_by_storage_path(self, storage_path).await.map_err(DomainError::from)
}
async fn list_folders(&self, parent_id: Option<&str>) -> Result<Vec<Folder>, DomainError> {
FolderRepository::list_folders(self, parent_id).await.map_err(DomainError::from)
}
async fn rename_folder(&self, id: &str, new_name: String) -> Result<Folder, DomainError> {
FolderRepository::rename_folder(self, id, new_name).await.map_err(DomainError::from)
}
async fn move_folder(&self, id: &str, new_parent_id: Option<&str>) -> Result<Folder, DomainError> {
FolderRepository::move_folder(self, id, new_parent_id).await.map_err(DomainError::from)
}
async fn delete_folder(&self, id: &str) -> Result<(), DomainError> {
FolderRepository::delete_folder(self, id).await.map_err(DomainError::from)
}
async fn folder_exists(&self, storage_path: &StoragePath) -> Result<bool, DomainError> {
FolderRepository::folder_exists_at_storage_path(self, storage_path).await.map_err(DomainError::from)
}
async fn get_folder_path(&self, id: &str) -> Result<StoragePath, DomainError> {
FolderRepository::get_folder_storage_path(self, id).await.map_err(DomainError::from)
}
async fn list_folders_paginated(
&self,
parent_id: Option<&str>,
offset: usize,
limit: usize,
include_total: bool
) -> Result<(Vec<Folder>, Option<usize>), DomainError> {
FolderRepository::list_folders_paginated(self, parent_id, offset, limit, include_total)
.await
.map_err(DomainError::from)
}
}
#[async_trait]
impl FolderRepository for FolderFsRepository {
#[instrument(skip(self))]
async fn move_to_trash(&self, folder_id: &str) -> FolderRepositoryResult<()> {
// Use the private implementation from folder_fs_repository_trash.rs
self._trash_move_to_trash(folder_id).await
}
#[instrument(skip(self))]
async fn restore_from_trash(&self, folder_id: &str, original_path: &str) -> FolderRepositoryResult<()> {
// Use the private implementation from folder_fs_repository_trash.rs
self._trash_restore_from_trash(folder_id, original_path).await
}
#[instrument(skip(self))]
async fn delete_folder_permanently(&self, folder_id: &str) -> FolderRepositoryResult<()> {
// Use the private implementation from folder_fs_repository_trash.rs
self._trash_delete_folder_permanently(folder_id).await
}
async fn create_folder(&self, name: String, parent_id: Option<String>) -> FolderRepositoryResult<Folder> {
async fn create_folder(&self, name: String, parent_id: Option<String>) -> Result<Folder, DomainError> {
// Get the parent folder path (if any)
let parent_storage_path = match &parent_id {
Some(id) => {
match self.get_folder_storage_path(id).await {
match self._get_folder_storage_path(id).await {
Ok(path) => {
tracing::info!("Using folder path: {:?} for parent_id: {:?}", path.to_string(), id);
Some(path)
},
Err(e) => {
tracing::error!("Error getting parent folder: {}", e);
return Err(e);
return Err(DomainError::from(e));
},
}
},
@@ -370,27 +269,28 @@ impl FolderRepository for FolderFsRepository {
tracing::info!("Creating folder at path: {:?}", folder_storage_path.to_string());
// Check if folder already exists
if self.folder_exists_at_storage_path(&folder_storage_path).await? {
return Err(FolderRepositoryError::AlreadyExists(folder_storage_path.to_string()));
if self.check_folder_exists_at_storage_path(&folder_storage_path).await.map_err(DomainError::from)? {
return Err(DomainError::already_exists("Folder", folder_storage_path.to_string()));
}
// Create the physical directory
let abs_path = self.resolve_storage_path(&folder_storage_path);
self.create_directory(&abs_path).await
.map_err(FolderRepositoryError::IoError)?;
.map_err(|e| DomainError::internal_error("Folder", e.to_string()))?;
// Create and return the folder entity with a persisted ID
let id = self.id_mapping_service.get_or_create_id(&folder_storage_path).await?;
let id = self.id_mapping_service.get_or_create_id(&folder_storage_path).await
.map_err(|e| DomainError::internal_error("Folder", e.to_string()))?;
let folder = self.create_folder_entity(
id.clone(), // Clone for logging
name.clone(), // Clone name for logging
folder_storage_path.clone(), // Clone for logging
parent_id.clone(), // Clone for logging
id.clone(),
name.clone(),
folder_storage_path.clone(),
parent_id.clone(),
None,
None,
).await?;
).await.map_err(DomainError::from)?;
// Ensure ID mapping is persisted - this is critical for later retrieval
// Ensure ID mapping is persisted
let save_result = self.id_mapping_service.save_changes().await;
if let Err(e) = &save_result {
tracing::error!("Failed to save ID mapping for folder {}: {}", id, e);
@@ -404,38 +304,36 @@ impl FolderRepository for FolderFsRepository {
Ok(folder)
}
async fn get_folder_by_id(&self, id: &str) -> FolderRepositoryResult<Folder> {
async fn get_folder(&self, id: &str) -> Result<Folder, DomainError> {
tracing::debug!("Looking for folder with ID: {}", id);
// Find path by ID using the mapping service
let storage_path = self.id_mapping_service.get_path_by_id(id).await
.map_err(FolderRepositoryError::from)?;
let storage_path = self.id_mapping_service.get_path_by_id(id).await?;
// Check if folder exists physically
let abs_path = self.resolve_storage_path(&storage_path);
if !abs_path.exists() || !abs_path.is_dir() {
tracing::error!("Folder not found at path: {}", abs_path.display());
return Err(FolderRepositoryError::NotFound(format!("Folder {} not found at {}", id, storage_path.to_string())));
return Err(DomainError::not_found("Folder", format!("Folder {} not found at {}", id, storage_path.to_string())));
}
// Get folder metadata
let (created_at, modified_at) = self.get_folder_metadata(&abs_path).await?;
let (created_at, modified_at) = self.get_folder_metadata(&abs_path).await.map_err(DomainError::from)?;
// Get folder name from the storage path
let name = match storage_path.file_name() {
Some(name) => name,
None => {
tracing::error!("Invalid folder path: {}", storage_path.to_string());
return Err(FolderRepositoryError::InvalidPath(storage_path.to_string()));
return Err(DomainError::validation_error(format!("Invalid path: {}", storage_path.to_string())));
}
};
// Determine parent ID if any
let parent = storage_path.parent();
let parent_id: Option<String> = if parent.is_none() || parent.as_ref().unwrap().is_empty() {
None // Root folder
None
} else {
// Try to get the parent ID from the mapping service
match self.id_mapping_service.get_or_create_id(parent.as_ref().unwrap()).await {
Ok(pid) => Some(pid),
Err(_) => None,
@@ -450,32 +348,31 @@ impl FolderRepository for FolderFsRepository {
parent_id,
Some(created_at),
Some(modified_at),
).await?;
).await.map_err(DomainError::from)?;
Ok(folder)
}
async fn get_folder_by_storage_path(&self, storage_path: &StoragePath) -> FolderRepositoryResult<Folder> {
async fn get_folder_by_path(&self, storage_path: &StoragePath) -> Result<Folder, DomainError> {
// Check if the physical directory exists
let abs_path = self.resolve_storage_path(storage_path);
if !abs_path.exists() || !abs_path.is_dir() {
return Err(FolderRepositoryError::NotFound(storage_path.to_string()));
return Err(DomainError::not_found("Folder", storage_path.to_string()));
}
// Extract folder name from storage path
let name = match storage_path.file_name() {
Some(name) => name,
None => {
return Err(FolderRepositoryError::InvalidPath(storage_path.to_string()));
return Err(DomainError::validation_error(format!("Invalid path: {}", storage_path.to_string())));
}
};
// Determine parent ID if any
let parent = storage_path.parent();
let parent_id: Option<String> = if parent.is_none() || parent.as_ref().unwrap().is_empty() {
None // Root folder
None
} else {
// Try to get the parent ID from the mapping service
match self.id_mapping_service.get_or_create_id(parent.as_ref().unwrap()).await {
Ok(pid) => Some(pid),
Err(_) => None,
@@ -483,7 +380,7 @@ impl FolderRepository for FolderFsRepository {
};
// Get folder metadata
let (created_at, modified_at) = self.get_folder_metadata(&abs_path).await?;
let (created_at, modified_at) = self.get_folder_metadata(&abs_path).await.map_err(DomainError::from)?;
// Get or create an ID for this path
let id = self.id_mapping_service.get_or_create_id(storage_path).await?;
@@ -497,7 +394,7 @@ impl FolderRepository for FolderFsRepository {
parent_id,
Some(created_at),
Some(modified_at),
).await?;
).await.map_err(DomainError::from)?;
// Ensure ID mapping is persisted
self.id_mapping_service.save_changes().await?;
@@ -505,8 +402,8 @@ impl FolderRepository for FolderFsRepository {
Ok(folder)
}
async fn list_folders(&self, parent_id: Option<&str>) -> FolderRepositoryResult<Vec<Folder>> {
use futures::stream::{StreamExt};
async fn list_folders(&self, parent_id: Option<&str>) -> Result<Vec<Folder>, DomainError> {
use futures::stream::StreamExt;
use tokio::time::{timeout, Duration};
tracing::info!("Listing folders in parent_id: {:?}", parent_id);
@@ -514,7 +411,7 @@ impl FolderRepository for FolderFsRepository {
// Get the parent storage path
let parent_storage_path = match parent_id {
Some(id) => {
match self.get_folder_storage_path(id).await {
match self._get_folder_storage_path(id).await {
Ok(path) => {
tracing::info!("Found parent folder with path: {:?}", path.to_string());
path
@@ -538,21 +435,20 @@ impl FolderRepository for FolderFsRepository {
return Ok(Vec::new());
}
// Read the directory with a timeout to avoid indefinite blocking
// Read the directory with a timeout
let read_dir_timeout = Duration::from_secs(30);
let read_dir_result = match timeout(
read_dir_timeout,
fs::read_dir(&abs_parent_path)
).await {
Ok(result) => result.map_err(FolderRepositoryError::IoError)?,
Ok(result) => result.map_err(|e| DomainError::internal_error("Folder", e.to_string()))?,
Err(_) => {
return Err(FolderRepositoryError::Other(
return Err(DomainError::internal_error("Folder",
format!("Timeout reading directory: {}", abs_parent_path.display())
));
}
};
// Process each entry sequentially to avoid async block type issues
let mut folders = Vec::new();
let mut entries = tokio_stream::wrappers::ReadDirStream::new(read_dir_result);
@@ -568,27 +464,22 @@ impl FolderRepository for FolderFsRepository {
let metadata = match entry.metadata().await {
Ok(m) => m,
Err(err) => {
tracing::error!("Error getting metadata for {}: {}",
entry.path().display(), err);
tracing::error!("Error getting metadata for {}: {}", entry.path().display(), err);
continue;
}
};
// Skip if not a directory
if !metadata.is_dir() {
continue;
}
let folder_name = entry.file_name().to_string_lossy().to_string();
// Create the storage path for this folder
let folder_storage_path = parent_storage_path.join(&folder_name);
// Try to get the folder by its storage path with timeout
let get_folder_timeout = Duration::from_secs(5);
let folder_result = timeout(
get_folder_timeout,
self.get_folder_by_storage_path(&folder_storage_path)
self.get_folder_by_path(&folder_storage_path)
).await;
match folder_result {
@@ -609,7 +500,6 @@ impl FolderRepository for FolderFsRepository {
}
}
// Persist any new ID mappings that were created
if let Err(e) = self.id_mapping_service.save_changes().await {
tracing::error!("Failed to save ID mappings: {}", e);
}
@@ -624,21 +514,17 @@ impl FolderRepository for FolderFsRepository {
offset: usize,
limit: usize,
include_total: bool
) -> FolderRepositoryResult<(Vec<Folder>, Option<usize>)> {
) -> Result<(Vec<Folder>, Option<usize>), DomainError> {
use futures::stream::StreamExt;
use tokio::time::{timeout, Duration};
tracing::info!("Listing folders in parent_id: {:?} with pagination (offset={}, limit={})",
parent_id, offset, limit);
// Get the parent storage path
let parent_storage_path = match parent_id {
Some(id) => {
match self.get_folder_storage_path(id).await {
Ok(path) => {
tracing::info!("Found parent folder with path: {:?}", path.to_string());
path
},
match self._get_folder_storage_path(id).await {
Ok(path) => path,
Err(e) => {
tracing::error!("Error getting parent folder by ID: {}: {}", id, e);
return Ok((Vec::new(), Some(0)));
@@ -648,17 +534,12 @@ impl FolderRepository for FolderFsRepository {
None => StoragePath::root(),
};
// Get the absolute folder path
let abs_parent_path = self.resolve_storage_path(&parent_storage_path);
tracing::info!("Absolute parent path: {:?}", abs_parent_path);
// Ensure the directory exists
if !abs_parent_path.exists() || !abs_parent_path.is_dir() {
tracing::error!("Directory does not exist or is not a directory: {:?}", abs_parent_path);
return Ok((Vec::new(), Some(0)));
}
// Get total count if requested
let total_count = if include_total {
match self.count_directory_items(&abs_parent_path).await {
Ok(count) => Some(count),
@@ -671,34 +552,29 @@ impl FolderRepository for FolderFsRepository {
None
};
// Read the directory with a timeout to avoid indefinite blocking
let read_dir_timeout = Duration::from_secs(30);
let read_dir_result = match timeout(
read_dir_timeout,
fs::read_dir(&abs_parent_path)
).await {
Ok(result) => result.map_err(FolderRepositoryError::IoError)?,
Ok(result) => result.map_err(|e| DomainError::internal_error("Folder", e.to_string()))?,
Err(_) => {
return Err(FolderRepositoryError::Other(
return Err(DomainError::internal_error("Folder",
format!("Timeout reading directory: {}", abs_parent_path.display())
));
}
};
// Process entries sequentially to avoid async block typing issues
let mut entries = tokio_stream::wrappers::ReadDirStream::new(read_dir_result);
let mut folders = Vec::new();
let mut current_idx = 0;
// Loop through entries, applying pagination manually
while let Some(entry_result) = entries.next().await {
// Skip entries before offset
if current_idx < offset {
current_idx += 1;
continue;
}
// Stop after reaching limit
if folders.len() >= limit {
break;
}
@@ -712,7 +588,6 @@ impl FolderRepository for FolderFsRepository {
}
};
// Check if it's a directory
let file_type = match entry.file_type().await {
Ok(ft) => ft,
Err(e) => {
@@ -727,7 +602,6 @@ impl FolderRepository for FolderFsRepository {
continue;
}
// Get the path and convert to StoragePath
let path = entry.path();
let rel_path = match path.strip_prefix(&self.root_path) {
Ok(rel) => StoragePath::from(rel.to_path_buf()),
@@ -738,10 +612,9 @@ impl FolderRepository for FolderFsRepository {
}
};
// Get the folder entity with timeout
let folder_result = timeout(
Duration::from_secs(10),
self.get_folder_by_storage_path(&rel_path)
self.get_folder_by_path(&rel_path)
).await;
match folder_result {
@@ -761,68 +634,54 @@ impl FolderRepository for FolderFsRepository {
current_idx += 1;
}
// Save ID mappings
if !folders.is_empty() {
if let Err(e) = self.id_mapping_service.save_changes().await {
tracing::error!("Error saving ID mappings: {}", e);
}
}
tracing::info!("Found {} folders in paginated request", folders.len());
Ok((folders, total_count))
}
async fn rename_folder(&self, id: &str, new_name: String) -> FolderRepositoryResult<Folder> {
// Get the original folder
let original_folder = self.get_folder_by_id(id).await?;
async fn rename_folder(&self, id: &str, new_name: String) -> Result<Folder, DomainError> {
let original_folder = self.get_folder(id).await?;
tracing::debug!("Renaming folder with ID: {}, Name: {}", id, original_folder.name());
// Create an immutable new version of the folder with updated name
let renamed_folder = original_folder.with_name(new_name)
.map_err(|e| FolderRepositoryError::ValidationError(e.to_string()))?;
.map_err(|e| DomainError::validation_error(e.to_string()))?;
// Check if target already exists
if self.folder_exists_at_storage_path(renamed_folder.storage_path()).await? {
return Err(FolderRepositoryError::AlreadyExists(renamed_folder.storage_path().to_string()));
if self.check_folder_exists_at_storage_path(renamed_folder.storage_path()).await.map_err(DomainError::from)? {
return Err(DomainError::already_exists("Folder", renamed_folder.storage_path().to_string()));
}
// Rename the physical directory
let abs_old_path = self.resolve_storage_path(original_folder.storage_path());
let abs_new_path = self.resolve_storage_path(renamed_folder.storage_path());
fs::rename(&abs_old_path, &abs_new_path).await
.map_err(FolderRepositoryError::IoError)?;
.map_err(|e| DomainError::internal_error("Folder", e.to_string()))?;
// Update the ID mapping
self.id_mapping_service.update_path(id, renamed_folder.storage_path()).await
.map_err(FolderRepositoryError::from)?;
// Save the updated mappings
self.id_mapping_service.update_path(id, renamed_folder.storage_path()).await?;
self.id_mapping_service.save_changes().await?;
tracing::debug!("Folder renamed successfully: ID={}, New name={}", id, renamed_folder.name());
Ok(renamed_folder)
}
async fn move_folder(&self, id: &str, new_parent_id: Option<&str>) -> FolderRepositoryResult<Folder> {
// Get the original folder
let original_folder = self.get_folder_by_id(id).await?;
async fn move_folder(&self, id: &str, new_parent_id: Option<&str>) -> Result<Folder, DomainError> {
let original_folder = self.get_folder(id).await?;
tracing::debug!("Moving folder with ID: {}, Name: {}", id, original_folder.name());
// If the target parent is the same as current, no need to move
if original_folder.parent_id() == new_parent_id {
tracing::info!("Folder is already in the target parent, no need to move");
return Ok(original_folder);
}
// Get the target parent path
let target_parent_storage_path = match new_parent_id {
Some(parent_id) => {
match self.get_folder_storage_path(parent_id).await {
match self._get_folder_storage_path(parent_id).await {
Ok(path) => Some(path),
Err(e) => {
return Err(FolderRepositoryError::Other(
return Err(DomainError::internal_error("Folder",
format!("Could not get target folder: {}", e)
));
}
@@ -831,68 +690,50 @@ impl FolderRepository for FolderFsRepository {
None => None
};
// Create an immutable new version of the folder with updated parent
let new_parent_id_option = new_parent_id.map(String::from);
let moved_folder = original_folder.with_parent(new_parent_id_option, target_parent_storage_path)
.map_err(|e| FolderRepositoryError::ValidationError(e.to_string()))?;
.map_err(|e| DomainError::validation_error(e.to_string()))?;
// Check if target already exists
if self.folder_exists_at_storage_path(moved_folder.storage_path()).await? {
return Err(FolderRepositoryError::AlreadyExists(
if self.check_folder_exists_at_storage_path(moved_folder.storage_path()).await.map_err(DomainError::from)? {
return Err(DomainError::already_exists("Folder",
format!("Folder already exists at destination: {}", moved_folder.storage_path().to_string())
));
}
// Move the physical directory
let old_abs_path = self.resolve_storage_path(original_folder.storage_path());
let new_abs_path = self.resolve_storage_path(moved_folder.storage_path());
// Ensure the target directory exists
if let Some(parent) = new_abs_path.parent() {
fs::create_dir_all(parent).await
.map_err(FolderRepositoryError::IoError)?;
.map_err(|e| DomainError::internal_error("Folder", e.to_string()))?;
}
// Move the directory physically (efficient rename operation)
fs::rename(&old_abs_path, &new_abs_path).await
.map_err(FolderRepositoryError::IoError)?;
.map_err(|e| DomainError::internal_error("Folder", e.to_string()))?;
tracing::info!("Folder moved successfully from {:?} to {:?}", old_abs_path, new_abs_path);
// Update the ID mapping
self.id_mapping_service.update_path(id, moved_folder.storage_path()).await
.map_err(FolderRepositoryError::from)?;
// Save the updated mappings
self.id_mapping_service.update_path(id, moved_folder.storage_path()).await?;
self.id_mapping_service.save_changes().await?;
tracing::debug!("Folder moved successfully: ID={}, New path={:?}", id, moved_folder.storage_path().to_string());
Ok(moved_folder)
}
async fn delete_folder(&self, id: &str) -> FolderRepositoryResult<()> {
async fn delete_folder(&self, id: &str) -> Result<(), DomainError> {
use tokio::time::{timeout, Duration};
// Get the folder first to check if it exists
let folder = self.get_folder_by_id(id).await?;
let folder = self.get_folder(id).await?;
let folder_name = folder.name().to_string();
let storage_path = folder.storage_path().clone();
tracing::info!("Deleting folder with ID: {}, Name: {}", id, folder_name);
// Para carpetas grandes, eliminar puede tomar tiempo
// Lo manejamos en un task separado para no bloquear
let abs_path = self.resolve_storage_path(&storage_path);
// Si la carpeta contiene muchos archivos, remove_dir_all puede tardar
// usamos tokio::spawn para hacerlo en un task separado
let path_for_display = abs_path.display().to_string();
let path_for_deletion = abs_path.clone();
let delete_task = tokio::spawn(async move {
tracing::debug!("Starting removal of folder: {}", path_for_display);
// Verificar si la carpeta existe y tiene muchas entradas
let path_for_counting = path_for_deletion.clone();
let entry_count = tokio::task::spawn_blocking(move || {
let mut count = 0;
@@ -900,19 +741,15 @@ impl FolderRepository for FolderFsRepository {
for _ in entries {
count += 1;
if count > 1000 {
break; // Solo necesitamos saber si es grande
break;
}
}
}
count
}).await.unwrap_or(0);
// Para carpetas muy grandes, usar remove_dir_all puede causar bloqueos
// Para carpetas pequeñas, usamos la versión asíncrona estándar
if entry_count > 1000 {
tracing::info!("Large folder detected with >1000 entries, using blocking removal");
// Para carpetas muy grandes, usamos spawn_blocking para no bloquear el runtime de tokio
let path_for_large_removal = path_for_deletion.clone();
tokio::task::spawn_blocking(move || {
if let Err(e) = std::fs::remove_dir_all(&path_for_large_removal) {
@@ -930,13 +767,11 @@ impl FolderRepository for FolderFsRepository {
))
})
} else {
tracing::debug!("Using async removal for folder with {} entries", entry_count);
fs::remove_dir_all(&path_for_deletion).await
}
});
// Esperar a que termine la eliminación con timeout
const DELETE_TIMEOUT_SECS: u64 = 60; // 1 minuto máximo para eliminar
const DELETE_TIMEOUT_SECS: u64 = 60;
let delete_result = timeout(
Duration::from_secs(DELETE_TIMEOUT_SECS),
@@ -948,29 +783,21 @@ impl FolderRepository for FolderFsRepository {
match task_result {
Ok(fs_result) => {
if let Err(e) = fs_result {
return Err(FolderRepositoryError::IoError(e));
return Err(DomainError::internal_error("Folder", e.to_string()));
}
},
Err(join_err) => {
return Err(FolderRepositoryError::Other(
return Err(DomainError::internal_error("Folder",
format!("Task panicked during folder deletion: {}", join_err)
));
}
}
},
Err(_) => {
// El timeout ocurrió, pero la tarea sigue ejecutándose en segundo plano
tracing::warn!("Timeout waiting for folder deletion, continuing with ID removal");
// No retornamos error, continuamos con la eliminación del ID
}
}
// Incluso si la eliminación física puede estar en progreso (timeout),
// procedemos a eliminar la entrada del mapping
// En el peor caso, si la eliminación física falla pero el ID se elimina,
// la carpeta se quedará huérfana, pero no afectará al sistema
// Remove the ID mapping con timeout
const MAPPING_TIMEOUT_SECS: u64 = 5;
let remove_id_result = timeout(
Duration::from_secs(MAPPING_TIMEOUT_SECS),
@@ -978,46 +805,37 @@ impl FolderRepository for FolderFsRepository {
).await;
match remove_id_result {
Ok(result) => result.map_err(FolderRepositoryError::from)?,
Ok(result) => result?,
Err(_) => {
return Err(FolderRepositoryError::Other(
return Err(DomainError::internal_error("Folder",
"Timeout removing folder ID from mapping".to_string()
));
}
}
// Save the updated mappings (asíncrono, no esperamos)
let _ = self.id_mapping_service.save_changes().await;
tracing::info!("Folder deleted successfully: ID={}, Name={}", id, folder_name);
Ok(())
}
async fn folder_exists_at_storage_path(&self, storage_path: &StoragePath) -> FolderRepositoryResult<bool> {
self.check_folder_exists_at_storage_path(storage_path).await
async fn folder_exists(&self, storage_path: &StoragePath) -> Result<bool, DomainError> {
self.check_folder_exists_at_storage_path(storage_path).await.map_err(DomainError::from)
}
async fn get_folder_storage_path(&self, id: &str) -> FolderRepositoryResult<StoragePath> {
// Use the ID mapping service to get the storage path
let storage_path = self.id_mapping_service.get_path_by_id(id).await
.map_err(FolderRepositoryError::from)?;
Ok(storage_path)
async fn get_folder_path(&self, id: &str) -> Result<StoragePath, DomainError> {
self._get_folder_storage_path(id).await.map_err(DomainError::from)
}
// Legacy method implementations
async fn folder_exists(&self, path: &std::path::PathBuf) -> FolderRepositoryResult<bool> {
let abs_path = self.resolve_legacy_path(path);
Ok(abs_path.exists() && abs_path.is_dir())
async fn move_to_trash(&self, folder_id: &str) -> Result<(), DomainError> {
self._trash_move_to_trash(folder_id).await.map_err(DomainError::from)
}
async fn get_folder_by_path(&self, path: &std::path::PathBuf) -> FolderRepositoryResult<Folder> {
// Convert PathBuf to StoragePath
let path_str = path.to_string_lossy().to_string();
let storage_path = StoragePath::from_string(&path_str);
// Use the new method
self.get_folder_by_storage_path(&storage_path).await
async fn restore_from_trash(&self, folder_id: &str, original_path: &str) -> Result<(), DomainError> {
self._trash_restore_from_trash(folder_id, original_path).await.map_err(DomainError::from)
}
async fn delete_folder_permanently(&self, folder_id: &str) -> Result<(), DomainError> {
self._trash_delete_folder_permanently(folder_id).await.map_err(DomainError::from)
}
}
@@ -2,7 +2,7 @@ use std::path::PathBuf;
use tokio::fs;
use tracing::{debug, error};
use crate::domain::repositories::folder_repository::FolderRepositoryResult;
use crate::infrastructure::repositories::repository_errors::FolderRepositoryResult;
use crate::infrastructure::repositories::folder_fs_repository::FolderFsRepository;
// Este archivo contiene la implementación de los métodos relacionados con la papelera
@@ -22,7 +22,7 @@ impl FolderFsRepository {
// Asegurarse que el directorio de la papelera existe
if !trash_dir.exists() {
fs::create_dir_all(&trash_dir).await
.map_err(|e| FolderRepositoryError::IoError(e))?;
.map_err(|e| FolderRepositoryError::StorageError(e.to_string()))?;
}
// Crear una ruta única para la carpeta en la papelera
@@ -72,7 +72,7 @@ impl FolderFsRepository {
},
Err(e) => {
error!("Error moviendo carpeta a papelera: {}", e);
Err(FolderRepositoryError::IoError(e))
Err(FolderRepositoryError::StorageError(e.to_string()))
}
}
}
@@ -99,7 +99,7 @@ impl FolderFsRepository {
fs::create_dir_all(parent).await
.map_err(|e| {
error!("Error creando directorio padre para restauración: {}", e);
FolderRepositoryError::IoError(e)
FolderRepositoryError::StorageError(e.to_string())
})?;
}
}
@@ -119,7 +119,7 @@ impl FolderFsRepository {
},
Err(e) => {
error!("Error restaurando carpeta: {}", e);
Err(FolderRepositoryError::IoError(e))
Err(FolderRepositoryError::StorageError(e.to_string()))
}
}
}
@@ -147,7 +147,7 @@ impl FolderFsRepository {
error!("Error eliminando carpeta permanentemente: {}", e);
// No reportar error si la carpeta ya no existe
if e.kind() != std::io::ErrorKind::NotFound {
return Err(FolderRepositoryError::IoError(e));
return Err(FolderRepositoryError::StorageError(e.to_string()));
}
}
}
@@ -165,4 +165,4 @@ impl FolderFsRepository {
}
// Re-exportaciones necesarias para el compilador
use crate::domain::repositories::folder_repository::FolderRepositoryError;
use crate::infrastructure::repositories::repository_errors::FolderRepositoryError;
+5 -7
View File
@@ -1,14 +1,13 @@
pub mod file_fs_repository;
pub mod folder_fs_repository;
pub mod parallel_file_processor;
pub mod repository_errors;
// Nuevos repositorios refactorizados
pub mod file_metadata_manager;
pub mod file_path_resolver;
// Repositorios CQRS (Read/Write) + composite
pub mod file_fs_read_repository;
pub mod file_fs_write_repository;
pub mod composite_file_repository;
pub mod trash_fs_repository;
pub mod file_fs_repository_trash;
pub mod folder_fs_repository_trash;
pub mod share_fs_repository;
@@ -16,8 +15,7 @@ pub mod share_fs_repository;
pub mod pg;
// Re-exportar para facilitar acceso
pub use file_metadata_manager::FileMetadataManager;
pub use file_path_resolver::FilePathResolver;
pub use file_fs_read_repository::FileFsReadRepository;
pub use file_fs_write_repository::FileFsWriteRepository;
pub use composite_file_repository::CompositeFileRepository;
pub use pg::{UserPgRepository, SessionPgRepository};
@@ -10,7 +10,7 @@ use tracing::{info, debug, error};
use bytes::{Bytes, BytesMut};
use crate::common::config::AppConfig;
use crate::domain::repositories::file_repository::FileRepositoryError;
use crate::infrastructure::repositories::repository_errors::FileRepositoryError;
use crate::infrastructure::services::buffer_pool::BufferPool;
/// Structure for the byte range to process
@@ -172,7 +172,7 @@ impl ParallelFileProcessor {
pub async fn read_file_parallel(&self, file_path: &PathBuf) -> Result<Vec<u8>, FileRepositoryError> {
// Get file size
let metadata = tokio::fs::metadata(file_path).await
.map_err(FileRepositoryError::IoError)?;
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
let file_size = metadata.len();
@@ -201,17 +201,17 @@ impl ParallelFileProcessor {
if buffer.capacity() < file_size as usize {
debug!("Buffer from pool too small ({}), using standard read", buffer.capacity());
let content = tokio::fs::read(file_path).await
.map_err(FileRepositoryError::IoError)?;
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
return Ok(content);
}
// Use memory buffer from the pool
let mut file = File::open(file_path).await
.map_err(FileRepositoryError::IoError)?;
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
let read_size = file.read(buffer.as_mut_slice()).await
.map_err(FileRepositoryError::IoError)?;
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
buffer.set_used(read_size);
@@ -221,7 +221,7 @@ impl ParallelFileProcessor {
} else {
// Standard implementation without pool
let content = tokio::fs::read(file_path).await
.map_err(FileRepositoryError::IoError)?;
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
return Ok(content);
}
@@ -241,7 +241,7 @@ impl ParallelFileProcessor {
// Open file once and share it
let file = Arc::new(File::open(file_path).await
.map_err(FileRepositoryError::IoError)?);
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?);
// Reference to BytesMut pool
let bytes_pool = self.bytes_pool.clone();
@@ -312,7 +312,7 @@ impl ParallelFileProcessor {
Ok(Ok(())) => {},
Ok(Err(e)) => {
error!("Error in chunk {}: {}", i, e);
return Err(FileRepositoryError::IoError(e));
return Err(FileRepositoryError::StorageError(e.to_string()));
},
Err(e) => {
error!("Task error in chunk {}: {}", i, e);
@@ -347,7 +347,7 @@ impl ParallelFileProcessor {
// Standard implementation (buffer pooling offers no advantages for simple writing)
tokio::fs::write(file_path, content).await
.map_err(FileRepositoryError::IoError)?;
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
return Ok(());
}
@@ -358,7 +358,7 @@ impl ParallelFileProcessor {
// Create file (we don't use Mutex to reduce contention)
let file = File::create(file_path).await
.map_err(FileRepositoryError::IoError)?;
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
// Convert content to Bytes (single copy step)
let content_bytes = Bytes::copy_from_slice(content);
@@ -369,7 +369,7 @@ impl ParallelFileProcessor {
// Process chunks in parallel
for chunk in chunks {
let file_clone = file.try_clone().await
.map_err(FileRepositoryError::IoError)?;
.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
let semaphore_clone = self.concurrency_limiter.clone();
// Create Bytes slice (doesn't copy data, only references)
@@ -406,7 +406,7 @@ impl ParallelFileProcessor {
Ok(Ok(())) => {},
Ok(Err(e)) => {
error!("Error in chunk {}: {}", i, e);
return Err(FileRepositoryError::IoError(e));
return Err(FileRepositoryError::StorageError(e.to_string()));
},
Err(e) => {
error!("Task error in chunk {}: {}", i, e);
@@ -417,7 +417,7 @@ impl ParallelFileProcessor {
// Ensure everything has been written correctly
let mut file_handle = file;
file_handle.flush().await.map_err(FileRepositoryError::IoError)?;
file_handle.flush().await.map_err(|e| FileRepositoryError::StorageError(e.to_string()))?;
info!("Successfully wrote file of {}MB in parallel with optimized Bytes", file_size / (1024 * 1024));
Ok(())
@@ -427,6 +427,7 @@ impl ParallelFileProcessor {
#[cfg(test)]
mod tests {
use super::*;
use bytes::BufMut;
use tempfile::tempdir;
#[tokio::test]
@@ -27,28 +27,28 @@ impl AddressBookRepository for AddressBookPgRepository {
RETURNING id, name, owner_id, description, color, is_public, created_at, updated_at
"#
)
.bind(address_book.id)
.bind(&address_book.name)
.bind(&address_book.owner_id)
.bind(&address_book.description)
.bind(&address_book.color)
.bind(address_book.is_public)
.bind(address_book.created_at)
.bind(address_book.updated_at)
.bind(address_book.id())
.bind(address_book.name())
.bind(address_book.owner_id())
.bind(address_book.description())
.bind(address_book.color())
.bind(address_book.is_public())
.bind(address_book.created_at())
.bind(address_book.updated_at())
.fetch_one(&*self.pool)
.await
.map_err(|e| DomainError::database_error(format!("Failed to create address book: {}", e)))?;
Ok(AddressBook {
id: row.get("id"),
name: row.get("name"),
owner_id: row.get("owner_id"),
description: row.get("description"),
color: row.get("color"),
is_public: row.get("is_public"),
created_at: row.get("created_at"),
updated_at: row.get("updated_at"),
})
Ok(AddressBook::from_raw(
row.get("id"),
row.get("name"),
row.get("owner_id"),
row.get("description"),
row.get("color"),
row.get("is_public"),
row.get("created_at"),
row.get("updated_at"),
))
}
async fn update_address_book(&self, address_book: AddressBook) -> AddressBookRepositoryResult<AddressBook> {
@@ -61,26 +61,26 @@ impl AddressBookRepository for AddressBookPgRepository {
RETURNING id, name, owner_id, description, color, is_public, created_at, updated_at
"#
)
.bind(&address_book.name)
.bind(&address_book.description)
.bind(&address_book.color)
.bind(address_book.is_public)
.bind(address_book.name())
.bind(address_book.description())
.bind(address_book.color())
.bind(address_book.is_public())
.bind(now)
.bind(address_book.id)
.bind(address_book.id())
.fetch_one(&*self.pool)
.await
.map_err(|e| DomainError::database_error(format!("Failed to update address book: {}", e)))?;
Ok(AddressBook {
id: row.get("id"),
name: row.get("name"),
owner_id: row.get("owner_id"),
description: row.get("description"),
color: row.get("color"),
is_public: row.get("is_public"),
created_at: row.get("created_at"),
updated_at: row.get("updated_at"),
})
Ok(AddressBook::from_raw(
row.get("id"),
row.get("name"),
row.get("owner_id"),
row.get("description"),
row.get("color"),
row.get("is_public"),
row.get("created_at"),
row.get("updated_at"),
))
}
async fn delete_address_book(&self, id: &Uuid) -> AddressBookRepositoryResult<()> {
@@ -111,16 +111,16 @@ impl AddressBookRepository for AddressBookPgRepository {
.await
.map_err(|e| DomainError::database_error(format!("Failed to get address book by id: {}", e)))?;
let result = maybe_row.map(|row| AddressBook {
id: row.get("id"),
name: row.get("name"),
owner_id: row.get("owner_id"),
description: row.get("description"),
color: row.get("color"),
is_public: row.get("is_public"),
created_at: row.get("created_at"),
updated_at: row.get("updated_at"),
});
let result = maybe_row.map(|row| AddressBook::from_raw(
row.get("id"),
row.get("name"),
row.get("owner_id"),
row.get("description"),
row.get("color"),
row.get("is_public"),
row.get("created_at"),
row.get("updated_at"),
));
Ok(result)
}
@@ -140,16 +140,16 @@ impl AddressBookRepository for AddressBookPgRepository {
.map_err(|e| DomainError::database_error(format!("Failed to get address books by owner: {}", e)))?;
let result = rows.into_iter()
.map(|row| AddressBook {
id: row.get("id"),
name: row.get("name"),
owner_id: row.get("owner_id"),
description: row.get("description"),
color: row.get("color"),
is_public: row.get("is_public"),
created_at: row.get("created_at"),
updated_at: row.get("updated_at"),
})
.map(|row| AddressBook::from_raw(
row.get("id"),
row.get("name"),
row.get("owner_id"),
row.get("description"),
row.get("color"),
row.get("is_public"),
row.get("created_at"),
row.get("updated_at"),
))
.collect();
Ok(result)
@@ -171,16 +171,16 @@ impl AddressBookRepository for AddressBookPgRepository {
.map_err(|e| DomainError::database_error(format!("Failed to get shared address books: {}", e)))?;
let result = rows.into_iter()
.map(|row| AddressBook {
id: row.get("id"),
name: row.get("name"),
owner_id: row.get("owner_id"),
description: row.get("description"),
color: row.get("color"),
is_public: row.get("is_public"),
created_at: row.get("created_at"),
updated_at: row.get("updated_at"),
})
.map(|row| AddressBook::from_raw(
row.get("id"),
row.get("name"),
row.get("owner_id"),
row.get("description"),
row.get("color"),
row.get("is_public"),
row.get("created_at"),
row.get("updated_at"),
))
.collect();
Ok(result)
@@ -200,16 +200,16 @@ impl AddressBookRepository for AddressBookPgRepository {
.map_err(|e| DomainError::database_error(format!("Failed to get public address books: {}", e)))?;
let result = rows.into_iter()
.map(|row| AddressBook {
id: row.get("id"),
name: row.get("name"),
owner_id: row.get("owner_id"),
description: row.get("description"),
color: row.get("color"),
is_public: row.get("is_public"),
created_at: row.get("created_at"),
updated_at: row.get("updated_at"),
})
.map(|row| AddressBook::from_raw(
row.get("id"),
row.get("name"),
row.get("owner_id"),
row.get("description"),
row.get("color"),
row.get("is_public"),
row.get("created_at"),
row.get("updated_at"),
))
.collect();
Ok(result)
@@ -23,9 +23,9 @@ impl ContactPgRepository {
impl ContactRepository for ContactPgRepository {
async fn create_contact(&self, contact: Contact) -> ContactRepositoryResult<Contact> {
// Convert domain entities to persistence DTOs for JSONB serialization
let email_dtos = emails_to_persistence(&contact.email);
let phone_dtos = phones_to_persistence(&contact.phone);
let address_dtos = addresses_to_persistence(&contact.address);
let email_dtos = emails_to_persistence(contact.email());
let phone_dtos = phones_to_persistence(contact.phone());
let address_dtos = addresses_to_persistence(contact.address());
let email_json = serde_json::to_value(&email_dtos).unwrap_or(JsonValue::Null);
let phone_json = serde_json::to_value(&phone_dtos).unwrap_or(JsonValue::Null);
@@ -48,26 +48,26 @@ impl ContactRepository for ContactPgRepository {
birthday, anniversary, vcard, etag, created_at, updated_at
"#
)
.bind(contact.id)
.bind(contact.address_book_id)
.bind(&contact.uid)
.bind(&contact.full_name)
.bind(&contact.first_name)
.bind(&contact.last_name)
.bind(&contact.nickname)
.bind(contact.id())
.bind(contact.address_book_id())
.bind(contact.uid())
.bind(contact.full_name_owned())
.bind(contact.first_name_owned())
.bind(contact.last_name_owned())
.bind(contact.nickname_owned())
.bind(email_json)
.bind(phone_json)
.bind(address_json)
.bind(&contact.organization)
.bind(&contact.title)
.bind(&contact.notes)
.bind(&contact.photo_url)
.bind(contact.birthday)
.bind(contact.anniversary)
.bind(&contact.vcard)
.bind(&contact.etag)
.bind(contact.created_at)
.bind(contact.updated_at)
.bind(contact.organization_owned())
.bind(contact.title_owned())
.bind(contact.notes_owned())
.bind(contact.photo_url_owned())
.bind(contact.birthday().copied())
.bind(contact.anniversary().copied())
.bind(contact.vcard())
.bind(contact.etag())
.bind(*contact.created_at())
.bind(*contact.updated_at())
.fetch_one(&*self.pool)
.await
.map_err(|e| DomainError::database_error(format!("Failed to create contact: {}", e)))?;
@@ -80,9 +80,9 @@ impl ContactRepository for ContactPgRepository {
async fn update_contact(&self, contact: Contact) -> ContactRepositoryResult<Contact> {
let now = Utc::now();
// Convert domain entities to persistence DTOs for JSONB serialization
let email_dtos = emails_to_persistence(&contact.email);
let phone_dtos = phones_to_persistence(&contact.phone);
let address_dtos = addresses_to_persistence(&contact.address);
let email_dtos = emails_to_persistence(contact.email());
let phone_dtos = phones_to_persistence(contact.phone());
let address_dtos = addresses_to_persistence(contact.address());
let email_json = serde_json::to_value(&email_dtos).unwrap_or(JsonValue::Null);
let phone_json = serde_json::to_value(&phone_dtos).unwrap_or(JsonValue::Null);
@@ -90,7 +90,7 @@ impl ContactRepository for ContactPgRepository {
// Create a clone of the contact with the updated timestamp
let mut updated_contact = contact.clone();
updated_contact.updated_at = now;
updated_contact.set_updated_at(now);
let _row = sqlx::query(
r#"
@@ -119,23 +119,23 @@ impl ContactRepository for ContactPgRepository {
birthday, anniversary, vcard, etag, created_at, updated_at
"#
)
.bind(&updated_contact.full_name)
.bind(&updated_contact.first_name)
.bind(&updated_contact.last_name)
.bind(&updated_contact.nickname)
.bind(updated_contact.full_name_owned())
.bind(updated_contact.first_name_owned())
.bind(updated_contact.last_name_owned())
.bind(updated_contact.nickname_owned())
.bind(email_json)
.bind(phone_json)
.bind(address_json)
.bind(&updated_contact.organization)
.bind(&updated_contact.title)
.bind(&updated_contact.notes)
.bind(&updated_contact.photo_url)
.bind(updated_contact.birthday)
.bind(updated_contact.anniversary)
.bind(&updated_contact.vcard)
.bind(&updated_contact.etag)
.bind(updated_contact.organization_owned())
.bind(updated_contact.title_owned())
.bind(updated_contact.notes_owned())
.bind(updated_contact.photo_url_owned())
.bind(updated_contact.birthday().copied())
.bind(updated_contact.anniversary().copied())
.bind(updated_contact.vcard())
.bind(updated_contact.etag())
.bind(now)
.bind(updated_contact.id)
.bind(updated_contact.id())
.fetch_one(&*self.pool)
.await
.map_err(|e| DomainError::database_error(format!("Failed to update contact: {}", e)))?;
@@ -0,0 +1,130 @@
use std::sync::Arc;
use async_trait::async_trait;
use sqlx::{PgPool, Row};
use tracing::error;
use uuid::Uuid;
use crate::application::dtos::favorites_dto::FavoriteItemDto;
use crate::application::ports::favorites_ports::FavoritesRepositoryPort;
use crate::common::errors::{Result, DomainError, ErrorKind};
/// Implementación PostgreSQL del puerto de persistencia de favoritos.
pub struct FavoritesPgRepository {
db_pool: Arc<PgPool>,
}
impl FavoritesPgRepository {
pub fn new(db_pool: Arc<PgPool>) -> Self {
Self { db_pool }
}
}
#[async_trait]
impl FavoritesRepositoryPort for FavoritesPgRepository {
async fn get_favorites(&self, user_id: &str) -> Result<Vec<FavoriteItemDto>> {
let user_uuid = Uuid::parse_str(user_id)?;
let rows = sqlx::query(
r#"
SELECT
id::TEXT AS "id",
user_id::TEXT AS "user_id",
item_id AS "item_id",
item_type AS "item_type",
created_at AS "created_at"
FROM auth.user_favorites
WHERE user_id = $1::TEXT
ORDER BY created_at DESC
"#,
)
.bind(user_uuid)
.fetch_all(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error fetching favorites: {}", e);
DomainError::new(ErrorKind::InternalError, "Favorites", format!("Failed to fetch favorites: {}", e))
})?;
let favorites = rows
.iter()
.map(|row| FavoriteItemDto {
id: row.get("id"),
user_id: row.get("user_id"),
item_id: row.get("item_id"),
item_type: row.get("item_type"),
created_at: row.get("created_at"),
})
.collect();
Ok(favorites)
}
async fn add_favorite(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<()> {
let user_uuid = Uuid::parse_str(user_id)?;
sqlx::query(
r#"
INSERT INTO auth.user_favorites (user_id, item_id, item_type)
VALUES ($1::TEXT, $2, $3)
ON CONFLICT (user_id, item_id, item_type) DO NOTHING
"#,
)
.bind(user_uuid)
.bind(item_id)
.bind(item_type)
.execute(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error adding favorite: {}", e);
DomainError::new(ErrorKind::InternalError, "Favorites", format!("Failed to add to favorites: {}", e))
})?;
Ok(())
}
async fn remove_favorite(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<bool> {
let user_uuid = Uuid::parse_str(user_id)?;
let result = sqlx::query(
r#"
DELETE FROM auth.user_favorites
WHERE user_id = $1::TEXT AND item_id = $2 AND item_type = $3
"#,
)
.bind(user_uuid)
.bind(item_id)
.bind(item_type)
.execute(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error removing favorite: {}", e);
DomainError::new(ErrorKind::InternalError, "Favorites", format!("Failed to remove from favorites: {}", e))
})?;
Ok(result.rows_affected() > 0)
}
async fn is_favorite(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<bool> {
let user_uuid = Uuid::parse_str(user_id)?;
let row = sqlx::query(
r#"
SELECT EXISTS (
SELECT 1 FROM auth.user_favorites
WHERE user_id = $1::TEXT AND item_id = $2 AND item_type = $3
) AS "is_favorite"
"#,
)
.bind(user_uuid)
.bind(item_id)
.bind(item_type)
.fetch_one(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error checking favorite status: {}", e);
DomainError::new(ErrorKind::InternalError, "Favorites", format!("Failed to check favorite status: {}", e))
})?;
Ok(row.try_get("is_favorite").unwrap_or(false))
}
}
@@ -3,6 +3,8 @@ mod calendar_pg_repository;
mod calendar_event_pg_repository;
mod contact_pg_repository;
mod contact_persistence_dto;
mod favorites_pg_repository;
mod recent_items_pg_repository;
mod session_pg_repository;
mod transaction_utils;
mod user_pg_repository;
@@ -12,5 +14,7 @@ pub use calendar_pg_repository::CalendarPgRepository;
pub use calendar_event_pg_repository::CalendarEventPgRepository;
pub use contact_pg_repository::ContactPgRepository;
pub use contact_persistence_dto::*;
pub use favorites_pg_repository::FavoritesPgRepository;
pub use recent_items_pg_repository::RecentItemsPgRepository;
pub use session_pg_repository::SessionPgRepository;
pub use user_pg_repository::UserPgRepository;
@@ -0,0 +1,155 @@
use std::sync::Arc;
use async_trait::async_trait;
use sqlx::{PgPool, Row};
use tracing::error;
use uuid::Uuid;
use crate::application::dtos::recent_dto::RecentItemDto;
use crate::application::ports::recent_ports::RecentItemsRepositoryPort;
use crate::common::errors::{Result, DomainError, ErrorKind};
/// Implementación PostgreSQL del puerto de persistencia de elementos recientes.
pub struct RecentItemsPgRepository {
db_pool: Arc<PgPool>,
}
impl RecentItemsPgRepository {
pub fn new(db_pool: Arc<PgPool>) -> Self {
Self { db_pool }
}
}
#[async_trait]
impl RecentItemsRepositoryPort for RecentItemsPgRepository {
async fn get_recent_items(&self, user_id: &str, limit: i32) -> Result<Vec<RecentItemDto>> {
let user_uuid = Uuid::parse_str(user_id)?;
let rows = sqlx::query(
r#"
SELECT
id::TEXT AS "id",
user_id::TEXT AS "user_id",
item_id AS "item_id",
item_type AS "item_type",
accessed_at AS "accessed_at"
FROM auth.user_recent_files
WHERE user_id = $1::TEXT
ORDER BY accessed_at DESC
LIMIT $2
"#,
)
.bind(user_uuid)
.bind(limit)
.fetch_all(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error fetching recent items: {}", e);
DomainError::new(ErrorKind::InternalError, "RecentItems", format!("Failed to fetch recent items: {}", e))
})?;
let items = rows
.iter()
.map(|row| RecentItemDto {
id: row.get("id"),
user_id: row.get("user_id"),
item_id: row.get("item_id"),
item_type: row.get("item_type"),
accessed_at: row.get("accessed_at"),
})
.collect();
Ok(items)
}
async fn upsert_access(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<()> {
let user_uuid = Uuid::parse_str(user_id)?;
sqlx::query(
r#"
INSERT INTO auth.user_recent_files (user_id, item_id, item_type, accessed_at)
VALUES ($1::TEXT, $2, $3, CURRENT_TIMESTAMP)
ON CONFLICT (user_id, item_id, item_type)
DO UPDATE SET accessed_at = CURRENT_TIMESTAMP
"#,
)
.bind(user_uuid)
.bind(item_id)
.bind(item_type)
.execute(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error upserting recent item access: {}", e);
DomainError::new(ErrorKind::InternalError, "RecentItems", format!("Failed to record item access: {}", e))
})?;
Ok(())
}
async fn remove_item(&self, user_id: &str, item_id: &str, item_type: &str) -> Result<bool> {
let user_uuid = Uuid::parse_str(user_id)?;
let result = sqlx::query(
r#"
DELETE FROM auth.user_recent_files
WHERE user_id = $1::TEXT AND item_id = $2 AND item_type = $3
"#,
)
.bind(user_uuid)
.bind(item_id)
.bind(item_type)
.execute(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error removing recent item: {}", e);
DomainError::new(ErrorKind::InternalError, "RecentItems", format!("Failed to remove recent item: {}", e))
})?;
Ok(result.rows_affected() > 0)
}
async fn clear_all(&self, user_id: &str) -> Result<()> {
let user_uuid = Uuid::parse_str(user_id)?;
sqlx::query(
r#"
DELETE FROM auth.user_recent_files
WHERE user_id = $1::TEXT
"#,
)
.bind(user_uuid)
.execute(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error clearing recent items: {}", e);
DomainError::new(ErrorKind::InternalError, "RecentItems", format!("Failed to clear recent items: {}", e))
})?;
Ok(())
}
async fn prune(&self, user_id: &str, max_items: i32) -> Result<()> {
let user_uuid = Uuid::parse_str(user_id)?;
sqlx::query(
r#"
DELETE FROM auth.user_recent_files
WHERE id IN (
SELECT id FROM auth.user_recent_files
WHERE user_id = $1::TEXT
ORDER BY accessed_at DESC
OFFSET $2
)
"#,
)
.bind(user_uuid)
.bind(max_items)
.execute(&*self.db_pool)
.await
.map_err(|e| {
error!("Database error pruning old recent items: {}", e);
DomainError::new(ErrorKind::InternalError, "RecentItems", format!("Failed to prune recent items: {}", e))
})?;
Ok(())
}
}
@@ -66,8 +66,8 @@ impl SessionRepository for SessionPgRepository {
.bind(session_clone.user_id())
.bind(session_clone.refresh_token())
.bind(session_clone.expires_at())
.bind(&session_clone.ip_address)
.bind(&session_clone.user_agent)
.bind(session_clone.ip_address())
.bind(session_clone.user_agent())
.bind(session_clone.created_at())
.bind(session_clone.is_revoked())
.execute(&mut **tx)
@@ -120,16 +120,16 @@ impl SessionRepository for SessionPgRepository {
.await
.map_err(Self::map_sqlx_error)?;
Ok(Session {
id: row.get("id"),
user_id: row.get("user_id"),
refresh_token: row.get("refresh_token"),
expires_at: row.get("expires_at"),
ip_address: row.get("ip_address"),
user_agent: row.get("user_agent"),
created_at: row.get("created_at"),
revoked: row.get("revoked"),
})
Ok(Session::from_raw(
row.get("id"),
row.get("user_id"),
row.get("refresh_token"),
row.get("expires_at"),
row.get("ip_address"),
row.get("user_agent"),
row.get("created_at"),
row.get("revoked"),
))
}
/// Obtiene una sesión por token de actualización
@@ -148,16 +148,16 @@ impl SessionRepository for SessionPgRepository {
.await
.map_err(Self::map_sqlx_error)?;
Ok(Session {
id: row.get("id"),
user_id: row.get("user_id"),
refresh_token: row.get("refresh_token"),
expires_at: row.get("expires_at"),
ip_address: row.get("ip_address"),
user_agent: row.get("user_agent"),
created_at: row.get("created_at"),
revoked: row.get("revoked"),
})
Ok(Session::from_raw(
row.get("id"),
row.get("user_id"),
row.get("refresh_token"),
row.get("expires_at"),
row.get("ip_address"),
row.get("user_agent"),
row.get("created_at"),
row.get("revoked"),
))
}
/// Obtiene todas las sesiones de un usuario
@@ -179,16 +179,16 @@ impl SessionRepository for SessionPgRepository {
let sessions = rows.into_iter()
.map(|row| {
Session {
id: row.get("id"),
user_id: row.get("user_id"),
refresh_token: row.get("refresh_token"),
expires_at: row.get("expires_at"),
ip_address: row.get("ip_address"),
user_agent: row.get("user_agent"),
created_at: row.get("created_at"),
revoked: row.get("revoked"),
}
Session::from_raw(
row.get("id"),
row.get("user_id"),
row.get("refresh_token"),
row.get("expires_at"),
row.get("ip_address"),
row.get("user_agent"),
row.get("created_at"),
row.get("revoked"),
)
})
.collect();
@@ -0,0 +1,96 @@
//! Infrastructure-layer error types for file and folder repository operations.
//!
//! These error types are used internally by the filesystem repository implementations
//! (FileFsReadRepository, FileFsWriteRepository, FolderFsRepository, etc.) to represent
//! errors that can occur during storage operations. They are converted to `DomainError`
//! at the port boundary before crossing into the application layer.
use crate::common::errors::DomainError;
/// Error types for file repository operations.
#[derive(Debug, thiserror::Error)]
pub enum FileRepositoryError {
#[error("File not found: {0}")]
NotFound(String),
#[error("File already exists: {0}")]
AlreadyExists(String),
#[error("Invalid file path: {0}")]
InvalidPath(String),
#[error("Operation not supported: {0}")]
OperationNotSupported(String),
#[error("Storage error: {0}")]
StorageError(String),
#[error("Domain error: {0}")]
DomainError(#[from] DomainError),
#[error("Other error: {0}")]
Other(String),
}
pub type FileRepositoryResult<T> = Result<T, FileRepositoryError>;
/// Error types for folder repository operations.
#[derive(Debug, thiserror::Error)]
pub enum FolderRepositoryError {
#[error("Folder not found: {0}")]
NotFound(String),
#[error("Folder already exists: {0}")]
AlreadyExists(String),
#[error("Invalid folder path: {0}")]
InvalidPath(String),
#[error("Operation not supported: {0}")]
OperationNotSupported(String),
#[error("Storage error: {0}")]
StorageError(String),
#[error("Validation error: {0}")]
ValidationError(String),
#[error("Domain error: {0}")]
DomainError(#[from] DomainError),
#[error("Other error: {0}")]
Other(String),
}
pub type FolderRepositoryResult<T> = Result<T, FolderRepositoryError>;
// ── Conversions to DomainError ──
impl From<FileRepositoryError> for DomainError {
fn from(err: FileRepositoryError) -> Self {
match err {
FileRepositoryError::NotFound(id) => DomainError::not_found("File", id),
FileRepositoryError::AlreadyExists(path) => DomainError::already_exists("File", path),
FileRepositoryError::InvalidPath(path) => DomainError::validation_error(format!("Invalid path: {}", path)),
FileRepositoryError::StorageError(msg) => DomainError::internal_error("File", format!("Storage error: {}", msg)),
FileRepositoryError::Other(msg) => DomainError::internal_error("File", msg),
FileRepositoryError::OperationNotSupported(msg) => DomainError::operation_not_supported("File", msg),
FileRepositoryError::DomainError(e) => e,
}
}
}
impl From<FolderRepositoryError> for DomainError {
fn from(err: FolderRepositoryError) -> Self {
match err {
FolderRepositoryError::NotFound(id) => DomainError::not_found("Folder", id),
FolderRepositoryError::AlreadyExists(path) => DomainError::already_exists("Folder", path),
FolderRepositoryError::InvalidPath(path) => DomainError::validation_error(format!("Invalid path: {}", path)),
FolderRepositoryError::StorageError(msg) => DomainError::internal_error("Folder", format!("Storage error: {}", msg)),
FolderRepositoryError::ValidationError(msg) => DomainError::validation_error(msg),
FolderRepositoryError::Other(msg) => DomainError::internal_error("Folder", msg),
FolderRepositoryError::OperationNotSupported(msg) => DomainError::operation_not_supported("Folder", msg),
FolderRepositoryError::DomainError(e) => e,
}
}
}
@@ -83,35 +83,35 @@ impl ShareFsRepository {
record.permissions_reshare,
);
Share {
id: record.id.clone(),
item_id: record.item_id.clone(),
Share::from_raw(
record.id.clone(),
record.item_id.clone(),
item_type,
token: record.token.clone(),
password_hash: record.password_hash.clone(),
expires_at: record.expires_at,
record.token.clone(),
record.password_hash.clone(),
record.expires_at,
permissions,
created_at: record.created_at,
created_by: record.created_by.clone(),
access_count: record.access_count,
}
record.created_at,
record.created_by.clone(),
record.access_count,
)
}
/// Convierte una entidad de dominio a un registro para el sistema de archivos
fn to_record(&self, share: &Share) -> ShareRecord {
ShareRecord {
id: share.id.clone(),
item_id: share.item_id.clone(),
item_type: share.item_type.to_string(),
token: share.token.clone(),
password_hash: share.password_hash.clone(),
expires_at: share.expires_at,
permissions_read: share.permissions.read,
permissions_write: share.permissions.write,
permissions_reshare: share.permissions.reshare,
created_at: share.created_at,
created_by: share.created_by.clone(),
access_count: share.access_count,
id: share.id().to_string(),
item_id: share.item_id().to_string(),
item_type: share.item_type().to_string(),
token: share.token().to_string(),
password_hash: share.password_hash().map(|s| s.to_string()),
expires_at: share.expires_at(),
permissions_read: share.permissions().read(),
permissions_write: share.permissions().write(),
permissions_reshare: share.permissions().reshare(),
created_at: share.created_at(),
created_by: share.created_by().to_string(),
access_count: share.access_count(),
}
}
}
@@ -123,7 +123,7 @@ impl ShareStoragePort for ShareFsRepository {
.map_err(|e| DomainError::internal_error("Share", e.to_string()))?;
// Verifica si el enlace ya existe
let existing_index = shares.iter().position(|s| s.id == share.id);
let existing_index = shares.iter().position(|s| s.id == share.id());
let record = self.to_record(share);
@@ -191,9 +191,9 @@ impl ShareStoragePort for ShareFsRepository {
.map_err(|e| DomainError::internal_error("Share", e.to_string()))?;
// Busca el índice del enlace a actualizar
let index = shares.iter().position(|s| s.id == share.id)
let index = shares.iter().position(|s| s.id == share.id())
.ok_or_else(|| {
DomainError::not_found("Share", format!("Share with ID {} not found for update", share.id))
DomainError::not_found("Share", format!("Share with ID {} not found for update", share.id()))
})?;
// Actualiza el registro
@@ -194,32 +194,32 @@ impl TrashFsRepository {
))?
.with_timezone(&Utc);
Ok(TrashedItem {
Ok(TrashedItem::from_raw(
id,
original_id,
user_id,
item_type,
name: entry.name,
original_path: entry.original_path,
entry.name,
entry.original_path,
trashed_at,
deletion_date,
})
))
}
/// Convierte una entidad TrashedItem a entrada JSON
fn trashed_item_to_entry(&self, item: &TrashedItem) -> TrashedItemEntry {
TrashedItemEntry {
id: item.id.to_string(),
original_id: item.original_id.to_string(),
user_id: item.user_id.to_string(),
item_type: match item.item_type {
id: item.id().to_string(),
original_id: item.original_id().to_string(),
user_id: item.user_id().to_string(),
item_type: match item.item_type() {
TrashedItemType::File => "file".to_string(),
TrashedItemType::Folder => "folder".to_string(),
},
name: item.name.clone(),
original_path: item.original_path.clone(),
trashed_at: item.trashed_at.to_rfc3339(),
deletion_date: item.deletion_date.to_rfc3339(),
name: item.name().to_string(),
original_path: item.original_path().to_string(),
trashed_at: item.trashed_at().to_rfc3339(),
deletion_date: item.deletion_date().to_rfc3339(),
}
}
}
@@ -228,10 +228,10 @@ impl TrashFsRepository {
impl TrashRepository for TrashFsRepository {
#[instrument(skip(self))]
async fn add_to_trash(&self, item: &TrashedItem) -> Result<()> {
debug!("Añadiendo elemento a la papelera: id={}, user={}", item.id, item.user_id);
debug!("Añadiendo elemento a la papelera: id={}, user={}", item.id(), item.user_id());
// Aseguramos que existe el directorio de la papelera para este usuario
let user_trash_dir = self.trash_dir.join("files").join(item.user_id.to_string());
let user_trash_dir = self.trash_dir.join("files").join(item.user_id().to_string());
debug!("User trash directory path: {}", user_trash_dir.display());
// Create the user-specific trash directory