Spaces:
Paused
Paused
Download app.py from collab-project/README: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/spaces/collab-project/README/resolve/main/app.py
- Command line
-
hf download hf://spaces/collab-project/README/app.py
-
curl -L -o app.py https://huggingface.co/spaces/collab-project/README/resolve/main/app.py
3.06 kB
| import os | |
| import re | |
| import json | |
| import asyncio | |
| from pathlib import Path | |
| from fastapi import FastAPI, HTTPException, Header | |
| RCLONE_CONFIG = os.getenv("RCLONE_CONFIG", "https://gist.githubusercontent.com/genstava789/e7fa4e34e126bfc257b21db4f8c0f3ba/raw/rclone.conf") | |
| HOST = os.getenv("HOST", "0.0.0.0") | |
| PORT = int(os.getenv("PORT", "8080")) | |
| API_TOKEN = os.getenv("API_TOKEN", "admin") | |
| app = FastAPI(title="rclone index backend") | |
| def check_auth(token: str | None): | |
| if API_TOKEN and token != API_TOKEN: | |
| raise HTTPException(status_code=401, detail="unauthorized") | |
| def parse_remotes(conf_path: str) -> list[str]: | |
| path = Path(conf_path) | |
| if not path.exists(): | |
| return [] | |
| text = path.read_text(encoding="utf-8", errors="ignore") | |
| out = [] | |
| for line in text.splitlines(): | |
| line = line.strip() | |
| if line.startswith("[") and line.endswith("]"): | |
| name = line[1:-1].strip() | |
| if name: | |
| out.append(name if name.endswith(":") else name + ":") | |
| return out | |
| def safe_remote(fs: str) -> bool: | |
| return bool(re.fullmatch(r"[A-Za-z0-9_.\-]+:", fs)) | |
| def safe_dir(remote: str) -> bool: | |
| if ".." in remote or "\x00" in remote: | |
| return False | |
| return True | |
| async def run_rclone(*args: str): | |
| cmd = ["rclone", *args, "--config", RCLONE_CONFIG, "--use-json-log=false"] | |
| proc = await asyncio.create_subprocess_exec( | |
| *cmd, | |
| stdout=asyncio.subprocess.PIPE, | |
| stderr=asyncio.subprocess.PIPE | |
| ) | |
| stdout, stderr = await proc.communicate() | |
| out = stdout.decode("utf-8", errors="ignore").strip() | |
| err = stderr.decode("utf-8", errors="ignore").strip() | |
| if proc.returncode != 0: | |
| raise HTTPException(status_code=500, detail=err or "rclone error") | |
| return out | |
| async def health(): | |
| return {"ok": True} | |
| async def api_remotes(x_api_token: str | None = Header(default=None)): | |
| check_auth(x_api_token) | |
| return {"ok": True, "remotes": parse_remotes(RCLONE_CONFIG)} | |
| async def api_list( | |
| fs: str, | |
| dir: str = "", | |
| x_api_token: str | None = Header(default=None) | |
| ): | |
| check_auth(x_api_token) | |
| if not safe_remote(fs): | |
| raise HTTPException(status_code=400, detail="invalid fs") | |
| if not safe_dir(dir): | |
| raise HTTPException(status_code=400, detail="invalid dir") | |
| target = fs + dir | |
| out = await run_rclone("lsjson", target) | |
| try: | |
| data = json.loads(out) | |
| except Exception: | |
| raise HTTPException(status_code=500, detail="invalid rclone response") | |
| items = [] | |
| for item in data: | |
| items.append({ | |
| "name": item.get("Name", ""), | |
| "path": item.get("Path", ""), | |
| "size": item.get("Size", 0), | |
| "isDir": item.get("IsDir", False) | |
| }) | |
| return { | |
| "ok": True, | |
| "fs": fs, | |
| "dir": dir, | |
| "list": items | |
| } |