from __future__ import annotations import sqlite3 import time import re import base64 from pathlib import Path from typing import Any from .settings import DATABASE_PATH, ONLINE_THRESHOLD_SECONDS, REFRESH_SECONDS DEFAULT_BACKUP_RETENTION = 20 def get_branding() -> dict[str, Any]: return {"name": _get("brand_name", "WGTools-Extended"), "color": _get("brand_color", "#8f1f27"), "logo": _get("brand_logo", "")} def update_branding(name: str, color: str, logo: str) -> dict[str, Any]: name = name.strip() if len(name) > 80 or any(ord(c) < 32 for c in name): raise ValueError("Nieprawidłowa nazwa marki") if not re.fullmatch(r"#[0-9a-fA-F]{6}", color): raise ValueError("Nieprawidłowy kolor") if logo: prefix = "data:image/png;base64," if not logo.startswith(prefix) or len(logo) > 400000: raise ValueError("Logo musi być plikiem PNG o rozmiarze do 300 KB") try: raw = base64.b64decode(logo[len(prefix):], validate=True) except ValueError as exc: raise ValueError("Nieprawidłowy plik logo") from exc if not raw.startswith(b"\x89PNG\r\n\x1a\n"): raise ValueError("Logo musi być plikiem PNG") _set("brand_name", name) _set("brand_color", color) _set("brand_logo", logo) return get_branding() def _connect() -> sqlite3.Connection: Path(DATABASE_PATH).parent.mkdir(parents=True, exist_ok=True) conn = sqlite3.connect(DATABASE_PATH) conn.row_factory = sqlite3.Row conn.execute("PRAGMA journal_mode=WAL") return conn def init_preferences() -> None: with _connect() as conn: conn.execute( """ CREATE TABLE IF NOT EXISTS app_settings ( key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at INTEGER NOT NULL ) """ ) def _get(key: str, default: str) -> str: init_preferences() with _connect() as conn: row = conn.execute("SELECT value FROM app_settings WHERE key=?", (key,)).fetchone() return str(row["value"]) if row else default def _set(key: str, value: str) -> None: init_preferences() with _connect() as conn: conn.execute( """ INSERT INTO app_settings(key,value,updated_at) VALUES(?,?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value, updated_at=excluded.updated_at """, (key, value, int(time.time())), ) def get_preferences() -> dict[str, Any]: refresh = int(_get("refresh_seconds", str(REFRESH_SECONDS))) threshold = int(_get("online_threshold_seconds", str(ONLINE_THRESHOLD_SECONDS))) retention = int(_get("backup_retention", str(DEFAULT_BACKUP_RETENTION))) return { "refresh_seconds": max(2, min(refresh, 60)), "online_threshold_seconds": max(30, min(threshold, 3600)), "backup_retention": max(1, min(retention, 200)), } def update_preferences(*, refresh_seconds: int, online_threshold_seconds: int, backup_retention: int) -> dict[str, Any]: if not 2 <= int(refresh_seconds) <= 60: raise ValueError("Refresh interval must be between 2 and 60 seconds") if not 30 <= int(online_threshold_seconds) <= 3600: raise ValueError("Online threshold must be between 30 and 3600 seconds") if not 1 <= int(backup_retention) <= 200: raise ValueError("Backup retention must be between 1 and 200") _set("refresh_seconds", str(int(refresh_seconds))) _set("online_threshold_seconds", str(int(online_threshold_seconds))) _set("backup_retention", str(int(backup_retention))) return get_preferences()