Refactor chart preview IDs to use one deterministic identifier
This commit is contained in:
@@ -8,6 +8,7 @@ from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks
|
|||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
from typing import Dict, Any, Optional, List
|
from typing import Dict, Any, Optional, List
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
from pathlib import Path
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from middleware.auth_middleware import get_current_user
|
from middleware.auth_middleware import get_current_user
|
||||||
@@ -84,19 +85,21 @@ async def generate_chart_preview(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
broll_service = get_broll_service()
|
broll_service = get_broll_service()
|
||||||
|
chart_id = uuid.uuid4().hex[:8]
|
||||||
|
|
||||||
preview_path = broll_service.generate_chart_preview(
|
preview_path = broll_service.generate_chart_preview(
|
||||||
chart_data=request.chart_data,
|
chart_data=request.chart_data,
|
||||||
chart_type=request.chart_type,
|
chart_type=request.chart_type,
|
||||||
title=request.title,
|
title=request.title,
|
||||||
subtitle=request.subtitle or "",
|
subtitle=request.subtitle or "",
|
||||||
|
chart_id=chart_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not preview_path:
|
if not preview_path:
|
||||||
raise HTTPException(status_code=500, detail="Failed to generate chart preview")
|
raise HTTPException(status_code=500, detail="Failed to generate chart preview")
|
||||||
|
|
||||||
chart_id = uuid.uuid4().hex[:8]
|
preview_filename = Path(preview_path).name
|
||||||
preview_url = f"/api/podcast/broll/preview/{chart_id}/{preview_path.split('/')[-1]}"
|
preview_url = f"/api/podcast/broll/preview/{chart_id}/{preview_filename}"
|
||||||
|
|
||||||
return ChartPreviewResponse(
|
return ChartPreviewResponse(
|
||||||
preview_url=preview_url,
|
preview_url=preview_url,
|
||||||
@@ -197,12 +200,14 @@ async def serve_chart_preview(
|
|||||||
current_user: Dict[str, Any] = Depends(get_current_user),
|
current_user: Dict[str, Any] = Depends(get_current_user),
|
||||||
):
|
):
|
||||||
"""Serve chart preview PNG files."""
|
"""Serve chart preview PNG files."""
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
user_id = require_authenticated_user(current_user)
|
user_id = require_authenticated_user(current_user)
|
||||||
|
|
||||||
broll_service = get_broll_service()
|
broll_service = get_broll_service()
|
||||||
file_path = broll_service.output_dir / f"chart_preview_{chart_id}.png"
|
expected_filename = broll_service.get_chart_preview_filename(chart_id)
|
||||||
|
if filename != expected_filename:
|
||||||
|
raise HTTPException(status_code=404, detail="Chart preview not found")
|
||||||
|
|
||||||
|
file_path = broll_service.get_output_path(filename)
|
||||||
|
|
||||||
if not file_path.exists():
|
if not file_path.exists():
|
||||||
raise HTTPException(status_code=404, detail="Chart preview not found")
|
raise HTTPException(status_code=404, detail="Chart preview not found")
|
||||||
@@ -238,4 +243,4 @@ async def serve_final_broll(
|
|||||||
@router.get("/health")
|
@router.get("/health")
|
||||||
async def broll_health():
|
async def broll_health():
|
||||||
"""Health check for B-roll service."""
|
"""Health check for B-roll service."""
|
||||||
return {"status": "ok", "service": "broll"}
|
return {"status": "ok", "service": "broll"}
|
||||||
|
|||||||
@@ -48,6 +48,14 @@ class BrollService:
|
|||||||
def get_output_path(self, filename: str) -> Path:
|
def get_output_path(self, filename: str) -> Path:
|
||||||
"""Get output path for a file."""
|
"""Get output path for a file."""
|
||||||
return self.output_dir / filename
|
return self.output_dir / filename
|
||||||
|
|
||||||
|
def get_chart_preview_filename(self, chart_id: str) -> str:
|
||||||
|
"""Build deterministic chart preview filename from chart ID."""
|
||||||
|
return f"chart_preview_{chart_id}.png"
|
||||||
|
|
||||||
|
def get_chart_preview_path(self, chart_id: str) -> Path:
|
||||||
|
"""Get deterministic chart preview path from chart ID."""
|
||||||
|
return self.get_output_path(self.get_chart_preview_filename(chart_id))
|
||||||
|
|
||||||
def generate_chart_preview(
|
def generate_chart_preview(
|
||||||
self,
|
self,
|
||||||
@@ -55,6 +63,7 @@ class BrollService:
|
|||||||
chart_type: str = "bar_comparison",
|
chart_type: str = "bar_comparison",
|
||||||
title: str = "",
|
title: str = "",
|
||||||
subtitle: str = "",
|
subtitle: str = "",
|
||||||
|
chart_id: Optional[str] = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Generate a chart PNG preview (static, for Write phase).
|
Generate a chart PNG preview (static, for Write phase).
|
||||||
@@ -68,8 +77,8 @@ class BrollService:
|
|||||||
Returns:
|
Returns:
|
||||||
Path to generated PNG file
|
Path to generated PNG file
|
||||||
"""
|
"""
|
||||||
chart_id = uuid.uuid4().hex[:8]
|
resolved_chart_id = chart_id or uuid.uuid4().hex[:8]
|
||||||
out_path = str(self.get_output_path(f"chart_preview_{chart_id}.png"))
|
out_path = str(self.get_chart_preview_path(resolved_chart_id))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if chart_type == "bar_comparison":
|
if chart_type == "bar_comparison":
|
||||||
@@ -250,4 +259,4 @@ def get_broll_service(output_dir: Optional[str] = None) -> BrollService:
|
|||||||
global _broll_service_instance
|
global _broll_service_instance
|
||||||
if _broll_service_instance is None:
|
if _broll_service_instance is None:
|
||||||
_broll_service_instance = BrollService(output_dir=output_dir)
|
_broll_service_instance = BrollService(output_dir=output_dir)
|
||||||
return _broll_service_instance
|
return _broll_service_instance
|
||||||
|
|||||||
Reference in New Issue
Block a user