chwellofficial/nt360Slides
0
1import json2import os3import aiohttp4from typing import Literal5import uuid6from fastapi import HTTPException7from pathvalidate import sanitize_filename8 9from models.pptx_models import PptxPresentationModel10from models.presentation_and_path import PresentationAndPath11from services.pptx_presentation_creator import PptxPresentationCreator12from services.temp_file_service import TEMP_FILE_SERVICE13from utils.asset_directory_utils import get_exports_directory14import uuid15 16 17async def export_presentation(18 presentation_id: uuid.UUID, title: str, export_as: Literal["pptx", "pdf"]19) -> PresentationAndPath:20 if export_as == "pptx":21 22 # Get the converted PPTX model from the Next.js service23 async with aiohttp.ClientSession() as session:24 async with session.get(25 f"http://localhost/api/presentation_to_pptx_model?id={presentation_id}"26 ) as response:27 if response.status != 200:28 error_text = await response.text()29 print(f"Failed to get PPTX model: {error_text}")30 raise HTTPException(31 status_code=500,32 detail="Failed to convert presentation to PPTX model",33 )34 pptx_model_data = await response.json()35 36 # Create PPTX file using the converted model37 pptx_model = PptxPresentationModel(**pptx_model_data)38 temp_dir = TEMP_FILE_SERVICE.create_temp_dir()39 pptx_creator = PptxPresentationCreator(pptx_model, temp_dir)40 await pptx_creator.create_ppt()41 42 export_directory = get_exports_directory()43 pptx_path = os.path.join(44 export_directory,45 f"{sanitize_filename(title or str(uuid.uuid4()))}.pptx",46 )47 pptx_creator.save(pptx_path)48 49 return PresentationAndPath(50 presentation_id=presentation_id,51 path=pptx_path,52 )53 else:54 async with aiohttp.ClientSession() as session:55 async with session.post(56 "http://localhost/api/export-as-pdf",57 json={58 "id": str(presentation_id),59 "title": sanitize_filename(title or str(uuid.uuid4())),60 },61 ) as response:62 response_json = await response.json()63 64 return PresentationAndPath(65 presentation_id=presentation_id,66 path=response_json["path"],67 )68 