Erlaube berechtigten Konten, verknüpfte Originale über die Zugänge des Owners zu lesen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
562c0848b1
commit
75a3949f7c
14 files changed
+815
-93
No files matched your search
+48
-67
@@ -5,10 +5,10 @@ from __future__ import annotations
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.responses import Response
|
||||
from fastapi.responses import JSONResponse, Response
|
||||
from psycopg import Connection
|
||||
|
||||
from app.access.policy import Ctx, element_rights, get_element, parse_query_context
|
||||
from app.access.policy import element_rights, get_element, parse_query_context
|
||||
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
|
||||
@@ -23,13 +23,19 @@ from app.sources import (
|
||||
imap_messages,
|
||||
imap_preview,
|
||||
list_connectors,
|
||||
load_imap_original,
|
||||
paperless_content,
|
||||
paperless_documents,
|
||||
probe,
|
||||
read_linked_mail,
|
||||
read_linked_paperless,
|
||||
save_connector,
|
||||
)
|
||||
|
||||
_PRIVATE = {
|
||||
"Cache-Control": "private, no-store",
|
||||
"Referrer-Policy": "no-referrer",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
}
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@@ -63,27 +69,17 @@ def view_paperless(
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
element = get_element(conn, element_id)
|
||||
if element is None or element["kind"] != "external_ref":
|
||||
raise not_found()
|
||||
rights = element_rights(conn, principal.account_id, element, parse_query_context(context_kind, context_id))
|
||||
if not rights.read:
|
||||
raise not_found()
|
||||
ref = conn.execute(
|
||||
"SELECT source, external_id FROM external_references WHERE element_id = %s",
|
||||
(element_id,),
|
||||
).fetchone()
|
||||
element, context, ref = _opened_ref(conn, principal, element_id, context_kind, context_id)
|
||||
if ref is None or ref["source"] != "paperless":
|
||||
raise unprocessable("Nur ein Paperless-Dokument lässt sich hier als Datei ansehen.")
|
||||
data, media_type, filename = paperless_content(conn, principal.account_id, ref["external_id"])
|
||||
safe_name = (filename or f"paperless-{ref['external_id']}.pdf").replace('"', "")
|
||||
data, media_type, filename = read_linked_paperless(conn, principal.account_id, element, context, ref)
|
||||
safe_name = (filename or "dokument.pdf").replace('"', "")
|
||||
return Response(
|
||||
content=data,
|
||||
media_type=media_type,
|
||||
headers={
|
||||
**_PRIVATE,
|
||||
"Content-Disposition": f'inline; filename="{safe_name}"',
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
)
|
||||
|
||||
@@ -96,52 +92,48 @@ 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)
|
||||
],
|
||||
}
|
||||
element, context, ref = _opened_ref(conn, principal, element_id, context_kind, context_id)
|
||||
parsed, _raw = _complete_mail(conn, principal, element, context, ref)
|
||||
return JSONResponse(
|
||||
content={
|
||||
"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)
|
||||
],
|
||||
},
|
||||
headers={"Cache-Control": "private, no-store", "Referrer-Policy": "no-referrer"},
|
||||
)
|
||||
|
||||
|
||||
def _mail_ref(conn, principal: Principal, element_id: UUID, context_kind: str | None, context_id: str | None):
|
||||
def _opened_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()
|
||||
rights = element_rights(conn, principal.account_id, element, parse_query_context(context_kind, context_id))
|
||||
context = parse_query_context(context_kind, context_id)
|
||||
rights = element_rights(conn, principal.account_id, element, context)
|
||||
if not rights.read:
|
||||
raise not_found()
|
||||
ref = conn.execute(
|
||||
"SELECT source, external_id, detail FROM external_references WHERE element_id = %s",
|
||||
(element_id,),
|
||||
).fetchone()
|
||||
return ref
|
||||
return element, context, 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 = 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"]))
|
||||
def _complete_mail(conn, principal: Principal, element, context, ref):
|
||||
raw = read_linked_mail(conn, principal.account_id, element, context, ref)
|
||||
parsed = parse_mail(raw)
|
||||
if parsed.missing:
|
||||
names = ", ".join(parsed.missing[:5])
|
||||
@@ -149,16 +141,6 @@ def _complete_mail(conn, principal: Principal, ref):
|
||||
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,
|
||||
@@ -167,8 +149,8 @@ def download_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)
|
||||
element, context, ref = _opened_ref(conn, principal, element_id, context_kind, context_id)
|
||||
_parsed, raw = _complete_mail(conn, principal, element, context, ref)
|
||||
return _mail_bytes(raw, "message/rfc822", "nachricht.eml")
|
||||
|
||||
|
||||
@@ -181,8 +163,8 @@ def download_mail_attachment(
|
||||
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)
|
||||
element, context, ref = _opened_ref(conn, principal, element_id, context_kind, context_id)
|
||||
parsed, _raw = _complete_mail(conn, principal, element, context, ref)
|
||||
if index < 0 or index >= len(parsed.attachments):
|
||||
raise not_found()
|
||||
item = parsed.attachments[index]
|
||||
@@ -197,9 +179,8 @@ def _mail_bytes(data: bytes, media_type: str, filename: str, inline: bool = Fals
|
||||
content=data,
|
||||
media_type=media_type,
|
||||
headers={
|
||||
**_PRIVATE,
|
||||
"Content-Disposition": disposition,
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user