diff --git a/backend/app/sources.py b/backend/app/sources.py index babad41..4667dd4 100644 --- a/backend/app/sources.py +++ b/backend/app/sources.py @@ -437,6 +437,7 @@ def paperless_documents(conn, owner_id: UUID, query: str, connector_id: UUID | N "document_type": _named(doc.get("document_type"), names["document_types"]), "storage_path": _named(doc.get("storage_path"), names["storage_paths"]), "tags": [name for name in (_named(tag, names["tags"]) for tag in doc.get("tags") or []) if name][:12], + "custom_fields": _custom_display(doc.get("custom_fields"), names["custom_fields"]), "pages": pages if isinstance(pages, int) and pages > 0 else None, "asn": asn if isinstance(asn, int) else None, "snippet": _snippet(doc, terms), @@ -508,11 +509,19 @@ def paperless_thumbnail(conn, owner_id: UUID, document_id: str, connector_id: UU assert_still_owned(conn, row["id"], owner_id) if fetched is None: raise not_found() - data, media, _name = fetched - # Raster pictures only. A vector file could carry markup. + data, header, _name = fetched + return data, _thumbnail_media(data, header) + + +def _thumbnail_media(data: bytes, header: str) -> str: + """Raster pictures only, told by their bytes. A vector file could carry markup, a header can be wrong.""" + try: + media = sniff(data) + except Problem: + media = "image/gif" if data[:4] == b"GIF8" else "" if media not in _THUMBNAIL_MEDIA: - raise unprocessable("Paperless hat kein Vorschaubild geliefert.") - return data, media + raise unprocessable(f"Paperless hat kein Vorschaubild geliefert (Antworttyp {header or 'unbekannt'}).") + return media _LOOKUP_TTL = 60.0 @@ -576,6 +585,33 @@ def _paperless_names_or_none(base: str, token: str, path: str) -> list[dict]: return [] +_CUSTOM_SHOWN = 8 + + +def _custom_display(values, fields: list[dict]) -> list[dict]: + """The custom fields of a hit as name, type and readable value. Empty values and unknown fields are left out.""" + if not isinstance(values, list): + return [] + known = {field["id"]: field for field in fields} + shown = [] + for entry in values: + if not isinstance(entry, dict): + continue + field = known.get(str(entry.get("field"))) + value = entry.get("value") + if field is None or value is None or value == "" or isinstance(value, list): + continue + if field["type"] == "boolean": + text = "Ja" if value else "Nein" + elif field["type"] == "select": + text = next((option["name"] for option in field["options"] if option["id"] == str(value)), "") + else: + text = str(value) + if text: + shown.append({"name": field["name"], "type": field["type"], "value": text[:200]}) + return shown[:_CUSTOM_SHOWN] + + def _named(value, entries: list[dict]) -> str | None: if value is None or isinstance(value, bool): return None diff --git a/backend/tests/test_connector_followup.py b/backend/tests/test_connector_followup.py index 6e4dd73..f5c9520 100644 --- a/backend/tests/test_connector_followup.py +++ b/backend/tests/test_connector_followup.py @@ -472,6 +472,14 @@ def test_paperless_default_search_reads_title_and_content_and_shows_a_preview(cl "tags": [2, 5, 99], "archive_serial_number": 1042, "page_count": 3, + "custom_fields": [ + {"field": 1, "value": "EUR100.50"}, + {"field": 2, "value": "2024-03-09"}, + {"field": 3, "value": "b"}, + {"field": 4, "value": True}, + {"field": 5, "value": None}, + {"field": 99, "value": "fremd"}, + ], "content": body, } ], @@ -482,6 +490,16 @@ def test_paperless_default_search_reads_title_and_content_and_shows_a_preview(cl return {"results": [{"id": 2, "name": "Miete"}, {"id": 5, "name": "Wohnung"}]} if path == "/api/correspondents/": return {"results": [{"id": 4, "name": "Hausverwaltung"}]} + if path == "/api/custom_fields/": + return { + "results": [ + {"id": 1, "name": "Rechnungsbetrag", "data_type": "monetary"}, + {"id": 2, "name": "bezahlt_am", "data_type": "date"}, + {"id": 3, "name": "Status", "data_type": "select", "extra_data": {"select_options": [{"id": "b", "label": "bezahlt"}]}}, + {"id": 4, "name": "Geprüft", "data_type": "boolean"}, + {"id": 5, "name": "Notiz", "data_type": "string"}, + ] + } return {"results": [{"id": 8, "name": "Brief"}]} monkeypatch.setattr(sources, "_paperless_get", fake_get) @@ -515,6 +533,12 @@ def test_paperless_default_search_reads_title_and_content_and_shows_a_preview(cl assert "Kündigung des Mietvertrags" in item["snippet"] assert len(item["snippet"]) <= 262 assert item["snippet"].startswith("…") + assert item["custom_fields"] == [ + {"name": "Rechnungsbetrag", "type": "monetary", "value": "EUR100.50"}, + {"name": "bezahlt_am", "type": "date", "value": "2024-03-09"}, + {"name": "Status", "type": "select", "value": "bezahlt"}, + {"name": "Geprüft", "type": "boolean", "value": "Ja"}, + ] assert found["total"] == 1 assert found["previous_page"] is None facets = ada.request("GET", "/api/v1/sources/paperless/facets", params={"connector_id": first["id"]}) @@ -639,6 +663,8 @@ def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeyp return b"RIFFxxxxWEBP", "image/webp", "" if path.endswith("/8/thumb/"): return b"

", "text/html", "" + if path.endswith("/6/thumb/"): + return b"\x89PNG\r\n\x1a\nxxxx", "application/octet-stream", "" if path.endswith("/7/thumb/"): return b"", "image/svg+xml", "" return None @@ -663,6 +689,8 @@ def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeyp assert ok.headers["content-type"] == "image/webp" assert ok.headers["cache-control"] == "private, no-store" assert seen[-1] == ("token-alpha", "/api/documents/9/thumb/") + typed = get(ada, 6) + assert typed.status_code == 200 and typed.headers["content-type"] == "image/png" assert get(ada, 8).status_code == 422 assert get(ada, 7).status_code == 422 assert get(ada, 5).status_code == 404 @@ -673,7 +701,7 @@ def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeyp register(db, ben.email, ben.name) ben.login() assert get(ben, 9).status_code == 404 - assert len(seen) == 4 + assert len(seen) == 5 def test_revoked_grant_hides_bytes_fetched_during_the_call(client, db, monkeypatch): diff --git a/frontend/src/pages/Sources.jsx b/frontend/src/pages/Sources.jsx index 12eb83d..e6d5c0f 100644 --- a/frontend/src/pages/Sources.jsx +++ b/frontend/src/pages/Sources.jsx @@ -265,21 +265,21 @@ function CustomFieldFilters({ fields, rows, onChange }) { // The thumbnail comes through the server like every other Paperless call, so the key stays there. function PaperlessThumb({ connectorId, id }) { const [url, setUrl] = useState(""); - const [failed, setFailed] = useState(false); + const [failed, setFailed] = useState(""); useEffect(() => { let alive = true; let made = ""; setUrl(""); - setFailed(false); + setFailed(""); fetchBlob(`/api/v1/sources/paperless/thumbnail?connector_id=${connectorId}&item=${id}`) .then((blob) => { if (!alive) return; made = URL.createObjectURL(blob); setUrl(made); }) - .catch(() => { - if (alive) setFailed(true); + .catch((err) => { + if (alive) setFailed(err.message || "Das Vorschaubild ist nicht verfügbar."); }); return () => { alive = false; @@ -288,7 +288,7 @@ function PaperlessThumb({ connectorId, id }) { }, [connectorId, id]); if (url) return ; - return ; + return {failed ? "Kein Bild" : ""}; } export function PaperlessPicker({ context, placement, chapterId = "", chapterTitle = "", targetLabel = "", onDone, onInserted }) { @@ -714,6 +714,16 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit {item.tags.map((tag) =>

  • {tag}
  • )} ) : null} + {item.custom_fields?.length ? ( +
    + {item.custom_fields.map((field) => ( +
    +
    {field.name}
    +
    {field.type === "date" && formatDate(field.value) !== "Ohne Datum" ? formatDate(field.value) : field.value}
    +
    + ))} +
    + ) : null} {item.snippet ?

    {item.snippet}

    : null}