File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77
88import sys
99import os
10+ import tempfile
1011from datetime import datetime
1112
1213# Add the parent directory to the path
@@ -50,9 +51,9 @@ def create_sample_report():
5051 # Format the data
5152 branch_data = generator .format_branch_data (branches , prs )
5253
53- # Generate output filename
54+ # Generate output filename (platform-independent)
5455 timestamp = datetime .now ().strftime ("%Y%m%d_%H%M%S" )
55- output_file = f"/tmp/ sample_branch_report_{ timestamp } .xlsx"
56+ output_file = os . path . join ( tempfile . gettempdir (), f" sample_branch_report_{ timestamp } .xlsx")
5657
5758 # Generate Excel file
5859 print (f"Generating sample branch report..." )
Original file line number Diff line number Diff line change @@ -1430,6 +1430,7 @@ async def get_plan_by_id(
14301430
14311431@app_v4 .get ("/generate_branch_report" )
14321432async def generate_branch_report (
1433+ background_tasks : BackgroundTasks ,
14331434 request : Request ,
14341435 owner : str = Query (..., description = "Repository owner (username or organization)" ),
14351436 repo : str = Query (..., description = "Repository name" ),
@@ -1513,12 +1514,26 @@ async def generate_branch_report(
15131514 },
15141515 )
15151516
1517+ # Create a background task to clean up the file after a delay
1518+ async def cleanup_file ():
1519+ """Delete the temporary file after a delay to ensure download completes."""
1520+ import asyncio
1521+ await asyncio .sleep (300 ) # Wait 5 minutes before cleanup
1522+ try :
1523+ if os .path .exists (output_path ):
1524+ os .remove (output_path )
1525+ logger .info (f"Cleaned up temporary file: { output_path } " )
1526+ except Exception as cleanup_error :
1527+ logger .warning (f"Failed to cleanup temporary file { output_path } : { cleanup_error } " )
1528+
1529+ # Schedule the cleanup task
1530+ background_tasks .add_task (cleanup_file )
1531+
15161532 # Return the file
15171533 return FileResponse (
15181534 path = output_path ,
15191535 media_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
15201536 filename = filename ,
1521- background = None , # Don't delete the file immediately to ensure download completes
15221537 )
15231538
15241539 except HTTPException :
You can’t perform that action at this time.
0 commit comments