main.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import os
  2. import shutil
  3. from fastapi import FastAPI, Request, File, UploadFile, Form
  4. from fastapi.responses import HTMLResponse, RedirectResponse
  5. from fastapi.templating import Jinja2Templates
  6. from fastapi.staticfiles import StaticFiles
  7. app = FastAPI()
  8. # Failsafe: Create the directory inside the container if it somehow gets missed
  9. os.makedirs("static", exist_ok=True)
  10. app.mount("/static", StaticFiles(directory="static"), name="static")
  11. templates = Jinja2Templates(directory="templates")
  12. # The persistent directory for your custom code
  13. OVERLAY_DIR = "/srv"
  14. @app.get("/", response_class=HTMLResponse)
  15. async def index(request: Request):
  16. files = []
  17. dirs = set([""]) # Always include the root directory as an option
  18. if os.path.exists(OVERLAY_DIR):
  19. for root, dirnames, filenames in os.walk(OVERLAY_DIR):
  20. # Calculate the relative directory path
  21. rel_root = os.path.relpath(root, OVERLAY_DIR)
  22. if rel_root != ".":
  23. dirs.add(rel_root)
  24. for filename in filenames:
  25. rel_path = os.path.relpath(os.path.join(root, filename), OVERLAY_DIR)
  26. files.append(rel_path)
  27. return templates.TemplateResponse("index.html", {
  28. "request": request,
  29. "files": sorted(files),
  30. "dirs": sorted(list(dirs)) # Pass the sorted directories to the template dropdown
  31. })
  32. @app.post("/upload")
  33. async def upload_file(file: UploadFile = File(...), target_path: str = Form("")):
  34. save_dir = os.path.join(OVERLAY_DIR, target_path.strip("/"))
  35. os.makedirs(save_dir, exist_ok=True)
  36. file_location = os.path.join(save_dir, file.filename)
  37. with open(file_location, "wb+") as file_object:
  38. shutil.copyfileobj(file.file, file_object)
  39. return RedirectResponse(url="/patch-manager/", status_code=303)
  40. @app.post("/create_folder")
  41. async def create_folder(folder_path: str = Form(...)):
  42. save_dir = os.path.join(OVERLAY_DIR, folder_path.strip("/"))
  43. os.makedirs(save_dir, exist_ok=True)
  44. return RedirectResponse(url="/patch-manager/", status_code=303)
  45. @app.post("/delete")
  46. async def delete_file(filepath: str = Form(...)):
  47. target = os.path.join(OVERLAY_DIR, filepath)
  48. if os.path.exists(target):
  49. os.remove(target)
  50. target_dir = os.path.dirname(target)
  51. if not os.listdir(target_dir) and target_dir != OVERLAY_DIR:
  52. os.rmdir(target_dir)
  53. return RedirectResponse(url="/patch-manager/", status_code=303)
  54. @app.get("/edit", response_class=HTMLResponse)
  55. async def edit_file(request: Request, filepath: str):
  56. target = os.path.join(OVERLAY_DIR, filepath)
  57. content = ""
  58. if os.path.exists(target):
  59. with open(target, "r", encoding="utf-8", errors="ignore") as f:
  60. content = f.read()
  61. return templates.TemplateResponse("edit.html", {"request": request, "filepath": filepath, "content": content})
  62. @app.post("/edit")
  63. async def save_file(filepath: str = Form(...), content: str = Form(...)):
  64. target = os.path.join(OVERLAY_DIR, filepath)
  65. if os.path.exists(target):
  66. with open(target, "w", encoding="utf-8") as f:
  67. f.write(content)
  68. return RedirectResponse(url="/patch-manager/", status_code=303)