freeleaps-service-hub/apps/content/backend/document/document_manager.py
2025-01-31 07:03:27 +00:00

59 lines
2.0 KiB
Python

from common.config.app_settings import app_settings
from backend.content.models import DocumentDoc
import httpx
class DocumentManager:
def __init__(self):
self.storage_service_api_base = (
app_settings.CENTRAL_STORAGE_WEBAPI_URL_BASE.rstrip("/") + "/"
)
async def retrieve_document_info(self, document_id: str):
api_url = (
self.storage_service_api_base + "retrieve_document_info/" + document_id
)
async with httpx.AsyncClient() as client:
response = await client.get(api_url)
return response.json()
async def retrieve_document_as_http_media(self, document_id: str):
api_url = (
self.storage_service_api_base + "read-document-as-http-media/" + document_id
)
async with httpx.AsyncClient() as client:
response = await client.get(api_url)
return response.json()
async def save_document_file(
self, associated_with: str, name: str, blob: bytes
) -> str:
api_url = self.storage_service_api_base + "upload-file"
files = {"file": (name, blob)}
async with httpx.AsyncClient() as client:
response = await client.post(
api_url, data={"associated_with": associated_with}, files=files
)
return response.json()["document_id"]
async def cleanup_document(self):
# Corrected query with regex
documents = await DocumentDoc.find(
{"created_by": {"$regex": "^content-service-"}}
).to_list()
if documents:
print(
f"Found {len(documents)} documents to modify and delete: {[doc.id for doc in documents]}"
)
else:
print("No documents found to modify or delete.")
for document in documents:
print(
f"Deleting document {document.id} - created_by: {document.created_by}"
)
await document.delete()
print("Modification and deletion completed.")