Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Clawdbot Unified Command Center
|
| 3 |
|
|
@@ -672,36 +674,50 @@ import zipfile
|
|
| 672 |
import shutil
|
| 673 |
|
| 674 |
def process_uploaded_file(file) -> str:
|
| 675 |
-
"""
|
| 676 |
-
|
| 677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
file_path = file.name if hasattr(file, 'name') else str(file)
|
| 679 |
file_name = os.path.basename(file_path)
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
if file_name.lower().endswith('.zip'):
|
| 685 |
-
extract_path = upload_dir / file_name.replace('.zip', '')
|
| 686 |
-
extract_path.mkdir(parents=True, exist_ok=True)
|
| 687 |
try:
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
except Exception as e:
|
| 692 |
-
return f"
|
| 693 |
-
|
| 694 |
-
#
|
| 695 |
-
suffix = os.path.splitext(file_name)[1].lower()
|
| 696 |
if suffix in TEXT_EXTENSIONS or suffix == '':
|
| 697 |
try:
|
| 698 |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 699 |
content = f.read()
|
| 700 |
-
if len(content) > 50000:
|
|
|
|
| 701 |
return f"π **Uploaded: {file_name}**\n```\n{content}\n```"
|
| 702 |
except Exception as e:
|
| 703 |
-
return f"π **Uploaded: {file_name}** (
|
| 704 |
-
|
|
|
|
| 705 |
|
| 706 |
|
| 707 |
# die Standard file handling remains the same...
|
|
|
|
| 1 |
+
import zipfile
|
| 2 |
+
import shutil
|
| 3 |
"""
|
| 4 |
Clawdbot Unified Command Center
|
| 5 |
|
|
|
|
| 674 |
import shutil
|
| 675 |
|
| 676 |
def process_uploaded_file(file) -> str:
|
| 677 |
+
"""
|
| 678 |
+
Read an uploaded file.
|
| 679 |
+
If it's text, read it into context.
|
| 680 |
+
If it's a ZIP, extract it so the agent can see the files inside.
|
| 681 |
+
"""
|
| 682 |
+
if file is None:
|
| 683 |
+
return ""
|
| 684 |
+
|
| 685 |
file_path = file.name if hasattr(file, 'name') else str(file)
|
| 686 |
file_name = os.path.basename(file_path)
|
| 687 |
+
suffix = os.path.splitext(file_name)[1].lower()
|
| 688 |
+
|
| 689 |
+
# NEW: Handle ZIP files
|
| 690 |
+
if suffix == '.zip':
|
|
|
|
|
|
|
|
|
|
| 691 |
try:
|
| 692 |
+
# Create a directory to unzip into
|
| 693 |
+
extract_to = Path(REPO_PATH) / "uploaded_assets" / file_name.replace(".zip", "")
|
| 694 |
+
extract_to.mkdir(parents=True, exist_ok=True)
|
| 695 |
+
|
| 696 |
+
import zipfile
|
| 697 |
+
with zipfile.ZipFile(file_path, 'r') as zip_ref:
|
| 698 |
+
zip_ref.extractall(extract_to)
|
| 699 |
+
|
| 700 |
+
# List what we found so the AI knows what's there
|
| 701 |
+
file_list = [f.name for f in extract_to.glob('*')]
|
| 702 |
+
return (f"π¦ **Unzipped: {file_name}**\n"
|
| 703 |
+
f"Location: `{extract_to}`\n"
|
| 704 |
+
f"Contents: {', '.join(file_list)[:200]}...\n"
|
| 705 |
+
f"SYSTEM NOTE: Use list_files('{extract_to.name}') to explore these files.")
|
| 706 |
except Exception as e:
|
| 707 |
+
return f"β οΈ Failed to unzip {file_name}: {e}"
|
| 708 |
+
|
| 709 |
+
# EXISTING: Handle Text files
|
|
|
|
| 710 |
if suffix in TEXT_EXTENSIONS or suffix == '':
|
| 711 |
try:
|
| 712 |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| 713 |
content = f.read()
|
| 714 |
+
if len(content) > 50000:
|
| 715 |
+
content = content[:50000] + f"\n\n... (truncated)"
|
| 716 |
return f"π **Uploaded: {file_name}**\n```\n{content}\n```"
|
| 717 |
except Exception as e:
|
| 718 |
+
return f"π **Uploaded: {file_name}** (error reading: {e})"
|
| 719 |
+
else:
|
| 720 |
+
return f"π **Uploaded: {file_name}** (binary file, {os.path.getsize(file_path):,} bytes)"
|
| 721 |
|
| 722 |
|
| 723 |
# die Standard file handling remains the same...
|