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")
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
"""Pure mail identity and attachment extraction for a complete export.
|
||||
|
||||
A preview cutoff does not belong here. Duplicate attachment names stay distinct.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from email.parser import BytesParser
|
||||
|
||||
_UID_PREFIX = "uid:"
|
||||
|
||||
|
||||
class MailIdentityError(Exception):
|
||||
def __init__(self, message: str) -> None:
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AttachmentBytes:
|
||||
filename: str
|
||||
media_type: str
|
||||
data: bytes
|
||||
sha256: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class ParsedMail:
|
||||
message_id: str
|
||||
subject: str
|
||||
sender: str
|
||||
recipient: str
|
||||
copy: str
|
||||
text: str
|
||||
attachments: list[AttachmentBytes] = field(default_factory=list)
|
||||
missing: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
def header_message_id(raw: bytes) -> str:
|
||||
message = BytesParser().parsebytes(raw)
|
||||
return str(message.get("Message-ID") or "").strip()
|
||||
|
||||
|
||||
def identities_match(found: str | None, expected: str | None) -> bool:
|
||||
"""A stored uid fallback matches only a message that still has no Message-ID."""
|
||||
expected_text = (expected or "").strip()
|
||||
found_text = (found or "").strip()
|
||||
if expected_text.casefold().startswith(_UID_PREFIX):
|
||||
return found_text == ""
|
||||
if not expected_text or not found_text:
|
||||
return False
|
||||
return _bare(expected_text) == _bare(found_text)
|
||||
|
||||
|
||||
def choose_message(candidate: bytes | None, alternates: list[bytes], expected: str) -> bytes:
|
||||
"""Keep the stored message when it still matches. Otherwise accept one unambiguous hit."""
|
||||
if candidate is not None and identities_match(header_message_id(candidate), expected):
|
||||
return candidate
|
||||
if (expected or "").strip().casefold().startswith(_UID_PREFIX):
|
||||
raise MailIdentityError("Die gespeicherte Nachricht lässt sich nicht eindeutig wiederfinden.")
|
||||
matches = [raw for raw in alternates if identities_match(header_message_id(raw), expected)]
|
||||
if len(matches) == 1:
|
||||
return matches[0]
|
||||
if len(matches) > 1:
|
||||
raise MailIdentityError("Die Nachrichtenidentität ist mehrdeutig. Es wurde keine E-Mail übernommen.")
|
||||
raise MailIdentityError("Die gespeicherte Nachricht lässt sich nicht eindeutig wiederfinden.")
|
||||
|
||||
|
||||
def parse_mail(raw: bytes) -> ParsedMail:
|
||||
message = BytesParser().parsebytes(raw)
|
||||
attachments: list[AttachmentBytes] = []
|
||||
missing: list[str] = []
|
||||
used: set[str] = set()
|
||||
index = 0
|
||||
for part in message.walk():
|
||||
if part.is_multipart():
|
||||
continue
|
||||
filename = part.get_filename()
|
||||
disposition = (part.get_content_disposition() or "").lower()
|
||||
if not filename and disposition != "attachment":
|
||||
continue
|
||||
index += 1
|
||||
label = _header_text(filename) if filename else f"anlage-{index}.bin"
|
||||
payload = part.get_payload(decode=True)
|
||||
if not isinstance(payload, (bytes, bytearray)):
|
||||
missing.append(label or f"Anlage {index}")
|
||||
continue
|
||||
name = _unique_filename(label or f"anlage-{index}.bin", used)
|
||||
data = bytes(payload)
|
||||
attachments.append(
|
||||
AttachmentBytes(
|
||||
filename=name,
|
||||
media_type=(part.get_content_type() or "application/octet-stream")[:120],
|
||||
data=data,
|
||||
sha256=hashlib.sha256(data).hexdigest(),
|
||||
)
|
||||
)
|
||||
text = _body_text(message)
|
||||
return ParsedMail(
|
||||
message_id=str(message.get("Message-ID") or "").strip(),
|
||||
subject=_header_text(message.get("Subject")) or "(ohne Betreff)",
|
||||
sender=_header_text(message.get("From")),
|
||||
recipient=_header_text(message.get("To")),
|
||||
copy=_header_text(message.get("Cc")),
|
||||
text=text,
|
||||
attachments=attachments,
|
||||
missing=missing,
|
||||
)
|
||||
|
||||
|
||||
def _bare(value: str) -> str:
|
||||
return value.strip().strip("<>").strip().casefold()
|
||||
|
||||
|
||||
def _header_text(value) -> str:
|
||||
if value is None:
|
||||
return ""
|
||||
text = str(value).replace("\r", " ").replace("\n", " ")
|
||||
return re.sub(r"\s+", " ", text).strip()
|
||||
|
||||
|
||||
def _unique_filename(name: str, used: set[str]) -> str:
|
||||
cleaned = name.replace("\\", "/").split("/")[-1].strip() or "anlage.bin"
|
||||
cleaned = cleaned[:180]
|
||||
if cleaned not in used:
|
||||
used.add(cleaned)
|
||||
return cleaned
|
||||
stem, dot, ext = cleaned.rpartition(".")
|
||||
if not dot:
|
||||
stem, ext = cleaned, ""
|
||||
else:
|
||||
ext = f".{ext}"
|
||||
number = 2
|
||||
while True:
|
||||
candidate = f"{stem} ({number}){ext}"
|
||||
if candidate not in used:
|
||||
used.add(candidate)
|
||||
return candidate
|
||||
number += 1
|
||||
|
||||
|
||||
def _body_text(message) -> str:
|
||||
plain: list[str] = []
|
||||
html: list[str] = []
|
||||
parts = message.walk() if message.is_multipart() else [message]
|
||||
for part in parts:
|
||||
if part.is_multipart() or part.get_content_disposition() == "attachment" or part.get_filename():
|
||||
continue
|
||||
kind = part.get_content_type()
|
||||
payload = part.get_payload(decode=True)
|
||||
if isinstance(payload, (bytes, bytearray)):
|
||||
charset = part.get_content_charset() or "utf-8"
|
||||
text = bytes(payload).decode(charset, errors="replace").strip()
|
||||
else:
|
||||
raw = part.get_payload()
|
||||
text = raw.strip() if isinstance(raw, str) else ""
|
||||
if kind == "text/plain":
|
||||
plain.append(text)
|
||||
elif kind == "text/html":
|
||||
html.append(_html_text(text))
|
||||
body = "\n".join(chunk for chunk in plain if chunk).strip()
|
||||
if not body:
|
||||
body = "\n".join(chunk for chunk in html if chunk).strip()
|
||||
return body
|
||||
|
||||
|
||||
def _html_text(value: str) -> str:
|
||||
text = re.sub(r"(?is)<(script|style)\b.*?>.*?</\1>", " ", value)
|
||||
text = re.sub(r"(?i)<br\s*/?>", "\n", text)
|
||||
text = re.sub(r"(?i)</p>", "\n", text)
|
||||
text = re.sub(r"<[^>]+>", " ", text)
|
||||
return re.sub(r"[ \t]{2,}", " ", text).strip()
|
||||
@@ -0,0 +1,186 @@
|
||||
"""Complete content retrieval for export.
|
||||
|
||||
Preview and original stay apart. A reference uses its own access, never an
|
||||
arbitrary latest connector. Missing capability is reported, not replaced.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from uuid import UUID
|
||||
|
||||
from app.core.errors import Problem
|
||||
from app.mailpack import ParsedMail, identities_match, parse_mail
|
||||
from app.media.storage import asset_path, read_bytes
|
||||
from app.sources import COMPLETE_MAIL_LIMIT, pull_paperless_original, pull_rfc822
|
||||
|
||||
EXPORT_BYTE_LIMIT = COMPLETE_MAIL_LIMIT
|
||||
|
||||
_TOO_LARGE = (
|
||||
"Die vollständige Nachricht überschreitet die Exportgrenze. "
|
||||
"Eine gekürzte Vorschau wird nicht als Original übernommen."
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RetrievalFailure:
|
||||
code: str
|
||||
message: str
|
||||
choices: tuple[dict, ...] = ()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FilePayload:
|
||||
filename: str
|
||||
media_type: str
|
||||
data: bytes
|
||||
sha256: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class MailPayload:
|
||||
raw: bytes
|
||||
parsed: ParsedMail
|
||||
notices: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
def load_external(conn, actor_id: UUID, element_id: UUID, assigned: UUID | None) -> FilePayload | MailPayload | RetrievalFailure:
|
||||
ref = conn.execute(
|
||||
"SELECT source, external_id, detail FROM external_references WHERE element_id = %s",
|
||||
(element_id,),
|
||||
).fetchone()
|
||||
if ref is None:
|
||||
return RetrievalFailure("missing", "Die Quellenreferenz fehlt.")
|
||||
source = ref["source"]
|
||||
detail = ref["detail"] or {}
|
||||
if source == "paperless":
|
||||
return _load_paperless(conn, actor_id, ref["external_id"], detail, assigned)
|
||||
if source == "imap":
|
||||
return _load_imap(conn, actor_id, ref["external_id"], detail, assigned)
|
||||
if source == "url":
|
||||
return RetrievalFailure("capability", "Für eine Webadresse ist kein Originalabruf vorgesehen.")
|
||||
return RetrievalFailure("capability", "Für diese Quelle ist kein Originalabruf vorgesehen.")
|
||||
|
||||
|
||||
def load_file(conn, element_id: UUID, media_root: str) -> FilePayload | RetrievalFailure:
|
||||
asset = conn.execute(
|
||||
"""
|
||||
SELECT id, original_filename, content_type, sha256, status
|
||||
FROM media_assets WHERE element_id = %s
|
||||
""",
|
||||
(element_id,),
|
||||
).fetchone()
|
||||
if asset is None or asset["status"] != "active":
|
||||
return RetrievalFailure("missing", "Die Originaldatei ist nicht mehr vorhanden.")
|
||||
try:
|
||||
data = read_bytes(asset_path(media_root, asset["id"]))
|
||||
except Problem:
|
||||
return RetrievalFailure("missing", "Die Originaldatei ist nicht mehr vorhanden.")
|
||||
if len(data) > EXPORT_BYTE_LIMIT:
|
||||
return RetrievalFailure("too_large", "Die Originaldatei ist für den Export zu groß.")
|
||||
return FilePayload(asset["original_filename"], asset["content_type"], data, asset["sha256"])
|
||||
|
||||
|
||||
def _load_paperless(conn, actor_id: UUID, external_id: str, detail: dict, assigned: UUID | None):
|
||||
stored = _uuid(detail.get("connector_id"))
|
||||
resolved = _resolve_connector(conn, actor_id, "paperless", stored, assigned)
|
||||
if isinstance(resolved, RetrievalFailure):
|
||||
return resolved
|
||||
try:
|
||||
data, media, name = pull_paperless_original(
|
||||
resolved["settings"]["base_url"].rstrip("/"),
|
||||
resolved["secret"],
|
||||
str(external_id),
|
||||
EXPORT_BYTE_LIMIT,
|
||||
)
|
||||
except Problem as exc:
|
||||
return _from_problem(exc)
|
||||
if len(data) > EXPORT_BYTE_LIMIT:
|
||||
return RetrievalFailure("too_large", "Die Originaldatei ist für den Export zu groß.")
|
||||
return FilePayload(name or f"paperless-{external_id}", media, data, "")
|
||||
|
||||
|
||||
def _load_imap(conn, actor_id: UUID, external_id: str, detail: dict, assigned: UUID | None):
|
||||
stored = _uuid(detail.get("connector_id"))
|
||||
resolved = _resolve_connector(conn, actor_id, "imap", stored, assigned)
|
||||
if isinstance(resolved, RetrievalFailure):
|
||||
return resolved
|
||||
mailbox = str(detail.get("mailbox") or "").strip()
|
||||
uid = detail.get("uid")
|
||||
if isinstance(uid, str) and uid.isdigit():
|
||||
uid = int(uid)
|
||||
if not mailbox or isinstance(uid, bool) or not isinstance(uid, int) or uid < 1:
|
||||
return RetrievalFailure("identity", "Für diese E-Mail fehlt die Zuordnung zu Fach und Nachricht.")
|
||||
try:
|
||||
raw = pull_rfc822(resolved["settings"], resolved["secret"], mailbox, uid, str(external_id), EXPORT_BYTE_LIMIT)
|
||||
except Problem as exc:
|
||||
return _from_problem(exc)
|
||||
if len(raw) > EXPORT_BYTE_LIMIT:
|
||||
return RetrievalFailure("too_large", _TOO_LARGE)
|
||||
parsed = parse_mail(raw)
|
||||
if not identities_match(parsed.message_id, str(external_id)):
|
||||
return RetrievalFailure("identity", "Die gefundene Nachricht ist nicht die referenzierte E-Mail.")
|
||||
if parsed.missing:
|
||||
names = ", ".join(parsed.missing[:5])
|
||||
return RetrievalFailure("attachment", f"Eine Anlage der E-Mail ließ sich nicht vollständig lesen: {names}.")
|
||||
return MailPayload(raw, parsed)
|
||||
|
||||
|
||||
def _resolve_connector(conn, actor_id: UUID, kind: str, stored: UUID | None, assigned: UUID | None):
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, kind, name, settings, secret
|
||||
FROM connectors
|
||||
WHERE owner_id = %s AND kind = %s
|
||||
ORDER BY name, id
|
||||
""",
|
||||
(actor_id, kind),
|
||||
).fetchall()
|
||||
if stored is not None and assigned is None:
|
||||
match = next((row for row in rows if row["id"] == stored), None)
|
||||
if match is None:
|
||||
return RetrievalFailure("missing", "Der gespeicherte Zugang für diese Quelle ist nicht mehr vorhanden.")
|
||||
return match
|
||||
chosen = assigned or stored
|
||||
if chosen is not None:
|
||||
match = next((row for row in rows if row["id"] == chosen), None)
|
||||
if match is None:
|
||||
return RetrievalFailure(
|
||||
"assignment",
|
||||
"Der gewählte Zugang gehört nicht zu diesem Konto oder passt nicht zur Quelle.",
|
||||
)
|
||||
return match
|
||||
if len(rows) == 1:
|
||||
return rows[0]
|
||||
if not rows:
|
||||
return RetrievalFailure("missing", "Für diese Quelle ist kein Zugang vorhanden.")
|
||||
return RetrievalFailure(
|
||||
"ambiguous",
|
||||
"Für diese Quelle ist kein eindeutiger Zugang zugeordnet. Die Zuordnung muss bewusst gewählt werden.",
|
||||
tuple({"id": str(row["id"]), "name": row["name"]} for row in rows),
|
||||
)
|
||||
|
||||
|
||||
def _uuid(value) -> UUID | None:
|
||||
if not value:
|
||||
return None
|
||||
try:
|
||||
return UUID(str(value))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def _from_problem(exc: Problem) -> RetrievalFailure:
|
||||
text = exc.detail
|
||||
lowered = text.casefold()
|
||||
if "exportgrenze" in lowered or "zu groß" in lowered:
|
||||
code = "too_large"
|
||||
elif "mehrdeutig" in lowered or "nicht eindeutig" in lowered or "nicht die referenzierte" in lowered:
|
||||
code = "identity"
|
||||
elif "nicht erreichbar" in lowered or "abgelehnt" in lowered:
|
||||
code = "unreachable"
|
||||
elif "vorschau wird nicht" in lowered or "nicht verfügbar" in lowered or "gibt es" in lowered:
|
||||
code = "missing"
|
||||
else:
|
||||
code = "missing"
|
||||
return RetrievalFailure(code, text)
|
||||
+133
-5
@@ -20,6 +20,7 @@ from psycopg.types.json import Json
|
||||
|
||||
from app.core.errors import Problem, not_found, unprocessable
|
||||
from app.core.settings import get_settings
|
||||
from app.mailpack import MailIdentityError, choose_message, identities_match, header_message_id
|
||||
from app.media.storage import sniff
|
||||
|
||||
|
||||
@@ -227,7 +228,12 @@ def paperless_content(conn, owner_id: UUID, document_id: str) -> tuple[bytes, st
|
||||
raise unprocessable("Paperless stellt diese Datei nicht zum Ansehen bereit.")
|
||||
|
||||
|
||||
def _paperless_file(base: str, token: str, path: str) -> tuple[bytes, str, str] | None:
|
||||
def _paperless_file(
|
||||
base: str,
|
||||
token: str,
|
||||
path: str,
|
||||
max_bytes: int = 40 * 1024 * 1024,
|
||||
) -> tuple[bytes, str, str] | None:
|
||||
request = Request(
|
||||
f"{base}{path}",
|
||||
headers={"Authorization": f"Token {token}", "Accept": "application/pdf, image/*, */*"},
|
||||
@@ -238,8 +244,10 @@ def _paperless_file(base: str, token: str, path: str) -> tuple[bytes, str, str]
|
||||
allowed = urlparse(base)
|
||||
if final.scheme not in {"http", "https"} or final.netloc != allowed.netloc:
|
||||
raise unprocessable("Paperless hat auf eine fremde Adresse verwiesen.")
|
||||
data = response.read(40 * 1024 * 1024 + 1)
|
||||
if len(data) > 40 * 1024 * 1024:
|
||||
data = response.read(max_bytes + 1)
|
||||
if len(data) > max_bytes:
|
||||
if max_bytes > 40 * 1024 * 1024:
|
||||
raise unprocessable("Die Originaldatei ist für den Export zu groß.")
|
||||
raise unprocessable("Die Datei ist zum Ansehen in Tsumugu zu groß.")
|
||||
media = (response.headers.get("Content-Type") or "application/octet-stream").split(";")[0].strip()
|
||||
return data, media, _filename(response.headers.get("Content-Disposition"))
|
||||
@@ -305,7 +313,12 @@ def _paperless_from_disk(document_id: str, meta: dict) -> tuple[bytes, str, str]
|
||||
return None
|
||||
|
||||
|
||||
def read_paperless_file(media_root: str, kind: str, relative: str) -> bytes | None:
|
||||
def read_paperless_file(
|
||||
media_root: str,
|
||||
kind: str,
|
||||
relative: str,
|
||||
max_bytes: int = 40 * 1024 * 1024,
|
||||
) -> bytes | None:
|
||||
if kind not in {"archive", "originals"}:
|
||||
return None
|
||||
raw = relative.replace("\\", "/").strip()
|
||||
@@ -318,7 +331,9 @@ def read_paperless_file(media_root: str, kind: str, relative: str) -> bytes | No
|
||||
if not target.is_file():
|
||||
return None
|
||||
data = target.read_bytes()
|
||||
if len(data) > 40 * 1024 * 1024:
|
||||
if len(data) > max_bytes:
|
||||
if max_bytes > 40 * 1024 * 1024:
|
||||
raise unprocessable("Die Originaldatei ist für den Export zu groß.")
|
||||
raise unprocessable("Die Datei ist zum Ansehen in Tsumugu zu groß.")
|
||||
return data
|
||||
|
||||
@@ -748,3 +763,116 @@ def _decode(value: str | None) -> str:
|
||||
else:
|
||||
parts.append(text)
|
||||
return "".join(parts).strip()
|
||||
|
||||
|
||||
def pull_paperless_original(base: str, token: str, document_id: str, max_bytes: int) -> tuple[bytes, str, str]:
|
||||
"""Original download only. A preview is never returned in its place."""
|
||||
if not document_id.isdigit() or len(document_id) > 12:
|
||||
raise unprocessable("Die Paperless-Nummer ist ungültig.")
|
||||
fetched = _paperless_file(base, token, f"/api/documents/{document_id}/download/", max_bytes)
|
||||
if fetched is not None:
|
||||
data, media, name = fetched
|
||||
return data, media or "application/octet-stream", name or f"paperless-{document_id}"
|
||||
meta = _paperless_metadata(base, token, document_id)
|
||||
if meta is None:
|
||||
raise unprocessable("Dieses Dokument gibt es in Paperless nicht.")
|
||||
local = _paperless_original_from_disk(meta, max_bytes)
|
||||
if local is None:
|
||||
raise unprocessable(
|
||||
"Das Paperless-Original ist nicht verfügbar. Eine Vorschau wird nicht als Original übernommen."
|
||||
)
|
||||
return local
|
||||
|
||||
|
||||
def _paperless_original_from_disk(meta: dict, max_bytes: int) -> tuple[bytes, str, str] | None:
|
||||
root = get_settings().paperless_media_root.strip()
|
||||
original_name = meta.get("media_filename")
|
||||
if not root or not isinstance(original_name, str) or not original_name.strip():
|
||||
return None
|
||||
data = read_paperless_file(root, "originals", original_name, max_bytes)
|
||||
if data is None:
|
||||
return None
|
||||
return data, "application/octet-stream", Path(original_name).name
|
||||
|
||||
|
||||
COMPLETE_MAIL_LIMIT = 80 * 1024 * 1024
|
||||
|
||||
|
||||
def load_imap_original(conn, owner_id: UUID, connector_id: UUID, mailbox: str, uid: int, expected_id: str) -> bytes:
|
||||
"""The referenced message in full. A preview is not returned in its place."""
|
||||
row = _owned(conn, owner_id, connector_id)
|
||||
if row["kind"] != "imap":
|
||||
raise unprocessable("Das ist kein E-Mail-Konto.")
|
||||
return pull_rfc822(row["settings"], row["secret"], mailbox, uid, expected_id, COMPLETE_MAIL_LIMIT)
|
||||
|
||||
|
||||
def pull_rfc822(settings: dict, secret: str, mailbox: str, uid: int, expected_id: str, max_bytes: int) -> bytes:
|
||||
"""Fetch the referenced message. A different message at the old UID is not returned."""
|
||||
client = _imap_login(settings, secret)
|
||||
try:
|
||||
_select_mailbox(client, mailbox)
|
||||
candidate = _fetch_rfc822(client, uid)
|
||||
if candidate is not None and len(candidate) > max_bytes:
|
||||
raise unprocessable(
|
||||
"Die vollständige Nachricht überschreitet die Exportgrenze. "
|
||||
"Eine gekürzte Vorschau wird nicht als Original übernommen."
|
||||
)
|
||||
alternates: list[bytes] = []
|
||||
if candidate is None or not identities_match(header_message_id(candidate), expected_id):
|
||||
alternates = _search_rfc822(client, expected_id, max_bytes)
|
||||
try:
|
||||
chosen = choose_message(candidate, alternates, expected_id)
|
||||
except MailIdentityError as exc:
|
||||
raise unprocessable(exc.message) from exc
|
||||
if len(chosen) > max_bytes:
|
||||
raise unprocessable(
|
||||
"Die vollständige Nachricht überschreitet die Exportgrenze. "
|
||||
"Eine gekürzte Vorschau wird nicht als Original übernommen."
|
||||
)
|
||||
return chosen
|
||||
finally:
|
||||
_imap_logout(client)
|
||||
|
||||
|
||||
def _fetch_rfc822(client: imaplib.IMAP4, uid: int) -> bytes | None:
|
||||
try:
|
||||
status, fetched = client.uid("FETCH", str(uid), "(BODY.PEEK[])")
|
||||
except imaplib.IMAP4.error:
|
||||
return None
|
||||
if status != "OK":
|
||||
return None
|
||||
return _literal(fetched)
|
||||
|
||||
|
||||
def _search_rfc822(client: imaplib.IMAP4, expected_id: str, max_bytes: int) -> list[bytes]:
|
||||
token = (expected_id or "").strip()
|
||||
if not token or token.casefold().startswith("uid:"):
|
||||
return []
|
||||
found: list[bytes] = []
|
||||
for folder in _imap_mailboxes(client):
|
||||
if not folder["selectable"]:
|
||||
continue
|
||||
try:
|
||||
_select_mailbox(client, folder["name"])
|
||||
status, data = client.uid("SEARCH", "HEADER", "Message-ID", token)
|
||||
except (imaplib.IMAP4.error, Problem):
|
||||
continue
|
||||
if status != "OK" or not data or not data[0]:
|
||||
continue
|
||||
for raw_uid in data[0].split():
|
||||
try:
|
||||
uid = int(raw_uid)
|
||||
except ValueError:
|
||||
continue
|
||||
blob = _fetch_rfc822(client, uid)
|
||||
if blob is None:
|
||||
continue
|
||||
if len(blob) > max_bytes:
|
||||
raise unprocessable(
|
||||
"Die vollständige Nachricht überschreitet die Exportgrenze. "
|
||||
"Eine gekürzte Vorschau wird nicht als Original übernommen."
|
||||
)
|
||||
found.append(blob)
|
||||
if len(found) > 5:
|
||||
return found
|
||||
return found
|
||||
@@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import app.sources as sources
|
||||
from tests.support import Actor, register
|
||||
from tests.support import Actor, matter_of, register
|
||||
|
||||
|
||||
class FakeIMAP:
|
||||
@@ -215,3 +215,68 @@ def test_oversized_message_still_returns_text(client, db, monkeypatch):
|
||||
assert preview["text_truncated"] is True
|
||||
assert preview["direction"] == "in"
|
||||
assert preview["message_from"]
|
||||
|
||||
|
||||
def _raw(actor: Actor, path: str):
|
||||
return actor.client.get(
|
||||
path,
|
||||
headers={
|
||||
"Origin": "http://testserver",
|
||||
"X-CSRF-Token": actor.csrf,
|
||||
"X-Scope-Kind": actor.scope["kind"],
|
||||
"X-Scope-Id": actor.scope["id"],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_an_adopted_message_loads_in_full_with_its_attachment(client, db, monkeypatch):
|
||||
ada = Actor(client, "ada-voll@example.test", "Ada Voll")
|
||||
ben = Actor(client, "ben-voll@example.test", "Ben Voll")
|
||||
register(db, ada.email, ada.name)
|
||||
register(db, ben.email, ben.name)
|
||||
ada.login()
|
||||
account = _account(monkeypatch, ada)
|
||||
matter = matter_of(ada, "Nebenkosten")
|
||||
created = ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Nachricht 2",
|
||||
"source": "imap",
|
||||
"external_id": "<2@example.test>",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"detail": {"connector_id": account, "mailbox": "INBOX", "uid": 2, "direction": "in"},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
letter = ada.request("GET", f"/api/v1/external-refs/{created['id']}/mail")
|
||||
assert letter["complete"] is True
|
||||
assert letter["text_truncated"] is False
|
||||
assert "Einwendung" in letter["text"]
|
||||
assert letter["attachments"][0]["filename"] == "einwendung.pdf"
|
||||
assert letter["attachments"][0]["content_type"] == "application/pdf"
|
||||
assert letter["attachments"][0]["byte_size"] >= 4
|
||||
original = _raw(ada, f"/api/v1/external-refs/{created['id']}/mail/original")
|
||||
assert original.status_code == 200
|
||||
assert b"Message-ID: <2@example.test>" in original.content
|
||||
assert b"%PDF" in original.content
|
||||
attachment = _raw(ada, f"/api/v1/external-refs/{created['id']}/mail/attachments/0")
|
||||
assert attachment.status_code == 200
|
||||
assert attachment.content.startswith(b"%PDF")
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Fremde Nachricht",
|
||||
"source": "imap",
|
||||
"external_id": "<andere@example.test>",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"detail": {"connector_id": account, "mailbox": "INBOX", "uid": 2},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
shown = ada.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
wrong = next(item for item in shown["bindings"] if item["element"]["title"] == "Fremde Nachricht")
|
||||
ada.request("GET", f"/api/v1/external-refs/{wrong['element']['id']}/mail", status=422)
|
||||
ben.login()
|
||||
ben.request("GET", f"/api/v1/external-refs/{created['id']}/mail", status=404)
|
||||
@@ -67,6 +67,35 @@ export async function api(path, { method = "GET", body, form } = {}) {
|
||||
return data;
|
||||
}
|
||||
|
||||
function mailQuery(context) {
|
||||
const params = new URLSearchParams();
|
||||
if (context?.kind && context?.id) {
|
||||
params.set("context_kind", context.kind);
|
||||
params.set("context_id", context.id);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
export async function downloadMailPart(elementId, context, suffix, filename) {
|
||||
const response = await fetch(`/api/v1/external-refs/${elementId}/mail/${suffix}?${mailQuery(context)}`, {
|
||||
credentials: "include",
|
||||
headers: scopeHeaders(),
|
||||
});
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => null);
|
||||
throw new Error(data?.detail || "Die vollständige E-Mail ist nicht verfügbar.");
|
||||
}
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = filename || "nachricht.eml";
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
||||
}
|
||||
|
||||
export async function fetchPaperless(elementId, context) {
|
||||
const params = new URLSearchParams();
|
||||
if (context?.kind && context?.id) {
|
||||
@@ -122,6 +151,36 @@ export async function downloadFile(elementId, context, filename) {
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export async function downloadPost(path, body, fallbackName) {
|
||||
const headers = { ...scopeHeaders(), "Content-Type": "application/json" };
|
||||
if (csrf) headers["X-CSRF-Token"] = csrf;
|
||||
const response = await fetch(path, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
credentials: "include",
|
||||
});
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => null);
|
||||
const error = new Error(data?.detail || "Die Anfrage ist fehlgeschlagen.");
|
||||
error.status = response.status;
|
||||
throw error;
|
||||
}
|
||||
const blob = await response.blob();
|
||||
const disposition = response.headers.get("Content-Disposition") || "";
|
||||
const extended = disposition.match(/filename\*=UTF-8''([^;]+)/i);
|
||||
const plain = disposition.match(/filename="([^"]+)"/);
|
||||
const filename = extended ? decodeURIComponent(extended[1]) : (plain && plain[1]) || fallbackName || "uebergabe.zip";
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export function textToDocument(text) {
|
||||
return { version: 1, blocks: [{ type: "markdown", text: text || "" }] };
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { documentToText, fetchFile, fetchPaperless, markDirty, textToDocument, api } from "../api.js";
|
||||
import { documentToText, downloadMailPart, fetchFile, fetchPaperless, markDirty, textToDocument, api } from "../api.js";
|
||||
import { ErrorNote } from "./widgets.jsx";
|
||||
import { dateCaption, DirectionChoice, DirectionMark, formatDate, mailFacts, MarkdownEditor, sourceLabel } from "./content.jsx";
|
||||
import { NavIcon } from "./icons.jsx";
|
||||
@@ -7,11 +7,17 @@ import workerSrc from "pdfjs-dist/build/pdf.worker.min.mjs?url";
|
||||
|
||||
const SCALES = [0.75, 1, 1.25, 1.5, 2];
|
||||
|
||||
function MailAttachments({ files }) {
|
||||
function MailAttachments({ files, onOpen }) {
|
||||
if (!files?.length) return <p className="muted">Keine Anhänge.</p>;
|
||||
return (
|
||||
<ul className="mail-files">
|
||||
{files.map((file) => <li key={file.filename}>{file.filename}</li>)}
|
||||
{files.map((file) => (
|
||||
<li key={`${file.index ?? "name"}-${file.filename}`}>
|
||||
{onOpen && Number.isInteger(file.index) ? (
|
||||
<button type="button" className="quiet" onClick={() => onOpen(file)}>{file.filename}</button>
|
||||
) : file.filename}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
@@ -218,6 +224,15 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
}, [onClose, pages]);
|
||||
|
||||
async function openMailPart(suffix, filename) {
|
||||
setError("");
|
||||
try {
|
||||
await downloadMailPart(element.id, context, suffix, filename);
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function openOriginal() {
|
||||
setError("");
|
||||
try {
|
||||
@@ -380,11 +395,17 @@ export function ReadingPane({ binding, context, backLabel, onClose, onReload, ca
|
||||
<p><span>Von</span> {mailDetail.message_from || "Ohne Absender"}</p>
|
||||
<p><span>An</span> {mailDetail.message_to || "Ohne Empfänger"}</p>
|
||||
{mailDetail.message_cc ? <p><span>Kopie</span> {mailDetail.message_cc}</p> : null}
|
||||
<MailAttachments files={mailDetail.attachments} />
|
||||
{letter?.text ? <pre className="mail-letter__body">{letter.text}</pre> : (
|
||||
<p className="muted">{error || "Zu dieser Nachricht liegt kein Text vor. Absender und Empfänger bleiben gespeichert."}</p>
|
||||
{letter?.complete ? (
|
||||
<>
|
||||
<MailAttachments files={letter.attachments} onOpen={(file) => openMailPart(`attachments/${file.index}`, file.filename)} />
|
||||
{letter.text ? <pre className="mail-letter__body">{letter.text}</pre> : (
|
||||
<p className="muted">Diese Nachricht hat keinen lesbaren Text. Die vollständige E-Mail bleibt abrufbar.</p>
|
||||
)}
|
||||
<button type="button" onClick={() => openMailPart("original", "nachricht.eml")}>Vollständige E-Mail</button>
|
||||
</>
|
||||
) : (
|
||||
<p className="muted">{error ? "Die vollständige Nachricht konnte nicht geladen werden." : "Die vollständige Nachricht wird geladen …"}</p>
|
||||
)}
|
||||
{letter?.text_truncated ? <p className="muted">Der Text ist gekürzt. Die Nachricht selbst ist größer als die Vorschau.</p> : null}
|
||||
</article>
|
||||
) : null}
|
||||
{element.kind === "note" ? (
|
||||
|
||||
Reference in new issue
Block a user