From 9f9e9b601e4ce420babe7b6d3e11fc0d6cbcdb67 Mon Sep 17 00:00:00 2001 From: Fireblade Date: Thu, 13 Mar 2025 16:23:49 -0400 Subject: [PATCH] Fixes not returning a download link if streaming is off and return_download_link is true --- api/src/routers/openai_compatible.py | 38 +++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/api/src/routers/openai_compatible.py b/api/src/routers/openai_compatible.py index 97844b1..10e32bf 100644 --- a/api/src/routers/openai_compatible.py +++ b/api/src/routers/openai_compatible.py @@ -282,6 +282,11 @@ async def create_speech( }, ) else: + headers = { + "Content-Disposition": f"attachment; filename=speech.{request.response_format}", + "Cache-Control": "no-cache", # Prevent caching + } + # Generate complete audio using public interface audio_data = await tts_service.generate_audio( text=request.input, @@ -309,13 +314,38 @@ async def create_speech( is_last_chunk=True, ) output=audio_data.output + final.output + + if request.return_download_link: + from ..services.temp_manager import TempFileWriter + # Use download_format if specified, otherwise use response_format + output_format = request.download_format or request.response_format + temp_writer = TempFileWriter(output_format) + await temp_writer.__aenter__() # Initialize temp file + + # Get download path immediately after temp file creation + download_path = temp_writer.download_path + headers["X-Download-Path"] = download_path + + try: + # Write chunks to temp file + logger.info("Writing chunks to tempory file for download") + await temp_writer.write(output) + # Finalize the temp file + await temp_writer.finalize() + + except Exception as e: + logger.error(f"Error in dual output: {e}") + await temp_writer.__aexit__(type(e), e, e.__traceback__) + raise + finally: + # Ensure temp writer is closed + if not temp_writer._finalized: + await temp_writer.__aexit__(None, None, None) + return Response( content=output, media_type=content_type, - headers={ - "Content-Disposition": f"attachment; filename=speech.{request.response_format}", - "Cache-Control": "no-cache", # Prevent caching - }, + headers=headers, ) except ValueError as e: