mirror of
https://github.com/idrainformatica/PecFlow.git
synced 2026-06-16 12:45:42 +02:00
vboxes fix
This commit is contained in:
@@ -29,9 +29,9 @@ from typing import Any
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.database import AsyncSessionLocal
|
||||
from app.models import Mailbox, Message, SendJob
|
||||
from app.models import Attachment, Mailbox, Message, SendJob
|
||||
from app.smtp.sender import SmtpSender
|
||||
from app.storage.minio_client import upload_outbound_eml
|
||||
from app.storage.minio_client import download_attachment, upload_outbound_eml
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -105,6 +105,36 @@ async def send_pec(ctx: dict[str, Any], send_job_id: str) -> dict:
|
||||
f"per job {send_job_id} → {msg.to_addresses}"
|
||||
)
|
||||
|
||||
# ── Carica allegati da MinIO (se presenti) ────────────────────────────
|
||||
attachments_data: list[dict] | None = None
|
||||
if msg.has_attachments:
|
||||
att_result = await db.execute(
|
||||
select(Attachment).where(Attachment.message_id == msg.id)
|
||||
)
|
||||
att_records = list(att_result.scalars().all())
|
||||
if att_records:
|
||||
attachments_data = []
|
||||
for att in att_records:
|
||||
try:
|
||||
content = await download_attachment(att.storage_path)
|
||||
attachments_data.append(
|
||||
{
|
||||
"filename": att.filename,
|
||||
"content": content,
|
||||
"content_type": att.content_type or "application/octet-stream",
|
||||
}
|
||||
)
|
||||
logger.debug(
|
||||
f"[send_pec] Allegato caricato per invio: "
|
||||
f"{att.filename} ({len(content)} bytes)"
|
||||
)
|
||||
except Exception as att_err:
|
||||
logger.warning(
|
||||
f"[send_pec] Impossibile caricare allegato "
|
||||
f"'{att.filename}' ({att.storage_path}): {att_err}"
|
||||
)
|
||||
# Continua senza questo allegato
|
||||
|
||||
# ── Tenta invio SMTP ──────────────────────────────────────────────────
|
||||
try:
|
||||
sender = SmtpSender(mailbox)
|
||||
@@ -114,7 +144,7 @@ async def send_pec(ctx: dict[str, Any], send_job_id: str) -> dict:
|
||||
subject=msg.subject or "",
|
||||
body_text=msg.body_text or "",
|
||||
body_html=msg.body_html,
|
||||
attachments=None, # allegati in fase successiva (Fase 5)
|
||||
attachments=attachments_data,
|
||||
)
|
||||
|
||||
# ── Successo: aggiorna DB ─────────────────────────────────────────
|
||||
|
||||
@@ -180,6 +180,39 @@ async def upload_outbound_eml(
|
||||
raise
|
||||
|
||||
|
||||
async def download_attachment(storage_path: str) -> bytes:
|
||||
"""
|
||||
Scarica un allegato da MinIO e restituisce i byte.
|
||||
|
||||
Args:
|
||||
storage_path: percorso oggetto MinIO (senza bucket name)
|
||||
|
||||
Returns:
|
||||
Byte del file scaricato
|
||||
|
||||
Raises:
|
||||
Exception: se il download fallisce
|
||||
"""
|
||||
client = get_minio_client()
|
||||
bucket = settings.minio_bucket
|
||||
|
||||
try:
|
||||
response = await client.get_object(
|
||||
bucket_name=bucket,
|
||||
object_name=storage_path,
|
||||
)
|
||||
data = await response.read()
|
||||
response.close()
|
||||
await response.release()
|
||||
logger.debug(
|
||||
f"Allegato scaricato: s3://{bucket}/{storage_path} ({len(data)} bytes)"
|
||||
)
|
||||
return data
|
||||
except Exception as e:
|
||||
logger.error(f"Errore download allegato {storage_path}: {e}")
|
||||
raise
|
||||
|
||||
|
||||
async def ensure_bucket_exists() -> None:
|
||||
"""Verifica che il bucket MinIO esista, altrimenti lo crea."""
|
||||
client = get_minio_client()
|
||||
|
||||
Reference in New Issue
Block a user