File size: 1,157 Bytes
7049395
 
 
 
 
 
 
 
 
 
5033fc1
7049395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)}")