Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import uuid
|
|
| 6 |
import shutil
|
| 7 |
import aiohttp
|
| 8 |
import gc # Memory management ke liye garbage collector
|
|
|
|
| 9 |
from fastapi import FastAPI, BackgroundTasks, Request, File, UploadFile
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from fastapi.responses import FileResponse, StreamingResponse
|
|
@@ -328,7 +329,7 @@ async def process_gofile_transfer(task_id: str, url: str):
|
|
| 328 |
del chunk
|
| 329 |
gc.collect()
|
| 330 |
|
| 331 |
-
# 2. Upload to GoFile
|
| 332 |
tasks[task_id]["status"] = "uploading_to_gofile"
|
| 333 |
tasks[task_id]["speed"] = 0.0
|
| 334 |
|
|
@@ -338,11 +339,9 @@ async def process_gofile_transfer(task_id: str, url: str):
|
|
| 338 |
servers_data = await resp.json()
|
| 339 |
server = servers_data["data"]["servers"][0]["name"]
|
| 340 |
|
| 341 |
-
# Pure file standard dynamic multi-part handling without raw python generator clash
|
| 342 |
upload_url = f"https://{server}.gofile.io/uploadFile"
|
| 343 |
|
| 344 |
data = aiohttp.FormData()
|
| 345 |
-
# File pointer reference integration
|
| 346 |
with open(file_path, 'rb') as f_upload:
|
| 347 |
data.add_field('file', f_upload, filename=tasks[task_id]["original_filename"])
|
| 348 |
|
|
@@ -366,6 +365,72 @@ async def process_gofile_transfer(task_id: str, url: str):
|
|
| 366 |
finally:
|
| 367 |
gc.collect()
|
| 368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
if __name__ == "__main__":
|
| 370 |
import uvicorn
|
| 371 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 6 |
import shutil
|
| 7 |
import aiohttp
|
| 8 |
import gc # Memory management ke liye garbage collector
|
| 9 |
+
import zipfile # ZIP file handle karne ke liye in-built library
|
| 10 |
from fastapi import FastAPI, BackgroundTasks, Request, File, UploadFile
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
from fastapi.responses import FileResponse, StreamingResponse
|
|
|
|
| 329 |
del chunk
|
| 330 |
gc.collect()
|
| 331 |
|
| 332 |
+
# 2. Upload to GoFile
|
| 333 |
tasks[task_id]["status"] = "uploading_to_gofile"
|
| 334 |
tasks[task_id]["speed"] = 0.0
|
| 335 |
|
|
|
|
| 339 |
servers_data = await resp.json()
|
| 340 |
server = servers_data["data"]["servers"][0]["name"]
|
| 341 |
|
|
|
|
| 342 |
upload_url = f"https://{server}.gofile.io/uploadFile"
|
| 343 |
|
| 344 |
data = aiohttp.FormData()
|
|
|
|
| 345 |
with open(file_path, 'rb') as f_upload:
|
| 346 |
data.add_field('file', f_upload, filename=tasks[task_id]["original_filename"])
|
| 347 |
|
|
|
|
| 365 |
finally:
|
| 366 |
gc.collect()
|
| 367 |
|
| 368 |
+
# ==================== NEW ZIP MANAGEMENT FEATURES ====================
|
| 369 |
+
|
| 370 |
+
@app.get("/list_zip/{task_id}")
|
| 371 |
+
async def list_zip_contents(task_id: str):
|
| 372 |
+
"""Bina extract kiye ZIP ke andar ki saari files ki list dikhayega"""
|
| 373 |
+
if task_id not in tasks:
|
| 374 |
+
return {"error": "Task not found"}
|
| 375 |
+
|
| 376 |
+
file_path = tasks[task_id]["file_path"]
|
| 377 |
+
if not os.path.exists(file_path):
|
| 378 |
+
return {"error": "File does not exist on server"}
|
| 379 |
+
|
| 380 |
+
if not zipfile.is_zipfile(file_path):
|
| 381 |
+
return {"error": "This file is not a valid ZIP archive"}
|
| 382 |
+
|
| 383 |
+
try:
|
| 384 |
+
with zipfile.ZipFile(file_path, 'r') as z:
|
| 385 |
+
file_list = []
|
| 386 |
+
for info in z.infolist():
|
| 387 |
+
file_list.append({
|
| 388 |
+
"filename": info.filename,
|
| 389 |
+
"file_size_mb": round(info.file_size / (1024 * 1024), 2),
|
| 390 |
+
"is_dir": info.is_dir()
|
| 391 |
+
})
|
| 392 |
+
return {"task_id": task_id, "total_files": len(file_list), "files": file_list}
|
| 393 |
+
except Exception as e:
|
| 394 |
+
return {"error": f"Failed to read ZIP: {str(e)}"}
|
| 395 |
+
|
| 396 |
+
@app.post("/extract_zip/{task_id}")
|
| 397 |
+
async def extract_zip_file(task_id: str, background_tasks: BackgroundTasks):
|
| 398 |
+
"""Background mein ZIP file ko extract karne ka process shuru karega"""
|
| 399 |
+
if task_id not in tasks:
|
| 400 |
+
return {"error": "Task not found"}
|
| 401 |
+
|
| 402 |
+
file_path = tasks[task_id]["file_path"]
|
| 403 |
+
if not os.path.exists(file_path):
|
| 404 |
+
return {"error": "File does not exist on server"}
|
| 405 |
+
|
| 406 |
+
if not zipfile.is_zipfile(file_path):
|
| 407 |
+
return {"error": "This file is not a valid ZIP archive"}
|
| 408 |
+
|
| 409 |
+
# Extraction folder name (Task ID ke sath folder banega jisse files mix na hon)
|
| 410 |
+
extract_folder = os.path.join(DATA_DIR, f"extracted_{task_id}")
|
| 411 |
+
os.makedirs(extract_folder, exist_ok=True)
|
| 412 |
+
|
| 413 |
+
tasks[task_id]["status"] = "extracting"
|
| 414 |
+
tasks[task_id]["extract_path"] = extract_folder
|
| 415 |
+
|
| 416 |
+
# Background mein execution taaki API request time out na ho aur UI freeze na ho
|
| 417 |
+
background_tasks.add_task(process_zip_extraction, task_id, file_path, extract_folder)
|
| 418 |
+
return {"status": "extraction_started", "task_id": task_id, "target_folder": extract_folder}
|
| 419 |
+
|
| 420 |
+
def process_zip_extraction(task_id: str, zip_path: str, target_dir: str):
|
| 421 |
+
"""Background extraction worker with automatic memory release"""
|
| 422 |
+
try:
|
| 423 |
+
with zipfile.ZipFile(zip_path, 'r') as z:
|
| 424 |
+
z.extractall(path=target_dir)
|
| 425 |
+
tasks[task_id]["status"] = "extracted"
|
| 426 |
+
except Exception as e:
|
| 427 |
+
tasks[task_id]["status"] = "extraction_error"
|
| 428 |
+
tasks[task_id]["error"] = str(e)
|
| 429 |
+
finally:
|
| 430 |
+
gc.collect()
|
| 431 |
+
|
| 432 |
+
# =====================================================================
|
| 433 |
+
|
| 434 |
if __name__ == "__main__":
|
| 435 |
import uvicorn
|
| 436 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|