Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,6 +5,7 @@ from pathlib import Path
|
|
| 5 |
import shutil
|
| 6 |
import zipfile
|
| 7 |
import logging
|
|
|
|
| 8 |
from typing import Set
|
| 9 |
|
| 10 |
# Configure logging
|
|
@@ -41,10 +42,19 @@ class SiteManager:
|
|
| 41 |
async def deploy_site(self, unique_id: str, zip_file: UploadFile) -> dict:
|
| 42 |
"""Deploy a new site from a ZIP file"""
|
| 43 |
site_path = self.sites_dir / unique_id
|
|
|
|
| 44 |
|
| 45 |
try:
|
| 46 |
-
#
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Clear existing site if present
|
| 49 |
if site_path.exists():
|
| 50 |
shutil.rmtree(site_path)
|
|
@@ -67,6 +77,14 @@ class SiteManager:
|
|
| 67 |
if site_path.exists():
|
| 68 |
shutil.rmtree(site_path)
|
| 69 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def remove_site(self, unique_id: str) -> bool:
|
| 72 |
"""Remove a deployed site"""
|
|
|
|
| 5 |
import shutil
|
| 6 |
import zipfile
|
| 7 |
import logging
|
| 8 |
+
import tempfile
|
| 9 |
from typing import Set
|
| 10 |
|
| 11 |
# Configure logging
|
|
|
|
| 42 |
async def deploy_site(self, unique_id: str, zip_file: UploadFile) -> dict:
|
| 43 |
"""Deploy a new site from a ZIP file"""
|
| 44 |
site_path = self.sites_dir / unique_id
|
| 45 |
+
temp_file = None
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
# Create a temporary file
|
| 49 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 50 |
+
# Read the uploaded file content
|
| 51 |
+
content = await zip_file.read()
|
| 52 |
+
# Write to the temporary file
|
| 53 |
+
temp_file.write(content)
|
| 54 |
+
temp_file.close()
|
| 55 |
+
|
| 56 |
+
# Extract ZIP contents
|
| 57 |
+
with zipfile.ZipFile(temp_file.name) as zip_ref:
|
| 58 |
# Clear existing site if present
|
| 59 |
if site_path.exists():
|
| 60 |
shutil.rmtree(site_path)
|
|
|
|
| 77 |
if site_path.exists():
|
| 78 |
shutil.rmtree(site_path)
|
| 79 |
raise HTTPException(status_code=500, detail=str(e))
|
| 80 |
+
|
| 81 |
+
finally:
|
| 82 |
+
# Clean up the temporary file
|
| 83 |
+
if temp_file:
|
| 84 |
+
try:
|
| 85 |
+
Path(temp_file.name).unlink()
|
| 86 |
+
except:
|
| 87 |
+
pass
|
| 88 |
|
| 89 |
def remove_site(self, unique_id: str) -> bool:
|
| 90 |
"""Remove a deployed site"""
|