浏览代码

Improved patch manager

Nicole Portas 1 月之前
父节点
当前提交
1b18013f16
共有 3 个文件被更改,包括 57 次插入33 次删除
  1. 3 0
      .gitignore
  2. 19 22
      overlay_manager/main.py
  3. 35 11
      overlay_manager/templates/index.html

+ 3 - 0
.gitignore

@@ -136,3 +136,6 @@ dump.rdb
 
 # base directory
 base
+
+# overlays
+custom_overlays

+ 19 - 22
overlay_manager/main.py

@@ -5,17 +5,7 @@ from fastapi.responses import HTMLResponse, RedirectResponse
 from fastapi.templating import Jinja2Templates
 from fastapi.staticfiles import StaticFiles
 
-# =====================================================================
-# MODIFICATION START: REMOVED ROOT_PATH
-# Reason: We removed the `root_path="/patch-manager"` argument. 
-# Because Nginx already strips the prefix before passing traffic to 
-# this container, setting it here was causing FastAPI to double-process 
-# the route and throw internal 404s. Standard FastAPI initialization is best.
-# =====================================================================
 app = FastAPI()
-# =====================================================================
-# MODIFICATION END
-# =====================================================================
 
 # Failsafe: Create the directory inside the container if it somehow gets missed
 os.makedirs("static", exist_ok=True)
@@ -29,12 +19,23 @@ OVERLAY_DIR = "/srv"
 @app.get("/", response_class=HTMLResponse)
 async def index(request: Request):
     files = []
+    dirs = set([""]) # Always include the root directory as an option
     if os.path.exists(OVERLAY_DIR):
-        for root, dirs, filenames in os.walk(OVERLAY_DIR):
+        for root, dirnames, filenames in os.walk(OVERLAY_DIR):
+            # Calculate the relative directory path
+            rel_root = os.path.relpath(root, OVERLAY_DIR)
+            if rel_root != ".":
+                dirs.add(rel_root)
+                
             for filename in filenames:
                 rel_path = os.path.relpath(os.path.join(root, filename), OVERLAY_DIR)
                 files.append(rel_path)
-    return templates.TemplateResponse("index.html", {"request": request, "files": sorted(files)})
+                
+    return templates.TemplateResponse("index.html", {
+        "request": request, 
+        "files": sorted(files),
+        "dirs": sorted(list(dirs)) # Pass the sorted directories to the template dropdown
+    })
 
 @app.post("/upload")
 async def upload_file(file: UploadFile = File(...), target_path: str = Form("")):
@@ -44,17 +45,13 @@ async def upload_file(file: UploadFile = File(...), target_path: str = Form(""))
     with open(file_location, "wb+") as file_object:
         shutil.copyfileobj(file.file, file_object)
         
-    # =====================================================================
-    # MODIFICATION START: EXPLICIT NGINX REDIRECTS
-    # Reason: Since we removed root_path, we must explicitly tell the 
-    # user's browser to redirect back to the Nginx URL ("/patch-manager/") 
-    # after a successful action, otherwise it redirects to the internal "/" 
-    # and gets lost on the main ArduPilot page.
-    # =====================================================================
     return RedirectResponse(url="/patch-manager/", status_code=303)
-    # =====================================================================
-    # MODIFICATION END
-    # =====================================================================
+
+@app.post("/create_folder")
+async def create_folder(folder_path: str = Form(...)):
+    save_dir = os.path.join(OVERLAY_DIR, folder_path.strip("/"))
+    os.makedirs(save_dir, exist_ok=True)
+    return RedirectResponse(url="/patch-manager/", status_code=303)
 
 @app.post("/delete")
 async def delete_file(filepath: str = Form(...)):

+ 35 - 11
overlay_manager/templates/index.html

@@ -17,24 +17,48 @@
                 <img src="/patch-manager/static/logo.png" alt="ArduPilot Logo" height="35" class="d-inline-block align-text-top me-3">
                 <span class="navbar-brand mb-0 h4 fw-bold">Overlay Manager</span>
             </div>
-            
             <div class="ms-auto">
                 <a href="javascript:void(0);" onclick="window.location.href = window.location.protocol + '//' + window.location.hostname;" class="btn btn-outline-light btn-sm">
                     <i class="bi bi-arrow-left me-1"></i>Back to Main App
                 </a>
             </div>
-            </div>
+        </div>
     </nav>
     <div class="container mt-4">
-        <div class="card shadow border-0 rounded-3 mb-4">
-            <div class="card-body p-4">
-                <h5 class="card-title fw-bold text-secondary mb-3"><i class="bi bi-cloud-arrow-up-fill me-2 text-primary"></i>Inject Custom Source</h5>
-                
-                <form action="/patch-manager/upload" method="post" enctype="multipart/form-data" class="row g-3">
-                    <div class="col-md-5"><input class="form-control border-primary" type="file" name="file" required></div>
-                    <div class="col-md-5"><input type="text" class="form-control border-primary" name="target_path" placeholder="Path (e.g., libraries/AP_HAL)"></div>
-                    <div class="col-md-2"><button type="submit" class="btn btn-custom w-100 fw-bold">Upload</button></div>
-                </form>
+        
+        <div class="row mb-4 g-4">
+            <div class="col-md-7">
+                <div class="card shadow border-0 rounded-3 h-100">
+                    <div class="card-body p-4">
+                        <h5 class="card-title fw-bold text-secondary mb-3"><i class="bi bi-cloud-arrow-up-fill me-2 text-primary"></i>Upload to Folder</h5>
+                        <form action="/patch-manager/upload" method="post" enctype="multipart/form-data" class="d-flex flex-column gap-3">
+                            <input class="form-control border-primary" type="file" name="file" required>
+                            
+                            <div class="input-group">
+                                <span class="input-group-text bg-light border-primary text-secondary"><i class="bi bi-folder2-open"></i></span>
+                                <select class="form-select border-primary" name="target_path">
+                                    {% for d in dirs %}
+                                        <option value="{{ d }}">{% if d == "" %}/ (Root Directory){% else %}{{ d }}{% endif %}</option>
+                                    {% endfor %}
+                                </select>
+                            </div>
+
+                            <button type="submit" class="btn btn-custom fw-bold mt-auto">Upload File</button>
+                        </form>
+                    </div>
+                </div>
+            </div>
+
+            <div class="col-md-5">
+                <div class="card shadow border-0 rounded-3 h-100">
+                    <div class="card-body p-4">
+                        <h5 class="card-title fw-bold text-secondary mb-3"><i class="bi bi-folder-plus me-2 text-success"></i>Create New Folder</h5>
+                        <form action="/patch-manager/create_folder" method="post" class="d-flex flex-column gap-3">
+                            <input type="text" class="form-control border-primary" name="folder_path" placeholder="Path (e.g., libraries/AP_HAL)" required>
+                            <button type="submit" class="btn btn-success fw-bold mt-auto">Create Folder</button>
+                        </form>
+                    </div>
+                </div>
             </div>
         </div>