Files
pdf/gateway/app/services/store.py
T
2026-06-08 11:22:15 +05:30

58 lines
1.8 KiB
Python

import threading
import uuid
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
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(timezone.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) -> Optional[Dict[str, Any]]:
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()