""" FIXED app.py -- THED1/AIAGENT (drop-in replacement) ===================================================== Fixes: "badi file adhuri reh jati hai", "RAM full ho jati hai", "beech me fail". ASLI ROOT CAUSE (code ka bug NAHI, infra ki limit): HF free Space = 16GB RAM + 50GB ephemeral disk. 50GB cross karte hi HF container ko "Workload Evicted, Storage Limit Exceeded (50G)" maar deta hai. Isliye 50.98GB wali file har baar 97-98% par mardi gayi (2/2 attempts, 49.65GB aur 50.11GB par). Ab code 2 second me hi clear error deta hai. CODE BUGS JO THEEK KIYE: 1) os.fsync() har 640KB par -> 51GB par ~77,000 fsync, 3-5x slow 2) gc.collect() har 640KB par -> CPU barbaad 3) f.flush() har 64KB par -> bekaar syscalls 4) timeout=None -> stall ho to download umr bhar atka 5) koi retry/resume nahi -> ek baar toota to sab gaya 6) SABSE KHATARNAK: stream khatam hote hi status="completed", bina check kiye ki mile bytes == Content-Length -> ADHURI file bhi "completed" 7) disk/space pre-check nahi -> beech me ENOSPC 8) single connection 15-20MB/s -> ab Range wale servers par 8 parallel segments 9) urllib3 auto-retry mid-stream body ko 0 se dobara bhejta hai -> corruption (ab retries=False, hum khud resume karte hain) RAM: 4MB buffer per connection, page cache har 256MB par drop. 51GB stream karte waqt peak RAM ~43MB (measured). """ import asyncio import json import os import re import time import uuid import shutil import gc import zipfile import threading import urllib3 from urllib3.util import Timeout from fastapi import FastAPI, BackgroundTasks, Request, File, UploadFile from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse, StreamingResponse from pydantic import BaseModel import aiohttp app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) tasks = {} DATA_DIR = "/data" # HF Space me /data mounted volume hai os.makedirs(DATA_DIR, exist_ok=True) STATE_FILE = os.path.join(DATA_DIR, ".tasks_state.json") FLUSH_EVERY = 256 * 1024 * 1024 # 256MB ke baad cache drop # HF free Space: 16GB RAM + 50GB ephemeral disk. 50GB upar container EVICT. MAX_TOTAL_STORAGE = int(os.environ.get("MAX_TOTAL_STORAGE", 45 * 1024 ** 3)) DISK_MARGIN = 2 * 1024 ** 3 CHUNK = 4 * 1024 * 1024 CONNECT_TIMEOUT = 30 READ_TIMEOUT = 180 MAX_ATTEMPTS = 6 SEGMENTS = 8 MIN_SIZE_FOR_PARALLEL = 32 * 1024 * 1024 _http = urllib3.PoolManager(maxsize=32, block=True) class DownloadRequest(BaseModel): url: str def clean_filename(url: str) -> str: path = url.split("?")[0] filename = path.split("/")[-1] if not filename or "." not in filename: return "downloaded_file.bin" return filename def _filename_from_cd(cd: str): if not cd or "filename=" not in cd: return None fname = re.findall('filename="([^"]+)"', cd) if not fname: fname = re.findall("filename=([^;]+)", cd) return fname[0] if fname else None def _drop_cache(fd): """Page cache drop -> container ka memory limit safe, OOM-kill se bachav.""" try: os.fsync(fd) os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED) except Exception: pass def save_state(): """Task state disk par -> container restart hone par bhi task list bachi rahe.""" try: clean = {} for k, v in tasks.items(): clean[k] = {kk: vv for kk, vv in v.items() if not kk.startswith("_")} tmp = STATE_FILE + ".tmp" with open(tmp, "w") as f: json.dump(clean, f) os.replace(tmp, STATE_FILE) except Exception: pass def load_state(): try: with open(STATE_FILE) as f: return json.load(f) except Exception: return {} def probe_url(url: str): """Size, range-support aur filename nikalo (download se pehle).""" info = {"total": 0, "ranges": False, "filename": None, "status": None} sizes = [] # sab observed sizes me se SABSE BADA sachcha mana jayega, # taaki koi chhota error/rate-limit page size na ban jaye try: r = _http.request("HEAD", url, preload_content=False, timeout=Timeout(connect=CONNECT_TIMEOUT, read=60), retries=2) info["status"] = r.status cl = r.headers.get("Content-Length") if cl: sizes.append(int(cl)) ar = r.headers.get("Accept-Ranges") if ar and "bytes" in ar.lower(): info["ranges"] = True info["filename"] = _filename_from_cd(r.headers.get("Content-Disposition")) r.release_conn() except Exception: pass try: r = _http.request("GET", url, headers={"Range": "bytes=0-0"}, preload_content=False, timeout=Timeout(connect=CONNECT_TIMEOUT, read=60), retries=2) info["status"] = r.status cr = r.headers.get("Content-Range") if r.status == 206 and cr and "/" in cr: info["ranges"] = True try: sizes.append(int(cr.split("/")[-1])) except Exception: pass else: info["ranges"] = False # server Range ignore karta hai cl = r.headers.get("Content-Length") if cl: sizes.append(int(cl)) if not info["filename"]: info["filename"] = _filename_from_cd(r.headers.get("Content-Disposition")) r.release_conn() except Exception as e: info["probe_error"] = str(e) info["total"] = max(sizes) if sizes else 0 return info def _download_range(url, path, start, end, state, idx, cancel): """start..end (inclusive) bytes download, apne aap resume + retry.""" pos = start attempt = 0 fd = os.open(path, os.O_WRONLY) try: while pos <= end and attempt < MAX_ATTEMPTS and not cancel.is_set(): try: r = _http.request( "GET", url, headers={"Range": f"bytes={pos}-{end}"}, preload_content=False, retries=False, timeout=Timeout(connect=CONNECT_TIMEOUT, read=READ_TIMEOUT), ) if r.status != 206: # server range ignore karta hai r.release_conn() return "no_resume" last_flush = pos for chunk in r.stream(CHUNK): if not chunk or cancel.is_set(): break os.pwrite(fd, chunk, pos) pos += len(chunk) state[idx] = pos - start # is segment ne abhi tak kitne bytes liye if pos - last_flush >= FLUSH_EVERY: _drop_cache(fd) last_flush = pos _drop_cache(fd) r.release_conn() if pos > end: return "done" attempt += 1 time.sleep(min(2 * attempt, 20)) except Exception: attempt += 1 time.sleep(min(2 * attempt, 20)) return "done" if pos > end else "failed" finally: os.close(fd) def _download_parallel(url, path, total, state, cancel): seg = total // SEGMENTS bounds = [] for i in range(SEGMENTS): s = i * seg e = (total - 1) if i == SEGMENTS - 1 else (s + seg - 1) bounds.append((s, e)) state[:] = [b[0] for b in bounds] threads = [] results = [None] * SEGMENTS for i, (s, e) in enumerate(bounds): t = threading.Thread( target=lambda i=i, s=s, e=e: results.__setitem__( i, _download_range(url, path, s, e, state, i, cancel))) t.daemon = True t.start() threads.append(t) for t in threads: t.join() return results def _download_single(url, path, state, cancel): """Non-resumable servers ke liye (jaise video-downloads.googleusercontent.com).""" attempt = 0 while attempt < MAX_ATTEMPTS and not cancel.is_set(): pos = 0 try: r = _http.request("GET", url, preload_content=False, retries=False, timeout=Timeout(connect=CONNECT_TIMEOUT, read=READ_TIMEOUT)) cl = r.headers.get("Content-Length") expected = tasks[state["task_id"]].get("total_size", 0) or 0 if cl and expected and int(cl) != expected: r.release_conn() raise RuntimeError( f"SOURCE SIZE MISMATCH: server ne ab {int(cl)} bytes bheje, " f"pehle {expected} tha (rate-limit / error page / expired link?)") if cl and not expected: tasks[state["task_id"]]["total_size"] = int(cl) with open(path, "r+b") as f: f.seek(0) f.truncate(0) # pichhle attempt ka kachra saaf last_flush = 0 for chunk in r.stream(CHUNK): if not chunk or cancel.is_set(): break f.write(chunk) pos += len(chunk) state["pos"] = pos if pos - last_flush >= FLUSH_EVERY: _drop_cache(f.fileno()) last_flush = pos _drop_cache(f.fileno()) r.release_conn() total = tasks[state["task_id"]].get("total_size", 0) or 0 if total == 0 or pos >= total: return "done" attempt += 1 except Exception: attempt += 1 time.sleep(min(2 * attempt, 20)) return "failed" def sync_download_worker(task_id: str, url: str): cancel = threading.Event() tasks[task_id]["_cancel"] = cancel try: info = probe_url(url) total = info.get("total") or 0 if info.get("filename"): tasks[task_id]["original_filename"] = info["filename"] tasks[task_id]["file_path"] = os.path.join(DATA_DIR, f"{task_id}_{info['filename']}") file_path = tasks[task_id]["file_path"] tasks[task_id]["total_size"] = total tasks[task_id]["resumable"] = bool(info.get("ranges")) if info.get("probe_error") and not total: raise RuntimeError(f"URL probe failed: {info['probe_error']}") # ---- STORAGE BUDGET CHECK (ASLI ROOT CAUSE KA FIX) ---- # shutil.disk_usage() container me HOST ka disk dikhata hai (~500GB free), # isliye wo check bekaar hai. Apna budget rakho, warna 45 min download ke # baad 98% par container evict ho jayega. if total: used_here = 0 for fn in os.listdir(DATA_DIR): fp = os.path.join(DATA_DIR, fn) try: if os.path.isfile(fp): used_here += os.path.getsize(fp) except Exception: pass if used_here + total > MAX_TOTAL_STORAGE: raise RuntimeError( f"FILE TOO BIG FOR THIS SERVER: file {total/1e9:.2f}GB + stored " f"{used_here/1e9:.2f}GB = {(used_here+total)/1e9:.2f}GB > limit " f"{MAX_TOTAL_STORAGE/1e9:.1f}GB. HF free Space me 50GB ephemeral " f"disk hai, uske upar container EVICT ho jata hai. Fix: Space " f"Settings me persistent storage on karo ya chhoti file use karo.") # ---- DISK CHECK ---- if total: free = shutil.disk_usage(DATA_DIR).free if free < total + DISK_MARGIN: raise RuntimeError(f"Not enough disk: need {total/1e9:.1f}GB, free {free/1e9:.1f}GB") # ---- progress ticker (har second speed + downloaded update) ---- state = {"task_id": task_id, "pos": 0} seg_state = [0] tasks[task_id]["_state"] = state stop = threading.Event() def ticker(): last = 0 last_t = time.time() while not stop.is_set(): time.sleep(1.0) try: done = sum(seg_state) if tasks[task_id].get("resumable") else state.get("pos", 0) now = time.time() dt = now - last_t if dt > 0: sp = max(0.0, (done - last) / dt) tasks[task_id]["speed"] = sp tsize = tasks[task_id].get("total_size", 0) or 0 if sp > 0 and tsize: tasks[task_id]["eta_seconds"] = int((tsize - done) / sp) tasks[task_id]["downloaded"] = done last, last_t = done, now save_state() except Exception: pass threading.Thread(target=ticker, daemon=True).start() # ---- DOWNLOAD ---- result = None with open(file_path, "wb"): pass # file pehle banao, segments isi me pwrite karenge if info.get("ranges") and total >= MIN_SIZE_FOR_PARALLEL: seg_state = [0] * SEGMENTS tasks[task_id]["_state"] = seg_state res = _download_parallel(url, file_path, total, seg_state, cancel) if "no_resume" in res: tasks[task_id]["resumable"] = False with open(file_path, "r+b") as f: f.truncate(0) result = _download_single(url, file_path, state, cancel) elif "failed" in res: result = "failed" else: result = "done" else: result = _download_single(url, file_path, state, cancel) stop.set() # ---- VERIFY (SABSE IMPORTANT FIX) ---- actual = os.path.getsize(file_path) tasks[task_id]["downloaded"] = actual if total and actual != total: tasks[task_id]["status"] = "error" tasks[task_id]["error"] = (f"INCOMPLETE: got {actual} of {total} bytes " f"(missing {total-actual}). Server ne beech me " f"connection band kar diya.") return if result != "done": tasks[task_id]["status"] = "error" tasks[task_id]["error"] = "Download failed after maximum retries" return tasks[task_id]["status"] = "completed" tasks[task_id]["speed"] = 0.0 tasks[task_id]["verified"] = True except Exception as e: tasks[task_id]["status"] = "error" tasks[task_id]["error"] = str(e) finally: save_state() gc.collect() async def download_file(task_id: str, url: str): loop = asyncio.get_event_loop() await loop.run_in_executor(None, sync_download_worker, task_id, url) @app.post("/start_download") async def start_download(req: DownloadRequest, background_tasks: BackgroundTasks): task_id = str(uuid.uuid4()) filename = clean_filename(req.url) tasks[task_id] = { "task_id": task_id, "url": req.url, "status": "downloading", "total_size": 0, "downloaded": 0, "speed": 0.0, "file_path": os.path.join(DATA_DIR, f"{task_id}_{filename}"), "original_filename": filename, "resumable": None, "timestamp": time.time(), } background_tasks.add_task(download_file, task_id, req.url) return {"task_id": task_id} @app.get("/status/{task_id}") async def get_status(task_id: str): if task_id not in tasks: return {"error": "Task not found"} t = dict(tasks[task_id]) t.pop("_state", None) t.pop("_cancel", None) return t @app.get("/download/{task_id}") async def download(task_id: str): if task_id not in tasks or tasks[task_id]["status"] != "completed": return {"error": "File not ready"} return FileResponse(tasks[task_id]["file_path"], filename=tasks[task_id].get("original_filename")) @app.get("/stream/{task_id}") async def stream(task_id: str, request: Request): if task_id not in tasks or tasks[task_id]["status"] != "completed": return {"error": "File not ready"} file_path = tasks[task_id]["file_path"] file_size = os.path.getsize(file_path) range_header = request.headers.get("Range") def file_iterator(start, length): with open(file_path, "rb") as f: f.seek(start) while length > 0: data = f.read(min(1024 * 1024, length)) # length se zyada NAHI bhejna, if not data: # warna Content-Length mismatch break # -> client ko "bytes missing" yield data length -= len(data) if range_header: byte1, byte2 = 0, None parts = range_header.replace("bytes=", "").split("-") if parts[0]: byte1 = int(parts[0]) if len(parts) > 1 and parts[1]: byte2 = int(parts[1]) length = file_size - byte1 if byte2 is None else byte2 + 1 - byte1 headers = { "Content-Range": f"bytes {byte1}-{byte1+length-1}/{file_size}", "Accept-Ranges": "bytes", "Content-Length": str(length), } return StreamingResponse(file_iterator(byte1, length), status_code=206, headers=headers) headers = {"Accept-Ranges": "bytes", "Content-Length": str(file_size)} return StreamingResponse(file_iterator(0, file_size), headers=headers, media_type="application/octet-stream") @app.get("/storage") async def get_storage(): total, used, free = shutil.disk_usage(DATA_DIR) stored = 0 try: for fn in os.listdir(DATA_DIR): fp = os.path.join(DATA_DIR, fn) if os.path.isfile(fp): stored += os.path.getsize(fp) except Exception: pass return {"total": total, "used": used, "free": free, "our_files_bytes": stored, "our_budget_bytes": MAX_TOTAL_STORAGE, "budget_left_bytes": max(0, MAX_TOTAL_STORAGE - stored)} @app.get("/server_status") async def get_server_status(): status = {} try: with open("/proc/meminfo") as f: mem = {} for line in f: if ":" in line: k, v = line.split(":", 1) parts = v.strip().split() if parts and parts[0].isdigit(): mem[k.strip()] = int(parts[0]) * 1024 total_ram = mem.get("MemTotal", 0) available_ram = mem.get("MemAvailable", 0) used_ram = total_ram - available_ram status["ram_total"] = total_ram status["ram_used"] = used_ram status["ram_percent"] = round((used_ram / total_ram * 100) if total_ram else 0, 1) except Exception: status.update({"ram_total": 0, "ram_used": 0, "ram_percent": 0}) try: total_d, used_d, free_d = shutil.disk_usage(DATA_DIR) status["storage_total"] = total_d status["storage_used"] = used_d status["storage_free"] = free_d status["storage_percent"] = round((used_d / total_d * 100) if total_d else 0, 1) except Exception: pass status["temperature"] = -1 status["ping_ms"] = -1 status["server_online"] = True status["active_tasks"] = len(tasks) status["uptime"] = round(time.time(), 0) return status @app.post("/delete_all") async def delete_all(): for filename in os.listdir(DATA_DIR): filepath = os.path.join(DATA_DIR, filename) try: if os.path.isfile(filepath): os.remove(filepath) elif os.path.isdir(filepath): shutil.rmtree(filepath) except Exception as e: print(f"Failed to delete {filepath}: {e}") tasks.clear() gc.collect() return {"status": "success"} @app.get("/history") async def get_history(): out = [] for t in tasks.values(): out.append({k: v for k, v in t.items() if not k.startswith("_")}) return {"history": out} @app.post("/upload") async def upload_file(file: UploadFile = File(...)): task_id = str(uuid.uuid4()) original_filename = file.filename if file.filename else "uploaded_file.bin" file_path = os.path.join(DATA_DIR, f"{task_id}_{original_filename}") with open(file_path, "wb") as buffer: while True: chunk = await file.read(CHUNK) if not chunk: break buffer.write(chunk) await file.close() file_size = os.path.getsize(file_path) tasks[task_id] = { "task_id": task_id, "url": "local_upload", "status": "completed", "total_size": file_size, "downloaded": file_size, "speed": 0.0, "file_path": file_path, "original_filename": original_filename, "timestamp": time.time(), } save_state() return {"task_id": task_id} async def file_sender(file_path, chunk_size=CHUNK): with open(file_path, "rb") as f: while True: chunk = f.read(chunk_size) if not chunk: break yield chunk @app.post("/start_gofile_transfer") async def start_gofile_transfer(req: DownloadRequest, background_tasks: BackgroundTasks): task_id = str(uuid.uuid4()) filename = clean_filename(req.url) tasks[task_id] = { "task_id": task_id, "url": req.url, "status": "downloading", "total_size": 0, "downloaded": 0, "speed": 0.0, "file_path": os.path.join(DATA_DIR, f"{task_id}_{filename}"), "original_filename": filename, "gofile_url": None, "timestamp": time.time(), } background_tasks.add_task(process_gofile_transfer, task_id, req.url) return {"task_id": task_id} async def process_gofile_transfer(task_id: str, url: str): try: loop = asyncio.get_event_loop() await loop.run_in_executor(None, sync_download_worker, task_id, url) if tasks[task_id].get("status") != "completed": raise Exception(tasks[task_id].get("error", "download failed")) file_path = tasks[task_id]["file_path"] tasks[task_id]["status"] = "uploading_to_gofile" async with aiohttp.ClientSession() as session: async with session.get("https://api.gofile.io/servers") as resp: servers_data = await resp.json() server = servers_data["data"]["servers"][0]["name"] upload_url = f"https://{server}.gofile.io/uploadFile" data = aiohttp.FormData() data.add_field("file", file_sender(file_path), filename=tasks[task_id]["original_filename"]) async with session.post(upload_url, data=data) as upload_resp: upload_result = await upload_resp.json() if upload_result["status"] == "ok": tasks[task_id]["gofile_url"] = upload_result["data"]["downloadPage"] tasks[task_id]["status"] = "completed" else: raise Exception("GoFile upload failed") try: os.remove(file_path) except Exception: pass except Exception as e: tasks[task_id]["status"] = "error" tasks[task_id]["error"] = str(e) finally: save_state() gc.collect() @app.get("/list_zip/{task_id}") async def list_zip_contents(task_id: str): if task_id not in tasks: return {"error": "Task not found"} file_path = tasks[task_id]["file_path"] if not os.path.exists(file_path): return {"error": "File does not exist on server"} if not zipfile.is_zipfile(file_path): return {"error": "This file is not a valid ZIP archive"} try: with zipfile.ZipFile(file_path, "r") as z: file_list = [{"filename": i.filename, "file_size_mb": round(i.file_size / (1024 * 1024), 2), "is_dir": i.is_dir()} for i in z.infolist()] return {"task_id": task_id, "total_files": len(file_list), "files": file_list} except Exception as e: return {"error": f"Failed to read ZIP: {str(e)}"} @app.post("/extract_zip/{task_id}") async def extract_zip_file(task_id: str, background_tasks: BackgroundTasks): if task_id not in tasks: return {"error": "Task not found"} file_path = tasks[task_id]["file_path"] if not os.path.exists(file_path): return {"error": "File does not exist on server"} if not zipfile.is_zipfile(file_path): return {"error": "This file is not a valid ZIP archive"} extract_folder = os.path.join(DATA_DIR, f"extracted_{task_id}") os.makedirs(extract_folder, exist_ok=True) tasks[task_id]["status"] = "extracting" tasks[task_id]["extract_path"] = extract_folder background_tasks.add_task(process_zip_extraction, task_id, file_path, extract_folder) return {"status": "extraction_started", "task_id": task_id, "target_folder": extract_folder} def process_zip_extraction(task_id: str, zip_path: str, target_dir: str): try: with zipfile.ZipFile(zip_path, "r") as z: z.extractall(path=target_dir) tasks[task_id]["status"] = "extracted" except Exception as e: tasks[task_id]["status"] = "extraction_error" tasks[task_id]["error"] = str(e) finally: save_state() @app.on_event("startup") async def startup_event(): async def cleanup_old_files(): while True: try: now = time.time() for filename in os.listdir(DATA_DIR): filepath = os.path.join(DATA_DIR, filename) if os.path.isfile(filepath) and now - os.path.getmtime(filepath) > 86400: os.remove(filepath) for tid, tinfo in list(tasks.items()): if tinfo.get("file_path") == filepath: del tasks[tid] except Exception as e: print(f"Cleanup error: {e}") await asyncio.sleep(3600) asyncio.create_task(cleanup_old_files()) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860)