Zeige referenzierte Paperless-Dokumente zum Ansehen und Drucken.
Deploy Development / deploy (push) Successful in 22s
Deploy Development / deploy (push) Successful in 22s
Die Datei bleibt in Paperless. Tsumugu laedt sie nur fuer die Ansicht und speichert keine Kopie. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
0108a296bc
commit
871c7d3ad5
7 files changed
+167
-14
No files matched your search
@@ -5,19 +5,21 @@ from __future__ import annotations
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.responses import Response
|
||||
from psycopg import Connection
|
||||
|
||||
from app.access.policy import Ctx
|
||||
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 unprocessable
|
||||
from app.core.errors import not_found, unprocessable
|
||||
from app.ops import create_external
|
||||
from app.sources import (
|
||||
clean_base_url,
|
||||
delete_connector,
|
||||
imap_messages,
|
||||
list_connectors,
|
||||
paperless_content,
|
||||
paperless_documents,
|
||||
probe,
|
||||
save_connector,
|
||||
@@ -48,6 +50,39 @@ def probe_connector(connector_id: UUID, principal: Principal = Depends(get_princ
|
||||
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 = 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()
|
||||
if ref is None or ref["source"] != "paperless":
|
||||
raise unprocessable("Nur ein Paperless-Dokument lässt sich hier 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('"', "")
|
||||
return Response(
|
||||
content=data,
|
||||
media_type=media_type,
|
||||
headers={
|
||||
"Content-Disposition": f'inline; filename="{safe_name}"',
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@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())}
|
||||
|
||||
Reference in new issue
Block a user