1919from playwright ._impl ._errors import Error as PlaywrightError
2020from bs4 import BeautifulSoup
2121import json
22+ import tempfile
2223
2324
2425# Initialize FastAPI application
@@ -277,7 +278,7 @@ async def dynamic_redirect(request: Request, key: str):
277278 "error_message" : "An error occurred while processing your code." ,
278279 "traceback" : tb_str ,
279280 "messages" : [["danger" , "An error occurred while executing your code." ]]
280- })
281+ }), 400
281282 else :
282283 return "The 'code' field is empty." , 400 # Bad request error for empty 'code'
283284 return "Handler function not found." , 404 # Not found error if document doesn't exist.
@@ -293,10 +294,9 @@ async def dynamic_download(request: Request, key: str, file_name: str = Query(..
293294 code = document .get ("code" )
294295 if code :
295296 try :
296- result = await execute_async_code (code )
297- print (result )
297+ result = str (await execute_async_code (code ))
298298
299- content : bytes = bytes ()
299+ content : bytes = None
300300 media_type : str = 'application/octet-stream'
301301
302302 if not BROWSER_WS :
@@ -305,33 +305,20 @@ async def dynamic_download(request: Request, key: str, file_name: str = Query(..
305305 response .raise_for_status ()
306306 content = await response .read ()
307307 else :
308- async with async_playwright () as playwright :
309- browser = await playwright . chromium . connect ( BROWSER_WS )
310- try :
308+ try :
309+ async with async_playwright () as playwright :
310+ browser = await playwright . chromium . connect ( BROWSER_WS )
311311 context = await browser .new_context ()
312312 page = await context .new_page ()
313313 response = await page .goto (result )
314314 if response .ok :
315315 content = await response .body ()
316316 media_type = response .headers .get ('Content-Type' , 'application/octet-stream' )
317- except PlaywrightError :
318- context = await browser .new_context (accept_downloads = True )
319- page = await context .new_page ()
320- try :
321- async with page .expect_download () as download_info :
322- await page .goto (result )
323- download = await download_info .value
324- stream = await download .create_read_stream ()
325- buffer = io .BytesIO ()
326- async for chunk in stream :
327- buffer .write (chunk )
328- content = buffer .getvalue ()
329- media_type = response .headers .get ('Content-Type' , 'application/octet-stream' )
330- except :
331- pass
332- finally :
333- await page .close ()
334- await browser .close ()
317+ except PlaywrightError :
318+ async with aiohttp .ClientSession () as session :
319+ async with session .get (result ) as response :
320+ response .raise_for_status ()
321+ content = await response .read ()
335322
336323 return StreamingResponse (
337324 iter ([content ]),
@@ -347,7 +334,7 @@ async def dynamic_download(request: Request, key: str, file_name: str = Query(..
347334 "error_message" : "An error occurred while processing your code." ,
348335 "traceback" : tb_str ,
349336 "messages" : [["danger" , "An error occurred while executing your code." ]]
350- })
337+ }), 400
351338 else :
352339 return "The 'code' field is empty." , 400 # Bad request error for empty 'code'
353340 return "Handler function not found." , 404 # Not found error if document doesn't exist.
0 commit comments