268 lines
9.7 KiB
Python
268 lines
9.7 KiB
Python
"""Connector administration and live Paperless / IMAP lookup."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from fastapi.responses import JSONResponse, Response
|
|
from psycopg import Connection
|
|
|
|
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
|
|
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,
|
|
delete_connector,
|
|
imap_mailboxes,
|
|
imap_messages,
|
|
imap_preview,
|
|
list_connectors,
|
|
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()
|
|
|
|
|
|
@router.get("/api/v1/connectors")
|
|
def get_connectors(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
|
return {"items": list_connectors(conn, principal.account_id)}
|
|
|
|
|
|
@router.post("/api/v1/connectors", status_code=201)
|
|
def post_connector(body: ConnectorIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
|
name, settings, secret = _connector_parts(body)
|
|
row = save_connector(conn, principal.account_id, body.kind, name, settings, secret)
|
|
return row
|
|
|
|
|
|
@router.delete("/api/v1/connectors/{connector_id}", status_code=204)
|
|
def remove_connector(connector_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
|
delete_connector(conn, principal.account_id, connector_id)
|
|
|
|
|
|
@router.post("/api/v1/connectors/{connector_id}/probe")
|
|
def probe_connector(connector_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
|
return probe(conn, principal.account_id, connector_id)
|
|
|
|
|
|
@router.get("/api/v1/external-refs/{element_id}/content")
|
|
def view_paperless(
|
|
element_id: UUID,
|
|
context_kind: str | None = None,
|
|
context_id: str | None = None,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
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 = 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}"',
|
|
},
|
|
)
|
|
|
|
|
|
@router.get("/api/v1/external-refs/{element_id}/mail")
|
|
def view_mail(
|
|
element_id: UUID,
|
|
context_kind: str | None = None,
|
|
context_id: str | None = None,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
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 _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()
|
|
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 element, context, ref
|
|
|
|
|
|
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])
|
|
raise unprocessable(f"Eine Anlage der E-Mail ließ sich nicht vollständig lesen: {names}.")
|
|
return parsed, raw
|
|
|
|
|
|
@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),
|
|
):
|
|
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")
|
|
|
|
|
|
@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),
|
|
):
|
|
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]
|
|
return _mail_bytes(item.data, item.media_type or "application/octet-stream", item.filename, inline=True)
|
|
|
|
|
|
def _mail_bytes(data: bytes, media_type: str, filename: str, inline: bool = False) -> Response:
|
|
disposition = content_disposition(filename)
|
|
if inline:
|
|
disposition = disposition.replace("attachment;", "inline;", 1)
|
|
return Response(
|
|
content=data,
|
|
media_type=media_type,
|
|
headers={
|
|
**_PRIVATE,
|
|
"Content-Disposition": disposition,
|
|
},
|
|
)
|
|
|
|
|
|
@router.get("/api/v1/sources/paperless/documents")
|
|
def search_paperless(q: str = "", principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
|
return {"items": paperless_documents(conn, principal.account_id, q.strip())}
|
|
|
|
|
|
@router.get("/api/v1/sources/imap/mailboxes")
|
|
def list_mailboxes(
|
|
connector_id: UUID,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
return {"items": imap_mailboxes(conn, principal.account_id, connector_id)}
|
|
|
|
|
|
@router.get("/api/v1/sources/imap/messages")
|
|
def search_mail(
|
|
connector_id: UUID,
|
|
q: str = "",
|
|
mailbox: str = "",
|
|
before: int | None = None,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
if before is not None and before < 1:
|
|
raise unprocessable("Die Seitenangabe ist ungültig.")
|
|
return imap_messages(conn, principal.account_id, connector_id, q.strip(), mailbox.strip(), before)
|
|
|
|
|
|
@router.get("/api/v1/sources/imap/message")
|
|
def read_mail(
|
|
connector_id: UUID,
|
|
mailbox: str,
|
|
uid: int,
|
|
principal: Principal = Depends(get_principal),
|
|
conn: Connection = Depends(get_conn),
|
|
):
|
|
if uid < 1:
|
|
raise unprocessable("Die Nachricht ist ungültig.")
|
|
return imap_preview(conn, principal.account_id, connector_id, mailbox.strip(), uid)
|
|
|
|
|
|
@router.post("/api/v1/sources/link", status_code=201)
|
|
def link_source(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
|
if body.source not in {"paperless", "imap"}:
|
|
raise unprocessable("Verknüpft werden Paperless-Dokumente und E-Mails.")
|
|
element = create_external(
|
|
conn,
|
|
principal.account_id,
|
|
principal.scope_id,
|
|
body.title,
|
|
body.source,
|
|
body.external_id,
|
|
Ctx(body.context.kind, body.context.id),
|
|
_ctx_model(body.placement),
|
|
body.chapter_id,
|
|
body.dates.document_on,
|
|
body.dates.sent_on,
|
|
body.dates.received_on,
|
|
body.detail.model_dump(mode="json", exclude_none=True) if body.detail else None,
|
|
)
|
|
return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]})
|
|
|
|
|
|
def _connector_parts(body: ConnectorIn) -> tuple[str, dict, str]:
|
|
if body.kind == "paperless":
|
|
if not body.base_url or not body.token or len(body.token.strip()) < 8:
|
|
raise unprocessable("Paperless braucht eine Adresse und einen API-Schlüssel.")
|
|
return body.name.strip(), {"base_url": clean_base_url(body.base_url)}, body.token.strip()
|
|
if not body.host or not body.username or not body.password:
|
|
raise unprocessable("Ein E-Mail-Konto braucht Server, Benutzername und Passwort.")
|
|
host = body.host.strip()
|
|
if " " in host or len(host) > 200:
|
|
raise unprocessable("Der Mailserver ist ungültig.")
|
|
return body.name.strip(), {
|
|
"host": host,
|
|
"port": body.port or 993,
|
|
"username": body.username.strip(),
|
|
"mailbox": (body.mailbox or "INBOX").strip() or "INBOX",
|
|
"security": body.security or "ssl",
|
|
}, body.password
|