from fastapi import FastAPI, Query, BackgroundTasks, HTTPException from fastapi.responses import FileResponse import os import shutil from gofile_downloader import download_gofile app = FastAPI(title="GoFile Downloader API") @app.get("/download") def download_endpoint( background_tasks: BackgroundTasks, # ← di chuyển lên đầu url: str = Query(..., description="Link GoFile: https://gofile.io/d/xxxx"), password: str | None = Query(None, description="Password nếu có"), ): if not url.startswith("https://gofile.io/d/"): raise HTTPException(400, "URL phải là link GoFile hợp lệ") try: zip_path, tmp_dir = download_gofile(url, password) def cleanup(): if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir, ignore_errors=True) background_tasks.add_task(cleanup) content_id = url.split("/")[-1].split("?")[0] return FileResponse( zip_path, filename=f"{content_id}.zip", media_type="application/zip" ) except Exception as e: raise HTTPException(500, f"Lỗi: {str(e)}")