Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -559,7 +559,12 @@ def process_heavy_files_background(space_id: str, saved_file_paths: List[Dict]):
|
|
| 559 |
print(f"Background processing failed: {e}")
|
| 560 |
|
| 561 |
@app.post("/api/spaces/{space_id}/upload")
|
| 562 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
"""Accepts files quickly and processes them in the background"""
|
| 564 |
try:
|
| 565 |
space_dir = get_space_dir(space_id)
|
|
@@ -568,7 +573,7 @@ async def upload_files(space_id: str, background_tasks: BackgroundTasks, files:
|
|
| 568 |
|
| 569 |
saved_files = []
|
| 570 |
|
| 571 |
-
# 1. Save files to hard drive
|
| 572 |
for file in files:
|
| 573 |
file_path = uploads_dir / file.filename
|
| 574 |
with open(file_path, "wb") as f:
|
|
@@ -583,6 +588,7 @@ async def upload_files(space_id: str, background_tasks: BackgroundTasks, files:
|
|
| 583 |
# 2. Hand heavy math and Mongo saving to background task
|
| 584 |
background_tasks.add_task(process_heavy_files_background, space_id, saved_files)
|
| 585 |
|
|
|
|
| 586 |
return {
|
| 587 |
"status": "processing",
|
| 588 |
"message": f"Successfully received {len(files)} files. Processing in the background."
|
|
|
|
| 559 |
print(f"Background processing failed: {e}")
|
| 560 |
|
| 561 |
@app.post("/api/spaces/{space_id}/upload")
|
| 562 |
+
@app.post("/api/spaces/{space_id}/upload")
|
| 563 |
+
async def upload_files(
|
| 564 |
+
space_id: str,
|
| 565 |
+
background_tasks: BackgroundTasks,
|
| 566 |
+
files: List[UploadFile] = File(...)
|
| 567 |
+
):
|
| 568 |
"""Accepts files quickly and processes them in the background"""
|
| 569 |
try:
|
| 570 |
space_dir = get_space_dir(space_id)
|
|
|
|
| 573 |
|
| 574 |
saved_files = []
|
| 575 |
|
| 576 |
+
# 1. Save files to hard drive (Extremely Fast)
|
| 577 |
for file in files:
|
| 578 |
file_path = uploads_dir / file.filename
|
| 579 |
with open(file_path, "wb") as f:
|
|
|
|
| 588 |
# 2. Hand heavy math and Mongo saving to background task
|
| 589 |
background_tasks.add_task(process_heavy_files_background, space_id, saved_files)
|
| 590 |
|
| 591 |
+
# 3. Reply instantly to prevent timeouts
|
| 592 |
return {
|
| 593 |
"status": "processing",
|
| 594 |
"message": f"Successfully received {len(files)} files. Processing in the background."
|