-
Notifications
You must be signed in to change notification settings - Fork 241
feat: mobile web UI + network access toggle #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| """Runtime, user-toggleable settings (persisted to disk). | ||
|
|
||
| These are read live (unlike the env-var constants in config.py, which are fixed | ||
| at startup), so the Settings UI can change them without a restart: | ||
|
|
||
| - `allow_network` — whether StemDeck answers requests from other devices. | ||
| - `max_duration_sec` — longest track accepted for processing. | ||
| - `video_max_height` — max video resolution for MP4 export / YouTube pulls. | ||
|
|
||
| Defaults fall back to the config.py constants (which honor their env vars), so | ||
| nothing changes until the user overrides a value. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| import os | ||
| import threading | ||
|
|
||
| from app.core.config import DATA_DIR, MAX_DURATION_SEC, VIDEO_MAX_HEIGHT | ||
|
|
||
| _log = logging.getLogger("stemdeck.settings") | ||
|
|
||
| _SETTINGS_PATH = DATA_DIR / "settings.json" | ||
| _LOCK = threading.RLock() | ||
| _state: dict | None = None # whole settings dict, loaded lazily | ||
|
|
||
| # Clamp bounds. Max track length is capped at 20 min (the product ceiling). | ||
| _DURATION_MIN, _DURATION_MAX = 60, 1200 # 1 min .. 20 min | ||
| _HEIGHT_MIN, _HEIGHT_MAX = 144, 2160 | ||
|
|
||
|
|
||
| def _default_allow_network() -> bool: | ||
| # Off by default everywhere — the user explicitly opts other devices in. | ||
| # STEMDECK_ALLOW_NETWORK=1 can pre-enable it (e.g. headless/Docker deploys). | ||
| env = os.environ.get("STEMDECK_ALLOW_NETWORK") | ||
| if env is not None: | ||
| return env.strip() == "1" | ||
| return False | ||
|
|
||
|
|
||
| def _load() -> dict: | ||
| try: | ||
| data = json.loads(_SETTINGS_PATH.read_text(encoding="utf-8")) | ||
| if isinstance(data, dict): | ||
| return data | ||
| except FileNotFoundError: | ||
| pass # no settings file yet — first run; use defaults | ||
| except Exception: | ||
| # Corrupt/unreadable file: fall back to defaults rather than crash. | ||
| _log.warning("could not read settings from %s", _SETTINGS_PATH, exc_info=True) | ||
| return {} | ||
|
|
||
|
|
||
| def _ensure() -> dict: | ||
| global _state | ||
| if _state is None: | ||
| _state = _load() | ||
| return _state | ||
|
|
||
|
|
||
| def _save() -> None: | ||
| try: | ||
| _SETTINGS_PATH.parent.mkdir(parents=True, exist_ok=True) | ||
| _SETTINGS_PATH.write_text(json.dumps(_ensure()), encoding="utf-8") | ||
| except Exception: | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
|
||
| # Persistence is best-effort (read-only FS, permissions): the in-memory | ||
| # value still applies for this session, so don't fail the request. | ||
| _log.warning("could not persist settings to %s", _SETTINGS_PATH, exc_info=True) | ||
|
|
||
|
|
||
| def _num(v: object) -> int | None: | ||
| return int(v) if isinstance(v, (int, float)) and not isinstance(v, bool) else None | ||
|
|
||
|
|
||
| # ── allow_network ── | ||
| def get_allow_network() -> bool: | ||
| with _LOCK: | ||
| v = _ensure().get("allow_network") | ||
| return v if isinstance(v, bool) else _default_allow_network() | ||
|
|
||
|
|
||
| def set_allow_network(value: bool) -> bool: | ||
| with _LOCK: | ||
| _ensure()["allow_network"] = bool(value) | ||
| _save() | ||
| return bool(value) | ||
|
|
||
|
|
||
| # ── max_duration_sec ── | ||
| def get_max_duration_sec() -> int: | ||
| with _LOCK: | ||
| v = _num(_ensure().get("max_duration_sec")) | ||
| return max(_DURATION_MIN, min(_DURATION_MAX, v)) if v is not None else MAX_DURATION_SEC | ||
|
|
||
|
|
||
| def set_max_duration_sec(value: int) -> int: | ||
| with _LOCK: | ||
| clamped = max(_DURATION_MIN, min(_DURATION_MAX, int(value))) | ||
| _ensure()["max_duration_sec"] = clamped | ||
| _save() | ||
| return clamped | ||
|
|
||
|
|
||
| # ── video_max_height ── | ||
| def get_video_max_height() -> int: | ||
| with _LOCK: | ||
| v = _num(_ensure().get("video_max_height")) | ||
| return max(_HEIGHT_MIN, min(_HEIGHT_MAX, v)) if v is not None else VIDEO_MAX_HEIGHT | ||
|
|
||
|
|
||
| def set_video_max_height(value: int) -> int: | ||
| with _LOCK: | ||
| clamped = max(_HEIGHT_MIN, min(_HEIGHT_MAX, int(value))) | ||
| _ensure()["video_max_height"] = clamped | ||
| _save() | ||
| return clamped | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.