Schließe die Connector-Kapselung ab, damit Paperless, IMAP und weitere Quellen über dieselbe Anbindung laufen.
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

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-19 15:00:50 +02:00
1 parent 75a3949f7c
commit d6b70d8f92
75 files changed
+9477 -414

No files matched your search

+16 -4
View File
@@ -93,8 +93,8 @@ from app.auth.security import (
)
from app.core.errors import Problem, forbidden, not_found, too_many, unprocessable
from app.core.timeutil import dump_date, dump_dt, utcnow
from app.media.storage import content_disposition, read_bytes
from app.media.storage import asset_path
from app.media.port import read_clear_bytes
from app.media.storage import content_disposition
from app.ops import (
add_annotation,
assign_task,
@@ -1119,12 +1119,24 @@ def download_file(
if not rights.read:
raise not_found()
asset = conn.execute(
"SELECT id, original_filename, content_type, status FROM media_assets WHERE element_id = %s",
"""
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":
raise not_found()
payload = read_bytes(asset_path(request.app.state.settings.media_root, asset["id"]))
payload = 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=request.app.state.settings.media_root,
protection_profile=asset["protection_profile"],
expected_sha256=asset["sha256"],
)
return Response(
content=payload,
media_type=asset["content_type"],
+13 -2
View File
@@ -278,7 +278,7 @@ class ExternalDetail(APIModel):
class ExternalIn(APIModel):
title: str = Field(min_length=1, max_length=200)
source: Literal["paperless", "url", "imap"]
source: str = Field(min_length=1, max_length=40)
external_id: str = Field(min_length=1, max_length=200)
context: ContextIn
placement: ContextIn | None = None
@@ -288,7 +288,13 @@ class ExternalIn(APIModel):
class ConnectorIn(APIModel):
kind: Literal["paperless", "imap"]
"""Kind is checked against the source registry. Source fields stay optional.
Paperless and IMAP still send their existing fields. A further module
uses ``config`` and validates that bag itself.
"""
kind: str = Field(min_length=1, max_length=40)
name: str = Field(min_length=1, max_length=120)
base_url: str | None = None
token: str | None = None
@@ -298,6 +304,11 @@ class ConnectorIn(APIModel):
password: str | None = None
mailbox: str | None = Field(default=None, max_length=200)
security: Literal["ssl", "starttls"] | None = None
config: dict[str, str] | None = None
class SourceAssignmentIn(APIModel):
connector_id: UUID
class DeleteElementIn(APIModel):
+212 -35
View File
@@ -8,26 +8,25 @@ 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.access.policy import Ctx, 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.api.schemas import ConnectorIn, ExternalIn, SourceAssignmentIn
from app.core.errors import not_found, unprocessable, unsupported
from app.mailpack import parse_mail
from app.media.storage import content_disposition
from app.ops import create_external
from app.sourcebind import assign_reference
from app.sourceport import capability_payload, with_capabilities
from app.sourceregistry import get_source, require_source
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,
update_connector,
)
_PRIVATE = {
@@ -44,6 +43,12 @@ def get_connectors(principal: Principal = Depends(get_principal), conn: Connecti
return {"items": list_connectors(conn, principal.account_id)}
@router.get("/api/v1/sources/capabilities")
def source_capabilities(principal: Principal = Depends(get_principal)):
del principal
return capability_payload()
@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)
@@ -51,6 +56,22 @@ def post_connector(body: ConnectorIn, principal: Principal = Depends(get_princip
return row
@router.patch("/api/v1/connectors/{connector_id}")
def patch_connector(
connector_id: UUID,
body: ConnectorIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
name, settings, secret = _connector_parts(body)
current = next((item for item in list_connectors(conn, principal.account_id) if item["id"] == str(connector_id)), None)
if current is None:
raise not_found()
if current["kind"] != body.kind:
raise unprocessable("Der Zugang bleibt bei seiner Quellenart.")
return update_connector(conn, principal.account_id, connector_id, name, settings, secret)
@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)
@@ -62,7 +83,7 @@ def probe_connector(connector_id: UUID, principal: Principal = Depends(get_princ
@router.get("/api/v1/external-refs/{element_id}/content")
def view_paperless(
def view_external_content(
element_id: UUID,
context_kind: str | None = None,
context_id: str | None = None,
@@ -70,9 +91,10 @@ def view_paperless(
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)
module = get_source(ref["source"]) if ref is not None else None
if module is None or module.open_original is None:
raise unprocessable("Für diese Quelle ist kein Dateiabruf vorgesehen.")
data, media_type, filename = module.open_original(conn, principal.account_id, element, context, ref)
safe_name = (filename or "dokument.pdf").replace('"', "")
return Response(
content=data,
@@ -126,7 +148,10 @@ def _opened_ref(conn, principal: Principal, element_id: UUID, context_kind: str
if not rights.read:
raise not_found()
ref = conn.execute(
"SELECT source, external_id, detail FROM external_references WHERE element_id = %s",
"""
SELECT source, external_id, detail, connector_id, resolution
FROM external_references WHERE element_id = %s
""",
(element_id,),
).fetchone()
return element, context, ref
@@ -186,8 +211,39 @@ def _mail_bytes(data: bytes, media_type: str, filename: str, inline: bool = Fals
@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())}
def search_paperless(
q: str = "",
connector_id: UUID | None = None,
title: str = "",
correspondent: str = "",
document_type: str = "",
tag: str = "",
document_from: str = "",
document_to: str = "",
sort: str = "",
cursor: str = "",
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
return _run_search(
"paperless",
conn,
principal.account_id,
q.strip(),
connector_id,
"",
None,
{
"title": title,
"correspondent": correspondent,
"document_type": document_type,
"tag": tag,
"document_from": document_from,
"document_to": document_to,
"sort": sort,
"cursor": cursor,
},
)
@router.get("/api/v1/sources/imap/mailboxes")
@@ -196,7 +252,7 @@ def list_mailboxes(
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
return {"items": imap_mailboxes(conn, principal.account_id, connector_id)}
return _run_navigate("imap", conn, principal.account_id, connector_id)
@router.get("/api/v1/sources/imap/messages")
@@ -205,12 +261,38 @@ def search_mail(
q: str = "",
mailbox: str = "",
before: int | None = None,
title: str = "",
correspondent: str = "",
document_type: str = "",
tag: str = "",
document_from: str = "",
document_to: str = "",
sort: str = "",
cursor: str = "",
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)
return _run_search(
"imap",
conn,
principal.account_id,
q.strip(),
connector_id,
mailbox.strip(),
before,
{
"title": title,
"correspondent": correspondent,
"document_type": document_type,
"tag": tag,
"document_from": document_from,
"document_to": document_to,
"sort": sort,
"cursor": cursor,
},
)
@router.get("/api/v1/sources/imap/message")
@@ -228,8 +310,8 @@ def read_mail(
@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.")
if get_source(body.source) is None:
raise unprocessable("Verknüpft werden registrierte Quellen.")
element = create_external(
conn,
principal.account_id,
@@ -248,20 +330,115 @@ def link_source(body: ExternalIn, principal: Principal = Depends(get_principal),
return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]})
@router.post("/api/v1/external-refs/{element_id}/assignment")
def assign_source(
element_id: UUID,
body: SourceAssignmentIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
assign_reference(conn, principal.account_id, element_id, body.connector_id)
return {"id": str(element_id), "connector_id": str(body.connector_id), "resolution": "bound"}
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
return require_source(body.kind).parse_config(body.model_dump())
_SEARCH_FLAGS = {
"title": "title",
"correspondent": "correspondent",
"document_type": "document_type",
"tag": "tags",
"document_from": "document_range",
"document_to": "document_range",
"sort": "sort",
"cursor": "continuation",
}
def _run_search(kind: str, conn, owner, query: str, connector_id: UUID | None, mailbox: str, before: int | None, filters: dict) -> dict:
module = require_source(kind)
search = module.capabilities["search"]
refused = [name for name, value in filters.items() if str(value or "").strip() and not search.get(_SEARCH_FLAGS.get(name, name))]
if before is not None and not search.get("continuation"):
refused.append("before")
if refused:
raise unsupported("Diese Suche unterstützt den Filter nicht: " + ", ".join(refused) + ".")
found = module.find(conn, owner, connector_id, query, {"mailbox": mailbox, "before": before})
bound = str(found.get("connector_id") or connector_id or "")
return with_capabilities(kind, bound, found)
def _run_navigate(kind: str, conn, owner, connector_id: UUID) -> dict:
module = require_source(kind)
if not module.capabilities.get("navigate") or module.navigate is None:
raise unsupported("Diese Quelle hat keine Fachnavigation.")
return {"items": module.navigate(conn, owner, connector_id)}
@router.get("/api/v1/sources/{kind}/search")
def search_source(
kind: str,
q: str = "",
connector_id: UUID | None = None,
mailbox: str = "",
before: int | None = None,
title: str = "",
correspondent: str = "",
document_type: str = "",
tag: str = "",
document_from: str = "",
document_to: str = "",
sort: str = "",
cursor: str = "",
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 _run_search(
kind,
conn,
principal.account_id,
q.strip(),
connector_id,
mailbox.strip(),
before,
{
"title": title,
"correspondent": correspondent,
"document_type": document_type,
"tag": tag,
"document_from": document_from,
"document_to": document_to,
"sort": sort,
"cursor": cursor,
},
)
@router.get("/api/v1/sources/{kind}/navigate")
def navigate_source(
kind: str,
connector_id: UUID,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
return _run_navigate(kind, conn, principal.account_id, connector_id)
@router.get("/api/v1/sources/{kind}/preview")
def preview_source(
kind: str,
connector_id: UUID,
mailbox: str = "",
uid: int = 0,
item: str = "",
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
module = require_source(kind)
if not module.capabilities.get("read_preview") or module.preview is None:
raise unsupported("Diese Quelle hat keine Vorschau vor dem Übernehmen.")
return module.preview(conn, principal.account_id, connector_id, {"mailbox": mailbox, "uid": uid, "item": item})