feat(fastapi): adds fetch network assets in pptx creator

This commit is contained in:
sauravniraula 2025-07-18 08:27:37 +05:45
parent a7516ee208
commit ee38963762
No known key found for this signature in database
GPG key ID: 60FCC1B5A5E83326
4 changed files with 41 additions and 2 deletions

View file

@ -1,5 +1,6 @@
import asyncio
import json
import os
import random
from typing import Annotated, List, Optional
import uuid
@ -20,6 +21,7 @@ from services.database import get_sql_session
from services.documents_loader import DocumentsLoader
from models.sql.presentation import PresentationModel
from services.pptx_presentation_creator import PptxPresentationCreator
from utils.asset_directory_utils import get_export_directory
from utils.llm_calls.generate_document_summary import generate_document_summary
from utils.llm_calls.generate_presentation_structure import (
generate_presentation_structure,
@ -28,6 +30,7 @@ from utils.llm_calls.generate_slide_content import (
get_slide_content_from_type_and_outline,
)
from utils.process_slides import process_slide_and_fetch_assets
from utils.randomizers import get_random_uuid
PRESENTATION_ROUTER = APIRouter(prefix="/presentation", tags=["Presentation"])
@ -280,4 +283,11 @@ def update_presentation(
def create_pptx(pptx_model: Annotated[PptxPresentationModel, Body()]):
pptx_creator = PptxPresentationCreator(pptx_model)
pptx_creator.create_ppt()
pptx_creator.save(pptx_model.id)
export_directory = get_export_directory()
pptx_path = os.path.join(
export_directory, f"{pptx_model.name or get_random_uuid()}.pptx"
)
pptx_creator.save(pptx_path)
return pptx_path

View file

@ -153,6 +153,6 @@ class PptxSlideModel(BaseModel):
class PptxPresentationModel(BaseModel):
name: str
name: Optional[str] = None
shapes: Optional[List[PptxShapeModel]] = None
slides: List[PptxSlideModel]

View file

@ -31,6 +31,7 @@ from models.pptx_models import (
PptxTextBoxModel,
PptxTextRunModel,
)
from utils.download_helpers import download_files
from utils.image_utils import (
change_image_color,
clip_image,
@ -54,6 +55,28 @@ class PptxPresentationCreator:
self._ppt.slide_width = Pt(1280)
self._ppt.slide_height = Pt(720)
async def fetch_network_assets(self):
image_urls = []
image_local_paths = []
for each_slide in self._slide_models:
for each_shape in each_slide.shapes:
if isinstance(each_shape, PptxPictureBoxModel):
image_path = each_shape.picture.path
# if image_path.startswith("http"):
# image_urls.append(image_path)
# parsed_url = unquote(urlparse(image_path).path)
# image_name = replace_file_name(
# os.path.basename(parsed_url), str(uuid.uuid4())
# )
# image_path = os.path.join(self.temp_dir, image_name)
# image_local_paths.append(image_path)
each_shape.picture.path = image_path
each_shape.picture.is_network = False
await download_files(image_urls, self._temp_dir)
def create_ppt(self):
for slide_model in self._slide_models:

View file

@ -6,3 +6,9 @@ def get_images_directory():
images_directory = os.path.join(get_app_data_directory_env(), "images")
os.makedirs(images_directory, exist_ok=True)
return images_directory
def get_export_directory():
export_directory = os.path.join(get_app_data_directory_env(), "exports")
os.makedirs(export_directory, exist_ok=True)
return export_directory