Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from fastapi.responses import FileResponse, JSONResponse
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import os
|
| 5 |
import uuid
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
@@ -19,6 +20,51 @@ UPLOAD_DIRECTORY = "./uploads"
|
|
| 19 |
|
| 20 |
os.makedirs(UPLOAD_DIRECTORY, exist_ok=True)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@app.post("/upload")
|
| 23 |
async def upload_mp4(file: UploadFile = File(...)):
|
| 24 |
if not file.filename.endswith(".mp4"):
|
|
@@ -55,3 +101,4 @@ async def delete_mp4(mp4_id: str):
|
|
| 55 |
return JSONResponse(content={"message": f"File {mp4_id} deleted successfully"}, status_code=200)
|
| 56 |
except Exception as e:
|
| 57 |
raise HTTPException(status_code=500, detail=f"Failed to delete file: {str(e)}")
|
|
|
|
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import os
|
| 5 |
import uuid
|
| 6 |
+
import shutil
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 20 |
|
| 21 |
os.makedirs(UPLOAD_DIRECTORY, exist_ok=True)
|
| 22 |
|
| 23 |
+
def format_bytes(bytes_value):
|
| 24 |
+
"""Convert bytes to human readable format"""
|
| 25 |
+
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
| 26 |
+
if bytes_value < 1024.0:
|
| 27 |
+
return f"{bytes_value:.2f} {unit}"
|
| 28 |
+
bytes_value /= 1024.0
|
| 29 |
+
return f"{bytes_value:.2f} PB"
|
| 30 |
+
|
| 31 |
+
def check_disk_space():
|
| 32 |
+
"""Check disk space on startup and log the information"""
|
| 33 |
+
try:
|
| 34 |
+
# Get disk usage for the current directory
|
| 35 |
+
total, used, free = shutil.disk_usage(".")
|
| 36 |
+
|
| 37 |
+
# Calculate usage percentage
|
| 38 |
+
usage_percentage = (used / total) * 100 if total > 0 else 0
|
| 39 |
+
|
| 40 |
+
print("=" * 50)
|
| 41 |
+
print("DISK SPACE CHECK ON STARTUP")
|
| 42 |
+
print("=" * 50)
|
| 43 |
+
print(f"Total space: {format_bytes(total)}")
|
| 44 |
+
print(f"Used space: {format_bytes(used)}")
|
| 45 |
+
print(f"Free space: {format_bytes(free)}")
|
| 46 |
+
print(f"Usage percentage: {usage_percentage:.2f}%")
|
| 47 |
+
print(f"Upload directory: {os.path.abspath(UPLOAD_DIRECTORY)}")
|
| 48 |
+
print(f"Upload directory exists: {os.path.exists(UPLOAD_DIRECTORY)}")
|
| 49 |
+
|
| 50 |
+
# Warning if disk space is low
|
| 51 |
+
if usage_percentage > 90:
|
| 52 |
+
print("⚠️ WARNING: Disk space is critically low!")
|
| 53 |
+
elif usage_percentage > 80:
|
| 54 |
+
print("⚠️ WARNING: Disk space is getting low!")
|
| 55 |
+
else:
|
| 56 |
+
print("✅ Disk space is adequate")
|
| 57 |
+
|
| 58 |
+
print("=" * 50)
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"❌ Failed to check disk space: {str(e)}")
|
| 62 |
+
|
| 63 |
+
@app.on_event("startup")
|
| 64 |
+
async def startup_event():
|
| 65 |
+
"""Run startup tasks"""
|
| 66 |
+
check_disk_space()
|
| 67 |
+
|
| 68 |
@app.post("/upload")
|
| 69 |
async def upload_mp4(file: UploadFile = File(...)):
|
| 70 |
if not file.filename.endswith(".mp4"):
|
|
|
|
| 101 |
return JSONResponse(content={"message": f"File {mp4_id} deleted successfully"}, status_code=200)
|
| 102 |
except Exception as e:
|
| 103 |
raise HTTPException(status_code=500, detail=f"Failed to delete file: {str(e)}")
|
| 104 |
+
|