freeleaps-service-hub/apps/central_storage/webapi/routes/delete_file.py

43 lines
1.4 KiB
Python

from fastapi import APIRouter, HTTPException, BackgroundTasks
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from backend.application.document_hub import DocumentHub
router = APIRouter()
class DeleteFileRequest(BaseModel):
document_ids: list[str]
async def background_delete_documents(document_ids: list[str]):
"""
Background task to delete documents from Azure Blob Storage.
"""
document_hub = DocumentHub()
try:
await document_hub.delete_documents(document_ids)
print(f"Successfully deleted documents: {document_ids}")
except Exception as e:
print(f"Error occurred during document deletion: {e}")
@router.delete(
"/delete-file",
summary="Queue file deletion from blob storage",
description="Queue deletion of files from Azure Blob Storage given a list of document IDs",
)
async def delete_file(request: DeleteFileRequest, background_tasks: BackgroundTasks):
"""
Queue the deletion process and respond immediately.
"""
try:
# Queue the background task for deletion
background_tasks.add_task(background_delete_documents, request.document_ids)
return JSONResponse(
content={"message": "File deletion has been queued for processing"},
status_code=202,
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))