adding pdf and image viewer
This commit is contained in:
@@ -21,4 +21,3 @@ pub use file_path_resolver::FilePathResolver;
|
||||
pub use file_fs_read_repository::FileFsReadRepository;
|
||||
pub use file_fs_write_repository::FileFsWriteRepository;
|
||||
pub use pg::{UserPgRepository, SessionPgRepository};
|
||||
pub use share_fs_repository::ShareFsRepository;
|
||||
@@ -5,4 +5,5 @@ pub mod cache_manager;
|
||||
pub mod file_metadata_cache;
|
||||
pub mod compression_service;
|
||||
pub mod buffer_pool;
|
||||
pub mod trash_cleanup_service;
|
||||
pub mod trash_cleanup_service;
|
||||
pub mod zip_service;
|
||||
@@ -0,0 +1,228 @@
|
||||
use std::io::{Cursor, Read, Write};
|
||||
use zip::{ZipWriter, write::SimpleFileOptions};
|
||||
use thiserror::Error;
|
||||
use tracing::*;
|
||||
use crate::{
|
||||
application::dtos::file_dto::FileDto,
|
||||
application::dtos::folder_dto::FolderDto,
|
||||
application::ports::inbound::{FileUseCase, FolderUseCase},
|
||||
common::errors::{Result, DomainError, ErrorKind},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Error relacionado con la creación de archivos ZIP
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ZipError {
|
||||
#[error("Error de IO: {0}")]
|
||||
IoError(#[from] std::io::Error),
|
||||
|
||||
#[error("Error de ZIP: {0}")]
|
||||
ZipError(#[from] zip::result::ZipError),
|
||||
|
||||
#[error("Error al leer el archivo: {0}")]
|
||||
FileReadError(String),
|
||||
|
||||
#[error("Error al obtener contenido de carpeta: {0}")]
|
||||
FolderContentsError(String),
|
||||
|
||||
#[error("Carpeta no encontrada: {0}")]
|
||||
FolderNotFound(String),
|
||||
}
|
||||
|
||||
// Implementar From<ZipError> para DomainError para permitir el uso de ?
|
||||
impl From<ZipError> for DomainError {
|
||||
fn from(err: ZipError) -> Self {
|
||||
DomainError::new(ErrorKind::InternalError, "zip_service", err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
// Implementar From<zip::result::ZipError> para DomainError directamente
|
||||
impl From<zip::result::ZipError> for DomainError {
|
||||
fn from(err: zip::result::ZipError) -> Self {
|
||||
DomainError::new(ErrorKind::InternalError, "zip_service", err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Servicio para crear archivos ZIP
|
||||
pub struct ZipService {
|
||||
file_service: Arc<dyn FileUseCase>,
|
||||
folder_service: Arc<dyn FolderUseCase>,
|
||||
}
|
||||
|
||||
impl ZipService {
|
||||
/// Crea una nueva instancia del servicio ZIP con una referencia al servicio de archivos
|
||||
pub fn new(file_service: Arc<dyn FileUseCase>, folder_service: Arc<dyn FolderUseCase>) -> Self {
|
||||
Self {
|
||||
file_service,
|
||||
folder_service,
|
||||
}
|
||||
}
|
||||
|
||||
/// Crea un archivo ZIP con el contenido de una carpeta y todas sus subcarpetas
|
||||
/// Retorna los bytes del ZIP
|
||||
pub async fn create_folder_zip(&self, folder_id: &str, folder_name: &str) -> Result<Vec<u8>> {
|
||||
info!("Creando ZIP para carpeta: {} (ID: {})", folder_name, folder_id);
|
||||
|
||||
// Verificar si la carpeta existe
|
||||
let folder = match self.folder_service.get_folder(folder_id).await {
|
||||
Ok(folder) => folder,
|
||||
Err(e) => {
|
||||
error!("Error al obtener carpeta {}: {}", folder_id, e);
|
||||
return Err(ZipError::FolderNotFound(folder_id.to_string()).into());
|
||||
}
|
||||
};
|
||||
|
||||
// Crear un buffer en memoria para el ZIP
|
||||
let buf = Cursor::new(Vec::new());
|
||||
let mut zip = ZipWriter::new(buf);
|
||||
|
||||
// Establecer opciones de compresión
|
||||
let options = SimpleFileOptions::default()
|
||||
.compression_method(zip::CompressionMethod::Deflated)
|
||||
.unix_permissions(0o755);
|
||||
|
||||
// Objeto para seguir las carpetas procesadas y evitar ciclos
|
||||
let mut processed_folders = std::collections::HashSet::new();
|
||||
|
||||
// Procesamos la carpeta raíz y construimos el ZIP
|
||||
self.process_folder_recursively(
|
||||
&mut zip,
|
||||
&folder,
|
||||
folder_name,
|
||||
&options,
|
||||
&mut processed_folders
|
||||
).await?;
|
||||
|
||||
// Finalizar el ZIP y obtener los bytes
|
||||
let mut zip_buf = zip.finish()?;
|
||||
|
||||
let mut bytes = Vec::new();
|
||||
match zip_buf.read_to_end(&mut bytes) {
|
||||
Ok(_) => Ok(bytes),
|
||||
Err(e) => {
|
||||
error!("Error al leer ZIP finalizado: {}", e);
|
||||
Err(ZipError::IoError(e).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implementación alternativa para evitar recursión en async
|
||||
async fn process_folder_recursively(
|
||||
&self,
|
||||
zip: &mut ZipWriter<Cursor<Vec<u8>>>,
|
||||
folder: &FolderDto,
|
||||
path: &str,
|
||||
options: &SimpleFileOptions,
|
||||
processed_folders: &mut std::collections::HashSet<String>
|
||||
) -> Result<()> {
|
||||
// Estructura para representar el trabajo pendiente
|
||||
struct PendingFolder {
|
||||
folder: FolderDto,
|
||||
path: String,
|
||||
}
|
||||
|
||||
// Cola de trabajo para procesamiento iterativo
|
||||
let mut work_queue = vec![PendingFolder {
|
||||
folder: folder.clone(),
|
||||
path: path.to_string(),
|
||||
}];
|
||||
|
||||
// Procesar la cola mientras haya elementos
|
||||
while let Some(current) = work_queue.pop() {
|
||||
let folder_id = current.folder.id.to_string();
|
||||
|
||||
// Evitar ciclos
|
||||
if processed_folders.contains(&folder_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
processed_folders.insert(folder_id.clone());
|
||||
|
||||
// Crear la entrada de directorio en el ZIP
|
||||
let folder_path = format!("{}/", current.path);
|
||||
match zip.add_directory(&folder_path, *options) {
|
||||
Ok(_) => debug!("Carpeta agregada al ZIP: {}", folder_path),
|
||||
Err(e) => {
|
||||
warn!("No se pudo agregar carpeta al ZIP (puede que ya exista): {}", e);
|
||||
// Continuamos aunque falle crear el directorio (podría estar duplicado)
|
||||
}
|
||||
}
|
||||
|
||||
// Agregar archivos de la carpeta al ZIP
|
||||
let files = match self.file_service.list_files(Some(&folder_id)).await {
|
||||
Ok(files) => files,
|
||||
Err(e) => {
|
||||
error!("Error al listar archivos en carpeta {}: {}", folder_id, e);
|
||||
return Err(ZipError::FolderContentsError(format!("Error al listar archivos: {}", e)).into());
|
||||
}
|
||||
};
|
||||
|
||||
// Agregar cada archivo al ZIP
|
||||
for file in files {
|
||||
self.add_file_to_zip(zip, &file, &folder_path, options).await?;
|
||||
}
|
||||
|
||||
// Procesar subcarpetas
|
||||
let subfolders = match self.folder_service.list_folders(Some(&folder_id)).await {
|
||||
Ok(folders) => folders,
|
||||
Err(e) => {
|
||||
error!("Error al listar subcarpetas en {}: {}", folder_id, e);
|
||||
return Err(ZipError::FolderContentsError(format!("Error al listar subcarpetas: {}", e)).into());
|
||||
}
|
||||
};
|
||||
|
||||
// Agregar subcarpetas a la cola
|
||||
for subfolder in subfolders {
|
||||
let subfolder_path = format!("{}/{}", current.path, subfolder.name);
|
||||
work_queue.push(PendingFolder {
|
||||
folder: subfolder,
|
||||
path: subfolder_path,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Agrega un archivo al ZIP
|
||||
async fn add_file_to_zip(
|
||||
&self,
|
||||
zip: &mut ZipWriter<Cursor<Vec<u8>>>,
|
||||
file: &FileDto,
|
||||
folder_path: &str,
|
||||
options: &SimpleFileOptions,
|
||||
) -> Result<()> {
|
||||
let file_path = format!("{}{}", folder_path, file.name);
|
||||
info!("Agregando archivo al ZIP: {}", file_path);
|
||||
|
||||
// Obtener el contenido del archivo
|
||||
let file_id = file.id.to_string();
|
||||
let content = match self.file_service.get_file_content(&file_id).await {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
error!("Error al leer contenido del archivo {}: {}", file_id, e);
|
||||
return Err(ZipError::FileReadError(format!("Error al leer archivo {}: {}", file_id, e)).into());
|
||||
}
|
||||
};
|
||||
|
||||
// Escribir archivo al ZIP
|
||||
match zip.start_file_from_path(std::path::Path::new(&file_path), *options) {
|
||||
Ok(_) => {
|
||||
match zip.write_all(&content) {
|
||||
Ok(_) => {
|
||||
debug!("Archivo agregado al ZIP: {}", file_path);
|
||||
Ok(())
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Error al escribir contenido del archivo {}: {}", file_path, e);
|
||||
Err(ZipError::IoError(e).into())
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Error al iniciar archivo en ZIP {}: {}", file_path, e);
|
||||
Err(ZipError::ZipError(e).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -196,10 +196,20 @@ impl FileHandler {
|
||||
Ok(content) => {
|
||||
// Create base headers
|
||||
let mut headers = HashMap::new();
|
||||
headers.insert(
|
||||
header::CONTENT_DISPOSITION.to_string(),
|
||||
|
||||
// Determine if the file should be displayed inline or downloaded
|
||||
// Images and PDFs should be displayed inline by default, or if inline param is present
|
||||
let force_inline = params.get("inline").map_or(false, |v| v == "true" || v == "1");
|
||||
|
||||
let disposition = if force_inline ||
|
||||
file.mime_type.starts_with("image/") ||
|
||||
file.mime_type == "application/pdf" {
|
||||
format!("inline; filename=\"{}\"", file.name)
|
||||
} else {
|
||||
format!("attachment; filename=\"{}\"", file.name)
|
||||
);
|
||||
};
|
||||
|
||||
headers.insert(header::CONTENT_DISPOSITION.to_string(), disposition);
|
||||
|
||||
if should_compress {
|
||||
// Add content-encoding header for compressed response
|
||||
@@ -290,10 +300,20 @@ impl FileHandler {
|
||||
Ok(content) => {
|
||||
// Create base headers
|
||||
let mut headers = HashMap::new();
|
||||
headers.insert(
|
||||
header::CONTENT_DISPOSITION.to_string(),
|
||||
|
||||
// Determine if the file should be displayed inline or downloaded
|
||||
// Images and PDFs should be displayed inline by default, or if inline param is present
|
||||
let force_inline = params.get("inline").map_or(false, |v| v == "true" || v == "1");
|
||||
|
||||
let disposition = if force_inline ||
|
||||
file.mime_type.starts_with("image/") ||
|
||||
file.mime_type == "application/pdf" {
|
||||
format!("inline; filename=\"{}\"", file.name)
|
||||
} else {
|
||||
format!("attachment; filename=\"{}\"", file.name)
|
||||
);
|
||||
};
|
||||
|
||||
headers.insert(header::CONTENT_DISPOSITION.to_string(), disposition);
|
||||
|
||||
if should_compress {
|
||||
// Add content-encoding header for compressed response
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use axum::{
|
||||
extract::{Path, State, Query},
|
||||
http::StatusCode,
|
||||
http::{StatusCode, header, HeaderName, HeaderValue, Response},
|
||||
response::IntoResponse,
|
||||
Json,
|
||||
};
|
||||
@@ -13,6 +14,7 @@ use crate::common::errors::ErrorKind;
|
||||
use crate::application::ports::inbound::FolderUseCase;
|
||||
use crate::common::di::AppState as GlobalAppState;
|
||||
use crate::interfaces::middleware::auth::AuthUser;
|
||||
use crate::infrastructure::services::zip_service::ZipService;
|
||||
|
||||
type AppState = Arc<FolderService>;
|
||||
|
||||
@@ -213,4 +215,79 @@ impl FolderHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Downloads a folder as a ZIP file
|
||||
pub async fn download_folder_zip(
|
||||
State(state): State<GlobalAppState>,
|
||||
Path(id): Path<String>,
|
||||
Query(_params): Query<HashMap<String, String>>,
|
||||
) -> impl IntoResponse {
|
||||
tracing::info!("Downloading folder as ZIP: {}", id);
|
||||
|
||||
// Get folder information first to check it exists and get name
|
||||
let folder_service = &state.applications.folder_service;
|
||||
let file_service = &state.applications.file_service;
|
||||
|
||||
match folder_service.get_folder(&id).await {
|
||||
Ok(folder) => {
|
||||
tracing::info!("Preparing ZIP for folder: {} ({})", folder.name, id);
|
||||
|
||||
// Create ZIP service with the required services
|
||||
let zip_service = ZipService::new(
|
||||
file_service.clone(),
|
||||
folder_service.clone()
|
||||
);
|
||||
|
||||
// Create the ZIP file
|
||||
match zip_service.create_folder_zip(&id, &folder.name).await {
|
||||
Ok(zip_data) => {
|
||||
tracing::info!("ZIP file created successfully, size: {} bytes", zip_data.len());
|
||||
|
||||
// Setup headers for download
|
||||
let filename = format!("{}.zip", folder.name);
|
||||
let content_disposition = format!("attachment; filename=\"{}\"", filename);
|
||||
|
||||
// Build response with the ZIP data
|
||||
let mut headers = HashMap::new();
|
||||
headers.insert(header::CONTENT_TYPE.to_string(), "application/zip".to_string());
|
||||
headers.insert(header::CONTENT_DISPOSITION.to_string(), content_disposition);
|
||||
headers.insert(header::CONTENT_LENGTH.to_string(), zip_data.len().to_string());
|
||||
|
||||
// Build the response
|
||||
let mut response = Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(axum::body::Body::from(zip_data))
|
||||
.unwrap();
|
||||
|
||||
// Add headers to response
|
||||
for (name, value) in headers {
|
||||
response.headers_mut().insert(
|
||||
HeaderName::from_bytes(name.as_bytes()).unwrap(),
|
||||
HeaderValue::from_str(&value).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
response
|
||||
},
|
||||
Err(err) => {
|
||||
tracing::error!("Error creating ZIP file: {}", err);
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
|
||||
"error": format!("Error creating ZIP file: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
tracing::error!("Folder not found: {}", err);
|
||||
let status = match err.kind {
|
||||
ErrorKind::NotFound => StatusCode::NOT_FOUND,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
};
|
||||
|
||||
(status, Json(serde_json::json!({
|
||||
"error": format!("Error finding folder: {}", err)
|
||||
}))).into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,6 +168,11 @@ pub fn create_api_routes(
|
||||
.route("/{id}/move", put(FolderHandler::move_folder))
|
||||
.with_state(folder_service.clone());
|
||||
|
||||
// Special route for ZIP download that requires AppState instead of just FolderService
|
||||
let folder_zip_router = Router::new()
|
||||
.route("/{id}/download", get(FolderHandler::download_folder_zip))
|
||||
.with_state(app_state.clone());
|
||||
|
||||
// Create folder operations that use trash separately
|
||||
let folders_ops_router = Router::new()
|
||||
.route("/{id}", delete(|
|
||||
@@ -200,7 +205,7 @@ pub fn create_api_routes(
|
||||
}));
|
||||
|
||||
// Merge the routers
|
||||
let folders_router = folders_basic_router.merge(folders_ops_router);
|
||||
let folders_router = folders_basic_router.merge(folders_ops_router).merge(folder_zip_router);
|
||||
|
||||
// Create file routes for basic operations and trash-enabled delete
|
||||
let basic_file_router = Router::new()
|
||||
|
||||
Reference in New Issue
Block a user