Files
tsumugu/backend/app/retrieval.py
T
LarsandCursor d6b70d8f92
Deploy Development / deploy (push) Successful in 28s
Test Suite / pytest-backend (push) Failing after 2m18s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 10s
Schließe die Connector-Kapselung ab, damit Paperless, IMAP und weitere Quellen über dieselbe Anbindung laufen.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-19 15:00:50 +02:00

109 lines
3.8 KiB
Python

"""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
from app.media.port import read_clear_bytes
from app.sources import COMPLETE_MAIL_LIMIT
EXPORT_BYTE_LIMIT = COMPLETE_MAIL_LIMIT
@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:
from app.sourceregistry import get_source
ref = conn.execute(
"""
SELECT source, external_id, detail, connector_id, resolution
FROM external_references WHERE element_id = %s
""",
(element_id,),
).fetchone()
if ref is None:
return RetrievalFailure("missing", "Die Quellenreferenz fehlt.")
module = get_source(ref["source"])
if module is None or module.export_original is None:
return RetrievalFailure("capability", "Für diese Quelle ist kein Originalabruf vorgesehen.")
return module.export_original(conn, actor_id, ref["external_id"], ref["detail"] or {}, assigned, ref)
def load_file(conn, element_id: UUID, media_root: str) -> FilePayload | RetrievalFailure:
asset = conn.execute(
"""
SELECT id, original_filename, content_type, sha256, status,
storage_instance_id, storage_generation, object_key, protection_profile
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.")
if (asset["protection_profile"] or "legacy-plaintext") != "legacy-plaintext":
return RetrievalFailure(
"unsupported",
"Dieser Inhalt ist geschützt und kann hier nicht im Klartext gelesen werden.",
)
try:
data = read_clear_bytes(
conn,
instance_id=asset["storage_instance_id"],
generation=asset["storage_generation"],
object_key=asset["object_key"] or str(asset["id"]),
media_root=media_root,
protection_profile=asset["protection_profile"],
expected_sha256=asset["sha256"],
)
except Problem as exc:
if exc.code == "unsupported":
return RetrievalFailure("unsupported", exc.detail)
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 failure_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)