from fastapi import FastAPI, HTTPException, Query from fastapi.responses import FileResponse from pathlib import Path from typing import List import os app = FastAPI() FILES_ROOT = Path("/var/www/lasereverything.net.db/files").resolve() @app.get("/list-files") def list_files( path: str = "", offset: int = 0, limit: int = 50 ): base_path = (FILES_ROOT / path).resolve() if not base_path.is_dir() or not str(base_path).startswith(str(FILES_ROOT)): raise HTTPException(status_code=400, detail="Invalid path") entries = [] for item in base_path.iterdir(): if item.is_dir(): entries.append(f"{item.name}/") else: entries.append(item.name) entries.sort() paginated = entries[offset:offset + limit] return { "total": len(entries), "items": paginated } @app.get("/download/{file_path:path}") def download_file(file_path: str): full_path = (FILES_ROOT / file_path).resolve() if not full_path.is_file() or not str(full_path).startswith(str(FILES_ROOT)): raise HTTPException(status_code=404, detail="File not found") return FileResponse(full_path, filename=full_path.name)