2025-03-03 15:35:24 -03:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
from datetime import datetime
|
|
|
|
import pytz
|
|
|
|
import mimetypes
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from flask import current_app
|
|
|
|
|
|
|
|
from utils.file_utils import (
|
2025-03-05 14:10:33 -03:00
|
|
|
load_json_file,
|
|
|
|
save_json_file,
|
|
|
|
ensure_dir_exists,
|
|
|
|
get_next_id,
|
|
|
|
format_document_directory_name,
|
|
|
|
format_version_filename,
|
2025-03-03 15:35:24 -03:00
|
|
|
)
|
|
|
|
from utils.security import calculate_checksum, check_file_type
|
|
|
|
from services.project_service import find_project_directory
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def get_allowed_filetypes():
|
2025-03-05 14:10:33 -03:00
|
|
|
"""Get all allowed filetypes from storage."""
|
|
|
|
storage_path = current_app.config["STORAGE_PATH"]
|
|
|
|
filetypes_path = os.path.join(storage_path, "filetypes", "filetypes.json")
|
|
|
|
|
|
|
|
if os.path.exists(filetypes_path):
|
|
|
|
with open(filetypes_path, "r", encoding="utf-8") as f:
|
|
|
|
return json.load(f)
|
|
|
|
return {}
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
|
|
|
|
def add_document(project_id, document_data, file, creator_username):
|
|
|
|
"""
|
|
|
|
Añadir un nuevo documento a un proyecto.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_id (int): ID del proyecto
|
|
|
|
document_data (dict): Datos del documento
|
|
|
|
file: Objeto de archivo (de Flask)
|
|
|
|
creator_username (str): Usuario que crea el documento
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
tuple: (success, message, document_id)
|
|
|
|
"""
|
|
|
|
# Buscar directorio del proyecto
|
|
|
|
project_dir = find_project_directory(project_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not project_dir:
|
|
|
|
return False, f"No se encontró el proyecto con ID {project_id}.", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Validar datos obligatorios
|
2025-03-05 14:10:33 -03:00
|
|
|
if "nombre" not in document_data or not document_data["nombre"]:
|
2025-03-03 15:35:24 -03:00
|
|
|
return False, "El nombre del documento es obligatorio.", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Validar tipo de archivo
|
|
|
|
allowed_filetypes = get_allowed_filetypes()
|
|
|
|
filename = secure_filename(file.filename)
|
2025-03-05 14:10:33 -03:00
|
|
|
extension = filename.rsplit(".", 1)[1].lower() if "." in filename else ""
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if extension not in allowed_filetypes:
|
|
|
|
return False, f"Tipo de archivo no permitido: {extension}", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Verificar MIME type
|
2025-03-05 14:10:33 -03:00
|
|
|
if not check_file_type(file.stream, [allowed_filetypes[extension]["mime_type"]]):
|
2025-03-03 15:35:24 -03:00
|
|
|
return False, "El tipo de archivo no coincide con su extensión.", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Obtener siguiente ID de documento
|
2025-03-05 14:10:33 -03:00
|
|
|
document_id = get_next_id("document")
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Preparar directorio del documento
|
2025-03-05 14:10:33 -03:00
|
|
|
documents_dir = os.path.join(project_dir, "documents")
|
|
|
|
dir_name = format_document_directory_name(document_id, document_data["nombre"])
|
2025-03-03 15:35:24 -03:00
|
|
|
document_dir = os.path.join(documents_dir, dir_name)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Verificar si ya existe
|
|
|
|
if os.path.exists(document_dir):
|
|
|
|
return False, "Ya existe un documento con ese nombre en este proyecto.", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Crear directorio
|
|
|
|
ensure_dir_exists(document_dir)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Preparar primera versión
|
|
|
|
version = 1
|
2025-03-05 14:10:33 -03:00
|
|
|
version_filename = format_version_filename(
|
|
|
|
version, document_data["nombre"], extension
|
|
|
|
)
|
2025-03-03 15:35:24 -03:00
|
|
|
version_path = os.path.join(document_dir, version_filename)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Guardar archivo
|
|
|
|
file.seek(0)
|
|
|
|
file.save(version_path)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Calcular checksum
|
|
|
|
checksum = calculate_checksum(version_path)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Obtener tamaño del archivo
|
|
|
|
file_size = os.path.getsize(version_path)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Preparar metadatos del documento
|
|
|
|
document_meta = {
|
2025-03-05 14:10:33 -03:00
|
|
|
"document_id": f"{document_id:03d}_{document_data['nombre'].lower().replace(' ', '_')}",
|
|
|
|
"original_filename": filename,
|
|
|
|
"versions": [
|
2025-03-03 15:35:24 -03:00
|
|
|
{
|
2025-03-05 14:10:33 -03:00
|
|
|
"version": version,
|
|
|
|
"filename": version_filename,
|
|
|
|
"created_at": datetime.now(pytz.UTC).isoformat(),
|
|
|
|
"created_by": creator_username,
|
|
|
|
"description": document_data.get("description", "Versión inicial"),
|
|
|
|
"file_size": file_size,
|
|
|
|
"mime_type": allowed_filetypes[extension]["mime_type"],
|
|
|
|
"checksum": checksum,
|
|
|
|
"downloads": [],
|
2025-03-03 15:35:24 -03:00
|
|
|
}
|
2025-03-05 14:10:33 -03:00
|
|
|
],
|
2025-03-03 15:35:24 -03:00
|
|
|
}
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Guardar metadatos
|
2025-03-05 14:10:33 -03:00
|
|
|
meta_file = os.path.join(document_dir, "meta.json")
|
2025-03-03 15:35:24 -03:00
|
|
|
save_json_file(meta_file, document_meta)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
return True, "Documento añadido correctamente.", document_id
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def add_version(project_id, document_id, version_data, file, creator_username):
|
|
|
|
"""
|
|
|
|
Añadir una nueva versión a un documento existente.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_id (int): ID del proyecto
|
|
|
|
document_id (int): ID del documento
|
|
|
|
version_data (dict): Datos de la versión
|
|
|
|
file: Objeto de archivo (de Flask)
|
|
|
|
creator_username (str): Usuario que crea la versión
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
tuple: (success, message, version_number)
|
|
|
|
"""
|
|
|
|
# Buscar directorio del proyecto
|
|
|
|
project_dir = find_project_directory(project_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not project_dir:
|
|
|
|
return False, f"No se encontró el proyecto con ID {project_id}.", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Buscar documento
|
|
|
|
document_dir = find_document_directory(project_dir, document_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not document_dir:
|
|
|
|
return False, f"No se encontró el documento con ID {document_id}.", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Cargar metadatos del documento
|
2025-03-05 14:10:33 -03:00
|
|
|
meta_file = os.path.join(document_dir, "meta.json")
|
2025-03-03 15:35:24 -03:00
|
|
|
document_meta = load_json_file(meta_file)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not document_meta:
|
|
|
|
return False, "Error al cargar metadatos del documento.", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Validar tipo de archivo
|
|
|
|
allowed_filetypes = get_allowed_filetypes()
|
|
|
|
filename = secure_filename(file.filename)
|
2025-03-05 14:10:33 -03:00
|
|
|
extension = filename.rsplit(".", 1)[1].lower() if "." in filename else ""
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if extension not in allowed_filetypes:
|
|
|
|
return False, f"Tipo de archivo no permitido: {extension}", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Verificar MIME type
|
2025-03-05 14:10:33 -03:00
|
|
|
if not check_file_type(file.stream, [allowed_filetypes[extension]["mime_type"]]):
|
2025-03-03 15:35:24 -03:00
|
|
|
return False, "El tipo de archivo no coincide con su extensión.", None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Determinar número de versión
|
2025-03-05 14:10:33 -03:00
|
|
|
last_version = max([v["version"] for v in document_meta["versions"]])
|
2025-03-03 15:35:24 -03:00
|
|
|
new_version = last_version + 1
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Preparar nombre de archivo
|
2025-03-05 14:10:33 -03:00
|
|
|
doc_name = (
|
|
|
|
document_meta["document_id"].split("_", 1)[1]
|
|
|
|
if "_" in document_meta["document_id"]
|
|
|
|
else "document"
|
|
|
|
)
|
2025-03-03 15:35:24 -03:00
|
|
|
version_filename = format_version_filename(new_version, doc_name, extension)
|
|
|
|
version_path = os.path.join(document_dir, version_filename)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Guardar archivo
|
|
|
|
file.seek(0)
|
|
|
|
file.save(version_path)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Calcular checksum
|
|
|
|
checksum = calculate_checksum(version_path)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Obtener tamaño del archivo
|
|
|
|
file_size = os.path.getsize(version_path)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Preparar metadatos de la versión
|
|
|
|
version_meta = {
|
2025-03-05 14:10:33 -03:00
|
|
|
"version": new_version,
|
|
|
|
"filename": version_filename,
|
|
|
|
"created_at": datetime.now(pytz.UTC).isoformat(),
|
|
|
|
"created_by": creator_username,
|
|
|
|
"description": version_data.get("description", f"Versión {new_version}"),
|
|
|
|
"file_size": file_size,
|
|
|
|
"mime_type": allowed_filetypes[extension]["mime_type"],
|
|
|
|
"checksum": checksum,
|
|
|
|
"downloads": [],
|
2025-03-03 15:35:24 -03:00
|
|
|
}
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Añadir versión a metadatos
|
2025-03-05 14:10:33 -03:00
|
|
|
document_meta["versions"].append(version_meta)
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Guardar metadatos actualizados
|
|
|
|
save_json_file(meta_file, document_meta)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
return True, "Nueva versión añadida correctamente.", new_version
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def get_document(project_id, document_id):
|
|
|
|
"""
|
|
|
|
Obtener información de un documento.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_id (int): ID del proyecto
|
|
|
|
document_id (int): ID del documento
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
dict: Datos del documento o None si no existe
|
|
|
|
"""
|
|
|
|
# Buscar directorio del proyecto
|
|
|
|
project_dir = find_project_directory(project_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not project_dir:
|
|
|
|
return None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Buscar documento
|
|
|
|
document_dir = find_document_directory(project_dir, document_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not document_dir:
|
|
|
|
return None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Cargar metadatos
|
2025-03-05 14:10:33 -03:00
|
|
|
meta_file = os.path.join(document_dir, "meta.json")
|
2025-03-03 15:35:24 -03:00
|
|
|
document_meta = load_json_file(meta_file)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Agregar ruta del directorio
|
|
|
|
if document_meta:
|
2025-03-05 14:10:33 -03:00
|
|
|
document_meta["directory"] = os.path.basename(document_dir)
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
return document_meta
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def get_document_version(project_id, document_id, version):
|
|
|
|
"""
|
|
|
|
Obtener información de una versión específica de un documento.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_id (int): ID del proyecto
|
|
|
|
document_id (int): ID del documento
|
|
|
|
version (int): Número de versión
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
tuple: (dict, str) - (Metadatos de la versión, ruta al archivo)
|
|
|
|
"""
|
|
|
|
document = get_document(project_id, document_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not document:
|
|
|
|
return None, None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Buscar versión específica
|
|
|
|
version_meta = None
|
2025-03-05 14:10:33 -03:00
|
|
|
for v in document["versions"]:
|
|
|
|
if v["version"] == int(version):
|
2025-03-03 15:35:24 -03:00
|
|
|
version_meta = v
|
|
|
|
break
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not version_meta:
|
|
|
|
return None, None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Preparar ruta al archivo
|
|
|
|
project_dir = find_project_directory(project_id)
|
|
|
|
document_dir = find_document_directory(project_dir, document_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
file_path = os.path.join(document_dir, version_meta["filename"])
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
return version_meta, file_path
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def get_latest_version(project_id, document_id):
|
|
|
|
"""
|
|
|
|
Obtener la última versión de un documento.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_id (int): ID del proyecto
|
|
|
|
document_id (int): ID del documento
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
tuple: (dict, str) - (Metadatos de la versión, ruta al archivo)
|
|
|
|
"""
|
|
|
|
document = get_document(project_id, document_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
|
|
|
if not document or not document["versions"]:
|
2025-03-03 15:35:24 -03:00
|
|
|
return None, None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Encontrar la versión más reciente
|
2025-03-05 14:10:33 -03:00
|
|
|
latest_version = max(document["versions"], key=lambda v: v["version"])
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Preparar ruta al archivo
|
|
|
|
project_dir = find_project_directory(project_id)
|
|
|
|
document_dir = find_document_directory(project_dir, document_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
file_path = os.path.join(document_dir, latest_version["filename"])
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
return latest_version, file_path
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def register_download(project_id, document_id, version, username):
|
|
|
|
"""
|
|
|
|
Registrar una descarga de documento.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_id (int): ID del proyecto
|
|
|
|
document_id (int): ID del documento
|
|
|
|
version (int): Número de versión
|
|
|
|
username (str): Usuario que descarga
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
bool: True si se registró correctamente, False en caso contrario
|
|
|
|
"""
|
|
|
|
# Buscar documento
|
|
|
|
project_dir = find_project_directory(project_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not project_dir:
|
|
|
|
return False
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
document_dir = find_document_directory(project_dir, document_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not document_dir:
|
|
|
|
return False
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Cargar metadatos
|
2025-03-05 14:10:33 -03:00
|
|
|
meta_file = os.path.join(document_dir, "meta.json")
|
2025-03-03 15:35:24 -03:00
|
|
|
document_meta = load_json_file(meta_file)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not document_meta:
|
|
|
|
return False
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Buscar versión
|
2025-03-05 14:10:33 -03:00
|
|
|
for v in document_meta["versions"]:
|
|
|
|
if v["version"] == int(version):
|
2025-03-03 15:35:24 -03:00
|
|
|
# Registrar descarga
|
|
|
|
download_info = {
|
2025-03-05 14:10:33 -03:00
|
|
|
"user_id": username,
|
|
|
|
"downloaded_at": datetime.now(pytz.UTC).isoformat(),
|
2025-03-03 15:35:24 -03:00
|
|
|
}
|
2025-03-05 14:10:33 -03:00
|
|
|
v["downloads"].append(download_info)
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Guardar metadatos actualizados
|
|
|
|
save_json_file(meta_file, document_meta)
|
|
|
|
return True
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
return False
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def find_document_directory(project_dir, document_id):
|
|
|
|
"""
|
|
|
|
Encontrar el directorio de un documento por su ID.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_dir (str): Ruta al directorio del proyecto
|
|
|
|
document_id (int): ID del documento
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
str: Ruta al directorio o None si no se encuentra
|
|
|
|
"""
|
2025-03-05 14:10:33 -03:00
|
|
|
documents_dir = os.path.join(project_dir, "documents")
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not os.path.exists(documents_dir):
|
|
|
|
return None
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Prefijo a buscar en nombres de directorios
|
|
|
|
prefix = f"@{int(document_id):03d}_@"
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
for dir_name in os.listdir(documents_dir):
|
|
|
|
if dir_name.startswith(prefix):
|
|
|
|
return os.path.join(documents_dir, dir_name)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
return None
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def get_project_documents(project_id):
|
|
|
|
"""
|
|
|
|
Obtener todos los documentos de un proyecto.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_id (int): ID del proyecto
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
list: Lista de documentos
|
|
|
|
"""
|
|
|
|
project_dir = find_project_directory(project_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not project_dir:
|
|
|
|
return []
|
2025-03-05 14:10:33 -03:00
|
|
|
|
|
|
|
documents_dir = os.path.join(project_dir, "documents")
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not os.path.exists(documents_dir):
|
|
|
|
return []
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
documents = []
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Iterar sobre directorios de documentos
|
|
|
|
for dir_name in os.listdir(documents_dir):
|
2025-03-05 14:10:33 -03:00
|
|
|
if dir_name.startswith("@") and os.path.isdir(
|
|
|
|
os.path.join(documents_dir, dir_name)
|
|
|
|
):
|
2025-03-03 15:35:24 -03:00
|
|
|
document_dir = os.path.join(documents_dir, dir_name)
|
2025-03-05 14:10:33 -03:00
|
|
|
meta_file = os.path.join(document_dir, "meta.json")
|
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if os.path.exists(meta_file):
|
|
|
|
document_meta = load_json_file(meta_file)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if document_meta:
|
|
|
|
# Extraer ID del documento del nombre del directorio
|
|
|
|
try:
|
2025-03-05 14:10:33 -03:00
|
|
|
doc_id = int(dir_name.split("_", 1)[0].replace("@", ""))
|
|
|
|
document_meta["id"] = doc_id
|
|
|
|
document_meta["directory"] = dir_name
|
2025-03-03 15:35:24 -03:00
|
|
|
documents.append(document_meta)
|
|
|
|
except (ValueError, IndexError):
|
|
|
|
pass
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
return documents
|
|
|
|
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
def delete_document(project_id, document_id):
|
|
|
|
"""
|
|
|
|
Eliminar un documento.
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Args:
|
|
|
|
project_id (int): ID del proyecto
|
|
|
|
document_id (int): ID del documento
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
Returns:
|
|
|
|
tuple: (success, message)
|
|
|
|
"""
|
|
|
|
# Buscar documento
|
|
|
|
project_dir = find_project_directory(project_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not project_dir:
|
|
|
|
return False, f"No se encontró el proyecto con ID {project_id}."
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
document_dir = find_document_directory(project_dir, document_id)
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
if not document_dir:
|
|
|
|
return False, f"No se encontró el documento con ID {document_id}."
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
# Eliminar directorio y contenido
|
|
|
|
try:
|
|
|
|
import shutil
|
2025-03-05 14:10:33 -03:00
|
|
|
|
2025-03-03 15:35:24 -03:00
|
|
|
shutil.rmtree(document_dir)
|
|
|
|
return True, "Documento eliminado correctamente."
|
|
|
|
except Exception as e:
|
2025-03-05 14:10:33 -03:00
|
|
|
return False, f"Error al eliminar el documento: {str(e)}"
|