docling-studio/document-parser/persistence/document_repo.py
Pier-Jean Malandrino 001cf6807c Fix audit findings: security, robustness, and dead code
- Remove dead get_analysis_service() function in main.py
- Scope file deletion to UPLOAD_DIR to prevent path traversal
- Parse datetime strings back to datetime objects in repos
- Add 10-minute polling timeout in frontend analysis store
- Accept .pdf extension (not just MIME type) on drag-and-drop
- Guard localStorage access for private browsing compatibility

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-31 16:52:36 +02:00

67 lines
2.2 KiB
Python

"""Document repository — SQLite CRUD for documents table."""
from __future__ import annotations
from datetime import datetime
from domain.models import Document
from persistence.database import get_connection
def _row_to_document(row) -> Document:
created = row["created_at"]
if isinstance(created, str):
created = datetime.fromisoformat(created)
return Document(
id=row["id"],
filename=row["filename"],
content_type=row["content_type"],
file_size=row["file_size"],
page_count=row["page_count"],
storage_path=row["storage_path"],
created_at=created,
)
async def insert(doc: Document) -> None:
async with get_connection() as db:
await db.execute(
"""INSERT INTO documents (id, filename, content_type, file_size, page_count, storage_path, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(doc.id, doc.filename, doc.content_type, doc.file_size,
doc.page_count, doc.storage_path, str(doc.created_at)),
)
await db.commit()
async def find_all(*, limit: int = 200, offset: int = 0) -> list[Document]:
async with get_connection() as db:
cursor = await db.execute(
"SELECT * FROM documents ORDER BY created_at DESC LIMIT ? OFFSET ?",
(limit, offset),
)
rows = await cursor.fetchall()
return [_row_to_document(r) for r in rows]
async def find_by_id(doc_id: str) -> Document | None:
async with get_connection() as db:
cursor = await db.execute("SELECT * FROM documents WHERE id = ?", (doc_id,))
row = await cursor.fetchone()
return _row_to_document(row) if row else None
async def update_page_count(doc_id: str, page_count: int) -> None:
async with get_connection() as db:
await db.execute(
"UPDATE documents SET page_count = ? WHERE id = ?",
(page_count, doc_id),
)
await db.commit()
async def delete(doc_id: str) -> bool:
async with get_connection() as db:
cursor = await db.execute("DELETE FROM documents WHERE id = ?", (doc_id,))
await db.commit()
return cursor.rowcount > 0