Załączniki: odczyt pliku poza pętlą zdarzeń serwera

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uwxqL6AphKDyHPDToUMin
This commit is contained in:
tobayah
2026-11-18 15:09:12 +01:00
co-authored by Claude Opus 5.5
parent bf46fb5724
commit c74276555e
+4 -1
View File
@@ -6,6 +6,7 @@ audit row names the file and its size, never its content.
from __future__ import annotations
from fastapi import APIRouter, Depends, Query, Request
from starlette.concurrency import run_in_threadpool
from sqlalchemy.orm import Session
from ..db import get_db
@@ -24,7 +25,9 @@ async def extract(request: Request, name: str = Query(min_length=1, max_length=2
data += chunk
if len(data) > attachments.MAX_BYTES:
break # extract() refuses it; nothing past the limit is read
out = attachments.extract(name, bytes(data))
# A 300-page PDF parses for seconds: off the event loop, or every other request waits for it.
# ponytail: no time budget per document; a subprocess with a timeout if a crafted PDF ever stalls a worker.
out = await run_in_threadpool(attachments.extract, name, bytes(data))
audit.record(db, action="chat.attachment", entity_type="attachment", actor_id=user.id, detail=name[:200],
ip=client_ip(request), diff={"kind": out["kind"], "bytes": len(data), "pages": out["pages"],
"chars": out["chars"]})