Lade eine uebernommene E-Mail bei Bedarf vollstaendig, damit Anzeige und Export nicht an der Vorschau haengen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
17a3b3a736
commit
7626c56f3d
7 files changed
+740
-19
No files matched your search
@@ -13,6 +13,8 @@ from app.api.deps import Principal, get_conn, get_principal
|
||||
from app.api.routes import _created, _ctx_model
|
||||
from app.api.schemas import ConnectorIn, ExternalIn
|
||||
from app.core.errors import not_found, unprocessable
|
||||
from app.mailpack import parse_mail
|
||||
from app.media.storage import content_disposition
|
||||
from app.ops import create_external
|
||||
from app.sources import (
|
||||
clean_base_url,
|
||||
@@ -21,6 +23,7 @@ from app.sources import (
|
||||
imap_messages,
|
||||
imap_preview,
|
||||
list_connectors,
|
||||
load_imap_original,
|
||||
paperless_content,
|
||||
paperless_documents,
|
||||
probe,
|
||||
@@ -93,6 +96,29 @@ def view_mail(
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
ref = _mail_ref(conn, principal, element_id, context_kind, context_id)
|
||||
parsed, _raw = _complete_mail(conn, principal, ref)
|
||||
return {
|
||||
"title": parsed.subject,
|
||||
"message_from": parsed.sender,
|
||||
"message_to": parsed.recipient,
|
||||
"message_cc": parsed.copy,
|
||||
"text": parsed.text,
|
||||
"text_truncated": False,
|
||||
"complete": True,
|
||||
"attachments": [
|
||||
{
|
||||
"index": index,
|
||||
"filename": item.filename,
|
||||
"content_type": item.media_type,
|
||||
"byte_size": len(item.data),
|
||||
}
|
||||
for index, item in enumerate(parsed.attachments)
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _mail_ref(conn, principal: Principal, element_id: UUID, context_kind: str | None, context_id: str | None):
|
||||
element = get_element(conn, element_id)
|
||||
if element is None or element["kind"] != "external_ref":
|
||||
raise not_found()
|
||||
@@ -100,18 +126,79 @@ def view_mail(
|
||||
if not rights.read:
|
||||
raise not_found()
|
||||
ref = conn.execute(
|
||||
"SELECT source, detail FROM external_references WHERE element_id = %s",
|
||||
"SELECT source, external_id, detail FROM external_references WHERE element_id = %s",
|
||||
(element_id,),
|
||||
).fetchone()
|
||||
return ref
|
||||
|
||||
|
||||
def _complete_mail(conn, principal: Principal, ref):
|
||||
detail = (ref or {}).get("detail") or {}
|
||||
if ref is None or ref["source"] != "imap":
|
||||
raise unprocessable("Das ist keine E-Mail.")
|
||||
connector_id = detail.get("connector_id")
|
||||
mailbox = detail.get("mailbox") or ""
|
||||
uid = detail.get("uid")
|
||||
if not connector_id or not mailbox or not uid:
|
||||
raise unprocessable("Für diese E-Mail ist keine Postfach-Vorschau hinterlegt.")
|
||||
return imap_preview(conn, principal.account_id, UUID(str(connector_id)), str(mailbox), int(uid))
|
||||
mailbox = str(detail.get("mailbox") or "").strip()
|
||||
uid = _mail_uid(detail.get("uid"))
|
||||
if not connector_id or not mailbox or uid is None:
|
||||
raise unprocessable("Für diese E-Mail fehlt die Zuordnung zu Fach und Nachricht.")
|
||||
raw = load_imap_original(conn, principal.account_id, UUID(str(connector_id)), mailbox, uid, str(ref["external_id"]))
|
||||
parsed = parse_mail(raw)
|
||||
if parsed.missing:
|
||||
names = ", ".join(parsed.missing[:5])
|
||||
raise unprocessable(f"Eine Anlage der E-Mail ließ sich nicht vollständig lesen: {names}.")
|
||||
return parsed, raw
|
||||
|
||||
|
||||
def _mail_uid(value) -> int | None:
|
||||
if isinstance(value, bool):
|
||||
return None
|
||||
if isinstance(value, int) and value >= 1:
|
||||
return value
|
||||
if isinstance(value, str) and value.isdigit() and int(value) >= 1:
|
||||
return int(value)
|
||||
return None
|
||||
|
||||
|
||||
@router.get("/api/v1/external-refs/{element_id}/mail/original")
|
||||
def download_mail(
|
||||
element_id: UUID,
|
||||
context_kind: str | None = None,
|
||||
context_id: str | None = None,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
ref = _mail_ref(conn, principal, element_id, context_kind, context_id)
|
||||
_parsed, raw = _complete_mail(conn, principal, ref)
|
||||
return _mail_bytes(raw, "message/rfc822", "nachricht.eml")
|
||||
|
||||
|
||||
@router.get("/api/v1/external-refs/{element_id}/mail/attachments/{index}")
|
||||
def download_mail_attachment(
|
||||
element_id: UUID,
|
||||
index: int,
|
||||
context_kind: str | None = None,
|
||||
context_id: str | None = None,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
ref = _mail_ref(conn, principal, element_id, context_kind, context_id)
|
||||
parsed, _raw = _complete_mail(conn, principal, ref)
|
||||
if index < 0 or index >= len(parsed.attachments):
|
||||
raise not_found()
|
||||
item = parsed.attachments[index]
|
||||
return _mail_bytes(item.data, item.media_type or "application/octet-stream", item.filename)
|
||||
|
||||
|
||||
def _mail_bytes(data: bytes, media_type: str, filename: str) -> Response:
|
||||
return Response(
|
||||
content=data,
|
||||
media_type=media_type,
|
||||
headers={
|
||||
"Content-Disposition": content_disposition(filename),
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/sources/paperless/documents")
|
||||
|
||||
Reference in new issue
Block a user