60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
import threading
|
|
import uuid
|
|
from datetime import UTC, datetime,timezone
|
|
from typing import Any
|
|
|
|
|
|
class DocumentStore:
|
|
def __init__(self):
|
|
self._lock = threading.Lock()
|
|
self._documents: dict[str, dict[str, Any]] = {}
|
|
|
|
def add_document(self, filename: str, bytes_data: bytes, doc_instance: Any) -> dict[str, Any]:
|
|
doc_id = str(uuid.uuid4())
|
|
uploaded_at = datetime.now(UTC).isoformat().replace("+00:00", "Z")
|
|
page_width = 612.0
|
|
page_height = 792.0
|
|
if doc_instance.page_count > 0:
|
|
try:
|
|
page_0 = doc_instance.get_page(0)
|
|
page_width = page_0.width
|
|
page_height = page_0.height
|
|
except Exception:
|
|
pass
|
|
|
|
info = {
|
|
"id": doc_id,
|
|
"filename": filename,
|
|
"sizeBytes": len(bytes_data),
|
|
"totalPages": doc_instance.page_count,
|
|
"pageWidth": page_width,
|
|
"pageHeight": page_height,
|
|
"uploadedAt": uploaded_at,
|
|
"status": "ready",
|
|
"doc_instance": doc_instance,
|
|
"bytes_data": bytes_data,
|
|
}
|
|
|
|
with self._lock:
|
|
self._documents[doc_id] = info
|
|
|
|
return info
|
|
|
|
def get_document(self, doc_id: str) -> dict[str, Any] | None:
|
|
with self._lock:
|
|
return self._documents.get(doc_id)
|
|
|
|
def list_documents(self) -> list[dict[str, Any]]:
|
|
with self._lock:
|
|
return list(self._documents.values())
|
|
|
|
def delete_document(self, doc_id: str) -> bool:
|
|
with self._lock:
|
|
if doc_id in self._documents:
|
|
del self._documents[doc_id]
|
|
return True
|
|
return False
|
|
|
|
|
|
document_store = DocumentStore()
|