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""" + + + + + + + +
+ +
+
+ + + """ + return Response(content=html, media_type="text/html") + + @APP.post("/notes/save") + async def save_notes_api(key: str = "", user: str = "default", content: dict = None): + """API endpoint to save notes for a specific user.""" + expected_key = os.environ.get("GLANCE_HELPER_KEY", "") + if key != expected_key: + return Response(content="Unauthorized", status_code=401) + + safe_user = re.sub(r'[^a-zA-Z0-9_-]', '', user) or "default" + + if content and "content" in content: + if save_notes(safe_user, content["content"]): + return {"status": "ok", "user": safe_user} + return Response(content="Failed to save", status_code=500) + # ================================ # Időkép configuration # ================================ diff --git a/glance-system/glance-orsi.yaml b/glance-system/glance-orsi.yaml index 586098c..34c396a 100644 --- a/glance-system/glance-orsi.yaml +++ b/glance-system/glance-orsi.yaml @@ -591,6 +591,10 @@ data: columns: - size: small widgets: + - type: iframe + source: https://glance-helper.dooplex.hu/notes?key=oplQqnLnJK2vErRVYJpvVUcSDBOSbCHZSbsYY2bwSifgTMfT&user=orsi + height: 250 + title: Quick Notes - type: bookmarks title: Links for Teaching groups: