Skip to content

Agent Zero AI framework

download and remove folders in browser

frdel857f8b6d82ec
diff --git a/python/api/download_work_dir_file.py b/python/api/download_work_dir_file.py
index 4e1c47274..dd6639443 100644
--- a/python/api/download_work_dir_file.py
+++ b/python/api/download_work_dir_file.py
@@ -6,22 +6,25 @@ from python.helpers import files
import os
-
-
class DownloadWorkDirFile(ApiHandler):
async def process(self, input: dict, request: Request) -> dict | Response:
- file_path = request.args.get('path', '')
+ file_path = request.args.get("path", "")
if not file_path:
raise ValueError("No file path provided")
-
+
work_dir = files.get_abs_path("work_dir")
browser = FileBrowser(work_dir)
-
- full_path = browser.get_file_path(file_path)
- if full_path:
+
+ full_path = browser.get_full_path(file_path, True)
+ if os.path.isdir(full_path):
+ zip_file = browser.zip_dir(full_path)
return send_file(
- full_path,
+ zip_file,
as_attachment=True,
- download_name=os.path.basename(file_path)
+ download_name=f"{os.path.basename(file_path)}.zip",
+ )
+ if full_path:
+ return send_file(
+ full_path, as_attachment=True, download_name=os.path.basename(file_path)
)
- raise Exception("File not found")
\ No newline at end of file
+ raise Exception("File not found")
diff --git a/python/helpers/file_browser.py b/python/helpers/file_browser.py
index 9ed72209b..8727442e4 100644
--- a/python/helpers/file_browser.py
+++ b/python/helpers/file_browser.py
@@ -1,9 +1,14 @@
import os
from pathlib import Path
+import shutil
+import tempfile
from typing import Dict, List, Tuple, Optional, Any
+import zipfile
from werkzeug.utils import secure_filename
from datetime import datetime
+from python.helpers import files
+
class FileBrowser:
ALLOWED_EXTENSIONS = {
'image': {'jpg', 'jpeg', 'png', 'bmp'},
@@ -69,10 +74,8 @@ class FileBrowser:
if os.path.exists(full_path):
if os.path.isfile(full_path):
os.remove(full_path)
- elif os.path.isdir(full_path) and not os.listdir(full_path):
- os.rmdir(full_path)
- else:
- raise ValueError("Can only delete files or empty directories")
+ elif os.path.isdir(full_path):
+ shutil.rmtree(full_path)
return True
return False
@@ -82,12 +85,14 @@ class FileBrowser:
return False
def _is_allowed_file(self, filename: str, file) -> bool:
- if not filename:
- return False
- ext = self._get_file_extension(filename)
- all_allowed = set().union(*self.ALLOWED_EXTENSIONS.values())
- if ext not in all_allowed:
- return False
+ # allow any file to be uploaded in file browser
+
+ # if not filename:
+ # return False
+ # ext = self._get_file_extension(filename)
+ # all_allowed = set().union(*self.ALLOWED_EXTENSIONS.values())
+ # if ext not in all_allowed:
+ # return False
return True # Allow the file if it passes the checks
@@ -154,20 +159,12 @@ class FileBrowser:
print(f"Error reading directory: {e}")
return {"entries": [], "current_path": "", "parent_path": ""}
- def get_file_path(self, file_path: str) -> Optional[Path]:
+ def get_full_path(self, file_path: str, allow_dir: bool = False) -> str:
"""Get full file path if it exists and is within base_dir"""
- try:
- full_path = (self.base_dir / file_path).resolve()
- if not str(full_path).startswith(str(self.base_dir)):
- raise ValueError("Invalid path")
-
- if os.path.isfile(full_path):
- return full_path
- return None
-
- except Exception as e:
- print(f"Error accessing file {file_path}: {e}")
- return None
+ full_path = files.get_abs_path(self.base_dir,file_path)
+ if not files.exists(full_path):
+ raise ValueError(f"File {file_path} not found")
+ return full_path
def _get_file_type(self, filename: str) -> str:
ext = self._get_file_extension(filename)
@@ -175,4 +172,16 @@ class FileBrowser:
if ext in extensions:
return file_type
return 'unknown'
+
+ def zip_dir(self, dir_path: str):
+ full_path = self.get_full_path(dir_path, allow_dir=True)
+ zip_file_path = tempfile.NamedTemporaryFile(suffix='.zip', delete=False).name
+ base_name = os.path.basename(full_path)
+ with zipfile.ZipFile(zip_file_path, "w", compression=zipfile.ZIP_DEFLATED) as zip:
+ for root, _, files in os.walk(full_path):
+ for file in files:
+ file_path = os.path.join(root, file)
+ rel_path = os.path.relpath(file_path, full_path)
+ zip.write(file_path, os.path.join(base_name, rel_path))
+ return zip_file_path
\ No newline at end of file
diff --git a/webui/index.html b/webui/index.html
index 974ce583e..86554193b 100644
--- a/webui/index.html
+++ b/webui/index.html
@@ -581,7 +581,7 @@
<div class="file-actions">
<button class="action-button download-button"
- @click.stop="downloadFile(file)" x-bind:class="{ 'invisible': file.is_dir }">
+ @click.stop="downloadFile(file)">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.5 19.5">
<path
d="m.75,14.25v2.25c0,1.24,1.01,2.25,2.25,2.25h13.5c1.24,0,2.25-1.01,2.25-2.25v-2.25m-4.5-4.5l-4.5,4.5m0,0l-4.5-4.5m4.5,4.5V.75"
@@ -589,8 +589,7 @@
stroke-linejoin="round" stroke-width="1.5"></path>
</svg>
</button>
- <button class="delete-button" @click.stop="deleteFile(file)"
- x-bind:class="{ 'invisible': file.is_dir && file.size !== 0 }">
+ <button class="delete-button" @click.stop="deleteFile(file)">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15.03 22.53"
fill="currentColor">
<path
diff --git a/webui/js/file_browser.js b/webui/js/file_browser.js
index 6823e0f98..de7ba443d 100644
--- a/webui/js/file_browser.js
+++ b/webui/js/file_browser.js
@@ -186,7 +186,6 @@ const fileBrowserModalProxy = {
},
async downloadFile(file) {
- if (file.is_dir) return;
try {