From 42bacad329a712fd6ef0de303095423e71990f14 Mon Sep 17 00:00:00 2001 From: nearxos Date: Fri, 20 Feb 2026 08:49:02 +0200 Subject: [PATCH] Enhance API response in portal files listing: streamline JSON structure by consolidating repeated code, improve error handling for directory checks, and add portal files directory information to the response. Update HTML template to display the server directory path dynamically. --- .../emmc-provisioning/dashboard/app.py | 18 ++++++++++++------ .../dashboard/templates/portal_files.html | 4 +++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/chromium-setup/emmc-provisioning/dashboard/app.py b/chromium-setup/emmc-provisioning/dashboard/app.py index 7344844..e86b989 100644 --- a/chromium-setup/emmc-provisioning/dashboard/app.py +++ b/chromium-setup/emmc-provisioning/dashboard/app.py @@ -615,17 +615,24 @@ def _save_portal_descriptions(descriptions): def api_portal_files_list(): """List one level: root or contents of path=... (folders and files).""" subpath = request.args.get("path", "").strip().strip("/") + base_url = request.host_url.rstrip("/") + "/files/" + empty = {"items": [], "base_url": base_url, "descriptions": {}, "current_path": subpath, "portal_files_dir": str(PORTAL_FILES_DIR)} if ".." in subpath or "\\" in subpath: - return jsonify({"items": [], "base_url": request.host_url.rstrip("/") + "/files/", "descriptions": {}, "current_path": ""}) + return jsonify(empty) if not PORTAL_FILES_DIR.is_dir(): - return jsonify({"items": [], "base_url": request.host_url.rstrip("/") + "/files/", "descriptions": {}, "current_path": ""}) + try: + PORTAL_FILES_DIR.mkdir(parents=True, exist_ok=True) + except OSError: + pass + if not PORTAL_FILES_DIR.is_dir(): + return jsonify(empty) list_dir = (PORTAL_FILES_DIR / subpath).resolve() if subpath else PORTAL_FILES_DIR try: list_dir.relative_to(PORTAL_FILES_DIR.resolve()) except ValueError: - return jsonify({"items": [], "base_url": request.host_url.rstrip("/") + "/files/", "descriptions": {}, "current_path": subpath}) + return jsonify({**empty, "current_path": subpath}) if not list_dir.is_dir(): - return jsonify({"items": [], "base_url": request.host_url.rstrip("/") + "/files/", "descriptions": {}, "current_path": subpath}) + return jsonify({**empty, "current_path": subpath}) items = [] for p in sorted(list_dir.iterdir(), key=lambda x: (not x.is_dir(), x.name.lower())): if ".." in p.name or p.name.startswith("."): @@ -639,8 +646,7 @@ def api_portal_files_list(): except OSError: pass descriptions = _load_portal_descriptions() - base = request.host_url.rstrip("/") + "/files/" - return jsonify({"items": items, "base_url": base, "descriptions": descriptions, "current_path": subpath}) + return jsonify({"items": items, "base_url": base_url, "descriptions": descriptions, "current_path": subpath, "portal_files_dir": str(PORTAL_FILES_DIR)}) @app.route("/api/portal-files/descriptions", methods=["GET", "PATCH"]) diff --git a/chromium-setup/emmc-provisioning/dashboard/templates/portal_files.html b/chromium-setup/emmc-provisioning/dashboard/templates/portal_files.html index 7bd8d42..87368b7 100644 --- a/chromium-setup/emmc-provisioning/dashboard/templates/portal_files.html +++ b/chromium-setup/emmc-provisioning/dashboard/templates/portal_files.html @@ -72,7 +72,8 @@ -

Served at /files/ — use in cloud-init e.g. curl -fsSL "http://SERVER/files/first-boot/splash.png" -o /tmp/splash.png

+

Served at /files/ — use in cloud-init e.g. curl -fsSL "http://SERVER/files/first-boot/splash.png" -o /tmp/splash.png

+

Directory on server:

@@ -121,6 +122,7 @@ descriptions = data.descriptions || {}; currentPath = data.current_path || ''; document.getElementById('baseUrl').textContent = baseUrl + (currentPath ? currentPath + '/' : ''); + document.getElementById('portalDir').textContent = data.portal_files_dir || '—'; buildBreadcrumb(); var tbody = document.getElementById('portalBody');
NameTypeSizeDescriptionActions