diff --git a/glance-system/glance-helper.yaml b/glance-system/glance-helper.yaml index 58b5caf..4304b9f 100644 --- a/glance-system/glance-helper.yaml +++ b/glance-system/glance-helper.yaml @@ -27,7 +27,174 @@ data: from prometheus_client import Counter, Histogram, Gauge, generate_latest, CONTENT_TYPE_LATEST APP = FastAPI() - + + # ================================ + # Simple Notes Widget - Multi-user + # ================================ + + def get_notes_file(user: str) -> str: + """Get notes file path for a user, with validation.""" + # Sanitize username: only allow alphanumeric, dash, underscore + safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) + if not safe_user: + safe_user = "default" + data_dir = os.environ.get("DATA_DIR", "/data") + return os.path.join(data_dir, f"notes_{safe_user}.txt") + + def load_notes(user: str) -> str: + """Load notes from file for a specific user.""" + notes_file = get_notes_file(user) + try: + if os.path.exists(notes_file): + with open(notes_file, "r", encoding="utf-8") as f: + return f.read() + except Exception as e: + print(f"Error loading notes for {user}: {e}") + return "" + + def save_notes(user: str, content: str) -> bool: + """Save notes to file for a specific user.""" + notes_file = get_notes_file(user) + try: + with open(notes_file, "w", encoding="utf-8") as f: + f.write(content) + return True + except Exception as e: + print(f"Error saving notes for {user}: {e}") + return False + + @APP.get("/notes") + def notes_widget(key: str = "", user: str = "default"): + """Serve the notes widget HTML page for a specific user.""" + expected_key = os.environ.get("GLANCE_HELPER_KEY", "") + if key != expected_key: + return Response(content="Unauthorized", status_code=401) + + # Sanitize user for display + safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default" + current_notes = load_notes(safe_user) + # Escape for safe HTML embedding + escaped_notes = current_notes.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """) + + html = f""" + +
+ + + + + +