Skip to content

Agent Zero AI framework

Better download (#628)

ehl0wr0ld65f928c633ef
diff --git a/python/api/download_work_dir_file.py b/python/api/download_work_dir_file.py
index 09ca4019f..454ae5a12 100644
--- a/python/api/download_work_dir_file.py
+++ b/python/api/download_work_dir_file.py
@@ -1,10 +1,77 @@
import base64
from io import BytesIO
+import mimetypes
+import os
-from python.helpers.api import ApiHandler, Input, Output, Request, Response, send_file
+from flask import Response
+from python.helpers.api import ApiHandler, Input, Output, Request
from python.helpers import files, runtime
from python.api import file_info
-import os
+
+
+def stream_file_download(file_source, download_name, chunk_size=8192):
+ """
+ Create a streaming response for file downloads that shows progress in browser.
+
+ Args:
+ file_source: Either a file path (str) or BytesIO object
+ download_name: Name for the downloaded file
+ chunk_size: Size of chunks to stream (default 8192 bytes)
+
+ Returns:
+ Flask Response object with streaming content
+ """
+ # Calculate file size for Content-Length header
+ if isinstance(file_source, str):
+ # File path - get size from filesystem
+ file_size = os.path.getsize(file_source)
+ elif isinstance(file_source, BytesIO):
+ # BytesIO object - get size from buffer
+ current_pos = file_source.tell()
+ file_source.seek(0, 2) # Seek to end
+ file_size = file_source.tell()
+ file_source.seek(current_pos) # Restore original position
+ else:
+ raise ValueError(f"Unsupported file source type: {type(file_source)}")
+
+ def generate():
+ if isinstance(file_source, str):
+ # File path - open and stream from disk
+ with open(file_source, 'rb') as f:
+ while True:
+ chunk = f.read(chunk_size)
+ if not chunk:
+ break
+ yield chunk
+ elif isinstance(file_source, BytesIO):
+ # BytesIO object - stream from memory
+ file_source.seek(0) # Ensure we're at the beginning
+ while True:
+ chunk = file_source.read(chunk_size)
+ if not chunk:
+ break
+ yield chunk
+
+ # Detect content type based on file extension
+ content_type, _ = mimetypes.guess_type(download_name)
+ if not content_type:
+ content_type = 'application/octet-stream'
+
+ # Create streaming response with proper headers for immediate streaming
+ response = Response(
+ generate(),
+ content_type=content_type,
+ direct_passthrough=True, # Prevent Flask from buffering the response
+ headers={
+ 'Content-Disposition': f'attachment; filename="{download_name}"',
+ 'Content-Length': str(file_size), # Critical for browser progress bars
+ 'Cache-Control': 'no-cache',
+ 'X-Accel-Buffering': 'no', # Disable nginx buffering
+ 'Accept-Ranges': 'bytes' # Allow browser to resume downloads
+ }
+ )
+
+ return response
class DownloadFile(ApiHandler):
@@ -32,31 +99,27 @@ class DownloadFile(ApiHandler):
if runtime.is_development():
b64 = await runtime.call_development_function(fetch_file, zip_file)
file_data = BytesIO(base64.b64decode(b64))
- return send_file(
+ return stream_file_download(
file_data,
- as_attachment=True,
- download_name=os.path.basename(zip_file),
+ download_name=os.path.basename(zip_file)
)
else:
- return send_file(
+ return stream_file_download(
zip_file,
- as_attachment=True,
- download_name=f"{os.path.basename(file_path)}.zip",
+ download_name=f"{os.path.basename(file_path)}.zip"
)
elif file["is_file"]:
if runtime.is_development():
b64 = await runtime.call_development_function(fetch_file, file["abs_path"])
file_data = BytesIO(base64.b64decode(b64))
- return send_file(
+ return stream_file_download(
file_data,
- as_attachment=True,
- download_name=os.path.basename(file_path),
+ download_name=os.path.basename(file_path)
)
else:
- return send_file(
+ return stream_file_download(
file["abs_path"],
- as_attachment=True,
- download_name=os.path.basename(file["file_name"]),
+ download_name=os.path.basename(file["file_name"])
)
raise Exception(f"File {file_path} not found")
diff --git a/python/api/get_work_dir_files.py b/python/api/get_work_dir_files.py
index b9c5efbb4..ed2fddb5f 100644
--- a/python/api/get_work_dir_files.py
+++ b/python/api/get_work_dir_files.py
@@ -1,6 +1,7 @@
from python.helpers.api import ApiHandler, Request, Response
from python.helpers.file_browser import FileBrowser
from python.helpers import runtime
+import python.api.get_work_dir_files as get_work_dir_files_module
class GetWorkDirFiles(ApiHandler):
@@ -20,7 +21,7 @@ class GetWorkDirFiles(ApiHandler):
# browser = FileBrowser()
# result = browser.get_files(current_path)
- result = await runtime.call_development_function(get_files, current_path)
+ result = await runtime.call_development_function(get_work_dir_files_module.get_files, current_path)
return {"data": result}