Zeige PDF-Anlagen in der Mail-Ansicht, damit der Browser sie nicht unter einem zufälligen Namen speichert.
Deploy Development / deploy (push) Successful in 47s
Test Suite / pytest-backend (push) Successful in 6m16s
Test Suite / smoke-dev (push) Successful in 1s
Test Suite / frontend-build (push) Successful in 22s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-23 12:59:07 +02:00
1 parent d0f09830bb
commit cd239a3ab8
5 files changed
+164 -26

No files matched your search

+47 -3
View File
@@ -6,6 +6,7 @@ The same tree feeds preview, reading, attachment download and export.
from __future__ import annotations
import base64
import hashlib
import html
import re
@@ -164,9 +165,13 @@ def parse_mail(raw: bytes) -> ParsedMail:
missing.append(label or f"Anlage {index}")
notices.append(f"Die Anlage {label or index} ließ sich nicht vollständig lesen.")
continue
data = _unwrap_pdf_payload(data, media, label)
if _is_pdf(data):
media = "application/pdf"
label = _pdf_filename(label)
name = _unique_filename(label or f"anlage-{index}.bin", used)
inline = disposition == "inline" or bool(content_id)
attachments.append(_attachment(part, name, label or name, data, inline=inline))
attachments.append(_attachment(part, name, label or name, data, inline=inline, media=media))
simplified = "\n".join(chunk for chunk in plain if chunk).strip()
rendered = ""
if html_parts:
@@ -410,10 +415,49 @@ def _content_id(part: EmailMessage) -> str:
return token
def _attachment(part: EmailMessage, filename: str, label: str, data: bytes, inline: bool) -> AttachmentBytes:
def _is_pdf(data: bytes) -> bool:
return data[:1024].lstrip(b"\xef\xbb\xbf\r\n\t ").startswith(b"%PDF")
def _unwrap_pdf_payload(data: bytes, media: str, name: str) -> bytes:
"""A PDF that arrived as undecoded Base64 is still the PDF, not the transfer text."""
if _is_pdf(data):
return data
declared = (media or "").split(";", 1)[0].strip().lower()
hinted = "pdf" in declared or (name or "").lower().endswith(".pdf") or declared in {
"application/octet-stream",
"application/x-octet-stream",
"binary/octet-stream",
}
if not hinted:
return data
stripped = b"".join(data.split())
if len(stripped) < 16 or re.fullmatch(rb"[A-Za-z0-9+/=]+", stripped) is None:
return data
try:
decoded = base64.b64decode(stripped, validate=True)
except Exception:
return data
return decoded if _is_pdf(decoded) else data
def _pdf_filename(name: str) -> str:
text = (name or "").strip() or "anlage"
if text.lower().endswith(".pdf"):
return text
if text.lower().endswith(".bin"):
text = text[:-4].rstrip(" .") or "anlage"
leaf = text.replace("\\", "/").split("/")[-1]
if "." in leaf:
return text
return f"{text}.pdf"
def _attachment(part: EmailMessage, filename: str, label: str, data: bytes, inline: bool, media: str | None = None) -> AttachmentBytes:
resolved = (media or part.get_content_type() or "application/octet-stream").split(";", 1)[0].strip()
return AttachmentBytes(
filename=filename,
media_type=(part.get_content_type() or "application/octet-stream")[:120],
media_type=(resolved or "application/octet-stream")[:120],
data=data,
sha256=hashlib.sha256(data).hexdigest(),
display_name=label or filename,