from fastapi import FastAPI, Request, Header, Depends, Response, HTTPException from fastapi.responses import FileResponse import os import json from pathlib import Path config_path: Path = Path("./config.json") config: dict = {} with open(config_path, "r") as f: config = json.load(f) CONTENT_PATH: Path = Path(config["content"]).resolve() MODS_PATH: Path = CONTENT_PATH / "mods" RESOURCEPACKS_PATH: Path = CONTENT_PATH / "resourcepacks" if not CONTENT_PATH.exists(): CONTENT_PATH.mkdir(parents=True) MODS_PATH.mkdir() RESOURCEPACKS_PATH.mkdir() # Токен для доступа к сервису TOKEN: str = None TOKENFILE = Path(__file__).parent / "token.txt" if not TOKENFILE.exists(): TOKEN = os.urandom(48).hex() with open(TOKENFILE, "w") as f: f.write(TOKEN) else: with open(TOKENFILE, "r") as f: TOKEN = f.read() # Функция для проверки токена def check_token(authorization: str = Header(...)): if authorization != TOKEN: raise HTTPException(status_code=401, detail="Неверный токен") app = FastAPI() @app.get("/mods/files") async def get_files(): # Получаем список всех файлов в указанной директории files = os.listdir(str(MODS_PATH)) return {"files": files} @app.get("/mods/download/{filename}") async def download_file(filename: str): file_path: Path = MODS_PATH / filename if not file_path.exists(): return {"error": f"Файл {filename} не найден"} return FileResponse(path=file_path, filename=str(filename), media_type="application/octet-stream")