diff --git a/.coderabbit.yaml b/.coderabbit.yaml new file mode 100644 index 0000000..3e314c5 --- /dev/null +++ b/.coderabbit.yaml @@ -0,0 +1,221 @@ +# .coderabbit.yaml — BeatsCheck (audio integrity / ID3 analysis; raw-Python HTTP server + Docker) +language: "en-US" +reviews: + # assertive: chill passed a diff clean that assertive flagged with two Major findings (2026-09-03); the "**" rule keeps style nits out + profile: "assertive" + request_changes_workflow: false + pre_merge_checks: + # off: the 80% docstring-coverage warning contradicts the one-line-docstring rule under "**" + docstrings: + mode: "off" + title: + mode: warning + requirements: |- + Conventional Commits, `type(scope)?: subject`. release-please reads the + squash-merge title to pick the next version: feat bumps minor, fix bumps + patch, a `!` after the type or a BREAKING CHANGE footer bumps major, and + chore/ci/test/build are hidden from the changelog and never release on + their own. Flag a title whose type understates the diff (a behaviour + change labelled chore or ci ships unversioned) or overstates it. + auto_review: + enabled: true + drafts: false + # Single branch: main is the default and is always auto-reviewed. No other PR + # targets exist, so base_branches is intentionally omitted. + path_filters: + - "!**/*.png" + - "!design_handoff_beatscheck_redesign/**" + - "!**/.pytest_cache/**" + - "!**/__pycache__/**" + - "!**/*.pyc" + path_instructions: + - path: "app/webui.py" + instructions: | + Raw http.server request handler — there is NO framework doing input + validation, routing safety, or auth for you. Enforce: + - FAIL CLOSED on every guard. _load_auth() must refuse setup AND login + when the auth file exists but is unreadable/corrupt or is missing + username/password_hash (it returns a sentinel for this — verify callers + treat the sentinel as DENY, never as "no account / allow setup"). + _check_auth() and _validate_session() must return False on any missing/ + expired/malformed token or lookup failure — never fall through to the + authorated path. Flag any `if config and not allowed(): deny` shape + (that skips the check when config is falsy) and any except-clause that + calls the handler / serves content on error instead of sending 401/503. + - PATH TRAVERSAL: any filesystem path derived from self.path (static + files, /api/paths parent, log/corrupt files) must be resolved with + os.path.realpath() and re-asserted inside the allowed root via + _is_subpath() BEFORE opening. A string-only prefix/".." check is + defeated by a symlinked component or ../ collapse — require the + realpath re-confinement. _serve_static must reject anything whose + realpath escapes static_dir; /api/paths must reject anything whose + realpath escapes /data. Flag new file-serving/listing endpoints that + skip this. + - Validate and bound every request input (query params, JSON body, + Content-Length, headers) before use; malformed/oversized/absent input + must return 4xx, not raise or default-through. + - Never place the session token (secrets.token_hex) in a URL, redirect, + /EventSource src, or log line — it is a 24h bearer credential. + Keep it in the HttpOnly cookie path only. + - Failed-login throttle (_login_lockout_remaining / _login_record_failure) + must count and lock BEFORE password verification, and must not be + bypassable by a malformed body reaching an early return. + - path: "app/main.py" + instructions: | + Scan engine, ID3 detector, and irreversible bulk-delete of user music. + - DOMAIN (ID3 false-positive): _strip_benign_metadata_noise / + _is_benign_metadata_noise must strip ONLY genuine ID3 tag-parse noise + (the _BENIGN_METADATA_MARKERS set) and MUST keep real decoder errors + (Header missing / Invalid data / backstep) so a corrupt file still + fails closed. Flag any broadening of the benign markers, substring + match, or logic that could make check_audio_file return is_corrupt=False + for a file with real decode errors. Old-ffmpeg (4.x-6.x) emits this + noise at error level; do not let that re-introduce false positives on + clean audio. + - TRANSIENT != CLEAN: a scan/decode failure that is transient — OSError + on stat, subprocess.TimeoutExpired, ffmpeg spawn failure, I/O error — + must NOT be recorded or cached as a "clean"/negative result. Distinguish + genuine decode-failure (real result) from infrastructure failure (retry, + do not persist as clean). Flag any except path that swallows a transient + error and returns/persists is_corrupt=False. + - DESTRUCTIVE BULK OPS (irrecoverable — rmtree/os.remove of the user's + music library): default to NOT deleting and gate on explicit intent. + Before any shutil.rmtree / os.remove, the RESOLVED physical target must + pass _validate_delete_folders: realpath, reject == music-root (mount + root), reject anything not under music_real + os.sep, reject symlinks, + reject non-directories. An EMPTY validated/allowed set means a FAILED + READ (corrupt.txt/details unreadable) — treat it as "delete nothing", + never "delete everything". delete_corrupt_files must only remove paths + confirmed present in the corrupt.txt allowed set; a path "not in corrupt + list" is an error, not a pass. + - DIFF THE SIBLING DELETE BRANCHES. _delete_one_folder has two destructive + arms: mode 'whole' -> _rmtree_and_collect (shutil.rmtree) and mode + 'corrupt' -> _direct_delete_untracked (os.remove). A containment/symlink/ + mount-root guard present on one arm and absent on the other is the bug — + require both arms to share the _validate_delete_folders confinement, and + flag any new delete path that reaches rmtree/remove without it. + - Confirm state cleanup (corrupt.txt / corrupt_details.json / + corrupt_tracking.json rewrite) is atomic (tmp + os.rename) and cannot + leave a deleted file still listed as corrupt or a surviving file dropped. + - path: "app/**/*.py" + instructions: | + - Python truthiness / 0-is-falsy: for durations, file sizes, counts, + corrupt totals, timestamps, and exit codes where 0 (or "") is a + legitimate value, use explicit `is None` / `is not None` checks, not + bare `if x:` / `x or default`. Flag `count or default`, `size or 0`, + `if not duration:` where 0 must be honoured. + - Read live config each pass in long-lived threads/handlers (scan loop, + background delete jobs); do not snapshot a config/state object at + startup and act on it after config is replaced on reload. + - Comments: navigational / gotcha only (<=1-2 lines, the non-obvious + "why"), matching existing density. Do not add restating-the-code or + history-essay comments. + - path: "app/static/app.js" + instructions: | + - JS 0/"" are falsy: do not use `||` to supply a fallback for a value + where 0 or "" is legitimate (corrupt counts, sizes, progress current/ + total, durations). Use `??` or an explicit null/NaN check + (Number.isFinite, `x == null`). The `n == null || isNaN(n)` guard is the + correct pattern; flag new `count || 0` / `value || '--'` fallbacks that + would mask a real 0. + - Never put the session/auth token in a URL, fetch query string, EventSource + src, or src — auth rides the HttpOnly cookie. Flag any polling/ + streaming endpoint that appends a credential to the URL. + - path: "scripts/**" + instructions: | + Container helper wrappers (delete/rescan/reset-password/entrypoint) that + run as root then drop to PUID/PGID via su-exec, plus fix_ownership.py. + - fix_ownership.py opens the root O_NOFOLLOW and walks with fwalk + follow_symlinks=False so a directory swapped for a symlink is never + descended or chowned through; flag any change that follows symlinks or + that turns a per-file chown failure into a start failure — the + entrypoint's write probe is the real gate, by design. + - FAIL CLOSED: delete.sh must abort when MUSIC_DIR is not writable / not + present (it already checks `-w`); a destructive wrapper must never + proceed on an unverified or unreadable target. Quote every path + expansion ("$MUSIC_DIR", "${PUID}") to survive spaces. + - Preserve correct PUID/PGID drop (default 99:100) and UMASK; do not run + the delete/scan Python as root. + - No secrets echoed or baked into the script or its logs. + - path: "**/Dockerfile" + instructions: | + - Base image must stay pinned by digest (alpine@sha256:...) on every + stage; flag an unpinned or floating tag. Keep the ffmpeg fetch stage's + SHA-256 verification intact — a download without checksum verification + is a supply-chain hole. + - HEALTHCHECK must be present and meaningful (pgrep main.py + heartbeat + freshness); flag its removal or weakening. + - PUID/PGID/UMASK handling: keep the su-exec privilege drop; do not switch + the runtime to root or bake a fixed UID that breaks the drop. + - No secrets/tokens/credentials baked into any layer, ARG default, or + LABEL; nothing sensitive in ENV. + - Run a LIVE dependency/CVE audit (pip-audit for Python, and re-check the + apk-added packages + the pinned ffmpeg version) before tagging a release; + never rely on a stale offline CVE list. + - A recursive chown/chmod after a COPY re-materialises the whole tree in a + new layer and duplicates it in the image — use COPY --chown instead. + - path: "tests/**/*.py" + instructions: | + - Tests live under tests/, so app/**/*.py does not reach them. + - No side-effecting call inside an assert: python -O strips assert + statements and takes the call with them. Bind to a local, then assert. + - A guard that cannot fail is worse than none — an assertion reachable only + when a collection is non-empty passes vacuously. Pair it with a control + that fails when the defect is injected. + - Never let a test invoke real ffmpeg on a real library path or hit the + network; stub the boundary. + - path: "compose.yaml" + instructions: | + - Keep PUID/PGID/UMASK and the volume layout consistent with the Dockerfile + and README; a drift here silently breaks the privilege drop for users. + - No secrets inline — reference an env file or an existing environment + variable, never a literal token. + - Don't add privileged, host network mode, or a docker-socket mount. + - path: ".github/workflows/**" + instructions: | + - A gate that stops blocking (a lint/CVE/test step made non-blocking or + continue-on-error) must have every downstream step that relied on it + audited in the same change. + - Enumerate allowed states positively (== 'success' || == 'skipped'), never + by negation — a third state slips through `!= 'failure'`. + - Pin third-party actions by commit SHA, not a mutable tag. + - Never echo a token, and never interpolate untrusted input (PR title, + branch name, issue body) straight into a `run:` block — pass it via env. + - A paths filter added or renamed must stay in step with what the job + actually needs, or the image silently stops being built when it changes. + - path: "beats-check.xml" + instructions: |- + Published Unraid CA template — what a fresh install gets: + - stays bridge; a personal network name belongs only in the + user's my-*.xml copy, never in the repo template. + - /data defaults to Mode="ro" (report mode is the safe default); flag a + change to rw or a new host path that widens what the container can + delete. + - Every (PUID/PGID/UMASK/TZ, /config, port 8484) must match the + Dockerfile, compose.yaml and README; a renamed env var here without + the matching entrypoint change breaks fresh installs silently. + - path: "**" + instructions: |- + Applies to every file, whatever the language: + - Comments are navigational or instructional only and capped at 1-2 lines: + what a non-obvious block does, or the gotcha it guards. No why/history + essays, no before/after narrative, no restating the code, no section + banners. Docstrings: one line unless the signature genuinely cannot + carry it. Never ask for a docstring to be added; a missing docstring + is not a finding. + - The 1-2 line cap wins even inside an already heavily-commented file — + matching local density is not licence to exceed it. + - A comment, docstring, help text, column header, log message or UI label + that disagrees with what the code does is a correctness finding, not a + stale-comment nit: report it and say which side is wrong. + - Do not flag stylistic preferences that are consistent across the file + (naming, quoting, import order, formatting, line length). Report + correctness, security, data-loss and the classes named above. + + tools: + ruff: + enabled: true + gitleaks: + enabled: true + hadolint: + enabled: true diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..63c74a0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.git +.github +*.md +LICENSE +beats-check.xml +compose.yaml +.dockerignore +.editorconfig +.gitignore +CLAUDE.md diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f04fdc4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,28 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +charset = utf-8 + +[*.py] +indent_style = space +indent_size = 4 +max_line_length = 127 + +[*.{yml,yaml}] +indent_style = space +indent_size = 2 + +[*.{sh}] +indent_style = space +indent_size = 4 + +[*.xml] +indent_style = space +indent_size = 2 + +[Dockerfile] +indent_style = space +indent_size = 4 diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 0000000..5ccf590 --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,61 @@ +# Security Policy + +## Supported Versions + +| Version | Supported | +|---------|-----------| +| latest | Yes | +| < latest | No | + +## Reporting a Vulnerability + +If you discover a security vulnerability, please report it responsibly: + +1. **GitHub Security Advisory** (preferred): [Create a private advisory](https://github.com/chodeus/BeatsCheck/security/advisories/new) +2. **Email**: Open a GitHub issue marked `[SECURITY]` if it is not sensitive + +Please do **not** open a public issue for security vulnerabilities that could be exploited. + +## Security Measures + +### Container Security +- Runs as a non-root user via `su-exec` (configurable PUID/PGID) +- Minimal Alpine base image with only required packages +- Music directory mounted read-only by default (`:ro`) +- Core scanning needs no network access (`--network none` is supported + when the optional Lidarr integration is not configured) +- Tini init process for proper signal handling +- HEALTHCHECK for container orchestrator monitoring +- Supports two hardened deployment modes (both documented in `compose.yaml`): + - **Capability-dropped PUID/PGID:** `cap_drop: ALL` + `cap_add` of the five + capabilities the entrypoint actually needs (CHOWN, SETUID, SETGID, FOWNER, + DAC_OVERRIDE) plus `no-new-privileges:true`. Removes NET_RAW, SYS_ADMIN, + mount, and module-loading powers even during the brief root window. + - **Fully rootless:** `user: "99:100"` instead of PUID/PGID env vars, with + the host config dir pre-chowned to match. Container never runs as root. + +### CI/CD Security +- Trivy vulnerability scanning on every image build +- SARIF results uploaded to GitHub Security tab +- Dependabot monitoring for GitHub Actions and Docker base image updates +- CodeQL static analysis for Python code +- All GitHub Actions pinned to specific commit SHAs (not floating tags) +- Multi-arch builds with SBOM and build attestation + +### Application Security +- Outbound HTTP is limited to the configured Lidarr API (opt-in); the + Lidarr API key is sent via the `X-Api-Key` header (never in URLs), + redirects are blocked, and the key is never logged +- ffmpeg arguments are list-based (no shell injection) +- Symlink boundary checking prevents directory traversal +- Handles credentials: the Lidarr API key (stored in `beatscheck.conf` + or a Docker secret, masked in WebUI responses) and WebUI logins + (PBKDF2-SHA256 hashed passwords + in-memory session tokens) +- All file operations use safe patterns (atomic JSON writes) + +## Best Practices for Users + +- Keep your container image updated (`docker pull ghcr.io/chodeus/beatscheck:latest`) +- Use read-only music mounts when running in report mode +- Set appropriate PUID/PGID for your environment +- Restrict the `/config` directory permissions to the container user diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..f3374b5 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,35 @@ +name: "CodeQL" + +on: + push: + branches: [ "main" ] + paths: + - '**/*.py' + - '.release-please-manifest.json' + - 'VERSION' + - '.github/workflows/codeql.yml' + pull_request: + branches: [ "main" ] + schedule: + - cron: '27 3 * * 1' + +permissions: + contents: read + security-events: write + +jobs: + analyze: + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Initialize CodeQL + uses: github/codeql-action/init@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2 + with: + languages: python + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2 diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..3a0003b --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,108 @@ +name: "Docker Build & Publish" + +# Builds on main, v* tags and every PR (PRs never push; the scan legs run once lint and test pass); +# workflow_dispatch is what image-refresh.yml sends on package drift. + +on: + push: + branches: + - main + tags: + - 'v*' + pull_request: + branches: + - main + workflow_dispatch: + +concurrency: + group: docker-build-${{ github.ref_name }} + cancel-in-progress: true + +jobs: + build-number: + permissions: + contents: read + runs-on: ubuntu-latest + outputs: + number: ${{ steps.count.outputs.number }} + date: ${{ steps.count.outputs.date }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + - id: count + run: | + { + echo "number=$(git rev-list --count HEAD)" + echo "date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" + } >> "$GITHUB_OUTPUT" + + # Gate the image build/publish on lint + smoke tests so a broken push to + # main can't publish :latest. (CodeQL runs in its own workflow; enforce it + # as a required status check via branch protection.) + lint: + permissions: + contents: read + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Set up Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: "3.12" + - name: Install flake8 + run: pip install flake8 + - name: Syntax errors and undefined names + run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + - name: Style warnings + run: flake8 . --count --max-complexity=25 --max-line-length=127 --statistics + + test: + permissions: + contents: read + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Set up Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: "3.12" + - name: Install pytest + run: pip install pytest + - name: Smoke tests + run: python -m pytest tests/ -q + - name: JS syntax check + run: node --check app/static/app.js + + build: + needs: [build-number, lint, test] + # The union the called workflow's jobs declare: checkout, the GHCR push, + # and the Trivy SARIF upload. A called workflow can only reduce these. + permissions: + contents: read + packages: write + security-events: write + uses: chodeus/chodeus-ops/.github/workflows/docker-build.yml@3dd606d5193ceb36366beed1f1ec90bc3d2084ee # main + with: + push: ${{ github.event_name != 'pull_request' }} + # Start the image on each platform and wait for its HEALTHCHECK before pushing. + smoke_healthcheck: true + build_args: | + BUILD_DATE=${{ needs.build-number.outputs.date }} + VCS_REF=${{ github.sha }} + VERSION=${{ github.ref_name }} + BUILD_NUMBER=${{ needs.build-number.outputs.number }} + secrets: inherit + + notify-failure: + needs: build + # notify-discord.yml declares permissions: {} — it needs no scopes. + permissions: {} + if: failure() && github.event_name != 'pull_request' + uses: chodeus/chodeus-ops/.github/workflows/notify-discord.yml@3dd606d5193ceb36366beed1f1ec90bc3d2084ee # main + with: + event_type: build + status: failure + title: "❌ Docker build failed: ${{ github.ref_name }}" + commit_sha: ${{ github.sha }} + secrets: inherit diff --git a/.github/workflows/image-refresh.yml b/.github/workflows/image-refresh.yml new file mode 100644 index 0000000..b1ba7a4 --- /dev/null +++ b/.github/workflows/image-refresh.yml @@ -0,0 +1,43 @@ +# Daily: when the published image's OS packages fall behind, dispatch the publish +# workflow, whose build, scan and smoke gates decide whether the refresh ships. + +name: Image refresh + +on: + schedule: + - cron: '23 4 * * *' # daily, off the hour so it does not queue behind everyone's :00 jobs + workflow_dispatch: {} + +permissions: + contents: read + +jobs: + refresh: + # The called workflow can only reduce what this job grants: the pull needs + # packages: read and the dispatch needs actions: write. + permissions: + packages: read + actions: write + # Pin to an immutable SHA, not @main: a commit to chodeus-ops main would + # otherwise run in every consumer repo with its GITHUB_TOKEN (actions: write). + uses: chodeus/chodeus-ops/.github/workflows/image-refresh.yml@3dd606d5193ceb36366beed1f1ec90bc3d2084ee # main + with: + # Must accept workflow_dispatch. A cached build must also bust the upgrade layer + # with a per-build ARG it references, or the rebuild ships the same packages. + workflow: docker-publish.yml + # Only the default branch publishes :latest, so a manual run from another branch still targets it. + ref: ${{ github.event.repository.default_branch }} + + notify: + # Only a dispatch or a failure is worth a message; a clean daily check is not. + if: always() && (failure() || needs.refresh.outputs.stale == 'true') + needs: refresh + permissions: {} + uses: chodeus/chodeus-ops/.github/workflows/notify-discord.yml@3dd606d5193ceb36366beed1f1ec90bc3d2084ee # main + with: + event_type: workflow + status: ${{ needs.refresh.result == 'success' && 'info' || 'failure' }} + title: ${{ needs.refresh.result == 'success' && '♻️ Image refresh dispatched' || '❌ Image refresh failed' }} + description: ${{ needs.refresh.result == 'success' && format('{0} pending package upgrade(s); the publish workflow was dispatched.', needs.refresh.outputs.pending) || 'The package check or the dispatch failed; see the run.' }} + secrets: + DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..02dccae --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,35 @@ +name: "Lint" + +on: + push: + branches: [ "main" ] + paths: + - '**/*.py' + - '.release-please-manifest.json' + - 'VERSION' + - '.github/workflows/lint.yml' + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: "3.12" + + - name: Install flake8 + run: pip install flake8 + + - name: Syntax errors and undefined names + run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + + - name: Style warnings + run: flake8 . --count --max-complexity=25 --max-line-length=127 --statistics diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml new file mode 100644 index 0000000..811c90e --- /dev/null +++ b/.github/workflows/release-please.yml @@ -0,0 +1,64 @@ +name: Release Please + +on: + push: + branches: [main] + workflow_dispatch: {} + # Merges a release PR whose checks finished after the run that updated it. + schedule: + - cron: '17,47 * * * *' + +permissions: + contents: write + pull-requests: write + # release-please.yml approves its release PR's CI runs. + actions: write + +jobs: + release: + uses: chodeus/chodeus-ops/.github/workflows/release-please.yml@3dd606d5193ceb36366beed1f1ec90bc3d2084ee # main + with: + # Keep the release PR current and merge it once its checks pass; the release follows. + automerge: true + + # docker-publish.yml's tags:['v*'] trigger never fires: release-please pushes + # its tag with GITHUB_TOKEN, and GitHub runs no workflow for events raised by + # that token — so no release has ever produced a version-tagged image. Build it + # here instead, where release_created and the version are already known. The + # push-triggered build cannot do it: it starts in the same second as this + # workflow, before the tag exists. + version-tag-meta: + needs: release + if: needs.release.outputs.release_created == 'true' + runs-on: ubuntu-latest + outputs: + build_number: ${{ steps.m.outputs.number }} + build_date: ${{ steps.m.outputs.date }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + - id: m + run: | + set -euo pipefail + echo "number=$(git rev-list --count HEAD)" >> "$GITHUB_OUTPUT" + echo "date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT" + + version-tag-image: + needs: [release, version-tag-meta] + # The workflow default above has no packages:write; the reusable build needs it. + permissions: + contents: read + packages: write + security-events: write + uses: chodeus/chodeus-ops/.github/workflows/docker-build.yml@3dd606d5193ceb36366beed1f1ec90bc3d2084ee # main + with: + release_version: ${{ needs.release.outputs.version }} + # Start the image on each platform and wait for its HEALTHCHECK before pushing. + smoke_healthcheck: true + build_args: | + BUILD_DATE=${{ needs.version-tag-meta.outputs.build_date }} + VCS_REF=${{ github.sha }} + VERSION=${{ needs.release.outputs.tag_name }} + BUILD_NUMBER=${{ needs.version-tag-meta.outputs.build_number }} + secrets: inherit diff --git a/.github/workflows/repo-events.yml b/.github/workflows/repo-events.yml new file mode 100644 index 0000000..5d227e2 --- /dev/null +++ b/.github/workflows/repo-events.yml @@ -0,0 +1,23 @@ +# Drop into .github/workflows/repo-events.yml of ANY repo. +# Fires Discord for: issue open/close, PR open/close/merge, release publish, discussion. +# Workflow-failure notifications are handled inline by the failing caller workflow (see caller-docker.yml). + +name: Repo events + +on: + issues: + types: [opened, closed, reopened] + pull_request: + types: [opened, closed, reopened, ready_for_review] + release: + types: [published] + discussion: + types: [created] + +permissions: + contents: read + +jobs: + notify: + uses: chodeus/chodeus-ops/.github/workflows/repo-events.yml@3dd606d5193ceb36366beed1f1ec90bc3d2084ee # main + secrets: inherit diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 0000000..44d0fdc --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,16 @@ +name: Stale issues & PRs + +on: + schedule: + - cron: '0 5 * * *' # daily + workflow_dispatch: + +permissions: + issues: write + pull-requests: write + # actions/stale replaces its saved-progress cache via the caches API. + actions: write + +jobs: + stale: + uses: chodeus/chodeus-ops/.github/workflows/stale-issues.yml@3dd606d5193ceb36366beed1f1ec90bc3d2084ee # main diff --git a/.github/workflows/validate-workflows.yml b/.github/workflows/validate-workflows.yml new file mode 100644 index 0000000..4aba73e --- /dev/null +++ b/.github/workflows/validate-workflows.yml @@ -0,0 +1,39 @@ +name: Validate workflows + +on: + push: + branches: + - main + paths: + - '.github/workflows/**' + - '.github/actionlint.yaml' + pull_request: + paths: + - '.github/workflows/**' + - '.github/actionlint.yaml' + workflow_dispatch: + +permissions: + contents: read + +jobs: + actionlint: + name: actionlint + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Install actionlint + id: install + shell: bash + run: | + bash <(curl -sSfL https://raw.githubusercontent.com/rhysd/actionlint/v1.7.12/scripts/download-actionlint.bash) + echo "exe=$PWD/actionlint" >> "$GITHUB_OUTPUT" + + - name: Run actionlint + shell: bash + env: + # Skip shellcheck info/style findings; real bugs still fail. + SHELLCHECK_OPTS: --severity=warning + run: ${{ steps.install.outputs.exe }} -color diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc8b89b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +__pycache__/ +*.pyc +CLAUDE.md + +# macOS Finder metadata +.DS_Store diff --git a/.release-please-manifest.json b/.release-please-manifest.json new file mode 100644 index 0000000..037c9e3 --- /dev/null +++ b/.release-please-manifest.json @@ -0,0 +1,3 @@ +{ + ".": "1.3.4" +} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..cf20b86 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,123 @@ +# Changelog + +## [1.3.4](https://github.com/chodeus/BeatsCheck/compare/v1.3.3...v1.3.4) (2026-09-24) + + +### Bug Fixes + +* **coderabbit:** point the tests instruction at tests/, not the repo root ([dd7169d](https://github.com/chodeus/BeatsCheck/commit/dd7169d0b5de08f51ead5629dbcc269e39451b9d)) +* **docker:** keep /app read-only and chown /config conditionally ([#253](https://github.com/chodeus/BeatsCheck/issues/253)) ([394fb79](https://github.com/chodeus/BeatsCheck/commit/394fb79607befaf141c8d80d645572becf5d3c90)) +* **entrypoint:** stop touching /app and /config, and share one write probe ([#254](https://github.com/chodeus/BeatsCheck/issues/254)) ([570f99c](https://github.com/chodeus/BeatsCheck/commit/570f99c509a2db9f07f3db70344e90c88f9b1cce)) + +## [1.3.3](https://github.com/chodeus/BeatsCheck/compare/v1.3.2...v1.3.3) (2026-08-13) + + +### Bug Fixes + +* re-verify corrupt verdicts before auto-delete and heal stale flags on clean rescan ([0168a43](https://github.com/chodeus/BeatsCheck/commit/0168a432f7e8a2e0fc57b277d43622a943042bc4)) + +## [1.3.2](https://github.com/chodeus/BeatsCheck/compare/v1.3.1...v1.3.2) (2026-08-08) + + +### Bug Fixes + +* let Renovate see ffmpeg tags without a patch component ([#210](https://github.com/chodeus/BeatsCheck/issues/210)) ([37c8c52](https://github.com/chodeus/BeatsCheck/commit/37c8c52d00eb2cd723e2b32497d876b258f23041)) +* move ffmpeg to n9.0 for the 8.1.2 advisory set ([9597705](https://github.com/chodeus/BeatsCheck/commit/959770504b9154c82962bd6bc9fb6d91d4740241)) + +## [1.3.1](https://github.com/chodeus/BeatsCheck/compare/v1.3.0...v1.3.1) (2026-07-09) + + +### Bug Fixes + +* strengthen decode-check and ignore benign ID3 tag errors ([f267968](https://github.com/chodeus/BeatsCheck/commit/f267968609883bc1c2330b0a94f654ac25fc9409)) + +## [1.3.0](https://github.com/chodeus/BeatsCheck/compare/v1.2.0...v1.3.0) (2026-06-05) + + +### Features + +* **docker:** add rootless --user support and cap-drop hardening guidance ([c9926a9](https://github.com/chodeus/BeatsCheck/commit/c9926a99b61d51dab45f67d635d0721f6f5a5793)) +* ship FFmpeg 8.1.x from chodeus/ffmpeg-static ([8df59bd](https://github.com/chodeus/BeatsCheck/commit/8df59bd48aecb75a79c075428ba96743bebfa6e9)) + + +### Documentation + +* reference chodeus/ffmpeg-static as the bundled ffmpeg source ([1a1de77](https://github.com/chodeus/BeatsCheck/commit/1a1de77fa31186f7a9891b3f0611fa4e827f15ad)) + +## [1.2.0](https://github.com/chodeus/BeatsCheck/compare/v1.1.0...v1.2.0) (2026-04-19) + + +### Features + +* add monotonic BUILD_NUMBER to docker image ([#32](https://github.com/chodeus/BeatsCheck/issues/32)) ([1e075ce](https://github.com/chodeus/BeatsCheck/commit/1e075ceb5639d2fd97421457b145bf16e803a856)) +* background file-delete jobs, per-album Lidarr search report, dynamic version ([7ecf7e4](https://github.com/chodeus/BeatsCheck/commit/7ecf7e44d08328781ba62e4fd11a37524034e080)) + +## [1.1.0](https://github.com/chodeus/BeatsCheck/compare/v1.0.0...v1.1.0) (2026-04-19) + + +### Features + +* **webui:** bulk album delete + Lidarr re-download completion tracking ([#20](https://github.com/chodeus/BeatsCheck/issues/20)) ([2e0c79a](https://github.com/chodeus/BeatsCheck/commit/2e0c79aaae82bd33211d6640a1a519a91afe4c33)) + +## 1.0.0 (2026-04-19) + + +### Features + +* add favicon to WebUI browser tab ([3c11ada](https://github.com/chodeus/BeatsCheck/commit/3c11adacd41e0df0d70ab89aceed982f4afdb548)) +* album view toggle, corrupt page improvements, config subtitle ([c95b6b8](https://github.com/chodeus/BeatsCheck/commit/c95b6b82856ebc545b058fec90dd0269ee7d3e7b)) +* collapsible Lidarr info banner on corrupt files page ([1410036](https://github.com/chodeus/BeatsCheck/commit/14100367abb0115d7c88e7866fe8cfc4e46fffd4)) +* interactive folder picker for scan and quarantine paths ([205358f](https://github.com/chodeus/BeatsCheck/commit/205358f928ab7d3543e31970350ab48e3e1fc062)) +* path dropdown for scan location and quarantine folder ([726e3a3](https://github.com/chodeus/BeatsCheck/commit/726e3a3a1533c66ca63b04033b410787733bcbe9)) +* production-ready WebUI with auth, security hardening, and accessibility ([a77c079](https://github.com/chodeus/BeatsCheck/commit/a77c07922f3fd8710d1de50d1b1b51282d8007a1)) +* redesign corrupt page and config with workflow-oriented UX ([af47832](https://github.com/chodeus/BeatsCheck/commit/af47832c8f0603ad4fdac54913557fabeb490f08)) +* write corrupt_details.json incrementally during scan ([5bb4e56](https://github.com/chodeus/BeatsCheck/commit/5bb4e56d30586260a14edd6056241d4a20c34c4e)) + + +### Bug Fixes + +* block deletes during scan, add scan-in-progress banner ([86b0f3a](https://github.com/chodeus/BeatsCheck/commit/86b0f3a6f0c7dc54beb131785de74facc7bc1701)) +* cancel scan responsiveness, log coloring, UI improvements ([8f1c5a4](https://github.com/chodeus/BeatsCheck/commit/8f1c5a4be3b786cd6f5111be2bb3c12a0730be69)) +* color the status dot icon instead of the status text on dashboard ([#5](https://github.com/chodeus/BeatsCheck/issues/5)) ([3ffa213](https://github.com/chodeus/BeatsCheck/commit/3ffa2133b13f3b04685c8599714f422ea45bff7f)) +* config hot-reload, env sync, and Lidarr log safety ([1815eb2](https://github.com/chodeus/BeatsCheck/commit/1815eb2922dbb940a8027094aa9b3c2f7b98e6d2)) +* delete processed.txt before writing rescan trigger to prevent race ([9e813c2](https://github.com/chodeus/BeatsCheck/commit/9e813c2ee818b0aafb6da7a5c2e78e952d57c8ef)) +* early return on scan cancel to prevent misleading completion log ([1d12552](https://github.com/chodeus/BeatsCheck/commit/1d12552ef95ec58f199953509eaf4d106fe4888b)) +* extract _post_scan_wait to reduce main() complexity below lint threshold ([fcd3815](https://github.com/chodeus/BeatsCheck/commit/fcd3815033e72a81ed3c4053f900b5102d5c671e)) +* folder picker 403 error and add host path display ([36f5876](https://github.com/chodeus/BeatsCheck/commit/36f58763733a68d29221a56706020567228f66ce)) +* graceful path validation instead of crashing container ([7e6c4e6](https://github.com/chodeus/BeatsCheck/commit/7e6c4e6196c7366b167c401fd3351a4300ec2d9e)) +* harden Lidarr integration in scan flow ([0d412c0](https://github.com/chodeus/BeatsCheck/commit/0d412c0bd6ed94ed0816d3a9e731760a89f3b5bf)) +* harden path containment checks and fix fresh-rescan race ([5a0e232](https://github.com/chodeus/BeatsCheck/commit/5a0e232628ef5a11595479c0e56f1a19d167cc84)) +* iOS input zoom and blocklist status in info banner ([ac49b59](https://github.com/chodeus/BeatsCheck/commit/ac49b59d1883df458e3b091471af2b31eac6b115)) +* log remaining in-progress files when scan is cancelled ([e8827a1](https://github.com/chodeus/BeatsCheck/commit/e8827a192babc5d8cc8356823f3823194ce5cc6a)) +* music_dir not applied on rescan, improve scan UX ([9e3af6e](https://github.com/chodeus/BeatsCheck/commit/9e3af6ef8f58eceef2599b739713d69d8dc34b98)) +* remove dead unfiltered Lidarr trackfile bulk fetch ([963986d](https://github.com/chodeus/BeatsCheck/commit/963986d5db07acc9ad4e048aac3eb42f78642832)) +* remove MUSIC_DIR/OUTPUT_DIR from Dockerfile ENV ([691a935](https://github.com/chodeus/BeatsCheck/commit/691a935cd5abc33d0964868b967643f4d91439fa)) +* remove redundant sys import that failed lint ([0bbe5a2](https://github.com/chodeus/BeatsCheck/commit/0bbe5a268c7521c409c115beeabf27da2abe103c)) +* **repo-events:** update chodeus-ops path to .github/workflows/ ([7a391e8](https://github.com/chodeus/BeatsCheck/commit/7a391e84e3810b033858a2fc951d267d6b7c9558)) +* resilient Lidarr delete with re-resolve, individual fallback, error reporting ([4a0a8e6](https://github.com/chodeus/BeatsCheck/commit/4a0a8e60d57f1c2a2c8a22f2b00ef12ecc22b28e)) +* resolve Lidarr IDs inline during scan instead of blocking at end ([50a42fa](https://github.com/chodeus/BeatsCheck/commit/50a42fae4e74755554435da4804a0596ac69ca1e)) +* revert host_data_path feature, keep 403 fix and editable input ([f2cca5f](https://github.com/chodeus/BeatsCheck/commit/f2cca5f22874f2ec49c458a436e6861373cac19c)) +* scan order, cancel bug, live library size, status colors ([#4](https://github.com/chodeus/BeatsCheck/issues/4)) ([7d95110](https://github.com/chodeus/BeatsCheck/commit/7d95110e4e59f23623e93f69dd66da383d783a2e)) +* select-all in album view, add Re-download Selected, sequential Lidarr deletes ([eb87ce7](https://github.com/chodeus/BeatsCheck/commit/eb87ce7cc4b75902c3a0a25edca15493bb42a7fd)) +* set status to idle before finalization on "Nothing to do" path ([#7](https://github.com/chodeus/BeatsCheck/issues/7)) ([1bcd58a](https://github.com/chodeus/BeatsCheck/commit/1bcd58a77e7549a672759002280b3b64b7df15fa)) +* skip Lidarr index build when there are no files to scan ([f7fcfb1](https://github.com/chodeus/BeatsCheck/commit/f7fcfb12450ba4ad6cd070c75793f8b4d09d1128)) +* sort JSON output and improve Lidarr resolution logging ([b761a9c](https://github.com/chodeus/BeatsCheck/commit/b761a9cc1f3b0cf9ef43d166987d449d90957979)) +* suppress CodeQL false positive for path injection in static file serving ([7409f7c](https://github.com/chodeus/BeatsCheck/commit/7409f7c829dffb6c50107356cc7319d97d67f579)) +* swallow client-disconnect errors in JSON response writer ([#10](https://github.com/chodeus/BeatsCheck/issues/10)) ([e5e69ba](https://github.com/chodeus/BeatsCheck/commit/e5e69babeb09b71366a44089e3752ce81788a74e)) +* update status to idle immediately after scan completes ([#6](https://github.com/chodeus/BeatsCheck/issues/6)) ([7907c5d](https://github.com/chodeus/BeatsCheck/commit/7907c5d8e3ddd9f238ca1067d5e2db4fbbe4cedf)) +* use explicit 16px for input font-size to prevent iOS zoom ([d616aaa](https://github.com/chodeus/BeatsCheck/commit/d616aaa156a8b5891b66239671b8c6a6ed25a520)) +* WebUI bugs — config labels, missing fields, layout, cancel scan ([1eaa348](https://github.com/chodeus/BeatsCheck/commit/1eaa3485499e1718f432d24e6732dd812e78d3c3)) +* write all logger output to log file so WebUI logs match container logs ([65dd7de](https://github.com/chodeus/BeatsCheck/commit/65dd7de9535e5158340c5b8cfb217e4ac63376aa)) + + +### Refactoring + +* /music → /data mount, remove /corrupted volume ([bb72619](https://github.com/chodeus/BeatsCheck/commit/bb72619d08789e993b26276f666d5efdb3668327)) +* reorganize repo into app/ and scripts/ directories ([dc53f02](https://github.com/chodeus/BeatsCheck/commit/dc53f022a2be5878416c01a40fe45563265102cc)) +* simplify config parsing, fix CodeQL alerts, remove dead code ([f7dfc22](https://github.com/chodeus/BeatsCheck/commit/f7dfc22d1e2d310900874a22b7cecd76530092fe)) +* simplify scan core, Lidarr resolver, WebUI polling, and auth ([d064fdd](https://github.com/chodeus/BeatsCheck/commit/d064fddb5dc25178a1ff4a0a0122f56add541a4c)) + + +### Documentation + +* simplify beatscheck.conf for non-technical users ([64c753b](https://github.com/chodeus/BeatsCheck/commit/64c753b82338476fbc40be3584e1359cb09b675f)) diff --git a/Dashboardscreenshot.png b/Dashboardscreenshot.png new file mode 100644 index 0000000..34e11cf Binary files /dev/null and b/Dashboardscreenshot.png differ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ff8fc6e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,109 @@ +# ---- ffmpeg fetch stage ---- +# Download + SHA-256 verify the static ffmpeg from chodeus/ffmpeg-static. The +# linux assets are built there from pinned upstream source (LGPL-3.0), not +# mirrored — only that repo's Windows assets are BtbN mirrors, and we take none. +# Runs on the build host; validated against musl in the final stage below. +FROM --platform=$BUILDPLATFORM alpine:3.24@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6 AS ffmpeg-fetch +ARG TARGETARCH +# renovate: datasource=github-releases depName=chodeus/ffmpeg-static versioning=regex:^n?(?\d+)\.(?\d+)(?:\.(?\d+))?$ +ARG FFMPEG_VERSION=n9.0.2 +RUN apk add --no-cache ca-certificates wget +RUN set -eux; \ + arch="${TARGETARCH:-$(uname -m | sed -e 's/x86_64/amd64/' -e 's/aarch64/arm64/')}"; \ + case "$arch" in \ + amd64) asset="ffmpeg-linux64" ;; \ + arm64) asset="ffmpeg-linuxarm64" ;; \ + *) echo "unsupported architecture: $arch" >&2; exit 1 ;; \ + esac; \ + base="https://github.com/chodeus/ffmpeg-static/releases/download/${FFMPEG_VERSION}"; \ + mkdir -p /out; \ + wget -qO /out/ffmpeg "${base}/${asset}"; \ + wget -qO /tmp/SHA256SUMS "${base}/SHA256SUMS"; \ + expected="$(awk -v f="$asset" '$2 == f { print $1 }' /tmp/SHA256SUMS)"; \ + actual="$(sha256sum /out/ffmpeg | awk '{ print $1 }')"; \ + [ -n "$expected" ] && [ "$expected" = "$actual" ] \ + || { echo "ffmpeg SHA-256 mismatch for $asset (want=$expected got=$actual)" >&2; exit 1; }; \ + chmod +x /out/ffmpeg + +# ---- final image ---- +FROM alpine:3.24@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6 + +ARG BUILD_DATE +ARG VCS_REF +ARG VERSION +ARG BUILD_NUMBER + +LABEL org.opencontainers.image.title="beatscheck" \ + org.opencontainers.image.description="Audio file integrity checker using ffmpeg decode testing" \ + org.opencontainers.image.version="${VERSION}" \ + org.opencontainers.image.created="${BUILD_DATE}" \ + org.opencontainers.image.revision="${VCS_REF}" \ + org.opencontainers.image.authors="chodeus" \ + org.opencontainers.image.source="https://github.com/chodeus/BeatsCheck" \ + org.opencontainers.image.licenses="MIT" \ + org.opencontainers.image.base.name="alpine:3.24" \ + net.unraid.docker.icon="https://raw.githubusercontent.com/chodeus/BeatsCheck/main/icon.png" \ + build.number="${BUILD_NUMBER}" + +ENV PUID=99 \ + PGID=100 \ + UMASK=002 \ + TZ=UTC \ + PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 \ + CONFIG_DIR=/config \ + BUILD_NUMBER=${BUILD_NUMBER} + +# Echoing BUILD_DATE makes every build re-run the upgrade instead of reusing a cached layer. +RUN echo "packages as of ${BUILD_DATE}" && \ + apk --no-cache upgrade && \ + apk --no-cache add \ + python3 \ + su-exec \ + tini \ + tzdata + +# The pinned FFmpeg from the fetch stage above (see FFMPEG_VERSION) is copied +# into the final image; no ffmpeg is installed from apk. The self-check fails +# the build loudly if the static binary can't run on Alpine's musl — turning a +# would-be runtime failure into a build-time one. +COPY --from=ffmpeg-fetch /out/ffmpeg /usr/local/bin/ffmpeg +RUN ffmpeg -version + +WORKDIR /app + +COPY VERSION /app/VERSION +COPY scripts/entrypoint.sh /app/ +COPY scripts/fix_ownership.py /app/ +COPY scripts/delete.sh /app/ +COPY scripts/rescan.sh /app/ +COPY scripts/reset-webui-password.sh /app/ +COPY app/static/ /app/static/ +COPY app/webui.py /app/ +COPY app/main.py /app/ +# Bytecode baked here; PYTHONDONTWRITEBYTECODE keeps /app read-only at runtime. +# The assertion replaces `chmod -R a+rX`, which re-materialised the tree in a layer. +RUN set -e; \ + chmod +x /app/entrypoint.sh /app/delete.sh /app/rescan.sh \ + /app/reset-webui-password.sh; \ + python3 -m compileall -q /app/main.py /app/webui.py; \ + unreadable="$(find /app \( -type f ! -perm -0004 \) -o \( -type d ! -perm -0005 \) | head -20)"; \ + if [ -n "$unreadable" ]; then echo "not world-readable:"; echo "$unreadable"; exit 1; fi; \ + ln -s /app/delete.sh /usr/local/bin/delete; \ + ln -s /app/rescan.sh /usr/local/bin/rescan; \ + ln -s /app/reset-webui-password.sh /usr/local/bin/reset-webui-password + +EXPOSE 8484 + +VOLUME ["/data", "/config"] + +STOPSIGNAL SIGTERM + +HEALTHCHECK --interval=60s --timeout=5s --start-period=30s --retries=3 \ + CMD pgrep -f "main.py" > /dev/null && \ + { [ ! -f /config/.heartbeat ] || \ + [ "$(( $(date +%s) - $(cat /config/.heartbeat 2>/dev/null || echo 0) ))" -lt 660 ]; } \ + || exit 1 + +ENTRYPOINT ["/sbin/tini", "--", "/app/entrypoint.sh"] +CMD [] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..da3c8d4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 chodeus + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..edb1741 --- /dev/null +++ b/README.md @@ -0,0 +1,465 @@ +# BeatsCheck + +> **This project is in active development.** Features may change between releases. Please report issues on [GitHub](https://github.com/chodeus/BeatsCheck/issues). + +Audio file integrity checker that performs a full decode test using ffmpeg. Catches audio stream corruption like `Decoding error: Invalid data` and `Decode error rate exceeds maximum`. + +**Default mode is setup — the container starts idle and won't scan until you configure it. No files are moved or deleted until you explicitly choose to.** + +## Supported Formats + +FLAC, MP3, M4A, OGG, Opus, WAV, WMA, AAC, AIFF, APE, WavPack, ALAC, M4B, M4P, MP2, MPC, DSF, DFF + +## How It Works + +Each audio file is decoded end-to-end with `ffmpeg -v error -xerror -nostdin -i -map 0:a -f null -`. The `-xerror` flag makes ffmpeg exit immediately on any decode error and `-map 0:a` ensures only audio streams are tested. Files that fail are flagged as corrupt. A clean `corrupt.txt` list is written for scripting or interactive deletion. + +## Modes + +| Mode | What it does | Music mount | Safe? | +|------|-------------|-------------|-------| +| `setup` | Container starts idle, no scanning (default) | `ro` | Yes | +| `report` | Scan and log only, writes `corrupt.txt` | `ro` | Yes | +| `delete` | Interactive — prompts per album folder to delete | **`rw`** | You choose | +| `move` | Auto-move corrupt files to quarantine folder | **`rw`** | Destructive | + +You can change modes without restarting — see [Rescan](#4-rescan) below. + +## Recommended Workflow + +### 1. Scan (report mode) + +Start the container normally. It scans your library, writes results, and waits for next interval: + +``` +BeatsCheck v1.0.0 starting +2026-01-15 10:00:00 | INFO | BeatsCheck v1.0.0 +2026-01-15 10:00:00 | INFO | Mode: report +2026-01-15 10:00:00 | INFO | Workers: 6 +2026-01-15 10:00:00 | INFO | Library: 98432 files (2.8 TB) +2026-01-15 10:00:00 | INFO | To scan: 98432 files (0 already processed) +2026-01-15 10:05:00 | INFO | [5%] 5000/98432 checked, 2 corrupt, ETA 3h42m +``` + +### 2. Review results + +Check the logs on the host: + +```bash +# Clean list of corrupt file paths +cat /path/to/config/corrupt.txt + +# Full log with error details +cat /path/to/config/beats_check.log +``` + +### 3. Delete corrupt files + +**Important:** The music mount must be `rw` (not `ro`) for delete mode to work. + +From the container console or via `docker exec`: + +```bash +# Inside container console +delete + +# Or from the host +docker exec -it beatscheck delete +``` + +You'll see a menu: + +``` +Found 5 corrupt files across 3 folders (142.5 MB) + + [a] delete ALL corrupt files now + [i] interactive (decide per folder) + [q] quit + + Choice: i + + [1/3] /data/Artist Name/Album Name/ + track01.flac (45.2 MB) + -> invalid residual | decode_frame() failed | Decoding error: Invalid data found + track05.flac (512 B) + -> File too small (512 bytes) + (2 corrupt / 12 total files in folder) + Action? [y/f/n/a/q] +``` + +Interactive options: +- `y` — delete entire folder (nuke the album, re-download later) +- `f` — delete just the corrupt files, keep the rest +- `n` — skip this folder +- `a` — delete all remaining folders without asking +- `q` — quit + +After deletion, `corrupt.txt` is updated to remove handled entries. + +### 4. Rescan + +Trigger a rescan or change modes without restarting the container: + +```bash +# Rescan with current mode +rescan + +# Change mode and scan (works from setup mode too) +rescan report +rescan move + +# Full rescan (clear resume cache) +rescan --fresh report + +# From the host +docker exec beatscheck rescan report +``` + +## Safety Features + +- **Setup mode by default** — container starts idle, nothing happens until you choose a mode +- **Read-only music mount** — kernel-enforced via Docker `:ro` flag (change to `rw` only for `delete`/`move` modes) +- **`-xerror` flag** — fail-fast on decode errors, no false positives from partial decodes +- **Symlink boundary check** — won't traverse symlinks that point outside the music directory +- **Graceful shutdown** — responds to SIGTERM/SIGINT, finishes in-progress files then exits cleanly +- **Resume support** — tracks already-checked files in `processed.txt` across runs (essential for multi-hour scans) +- **CPU throttled** — `nice(10)` + configurable `--cpus` to avoid impacting other services +- **10 min per-file timeout** — prevents hangs on severely corrupt files +- **Docker HEALTHCHECK** — verifies process is running and heartbeat is fresh +- **Hardlink aware** — logs link count when corrupt files have multiple hard links +- **Atomic JSON writes** — crash-safe output files (no corruption on power loss) +- **Auto-delete safety threshold** — aborts if too many files flagged (prevents catastrophic deletion) + +## Configuration + +### Config File + +On first run, BeatsCheck creates `/config/beatscheck.conf` with all options and their defaults. Edit this file to configure scanning, scheduling, and Lidarr integration. Credentials stored here stay out of `docker inspect` and process listings. + +| Setting | Default | Description | +|---------|---------|-------------| +| `output_dir` | `/corrupted` | Quarantine destination for move mode. Must match a mounted volume | +| `mode` | `setup` | `setup` (idle), `report`, `delete`, or `move`. Can be changed at runtime via `rescan` | +| `workers` | `4` | Parallel ffmpeg decode workers. 2 = conservative, 4 = balanced, 8+ = fast | +| `run_interval` | `0` | Hours between scans. `0` = run once and exit. `168` = weekly. `24` = daily | +| `delete_after` | `0` | Days before corrupt files are auto-deleted. `0` = never (manual only). `7` = 7 day review window | +| `max_auto_delete` | `50` | Safety threshold — abort auto-delete if more than this many files would be removed. `0` = no limit | +| `min_file_age` | `30` | Skip files modified within this many minutes. Prevents flagging active downloads | +| `log_level` | `INFO` | Logging verbosity: `DEBUG`, `INFO`, `WARNING`, `ERROR` | +| `max_log_mb` | `50` | Rotate log and do fresh full scan when log exceeds this size. `0` = never rotate | +| `lidarr_url` | *(empty)* | Lidarr instance URL (e.g. `http://lidarr:8686`). Enables Lidarr API integration | +| `lidarr_api_key` | *(empty)* | Lidarr API key (Settings → General in Lidarr). Also reads from `/run/secrets/lidarr_api_key` | +| `lidarr_search` | `false` | Queue search for unmonitored albums after auto-delete. Monitored albums are auto-searched by Lidarr. 5 albums/hour during idle | +| `lidarr_blocklist` | `false` | Blocklist the release in Lidarr before deleting, preventing re-download of the same corrupt copy | +| `webui` | `false` | Enable the built-in web interface. Requires a published port | +| `webui_port` | `8484` | Port for the web interface | + +Environment variables (uppercase, e.g. `MODE`, `WORKERS`) override the config file if set. + +### Container Environment Variables + +These Docker-level settings are configured as environment variables: + +| Env Var | Default | Description | +|---------|---------|-------------| +| `PUID` | `99` | User ID for file ownership | +| `PGID` | `100` | Group ID for file ownership | +| `TZ` | `UTC` | Timezone for log timestamps. Auto-detected if `/etc/localtime` is bind-mounted | +| `UMASK` | `002` | File creation mask | + +Volume paths (`MUSIC_DIR`, `OUTPUT_DIR`, `CONFIG_DIR`) default to `/data`, `/corrupted`, `/config` and are set via Docker volume mounts. + +## Web UI + +BeatsCheck includes an optional web interface for monitoring and control. Disabled by default. + +### Enabling the WebUI + +1. Edit `/config/beatscheck.conf`: + ```conf + webui = true + webui_port = 8484 + ``` + +2. Publish the port in your Docker setup: + ```yaml + # compose.yaml + ports: + - "8484:8484" + ``` + +3. Access at `http://your-server:8484` +4. On first visit, create your login credentials via the setup wizard + +### Features + +- **Authentication** — first-run setup wizard, session-based login with PBKDF2-hashed passwords +- **Dashboard** — live scan status, progress bar with ETA, library stats +- **Corrupt Files** — sortable/searchable table with individual and bulk delete +- **Configuration** — edit all settings from the browser (config key allowlist enforced) +- **Logs** — real-time log viewer with syntax highlighting, level filter, search, copy/download +- **Dark/Light mode** — toggle with one click, preference saved +- **Mobile responsive** — full functionality on phones and tablets +- **Accessible** — keyboard navigation, ARIA labels, skip-to-content, reduced motion support + +### Resetting Credentials + +If you forget your WebUI password: + +```bash +docker exec beatscheck reset-webui-password +``` + +This removes the credential file. The next visit to the WebUI will show the setup wizard to create new credentials. + +### WebUI Security + +- **Authentication required** — all API endpoints require a valid session (PBKDF2-SHA256 hashed passwords, HttpOnly session cookies) +- **Setup wizard** — credentials created on first access, stored hashed in `/config/webui_auth.json` +- **Config allowlist** — only known configuration keys are accepted (arbitrary key injection blocked) +- **Thread-safe config writes** — concurrent requests cannot corrupt the config file +- **Delete validation** — files must be in `corrupt.txt` and inside the music directory; symlinks rejected +- **Path traversal protection** — static file serving validates all paths against the static directory +- **API key masking** — Lidarr API key shown as `********` in the UI +- **No external dependencies** — built on Python stdlib only (no supply chain risk) + +**Important:** The WebUI is designed for trusted LAN / Docker bridge networks. For remote access, use a reverse proxy with HTTPS and authentication (Nginx, Caddy, Traefik). Do not expose the WebUI port directly to the internet. + +## Docker Usage + +### Docker Compose (Recommended) + +A complete `compose.yaml` is included in the repository: + +```bash +# Copy and edit the compose file +cp compose.yaml /path/to/your/compose.yaml +# Edit paths and settings, then: +docker compose up -d +``` + +### Docker Run + +```bash +# Scan (report mode, run once) +docker run --rm \ + -v /path/to/music:/data:ro \ + -v /path/to/config:/config \ + -e MODE=report \ + -e WORKERS=6 \ + --cpus=2 \ + ghcr.io/chodeus/beatscheck:latest + +# Daemon mode (weekly scans, stays running) +docker run -d --restart unless-stopped \ + --name beatscheck \ + -v /path/to/music:/data:ro \ + -v /path/to/config:/config \ + -e MODE=report \ + -e RUN_INTERVAL=168 \ + -e WORKERS=6 \ + ghcr.io/chodeus/beatscheck:latest + +# Interactive delete (requires rw music mount) +docker exec -it beatscheck delete + +# Trigger a rescan +docker exec beatscheck rescan +``` + +## Installation on Unraid + +### Pull from GHCR + +```bash +docker pull ghcr.io/chodeus/beatscheck:latest +``` + +### Install the Template + +```bash +wget -O /boot/config/plugins/dockerMan/templates-user/my-BeatsCheck.xml \ + https://raw.githubusercontent.com/chodeus/BeatsCheck/main/beats-check.xml +``` + +### Add the Container + +1. Go to **Docker** tab in Unraid web UI +2. Click **Add Container** +3. Select **BeatsCheck** from the template dropdown +4. Verify paths match your setup: + - **Music Library**: `/mnt/user/data/media/music` (or your music directory) + - **Config**: `/mnt/user/appdata/beatscheck` +5. Configure your settings (paths, workers, Lidarr if needed) +6. Click **Apply** — the container starts idle in setup mode +7. Open the container console and type `rescan report` to start scanning + +### How the Container Runs + +The container defaults to **setup mode** — it starts idle and waits for you to trigger a scan. This lets you configure everything before any scanning begins. + +**To start scanning:** run `rescan report` from the container console or set `MODE=report` and restart. + +**With `RUN_INTERVAL` set (e.g., 168 for weekly):** +After the first scan, the container sleeps for the interval, then scans again. Only new/changed files are checked on subsequent runs (resume support). + +**With `RUN_INTERVAL=0` (default):** +The container scans once and stays idle. Use `rescan` to trigger another scan without restarting. + +### Deleting Corrupt Files + +Three options — pick what suits your workflow: + +**Option 1: Auto-delete after X days (fully automated)** + +Set `DELETE_AFTER=7` in the container config. Corrupt files are automatically deleted 7 days after being first detected. This gives you time to review `corrupt.txt` before anything is removed. + +**Option 2: Interactive delete (on demand)** + +Change the music mount to `rw`, then from the Unraid **terminal**: + +```bash +docker exec -it BeatsCheck delete +``` + +**Option 3: Manual delete from corrupt.txt** + +```bash +cat /mnt/user/appdata/beatscheck/corrupt.txt +while IFS= read -r f; do rm -v "$f"; done < /mnt/user/appdata/beatscheck/corrupt.txt +``` + +### Unraid Notifications (Optional) + +Use a **User Scripts** wrapper to get notified after scans: + +```bash +#!/bin/bash +LOG_DIR="/mnt/user/appdata/beatscheck" + +if [ -f "$LOG_DIR/summary.json" ]; then + CHECKED=$(jq -r '.files_checked' "$LOG_DIR/summary.json") + CORRUPT=$(jq -r '.corrupted' "$LOG_DIR/summary.json") + DURATION=$(jq -r '.duration' "$LOG_DIR/summary.json") + SIZE=$(jq -r '.library_size_human' "$LOG_DIR/summary.json") + + if [ "$CORRUPT" -gt 0 ]; then + /usr/local/emhttp/webGui/scripts/notify \ + -i warning -s "BeatsCheck" \ + -d "Scan complete: $CHECKED files ($SIZE), $CORRUPT corrupt found ($DURATION)" + else + /usr/local/emhttp/webGui/scripts/notify \ + -i normal -s "BeatsCheck" \ + -d "Scan complete: $CHECKED files ($SIZE), no corruption ($DURATION)" + fi +fi +``` + +## Standalone Usage (No Docker) + +Requires Python 3.9+ and ffmpeg installed. The Docker image bundles a +fully-static **FFmpeg 8.1.x** from [chodeus/ffmpeg-static](https://github.com/chodeus/ffmpeg-static) +(kept current via Renovate); this section is only for running outside Docker, +where you provide your own ffmpeg. + +```bash +MODE=report WORKERS=6 python3 app/main.py /path/to/music /path/to/quarantine /path/to/config/beats_check.log +``` + +The third argument is the log file path. All state files (`processed.txt`, `corrupt.txt`, etc.) are written to the same directory as the log file. Unix-only (requires `fcntl`). + +## Output Files (in /config) + +| File | Contents | +|------|----------| +| `beats_check.log` | Full scan log — errors, moves, deletes, and scan summaries | +| `beats_check.log.1` `.2` `.3` | Previous logs after rotation (last 3 kept) | +| `processed.txt` | Resume cache — one checked file path per line. Rotated alongside the log | +| `corrupt.txt` | One corrupt file path per line (deduplicated) — for scripting or delete mode | +| `corrupt_details.json` | Path-to-error mapping with Lidarr trackfile/album IDs (when configured) | +| `corrupt_tracking.json` | Path-to-first-seen timestamps — used by `DELETE_AFTER` auto-delete | +| `summary.json` | Machine-readable scan results for notification scripts | +| `search_queue.json` | Pending Lidarr album search queue — drained during idle (5/hour) | +| `webui_auth.json` | WebUI login credentials (username + PBKDF2-hashed password) | +| `.scanning` | Lock file (exists only during active scans, uses `flock`) | +| `.heartbeat` | Timestamp updated during scans and idle — used by Docker healthcheck | + +### Log Rotation + +When the log exceeds 50 MB (`MAX_LOG_MB=50`), it's rotated to `beats_check.log.1` (keeping up to 3 old copies). The resume cache (`processed.txt`) is rotated alongside it, so the next scan does a fresh full re-check. Set `MAX_LOG_MB=0` to disable rotation. + +For a 100K file library, expect ~10 MB per full scan. With daemon mode, subsequent scans only log corrupt files so growth is slow. + +## Performance (3TB Library) + +| Resource | Impact | +|----------|--------| +| **CPU** | 10-20% total with 6 workers (audio decode is light) | +| **RAM** | Under 500MB | +| **Disk I/O** | The bottleneck — expect 6-15 hours depending on array speed | +| **Other services** | Unaffected (low priority + CPU cap) | + +## Lidarr Integration + +When `LIDARR_URL` and `LIDARR_API_KEY` are set, BeatsCheck uses the Lidarr API for all deletion operations. Track file records are removed via the API while albums stay monitored — Lidarr automatically re-downloads monitored albums after deletion. + +**How it works:** + +1. **Scan** — corrupt files are detected and matched to Lidarr trackfile IDs via path suffix matching. IDs are stored in `corrupt_details.json` so delete operations don't need to re-resolve them. +2. **Blocklist** (`LIDARR_BLOCKLIST=true`) — before deletion, the most recent grab for each affected album is marked as failed. Lidarr auto-creates a blocklist entry so the same corrupt release is not re-downloaded. **If blocklist fails, deletion is aborted.** +3. **Delete** — track files are removed via Lidarr's bulk delete API. Albums stay monitored and show as "missing". **If the API call fails, deletion is aborted** (no silent fallback to filesystem delete). +4. **Re-download** — monitored albums are automatically re-searched by Lidarr after the trackfiles are deleted. No manual intervention needed. + +**Unmonitored albums:** + +If a deleted album is unmonitored in Lidarr, it won't be auto-searched. In interactive delete, you'll be prompted: + +``` +2 deleted albums are unmonitored in Lidarr. Queue search? [y/n] +``` + +With `LIDARR_SEARCH=true` and auto-delete, unmonitored album IDs are written to a persistent search queue (`search_queue.json`). The container drains this queue during idle — one album at a time, rate limited to 5/hour to avoid flooding indexers. + +**Path handling:** + +BeatsCheck runs in a container where music is mounted at `/data`, while Lidarr sees the same files at the same or different mount path. When both containers mount the same host path to `/data`, the paths match exactly. At scan time, corrupt files are matched to Lidarr trackfiles by comparing path components from the right (suffix matching). This works regardless of how the music directory is mounted in each container. + +**Error handling:** + +All Lidarr API operations are fail-safe: +- If blocklist fails → delete is aborted, error shown +- If bulk delete API fails → delete is aborted, error shown +- If Lidarr is unreachable at scan time → files are tracked without Lidarr IDs, direct filesystem delete is used as fallback +- Files not tracked by Lidarr (cover art, .nfo, etc.) are deleted directly via `os.remove` + +**Security:** +- API key is sent only via HTTP header, never in URLs or logs +- Lidarr URL is masked in all log output +- Config file support — store the API key in `/config/beatscheck.conf` to keep it out of `docker inspect` and process listings +- Also supports Docker secrets (`/run/secrets/lidarr_api_key`) +- HTTP redirects are blocked to prevent credential leaking +- All API calls have explicit timeouts + +```conf +## /config/beatscheck.conf — API key stays off the command line +lidarr_url = "http://lidarr:8686" +lidarr_api_key = "your-api-key-here" +lidarr_search = true +lidarr_blocklist = true +``` + +## Updating + +```bash +docker pull ghcr.io/chodeus/beatscheck:latest +``` + +Then restart the container. + +## Security + +See [SECURITY.md](.github/SECURITY.md) for the security policy, vulnerability reporting, and container security measures. + +## License + +[MIT](LICENSE) diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..9a1641b --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.3.4 # x-release-please-version diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..58362a1 --- /dev/null +++ b/app/main.py @@ -0,0 +1,3522 @@ +import concurrent.futures +import fcntl +import json +import logging +import logging.handlers +import os +import re +import shutil +import signal +import subprocess +import sys +import time +import types +import urllib.error +import urllib.request +from concurrent.futures import ThreadPoolExecutor, as_completed + + +def _read_version(): + here = os.path.dirname(os.path.abspath(__file__)) + for path in (os.path.join(here, "VERSION"), + os.path.join(here, "..", "VERSION")): + try: + with open(path, "r", encoding="utf-8") as f: + for line in f: + v = line.split("#", 1)[0].strip() + if v: + return v + except OSError: + continue + return "dev" + + +__version__ = _read_version() + +AUDIO_EXTENSIONS = { + '.flac', '.mp3', '.m4a', '.ogg', '.opus', '.wav', + '.wma', '.aac', '.aiff', '.aif', '.ape', '.wv', + '.alac', '.m4b', '.m4p', '.mp2', '.mpc', '.dsf', '.dff', +} + +shutdown_requested = False +scan_cancelled = False +logger = logging.getLogger("beatscheck") + + +# --- Utilities --- + +def handle_shutdown(signum, frame): + global shutdown_requested + shutdown_requested = True + logger.info("Shutdown requested, finishing in-progress files...") + + +def cancel_scan(): + """Cancel the current scan. Called from WebUI.""" + global scan_cancelled + scan_cancelled = True + logger.info("Scan cancel requested.") + + +def _decode_mountinfo_path(path): + """Decode octal escapes in /proc/self/mountinfo paths (\\040 -> space).""" + return re.sub(r'\\([0-7]{3})', + lambda m: chr(int(m.group(1), 8)), path) + + +def _get_host_mount_path(container_path): + """Resolve the host bind-mount path for a container mount point + by reading /proc/self/mountinfo. Returns the host path or None.""" + real = os.path.realpath(container_path) + try: + with open("/proc/self/mountinfo", "r") as f: + best = None + for line in f: + parts = line.split() + if len(parts) < 5: + continue + mount_point = _decode_mountinfo_path(parts[4]) + mount_root = _decode_mountinfo_path(parts[3]) + try: + sep = parts.index("-") + if sep + 2 < len(parts): + mount_source = _decode_mountinfo_path( + parts[sep + 2]) + else: + continue + except (ValueError, IndexError): + continue + if real == mount_point or real.startswith(mount_point + "/"): + if best is None or len(mount_point) > len(best[0]): + best = (mount_point, mount_source, mount_root) + if best: + mount_point, source, root = best + # For Docker bind mounts, source is the host path + # For Unraid shfs/fuse, source is the fs name — + # use mount root which has the subpath + if source.startswith("/"): + rel = real[len(mount_point):].lstrip("/") + if root and root != "/": + return os.path.join( + source, root.lstrip("/"), rel) + return os.path.join(source, rel) if rel else source + elif root and root != "/": + # Unraid: root has the path (e.g. /data/media/music) + return root + except OSError: + pass + return None + + +def format_size(bytes_val): + if bytes_val >= 1024 ** 4: + return f"{bytes_val / 1024 ** 4:.1f} TB" + if bytes_val >= 1024 ** 3: + return f"{bytes_val / 1024 ** 3:.1f} GB" + if bytes_val >= 1024 ** 2: + return f"{bytes_val / 1024 ** 2:.1f} MB" + if bytes_val >= 1024: + return f"{bytes_val / 1024:.1f} KB" + return f"{bytes_val} B" + + +def format_eta(seconds): + if seconds < 0: + return "unknown" + h, remainder = divmod(int(seconds), 3600) + m, s = divmod(remainder, 60) + if h > 0: + return f"{h}h{m:02d}m" + if m > 0: + return f"{m}m{s:02d}s" + return f"{s}s" + + +# --- File I/O --- + +def write_json_atomic(path, data): + """Write JSON data atomically using a temp file + rename.""" + tmp_path = path + ".tmp" + with open(tmp_path, 'w', encoding='utf-8') as f: + json.dump(data, f, indent=2, sort_keys=True) + os.rename(tmp_path, path) + + +def write_text_atomic(path, text): + """Write text atomically using a temp file + rename.""" + tmp_path = path + ".tmp" + with open(tmp_path, 'w', encoding='utf-8') as f: + f.write(text) + os.rename(tmp_path, path) + + +def _migrate_corrupt_details(details): + """Normalize legacy string values to {'reason': str}. Mutates in place.""" + for k, v in list(details.items()): + if isinstance(v, str): + details[k] = {"reason": v} + return details + + +def _load_corrupt_details(log_dir): + """Load and migrate corrupt_details.json.""" + return _migrate_corrupt_details( + _load_json(os.path.join(log_dir, "corrupt_details.json"))) + + +def _load_json(path, default=None): + """Load a JSON file, returning default if missing or invalid.""" + if default is None: + default = {} + if not os.path.exists(path): + return default + try: + with open(path, 'r', encoding='utf-8') as f: + return json.load(f) + except (json.JSONDecodeError, ValueError): + logger.warning("Corrupt JSON file %s — using defaults", path) + return default + + +def _load_lines_as_set(path): + """Load a text file as a set of non-empty stripped lines.""" + if not os.path.exists(path): + return set() + with open(path, 'r', encoding='utf-8') as f: + return {line.strip() for line in f if line.strip()} + + +def delete_corrupt_files(paths, config_dir, music_dir=None, + progress_cb=None, cancel_cb=None): + """Delete corrupt files with Lidarr support and full state cleanup. + + Validates paths, routes Lidarr-tracked files through the Lidarr API, + and updates corrupt.txt, corrupt_details.json, and corrupt_tracking.json. + + Args: + paths: List of file paths to delete. + config_dir: Directory containing state files. + music_dir: If set, rejects paths outside this directory. + progress_cb: Optional ``(index, total, label, phase)`` callback + invoked once per album; used by the WebUI progress modal. + cancel_cb: Optional ``() -> bool`` callback; when it returns + True the loop aborts between albums. + + Returns: + dict with 'deleted' (list), 'errors' (list of dicts), 'count' (int). + """ + corrupt_path = os.path.join(config_dir, "corrupt.txt") + details_path = os.path.join(config_dir, "corrupt_details.json") + tracking_path = os.path.join(config_dir, "corrupt_tracking.json") + log_file = os.path.join(config_dir, "beats_check.log") + + allowed = _load_lines_as_set(corrupt_path) + + # Resolve music_dir realpath once for containment checks + music_real = os.path.realpath(music_dir) if music_dir else None + + # Validate all paths first — store {original: realpath} for safe ops + validated = {} + errors = [] + for fp in paths: + if fp not in allowed: + errors.append({"path": fp, "error": "not in corrupt list"}) + continue + if os.path.islink(fp): + errors.append({"path": fp, "error": "symlink rejected"}) + continue + real = os.path.realpath(fp) + if music_real and not real.startswith(music_real + os.sep): + errors.append({"path": fp, "error": "outside music directory"}) + continue + if not os.path.isfile(real): + errors.append({"path": fp, "error": "not found"}) + continue + validated[fp] = real + + if not validated: + return {"deleted": [], "errors": errors, "count": 0} + + lidarr_url = os.environ.get("LIDARR_URL", "").rstrip("/") + lidarr_key = _load_lidarr_api_key() + lidarr_blocklist = _parse_env_bool("LIDARR_BLOCKLIST", False) + details = _load_corrupt_details(config_dir) + + lidarr_paths = [] + direct_paths = [] + for fp in validated: + detail = details.get(fp, {}) + if (lidarr_url and lidarr_key + and "trackfileId" in detail): + lidarr_paths.append(fp) + else: + direct_paths.append(fp) + + deleted = [] + + # Delete Lidarr-tracked files via API, waiting between albums + # so we don't flood Lidarr/indexers with simultaneous searches + if lidarr_paths: + count, _ = _lidarr_delete_corrupt( + lidarr_url, lidarr_key, lidarr_paths, log_file, + log_dir=config_dir, blocklist=lidarr_blocklist, + search=True, progress_cb=progress_cb, + cancel_cb=cancel_cb) + if count > 0: + # Check which files were actually removed + for fp in lidarr_paths: + if not os.path.exists(validated[fp]): + deleted.append(fp) + else: + errors.append({"path": fp, + "error": "Lidarr delete failed"}) + else: + # API reported failure but may have partially succeeded + for fp in lidarr_paths: + if not os.path.exists(validated[fp]): + deleted.append(fp) + else: + errors.append( + {"path": fp, + "error": "Lidarr API delete failed"}) + + for fp in direct_paths: + try: + os.remove(validated[fp]) + deleted.append(fp) + except OSError as e: + errors.append({"path": fp, "error": str(e)}) + + if deleted: + # Capture album IDs before pruning details + affected_album_ids = { + details.get(fp, {}).get("albumId") + for fp in deleted + if details.get(fp, {}).get("albumId") + } + + deleted_set = set(deleted) + remaining = sorted(allowed - deleted_set) + # Rewrite corrupt.txt atomically + tmp = corrupt_path + ".tmp" + try: + with open(tmp, 'w', encoding='utf-8') as f: + for p in remaining: + f.write(p + '\n') + os.rename(tmp, corrupt_path) + except OSError: + pass + + # Remove deleted entries from corrupt_details.json + for fp in deleted: + details.pop(fp, None) + write_json_atomic(details_path, details) + + # Remove deleted entries from corrupt_tracking.json + tracking = _load_json(tracking_path) + for fp in deleted: + tracking.pop(fp, None) + write_json_atomic(tracking_path, tracking) + + if affected_album_ids and lidarr_url and lidarr_key: + _record_pending_redownloads( + config_dir, list(affected_album_ids), + lidarr_url, lidarr_key) + + return {"deleted": deleted, "errors": errors, "count": len(deleted)} + + +# --- Pending re-download tracker --- + +PENDING_REDOWNLOADS_FILE = "pending_redownloads.json" +_PENDING_REDOWNLOAD_TTL = 86400 + + +def _pending_redownloads_path(config_dir): + return os.path.join(config_dir, PENDING_REDOWNLOADS_FILE) + + +def _record_pending_redownloads(config_dir, album_ids, + lidarr_url, lidarr_api_key): + """Track album IDs affected by a Lidarr-backed delete so the next + scan cycle can detect grabbed re-downloads via Lidarr history.""" + if not album_ids: + return + path = _pending_redownloads_path(config_dir) + pending = _load_json(path, default={}) + if not isinstance(pending, dict): + pending = {} + now_iso = time.strftime('%Y-%m-%dT%H:%M:%S') + now_ts = time.time() + for aid in album_ids: + key = str(aid) + entry = pending.get(key, {}) + entry["deletedAt"] = now_iso + entry["deletedAtTs"] = now_ts + if "albumName" not in entry and lidarr_url and lidarr_api_key: + album = _lidarr_get_album(lidarr_url, lidarr_api_key, aid) + if album: + title = album.get("title", "") + artist_obj = album.get("artist", {}) or {} + artist = artist_obj.get("artistName", "") + entry["albumName"] = ( + f"{artist} — {title}" + if artist and title else title or key) + entry["monitored"] = album.get("monitored", True) + pending[key] = entry + try: + write_json_atomic(path, pending) + except OSError: + pass + + +def _poll_pending_redownloads(cfg): + """Check Lidarr history for pending album re-downloads. + Logs grabs to beats_check.log and removes resolved/stale entries. + Called at the top of each scan cycle.""" + if not (cfg.lidarr_url and cfg.lidarr_api_key): + return + path = _pending_redownloads_path(cfg.log_dir) + pending = _load_json(path, default={}) + if not isinstance(pending, dict) or not pending: + return + now_ts = time.time() + changed = False + for aid_str in list(pending.keys()): + entry = pending[aid_str] + try: + aid = int(aid_str) + except (ValueError, TypeError): + pending.pop(aid_str, None) + changed = True + continue + deleted_ts = entry.get("deletedAtTs", 0) + label = entry.get("albumName", aid_str) + if now_ts - deleted_ts > _PENDING_REDOWNLOAD_TTL: + logger.info( + "LIDARR REDOWNLOAD: %s — no grab detected within 24h", + label) + try: + with open(cfg.log_file, 'a', encoding='utf-8') as lf: + lf.write( + f"LIDARR REDOWNLOAD TIMEOUT: {label} " + f"— no grab within 24h\n") + except OSError: + pass + pending.pop(aid_str, None) + changed = True + continue + records = _lidarr_get_album_history( + cfg.lidarr_url, cfg.lidarr_api_key, aid) + if not records: + continue + record = records[0] + rec_date = record.get("date", "") + try: + rec_ts = time.mktime(time.strptime( + rec_date[:19], '%Y-%m-%dT%H:%M:%S')) + except (ValueError, TypeError): + rec_ts = 0 + if rec_ts <= deleted_ts: + continue + source = (record.get("sourceTitle") + or (record.get("data") or {}).get("releaseTitle") + or "") + msg = (f"LIDARR REDOWNLOAD: {label} — " + f"grabbed (1 report)" + + (f" [{source}]" if source else "")) + logger.info(msg) + try: + with open(cfg.log_file, 'a', encoding='utf-8') as lf: + lf.write(msg + "\n") + except OSError: + pass + pending.pop(aid_str, None) + changed = True + if changed: + try: + write_json_atomic(path, pending) + except OSError: + pass + + +# --- Whole-album delete (invoked from the WebUI) --- + +_MAX_DELETE_ALBUMS = 50 +_LIDARR_ALBUM_DELAY_SECONDS = 30 + + +def _list_folder_files(real): + """Return top-level regular-file paths in *real* (excluding symlinks).""" + try: + return [ + os.path.join(real, e) for e in os.listdir(real) + if os.path.isfile(os.path.join(real, e)) + and not os.path.islink(os.path.join(real, e)) + ] + except OSError: + return None + + +def _build_file_to_tfid(all_files, album_ids, corrupt_details, + lidarr_url, lidarr_key): + """Map folder files to Lidarr trackfile IDs via corrupt_details + + suffix-match fallback. Mutates *album_ids* with any new matches.""" + file_to_tfid = { + fp: corrupt_details[fp]["trackfileId"] + for fp in all_files + if "trackfileId" in corrupt_details.get(fp, {}) + } + unmatched = [fp for fp in all_files if fp not in file_to_tfid] + if not unmatched or not album_ids: + return file_to_tfid + album_tfs = [] + for aid in album_ids: + album_tfs.extend(_lidarr_get_trackfiles_by_album( + lidarr_url, lidarr_key, aid)) + for fp in unmatched: + match = _best_suffix_match(fp, album_tfs, min_score=1) + if match: + file_to_tfid[fp] = match["id"] + if match.get("albumId"): + album_ids.add(match["albumId"]) + return file_to_tfid + + +def _lidarr_delete_folder_api(folder, file_to_tfid, album_ids, + lidarr_url, lidarr_key, log, + file_to_album=None, log_dir=None): + """Delete *file_to_tfid* one album at a time, waiting up to 30s + after each album for Lidarr to report whether it grabbed a + replacement release. The wait both paces indexer requests and + produces the search-result log line per album. Emits the same + ``[i/total] label: ...`` log shape as the file-level delete path. + Returns (deleted_paths, error_or_None).""" + if not file_to_tfid: + return ([], None) + + per_album = {} + for fp, tfid in file_to_tfid.items(): + aid = (file_to_album or {}).get(fp, 0) + per_album.setdefault(aid, []).append((fp, tfid)) + + deleted_paths = [] + total = len(per_album) + for i, (aid, entries) in enumerate(per_album.items(), 1): + tfids = [e[1] for e in entries] + album_info = _lidarr_get_album( + lidarr_url, lidarr_key, aid) if aid else None + label = _format_album_label(album_info, aid, folder) + prefix = f" [{i}/{total}] {label}" + + max_search_id = _snapshot_search_id(lidarr_url, lidarr_key) + result = _lidarr_delete_trackfiles_bulk( + lidarr_url, lidarr_key, tfids) + if result is None: + logger.error("%s: Lidarr API delete failed", prefix) + log.write(f"ERROR: Lidarr delete failed for {label}\n") + log.flush() + return (deleted_paths, "Lidarr bulk delete API failed") + + log.write(f"LIDARR DELETE: {label} — " + f"{len(tfids)} track files\n") + log.flush() + deleted_paths.extend( + fp for fp, _ in entries if not os.path.exists(fp)) + + _log_delete_and_search( + lidarr_url, lidarr_key, album_info, log_dir, + prefix, len(tfids), True, max_search_id, + max_wait=30) + + return (deleted_paths, None) + + +def _format_album_label(album_info, album_id, fallback): + """Render ``Artist — Album`` when album_info is available, else + fall back to album_id or the provided fallback string.""" + if album_info: + album_title = album_info.get("title", "") + artist = (album_info.get("artist") or {}).get( + "artistName", "") + if artist and album_title: + return f"{artist} — {album_title}" + if album_title: + return album_title + if album_id: + return f"album {album_id}" + return fallback + + +def _snapshot_search_id(lidarr_url, lidarr_key): + """Snapshot the highest AlbumSearch command ID currently known.""" + cmds = _lidarr_request( + f"{lidarr_url}/api/v1/command", lidarr_key) + if not cmds: + return 0 + return max( + (c.get("id", 0) for c in cmds + if c.get("name") == "AlbumSearch"), + default=0, + ) + + +def _rmtree_and_collect(real, folder, log, deleted_paths): + """Walk *real* to record file paths then rmtree the folder. + Returns (deleted_paths, error_or_None). Caller is responsible + for ensuring *real* is safe to remove (not a mount root).""" + for dirpath, _, filenames in os.walk(real): + for fn in filenames: + fp = os.path.join(dirpath, fn) + if os.path.exists(fp) and fp not in deleted_paths: + deleted_paths.append(fp) + try: + shutil.rmtree(real) + log.write(f"REMOVED FOLDER: {folder}\n") + log.flush() + except OSError as e: + log.write(f"ERROR removing folder {folder}: {e}\n") + return (deleted_paths, f"rmtree failed: {e}") + return (deleted_paths, None) + + +def _direct_delete_untracked(all_files, deleted_paths, + corrupt_details, lidarr_url, + lidarr_key, log): + """Direct FS delete for files not already handled by Lidarr.""" + for fp in all_files: + if fp in deleted_paths: + continue + if (lidarr_url and lidarr_key + and "trackfileId" in corrupt_details.get(fp, {})): + continue + try: + os.remove(fp) + deleted_paths.append(fp) + log.write(f"DELETED FILE: {fp}\n") + except OSError as e: + log.write(f"ERROR deleting {fp}: {e}\n") + log.flush() + + +def _delete_one_folder(folder, real, existing_corrupt, corrupt_details, + log, lidarr_url, lidarr_key, lidarr_blocklist, + mode, music_real): + """Delete a single album folder. Returns + (deleted_paths, album_ids, error_message_or_None).""" + if mode == "whole": + all_files = _list_folder_files(real) + if all_files is None: + return ([], [], "listdir failed") + else: + all_files = [cf for cf in existing_corrupt + if os.path.dirname(cf) == folder + and os.path.exists(cf)] + if not all_files: + return ([], [], None) + + album_ids = { + corrupt_details[cf]["albumId"] + for cf in existing_corrupt + if corrupt_details.get(cf, {}).get("albumId") + } + + deleted_paths = [] + if lidarr_url and lidarr_key: + file_to_tfid = _build_file_to_tfid( + all_files, album_ids, corrupt_details, + lidarr_url, lidarr_key) + file_to_album = { + fp: corrupt_details[fp]["albumId"] + for fp in file_to_tfid + if corrupt_details.get(fp, {}).get("albumId") + } + if file_to_tfid and lidarr_blocklist and album_ids: + _, bl_fail = _lidarr_blocklist_albums( + lidarr_url, lidarr_key, album_ids) + if bl_fail: + return (deleted_paths, list(album_ids), + f"blocklist failed ({bl_fail} albums)") + log_dir = os.path.dirname(log.name) if hasattr(log, "name") else None + api_deleted, err = _lidarr_delete_folder_api( + folder, file_to_tfid, album_ids, + lidarr_url, lidarr_key, log, + file_to_album=file_to_album, log_dir=log_dir) + deleted_paths.extend(api_deleted) + if err: + return (deleted_paths, list(album_ids), err) + + if mode == "whole" and real != music_real and os.path.isdir(real): + deleted_paths, err = _rmtree_and_collect( + real, folder, log, deleted_paths) + if err: + return (deleted_paths, list(album_ids), err) + elif mode == "corrupt": + _direct_delete_untracked( + all_files, deleted_paths, corrupt_details, + lidarr_url, lidarr_key, log) + + return (deleted_paths, list(album_ids), None) + + +def _validate_delete_folders(folders, music_real): + """Return (validated=[(folder, real)], errors=[...]) after safety + checks: mount-root guard, MUSIC_DIR containment, symlink guard, + is-a-directory.""" + validated = [] + errors = [] + for folder in folders: + real = os.path.realpath(folder) + if music_real and real == music_real: + errors.append({"folder": folder, + "error": "cannot delete mount root"}) + continue + if music_real and not real.startswith(music_real + os.sep): + errors.append({"folder": folder, + "error": "outside music directory"}) + continue + if os.path.islink(folder): + errors.append({"folder": folder, + "error": "symlink rejected"}) + continue + if not os.path.isdir(real): + errors.append({"folder": folder, + "error": "not a directory"}) + continue + validated.append((folder, real)) + return validated, errors + + +def _stagger_sleep(cancel_cb): + """Sleep the inter-album delay, checking *cancel_cb* every second. + Returns True if cancellation was requested.""" + for _ in range(_LIDARR_ALBUM_DELAY_SECONDS): + if cancel_cb and cancel_cb(): + return True + time.sleep(1) + return bool(cancel_cb and cancel_cb()) + + +def _finalize_delete_state(config_dir, deleted_files, + corrupt_details): + """After a bulk delete, strip *deleted_files* from corrupt.txt, + corrupt_details.json, and corrupt_tracking.json.""" + if not deleted_files: + return + corrupt_path = os.path.join(config_dir, "corrupt.txt") + details_path = os.path.join(config_dir, "corrupt_details.json") + tracking_path = os.path.join(config_dir, "corrupt_tracking.json") + allowed_corrupt = _load_lines_as_set(corrupt_path) + deleted_set = set(deleted_files) + remaining = sorted(allowed_corrupt - deleted_set) + tmp = corrupt_path + ".tmp" + try: + with open(tmp, 'w', encoding='utf-8') as f: + for p in remaining: + f.write(p + '\n') + os.rename(tmp, corrupt_path) + except OSError: + pass + for fp in deleted_files: + corrupt_details.pop(fp, None) + write_json_atomic(details_path, corrupt_details) + tracking = _load_json(tracking_path) + for fp in deleted_files: + tracking.pop(fp, None) + write_json_atomic(tracking_path, tracking) + + +def delete_album_folders(folders, config_dir, music_dir=None, + mode="whole", progress_cb=None, + cancel_cb=None): + """Delete album folders with staggered Lidarr deletion. + + mode='whole' removes the entire folder (Lidarr-tracked files go + through the API, then shutil.rmtree handles non-tracked files and + subdirectories). mode='corrupt' only removes files currently in + corrupt.txt for the folder. + + Per-album pacing (wait up to 30s for Lidarr's post-delete search + to report) happens inside ``_lidarr_delete_folder_api``. + progress_cb(index, total, folder, phase) reports status; + cancel_cb() returning True aborts between folders. + + Returns dict: {deleted, errors, count, albums, cancelled}. + """ + if mode not in ("whole", "corrupt"): + return {"deleted": [], "errors": [ + {"folder": "", "error": "invalid mode"}], + "count": 0, "albums": [], "cancelled": False} + if len(folders) > _MAX_DELETE_ALBUMS: + return {"deleted": [], "errors": [ + {"folder": "", + "error": f"too many folders (>{_MAX_DELETE_ALBUMS})"}], + "count": 0, "albums": [], "cancelled": False} + + music_real = os.path.realpath(music_dir) if music_dir else None + log_file = os.path.join(config_dir, "beats_check.log") + + validated, errors = _validate_delete_folders(folders, music_real) + if not validated: + return {"deleted": [], "errors": errors, "count": 0, + "albums": [], "cancelled": False} + + lidarr_url = os.environ.get("LIDARR_URL", "").rstrip("/") + lidarr_key = _load_lidarr_api_key() + lidarr_blocklist = _parse_env_bool("LIDARR_BLOCKLIST", False) + corrupt_details = _load_corrupt_details(config_dir) + + # mode='whole' wipes the entire folder, so restrict it to folders that + # actually contain a file flagged in corrupt.txt (the canonical delete + # allowlist). Without this a mis-driven UI / authenticated request could + # rmtree any album folder under the rw-mounted MUSIC_DIR. + corrupt_dirs = { + os.path.dirname(cf) + for cf in _load_lines_as_set( + os.path.join(config_dir, "corrupt.txt")) + } + + deleted_files = [] + affected_albums = [] + cancelled = False + total = len(validated) + + with open(log_file, 'a', encoding='utf-8') as log: + log.write(f"\n{'='*60}\n") + log.write(f"Bulk delete ({mode}) started: " + f"{time.strftime('%Y-%m-%d %H:%M:%S')} " + f"— {total} folders\n") + log.write(f"{'='*60}\n") + log.flush() + + for i, (folder, real) in enumerate(validated, 1): + if (cancel_cb and cancel_cb()) or shutdown_requested: + cancelled = True + log.write(f"CANCELLED at folder {i}/{total}\n") + break + + if mode == "whole" and folder not in corrupt_dirs: + log.write( + f"REFUSED whole-delete (no flagged corrupt file): " + f"{folder}\n") + errors.append({ + "folder": folder, + "error": "no flagged corrupt files in folder"}) + continue + + if progress_cb: + progress_cb(i, total, folder, "deleting") + + existing_corrupt = [ + cf for cf in corrupt_details + if os.path.dirname(cf) == folder + and os.path.exists(cf) + ] + + del_files, album_ids, err = _delete_one_folder( + folder, real, existing_corrupt, corrupt_details, + log, lidarr_url, lidarr_key, lidarr_blocklist, + mode, music_real) + deleted_files.extend(del_files) + affected_albums.extend(album_ids) + if err: + errors.append({"folder": folder, "error": err}) + + log.write(f"Bulk delete ({mode}) complete: " + f"{len(deleted_files)} files, " + f"{len(affected_albums)} albums\n") + log.flush() + + _finalize_delete_state(config_dir, deleted_files, corrupt_details) + + if affected_albums and lidarr_url and lidarr_key: + _record_pending_redownloads( + config_dir, affected_albums, lidarr_url, lidarr_key) + + return {"deleted": deleted_files, "errors": errors, + "count": len(deleted_files), + "albums": affected_albums, "cancelled": cancelled} + + +def _rotate_file(path, keep=3): + """Rotate path -> path.1 -> path.2 -> ... keeping last N copies.""" + oldest = f"{path}.{keep}" + if os.path.exists(oldest): + os.remove(oldest) + for i in range(keep - 1, 0, -1): + src = f"{path}.{i}" + dst = f"{path}.{i + 1}" + if os.path.exists(src): + shutil.move(src, dst) + if os.path.exists(path): + shutil.move(path, f"{path}.1") + + +def _total_file_size(files): + """Sum file sizes, ignoring missing files.""" + total = 0 + for f in files: + try: + total += os.path.getsize(f) + except OSError: + pass + return total + + +# --- Heartbeat & idle --- + +def _write_heartbeat(heartbeat_path): + """Write current timestamp to heartbeat file for healthcheck.""" + try: + with open(heartbeat_path, 'w') as f: + f.write(str(int(time.time()))) + except OSError: + pass + + +def _read_rescan_trigger(log_dir): + """Check for .rescan file. Returns mode override string or empty string.""" + rescan_path = os.path.join(log_dir, ".rescan") + if not os.path.exists(rescan_path): + return None + try: + with open(rescan_path, 'r') as f: + content = f.read().strip() + os.remove(rescan_path) + except OSError: + content = "" + return content + + +def _apply_rescan_trigger(trigger, cfg): + """Parse a rescan trigger string and update *cfg* in place. + Handles the ``fresh:`` prefix written by the WebUI when the user + requests a fresh (non-resuming) scan. Returns True if *trigger* + contained a valid mode override, False otherwise.""" + if not isinstance(trigger, str) or not trigger: + return False + if trigger.startswith("fresh:"): + cfg.fresh_rescan = True + trigger = trigger[len("fresh:"):] + if trigger in ("report", "move"): + cfg.mode = trigger + return True + return False + + +def _clear_resume_if_fresh(cfg): + """If a fresh rescan was requested, delete processed.txt now + (at scan start) and reset the flag.""" + if not cfg.fresh_rescan: + return + processed = os.path.join(cfg.log_dir, "processed.txt") + try: + os.remove(processed) + logger.info("Fresh rescan: cleared resume cache.") + except OSError: + pass + cfg.fresh_rescan = False + + +def _idle_wait(log_dir, timeout_seconds, lidarr_url=None, lidarr_api_key=None): + """Sleep until timeout, shutdown, or .rescan trigger. + Drains the Lidarr search queue during idle (max 5/hour). + Returns the raw trigger string when a rescan is requested (may be empty + or include a 'fresh:' prefix); returns None on timeout or shutdown.""" + heartbeat_path = os.path.join(log_dir, ".heartbeat") + deadline = time.time() + timeout_seconds if timeout_seconds is not None else None + last_search_time = 0 + while not shutdown_requested: + _write_heartbeat(heartbeat_path) + trigger = _read_rescan_trigger(log_dir) + if trigger is not None: + logger.info("Rescan requested.") + return trigger + if deadline and time.time() >= deadline: + return None + if (lidarr_url and lidarr_api_key + and time.time() - last_search_time >= 720): + if _search_queue_drain_one(log_dir, lidarr_url, lidarr_api_key): + last_search_time = time.time() + time.sleep(30) + return None + + +# --- Scan lock --- + +def _acquire_scan_lock(log_dir): + """Acquire an exclusive file lock for scanning. Returns the lock fd.""" + lock_path = os.path.join(log_dir, ".scanning") + lf = open(lock_path, 'w') + try: + fcntl.flock(lf.fileno(), fcntl.LOCK_EX) + except OSError: + lf.close() + raise + lf.write(str(os.getpid())) + lf.flush() + return lf + + +def _wait_for_scan_lock(log_dir): + """Block until the scan lock is available, then release immediately.""" + lock_path = os.path.join(log_dir, ".scanning") + if not os.path.exists(lock_path): + return + try: + with open(lock_path, 'r') as lf: + fcntl.flock(lf.fileno(), fcntl.LOCK_SH) + except OSError: + pass + + +# --- Scanning --- + +def _clean_ffmpeg_errors(stderr): + """Strip ffmpeg memory addresses and internal codec references from errors.""" + if not stderr or not stderr.strip(): + return "Non-zero exit code" + # Strip [codec @ 0x...] prefixes and [aist#N:N/codec @ 0x...] wrappers + cleaned = re.sub(r'\[[\w:#/]+ @ 0x[0-9a-f]+\]\s*', '', stderr) + # Collapse duplicate whitespace and pipe separators + cleaned = re.sub(r'\s*\|\s*', ' | ', cleaned) + cleaned = re.sub(r'\s+', ' ', cleaned).strip() + # Deduplicate repeated error messages + parts = [p.strip() for p in cleaned.split(' | ') if p.strip()] + seen = set() + unique = [] + for p in parts: + if p not in seen: + seen.add(p) + unique.append(p) + return ' | '.join(unique) if unique else "Non-zero exit code" + + +# ID3v2 tag-parse errors ffmpeg logs at *error* level but recovers from by +# skipping the tag frame (bad UTF-16 BOM, unreadable comment/lyrics/text frame). +# A malformed metadata tag is never audio corruption. Older ffmpeg (4.x-6.x) +# prints these at error level; 8.x no longer does. From libavformat/id3v2.c. +_BENIGN_METADATA_MARKERS = ( + "Incorrect BOM value", + "Cannot read BOM value", + "Error reading comment frame", + "Error reading lyrics", + "Error reading frame", + # Cover-art block, not audio (libavformat/flac_picture.c). Without + # -err_detect explode the demuxer skips the picture and decodes normally. + "Could not read mimetype from an attached picture", + "Error parsing attached picture", +) + +_ATTACHED_PICTURE_MARKERS = ( + "Could not read mimetype from an attached picture", + "Error parsing attached picture", +) + + +def _is_attached_picture_failure(stderr): + """True when ffmpeg failed parsing an embedded cover-art block. Under + -err_detect explode that aborts input open BEFORE -map 0:a excludes the + picture, so the audio was never judged — mirrors sleezer's scanner.""" + return bool(stderr) and any(m in stderr for m in _ATTACHED_PICTURE_MARKERS) + + +def _is_benign_metadata_noise(line): + """True if an ffmpeg stderr line is an ID3 tag-parse error the demuxer + recovered from by skipping the tag (a metadata defect, not corruption).""" + return any(marker in line for marker in _BENIGN_METADATA_MARKERS) + + +def _strip_benign_metadata_noise(stderr): + """Return stderr with benign ID3 tag-parse lines removed. Empty result means + ffmpeg emitted only metadata noise and the decode is clean; real decoder + errors (Header missing, Invalid data, backstep) are kept so we fail closed.""" + if not stderr or not stderr.strip(): + return "" + kept = [ln for ln in stderr.splitlines() + if ln.strip() and not _is_benign_metadata_noise(ln)] + return "\n".join(kept).strip() + + +def _ffmpeg_decode(file_path, explode=True): + """Run the null-decode. Returns the CompletedProcess, or None on timeout.""" + cmd = ["ffmpeg", "-v", "error"] + if explode: + cmd += ["-err_detect", "explode"] + cmd += ["-xerror", "-nostdin", "-i", file_path, + "-map", "0:a", "-f", "null", "-"] + try: + return subprocess.run( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + timeout=600, + ) + except subprocess.TimeoutExpired: + return None + + +def _recheck_past_attached_picture(file_path, file_size): + """Second decode without explode so the demuxer skips the malformed + picture and the AUDIO is judged on its own. Still fails closed — any + non-picture error in this pass is corruption.""" + result = _ffmpeg_decode(file_path, explode=False) + if result is None: + return (file_path, True, "Decode timed out (>10 minutes)", file_size) + + significant = _strip_benign_metadata_noise(result.stderr) + if result.returncode != 0 or significant: + return (file_path, True, + _clean_ffmpeg_errors(significant or result.stderr), file_size) + + logger.info("Malformed embedded cover art but audio decodes clean, " + "keeping: %s", file_path) + return (file_path, False, None, file_size) + + +def check_audio_file(file_path): + """Decode-test a single audio file. Pure function — no shared state. + Returns (file_path, is_corrupt, reason, size).""" + try: + file_size = os.path.getsize(file_path) + if file_size < 1024: + return (file_path, True, + f"File too small ({file_size} bytes)", file_size) + except OSError as e: + return (file_path, True, f"File not accessible: {e}", 0) + + result = _ffmpeg_decode(file_path) + if result is None: + return (file_path, True, "Decode timed out (>10 minutes)", file_size) + + if result.returncode != 0: + # A malformed cover-art block aborts input open under explode before + # -map 0:a excludes the picture — the audio was never judged. + if _is_attached_picture_failure(result.stderr): + return _recheck_past_attached_picture(file_path, file_size) + return (file_path, True, + _clean_ffmpeg_errors(result.stderr), file_size) + + # Defence in depth: -xerror alone lets the decoder skip a bad frame and + # still exit 0; -err_detect explode makes it abort, but a skipped frame that + # never reaches the transcode loop can still exit 0 with error-level stderr. + # At -v error any leftover stderr is an error, so treat it as corrupt after + # stripping benign ID3 tag-parse noise (a bad tag is not bad audio). + significant = _strip_benign_metadata_noise(result.stderr) + if significant: + return (file_path, True, + _clean_ffmpeg_errors(significant), file_size) + + return (file_path, False, None, file_size) + + +def collect_audio_files(input_folder, min_age_minutes=30): + """Walk the input folder and return all audio file paths. + Does not follow symlinks to prevent traversal outside the music dir. + Skips files modified within min_age_minutes to avoid flagging files + being actively written by download clients.""" + real_root = os.path.realpath(input_folder) + age_threshold = time.time() - (min_age_minutes * 60) + files = [] + skipped_young = 0 + for root, dirs, filenames in os.walk(input_folder, followlinks=False): + dirs.sort() + for f in sorted(filenames): + file_path = os.path.join(root, f) + real_path = os.path.realpath(file_path) + if not real_path.startswith(real_root + os.sep) and real_path != real_root: + continue + if os.path.splitext(f)[1].lower() not in AUDIO_EXTENSIONS: + continue + try: + if os.path.getmtime(file_path) > age_threshold: + skipped_young += 1 + continue + except OSError: + continue + files.append(file_path) + if skipped_young > 0: + logger.debug( + "Skipped %d files modified within last %d minutes", + skipped_young, min_age_minutes + ) + return files + + +def _handle_corrupt_file(file_path, reason, mode, input_folder, output_folder, + corrupt_log, log, existing_corrupt, corrupt_details, + log_dir=None, lidarr_index=None): + logger.info("CORRUPT: %s", file_path) + logger.debug(" %s", reason) + + try: + nlinks = os.stat(file_path).st_nlink + if nlinks > 1: + logger.info(" hardlinked (%d links) — " + "other links share the same data", nlinks) + except OSError: + pass + + if file_path not in existing_corrupt: + corrupt_log.write(file_path + "\n") + corrupt_log.flush() + existing_corrupt.add(file_path) + corrupt_details[file_path] = {"reason": reason} + if lidarr_index: + _resolve_single_lidarr_id(file_path, corrupt_details, lidarr_index) + + # Write details incrementally so WebUI shows reasons in real time + if log_dir: + details_path = os.path.join(log_dir, "corrupt_details.json") + write_json_atomic(details_path, corrupt_details) + + if mode == "move": + rel = os.path.relpath(os.path.dirname(file_path), input_folder) + dest_dir = os.path.join(output_folder, rel) + os.makedirs(dest_dir, exist_ok=True) + dest = os.path.join(dest_dir, os.path.basename(file_path)) + try: + shutil.move(file_path, dest) + log.write(f"File moved: {file_path} -> {dest}\n") + log.flush() + logger.info(" moved -> %s", dest) + corrupt_details[dest] = {"reason": reason} + except (OSError, shutil.Error) as e: + log.write(f"ERROR: Failed to move {file_path}: {e}\n") + log.flush() + logger.error(" ERROR: failed to move: %s", e) + + +def _log_scan_banner(mode, workers, input_folder, output_folder, log_file, + corrupt_list_path, all_files, total_library_size, + total, skipped): + """Log the scan configuration banner.""" + logger.info("BeatsCheck v%s — %s mode, %d workers", __version__, mode, workers) + logger.info(" Library: %d files (%s), %d to scan (%d already processed)", + len(all_files), format_size(total_library_size), total, skipped) + host_path = _get_host_mount_path(input_folder) + if host_path: + logger.debug(" Music: %s (host: %s)", input_folder, host_path) + else: + logger.debug(" Music: %s", input_folder) + logger.debug(" Log: %s", log_file) + logger.debug(" Corrupt: %s", corrupt_list_path) + if mode == "move": + logger.debug(" Output: %s", output_folder) + if mode == "report": + logger.info(" (report mode - no files will be moved)") + + +def _finalize_scan(log_dir, corrupt_list_path, corrupt_details, + output_folder, scan_stats, + lidarr_url=None, lidarr_api_key=None, healed=None): + """Post-scan cleanup: update state files and write summary.""" + details_path = os.path.join(log_dir, "corrupt_details.json") + + # Write summary first so the dashboard shows accurate counts immediately + summary_path = os.path.join(log_dir, "summary.json") + write_json_atomic(summary_path, scan_stats) + + # Prune corrupt_details to existing files + moved files in output dir + output_real = os.path.realpath(output_folder) if output_folder else "" + corrupt_details = { + p: r for p, r in corrupt_details.items() + if os.path.exists(p) or (output_real and + os.path.realpath(p).startswith( + output_real + os.sep)) + } + + # Clean stale entries from corrupt.txt (files moved in move mode, deleted + # files, and paths re-scanned clean this run) + healed = healed or set() + current_corrupt = _load_lines_as_set(corrupt_list_path) + live_corrupt = [p for p in current_corrupt + if os.path.exists(p) and p not in healed] + tmp_corrupt = corrupt_list_path + ".tmp" + with open(tmp_corrupt, 'w', encoding='utf-8') as f: + for p in live_corrupt: + f.write(p + "\n") + os.rename(tmp_corrupt, corrupt_list_path) + + if lidarr_url and lidarr_api_key and corrupt_details: + _resolve_lidarr_ids(corrupt_details, lidarr_url, lidarr_api_key) + + write_json_atomic(details_path, corrupt_details) + + if scan_stats["corrupted"] > 0: + logger.info("Corrupt file list: %s", corrupt_list_path) + logger.info("Review with: cat %s", corrupt_list_path) + + +def _handle_nothing_to_do(log_dir, corrupt_list_path, output_folder, + all_files, total_library_size, mode): + """Fast path when every file has already been processed.""" + logger.debug("Nothing to do.") + _webui_app_state.update(status="idle", scan_progress=None) + existing_details = _load_corrupt_details(log_dir) + _finalize_scan(log_dir, corrupt_list_path, existing_details, + output_folder, { + "version": __version__, + "finished": time.strftime('%Y-%m-%d %H:%M:%S'), + "duration": "0s", + "library_files": len(all_files), + "library_size": total_library_size, + "library_size_human": format_size(total_library_size), + "files_checked": 0, + "corrupted": 0, + "corrupt_size": 0, + "corrupt_size_human": format_size(0), + "mode": mode, + }) + + +def _run_scan_inner(input_folder, output_folder, log_file, log_dir, + mode, workers, corrupt_list_path, min_age_minutes, + lidarr_url=None, lidarr_api_key=None): + """Inner scan logic.""" + already_processed = _load_lines_as_set(os.path.join(log_dir, "processed.txt")) + processed_path = os.path.join(log_dir, "processed.txt") + + logger.debug("Scanning for audio files...") + all_files = collect_audio_files(input_folder, min_age_minutes) + total_library_size = _total_file_size(all_files) + + _webui_app_state.update( + library_size_human=format_size(total_library_size), + library_files=len(all_files), + ) + + files_to_check = [f for f in all_files if f not in already_processed] + skipped = len(all_files) - len(files_to_check) + total = len(files_to_check) + + _log_scan_banner(mode, workers, input_folder, output_folder, log_file, + corrupt_list_path, all_files, total_library_size, + total, skipped) + + if total == 0: + _handle_nothing_to_do(log_dir, corrupt_list_path, output_folder, + all_files, total_library_size, mode) + return 0 + + # Pre-build Lidarr trackfile index so corrupt files get IDs immediately + lidarr_index = (_build_lidarr_index(lidarr_url, lidarr_api_key) + if lidarr_url and lidarr_api_key else None) + + checked = 0 + corrupted = 0 + corrupt_size = 0 + start_time = time.time() + + corrupt_details = _load_corrupt_details(log_dir) + existing_corrupt = _load_lines_as_set(corrupt_list_path) + healed = set() + + heartbeat_path = os.path.join(log_dir, ".heartbeat") + with open(log_file, 'a', encoding='utf-8') as log, \ + open(corrupt_list_path, 'a', encoding='utf-8') as corrupt_log, \ + open(processed_path, 'a', encoding='utf-8') as processed_log: + + log.write(f"\n{'='*60}\n") + log.write(f"Scan started: {time.strftime('%Y-%m-%d %H:%M:%S')}\n") + log.write(f"Mode: {mode} | Workers: {workers}\n") + log.write(f"Library: {len(all_files)} files ({format_size(total_library_size)})\n") + log.write(f"Files to check: {total} (skipped {skipped} already processed)\n") + log.write(f"{'='*60}\n") + log.flush() + + max_pending = workers * 4 + pending = {} + + def _process_future(future): + nonlocal checked, corrupted, corrupt_size + file_path = pending.pop(future) + try: + file_path, is_corrupt, reason, size = future.result() + except Exception as exc: + logger.exception("Unexpected error checking %s", + file_path) + log.write(f"ERROR: {file_path} - {exc}\n") + log.flush() + checked += 1 + return + + checked += 1 + + processed_log.write(file_path + "\n") + if is_corrupt: + log.write(f"CORRUPT: {file_path} - {reason}\n") + processed_log.flush() + log.flush() + + if is_corrupt: + corrupted += 1 + corrupt_size += size + _handle_corrupt_file( + file_path, reason, mode, input_folder, + output_folder, corrupt_log, log, + existing_corrupt, corrupt_details, + log_dir=log_dir, + lidarr_index=lidarr_index) + elif file_path in existing_corrupt: + # A fresh rescan found a previously-flagged file clean (e.g. a + # verdict recorded mid-tag-rewrite) — let the false positive + # age out instead of keeping it corrupt forever. + existing_corrupt.discard(file_path) + healed.add(file_path) + corrupt_details.pop(file_path, None) + log.write(f"HEALED: {file_path} - re-scanned clean\n") + log.flush() + logger.info("HEALED: %s (re-scanned clean, clearing " + "corrupt status)", file_path) + + _write_heartbeat(heartbeat_path) + + if checked % 100 == 0 or checked == total: + elapsed = time.time() - start_time + rate = checked / elapsed if elapsed > 0 else 0 + eta = (total - checked) / rate if rate > 0 else 0 + pct = checked * 100 // total + logger.info( + "[%d%%] %d/%d checked, %d corrupt, ETA %s", + pct, checked, total, corrupted, + format_eta(eta)) + + # Update WebUI progress (every 10 files to reduce overhead) + if checked % 10 == 0 or checked == total: + _webui_progress(checked, total, corrupted, file_path) + + with ThreadPoolExecutor(max_workers=workers) as pool: + for f in files_to_check: + if shutdown_requested or scan_cancelled: + remaining = sum(1 for ft in pending + if ft.running()) + logger.info( + "Cancelling — waiting for %d in-progress " + "file(s) to finish.", remaining) + pool.shutdown(wait=True, cancel_futures=True) + # Process results from completed futures + for fut in list(pending): + if fut.done() and not fut.cancelled(): + _process_future(fut) + break + pending[pool.submit(check_audio_file, f)] = f + # Drain completed futures to bound memory usage + while len(pending) >= max_pending: + done, _ = concurrent.futures.wait( + pending, return_when=concurrent.futures.FIRST_COMPLETED) + for fut in done: + _process_future(fut) + if shutdown_requested or scan_cancelled: + break + else: + # Normal completion — drain remaining futures + for future in as_completed(pending.copy()): + if shutdown_requested or scan_cancelled: + remaining = sum(1 for ft in pending + if ft.running()) + logger.info( + "Cancelling — waiting for %d " + "in-progress file(s) to finish.", + remaining) + pool.shutdown(wait=True, cancel_futures=True) + for fut in list(pending): + if fut.done() and not fut.cancelled(): + _process_future(fut) + break + _process_future(future) + + elapsed = time.time() - start_time + + if scan_cancelled or shutdown_requested: + summary = ( + f"\n{'='*60}\n" + f"Scan cancelled: {time.strftime('%Y-%m-%d %H:%M:%S')}\n" + f"Duration: {format_eta(elapsed)}\n" + f"Library: {len(all_files)} files" + f" ({format_size(total_library_size)})\n" + f"Files checked: {checked} of {total}\n" + f"Corrupted: {corrupted}" + f" ({format_size(corrupt_size)})\n" + f"{'='*60}\n" + ) + logger.info(summary.strip()) + log.write(summary) + log.flush() + return corrupted + + summary = ( + f"\n{'='*60}\n" + f"Scan finished: {time.strftime('%Y-%m-%d %H:%M:%S')}\n" + f"Duration: {format_eta(elapsed)}\n" + f"Library: {len(all_files)} files ({format_size(total_library_size)})\n" + f"Files checked: {checked}\n" + f"Corrupted: {corrupted} ({format_size(corrupt_size)})\n" + f"{'='*60}\n" + ) + logger.info(summary.strip()) + log.write(summary) + log.flush() + + # Mark idle before finalization — Lidarr ID resolution can take minutes + _webui_app_state.update(status="idle", scan_progress=None) + + _finalize_scan(log_dir, corrupt_list_path, corrupt_details, + output_folder, { + "version": __version__, + "finished": time.strftime('%Y-%m-%d %H:%M:%S'), + "duration": format_eta(elapsed), + "healed": len(healed), + "library_files": len(all_files), + "library_size": total_library_size, + "library_size_human": format_size(total_library_size), + "files_checked": checked, + "corrupted": corrupted, + "corrupt_size": corrupt_size, + "corrupt_size_human": format_size(corrupt_size), + "mode": mode, + }, lidarr_url, lidarr_api_key, healed=healed) + + return corrupted + + +def run_scan(input_folder, output_folder, log_file, log_dir, mode, workers, + min_age_minutes=30, lidarr_url=None, lidarr_api_key=None): + """Scan mode: decode-test all audio files with parallel workers.""" + corrupt_list_path = os.path.join(log_dir, "corrupt.txt") + + lock_fd = _acquire_scan_lock(log_dir) + try: + return _run_scan_inner(input_folder, output_folder, log_file, log_dir, + mode, workers, corrupt_list_path, + min_age_minutes, lidarr_url, lidarr_api_key) + finally: + try: + fcntl.flock(lock_fd.fileno(), fcntl.LOCK_UN) + lock_fd.close() + os.remove(os.path.join(log_dir, ".scanning")) + except OSError: + pass + + +# --- Delete mode --- + +def _get_detail_reason(corrupt_details, path): + """Extract reason string from a migrated corrupt_details dict.""" + return corrupt_details.get(path, {}).get("reason", "") + + +def _display_folder_files(corrupt_files, corrupt_details): + """Display corrupt files in a folder with reasons and sizes.""" + for cf in corrupt_files: + name = os.path.basename(cf) + reason = _get_detail_reason(corrupt_details, cf) + try: + size_str = format_size(os.path.getsize(cf)) + print(f" {name} ({size_str})") + if reason: + if len(reason) > 200: + cut = reason[:200].rsplit(' ', 1)[0] + reason = cut + "..." + print(f" -> {reason}") + except OSError: + print(f" {name} (already deleted)") + + +def _handle_folder_action(choice, folder, existing, log, + input_folder=None, corrupt_details=None, + lidarr_url=None, lidarr_api_key=None, + lidarr_blocklist=False): + """Execute a delete action on a folder. + Returns (folders_del, files_del, skipped, album_ids). + When Lidarr is configured, deletes via API using stored trackfile IDs.""" + album_ids = set() + + if choice == 'y' and os.path.isdir(folder): + if lidarr_url and lidarr_api_key and corrupt_details: + return _handle_folder_delete_lidarr( + folder, existing, log, input_folder, corrupt_details, + lidarr_url, lidarr_api_key, lidarr_blocklist) + + # No Lidarr — direct filesystem delete + if folder == input_folder: + # Never rmtree the mount root — delete files individually + deleted = 0 + for entry in os.listdir(folder): + fp = os.path.join(folder, entry) + try: + if os.path.isfile(fp): + os.remove(fp) + deleted += 1 + except OSError as e: + print(f" ERROR: {os.path.basename(fp)}: {e}") + log.write(f"ERROR deleting {fp}: {e}\n") + log.write(f"DELETED FOLDER CONTENTS: {folder} " + f"({deleted} files)\n") + print(f" -> {deleted} files deleted " + f"(mount root preserved)\n") + return (1, deleted, 0, album_ids) + try: + shutil.rmtree(folder) + log.write(f"DELETED FOLDER: {folder} " + f"({len(existing)} corrupt files)\n") + print(" -> Folder deleted\n") + return (1, len(existing), 0, album_ids) + except OSError as e: + print(f" ERROR: {e}\n") + log.write(f"ERROR deleting folder {folder}: {e}\n") + return (0, 0, 0, album_ids) + + elif choice == 'f': + if lidarr_url and lidarr_api_key and corrupt_details: + return _handle_files_delete_lidarr( + existing, log, corrupt_details, + lidarr_url, lidarr_api_key, lidarr_blocklist) + + # No Lidarr — direct filesystem delete + deleted = 0 + for cf in existing: + try: + os.remove(cf) + deleted += 1 + log.write(f"DELETED FILE: {cf}\n") + except OSError as e: + print(f" ERROR deleting " + f"{os.path.basename(cf)}: {e}") + log.write(f"ERROR deleting {cf}: {e}\n") + print(f" -> {deleted} corrupt files deleted\n") + return (0, deleted, 0, album_ids) + + else: + log.write(f"SKIPPED: {folder}\n") + print() + return (0, 0, 1, album_ids) + + +def _handle_files_delete_lidarr(file_paths, log, corrupt_details, + lidarr_url, lidarr_api_key, + lidarr_blocklist): + """Delete specific files via Lidarr API, one album at a time. + Returns (folders_del, files_del, skipped, album_ids).""" + album_to_tfids = {} + direct_paths = [] + + for fp in file_paths: + detail = corrupt_details.get(fp, {}) + if "trackfileId" in detail: + aid = detail.get("albumId", 0) + album_to_tfids.setdefault(aid, []).append(detail["trackfileId"]) + else: + direct_paths.append(fp) + + deleted = 0 + album_ids = set() + total = len(album_to_tfids) + + for i, (album_id, tf_ids) in enumerate( + album_to_tfids.items(), 1): + if i > 1: + _stagger_sleep(None) + # Blocklist before deletion + if lidarr_blocklist: + bl_ok, bl_fail = _lidarr_blocklist_albums( + lidarr_url, lidarr_api_key, [album_id]) + if bl_fail: + print(f" -> ERROR: Blocklist failed for " + f"album {album_id}. Skipping.\n") + log.write(f"ERROR: Blocklist failed for album " + f"{album_id} — skipped\n") + continue + if bl_ok: + print(f" -> Lidarr: blocklisted album " + f"[{i}/{total}]") + + # Delete this album's trackfiles + result = _lidarr_delete_trackfiles_bulk( + lidarr_url, lidarr_api_key, tf_ids) + if result is not None: + deleted += len(tf_ids) + album_ids.add(album_id) + log.write(f"LIDARR DELETE: album {album_id} — " + f"{len(tf_ids)} track files\n") + print(f" -> [{i}/{total}] Deleted " + f"{len(tf_ids)} trackfiles") + else: + print(f" -> ERROR: Bulk delete failed for " + f"album {album_id}. Skipping.\n") + log.write(f"ERROR: Bulk delete failed for album " + f"{album_id}\n") + + for fp in direct_paths: + try: + os.remove(fp) + deleted += 1 + log.write(f"DIRECT DELETE (not in Lidarr): {fp}\n") + except OSError as e: + print(f" ERROR: {os.path.basename(fp)}: {e}") + log.write(f"ERROR deleting {fp}: {e}\n") + + print(f" -> {deleted} files deleted\n") + return (0, deleted, 0, album_ids) + + +def _resolve_folder_trackfiles(folder, existing, corrupt_details, + album_ids, lidarr_url, lidarr_api_key): + """Build trackfile ID map for all files in a folder. + Uses stored IDs from corrupt_details, fetches remaining from Lidarr. + Returns (all_files, file_to_tfid, unmatched) or None on error.""" + all_files = [] + try: + for entry in os.listdir(folder): + fp = os.path.join(folder, entry) + if os.path.isfile(fp): + all_files.append(fp) + except OSError: + return None + + file_to_tfid = { + fp: corrupt_details[fp]["trackfileId"] + for fp in all_files + if "trackfileId" in corrupt_details.get(fp, {}) + } + + unmatched = [fp for fp in all_files if fp not in file_to_tfid] + if unmatched and album_ids: + album_tfs = [] + for aid in album_ids: + album_tfs.extend(_lidarr_get_trackfiles_by_album( + lidarr_url, lidarr_api_key, aid)) + for fp in unmatched: + match = _best_suffix_match(fp, album_tfs, min_score=1) + if match: + file_to_tfid[fp] = match["id"] + if match.get("albumId"): + album_ids.add(match["albumId"]) + unmatched = [fp for fp in all_files if fp not in file_to_tfid] + + return all_files, file_to_tfid, unmatched + + +def _handle_folder_delete_lidarr(folder, existing, log, input_folder, + corrupt_details, lidarr_url, + lidarr_api_key, lidarr_blocklist): + """Delete entire folder via Lidarr API. + Returns (folders_del, files_del, skipped, album_ids).""" + album_ids = { + corrupt_details[cf]["albumId"] + for cf in existing + if corrupt_details.get(cf, {}).get("albumId") + } + + resolved = _resolve_folder_trackfiles( + folder, existing, corrupt_details, album_ids, + lidarr_url, lidarr_api_key) + if resolved is None: + print(" ERROR listing folder\n") + log.write(f"ERROR listing {folder}\n") + return (0, 0, 0, album_ids) + + all_files, file_to_tfid, direct_paths = resolved + tf_ids = list(file_to_tfid.values()) + deleted = 0 + + if lidarr_blocklist and album_ids: + bl_ok, bl_fail = _lidarr_blocklist_albums( + lidarr_url, lidarr_api_key, album_ids) + if bl_fail: + print(f" -> ERROR: Blocklist failed for " + f"{bl_fail} albums. Delete aborted.\n") + log.write(f"ERROR: Blocklist failed for {bl_fail} albums " + f"— delete aborted\n") + return (0, 0, 0, set()) + if bl_ok: + log.write(f"LIDARR BLOCKLIST: {bl_ok} albums\n") + print(f" -> Lidarr: blocklisted {bl_ok} albums") + + if tf_ids: + result = _lidarr_delete_trackfiles_bulk( + lidarr_url, lidarr_api_key, tf_ids) + if result is not None: + deleted += len(tf_ids) + log.write(f"LIDARR DELETE: {len(tf_ids)} track files " + f"({len(album_ids)} albums)\n") + # Check monitored status and wait for search + has_monitored = False + for aid in album_ids: + album = _lidarr_get_album( + lidarr_url, lidarr_api_key, aid) + monitored = album.get("monitored", True) if album else True + if monitored: + has_monitored = True + else: + print(f" -> Lidarr: album {aid} " + f"unmonitored — will not re-download") + print(f" -> Lidarr: deleted {len(tf_ids)} " + f"track files ({len(album_ids)} albums)") + if has_monitored: + print(" -> Waiting for Lidarr search") + msg = _lidarr_wait_for_search( + lidarr_url, lidarr_api_key) + if msg: + print(f" -> {msg}") + else: + print(" -> Search complete") + else: + print(" -> ERROR: Lidarr bulk delete failed. " + "Check Lidarr logs.\n") + log.write("ERROR: Lidarr bulk delete API failed\n") + return (0, 0, 0, set()) + + # Direct delete for non-Lidarr files (non-audio, etc.) + for fp in direct_paths: + try: + os.remove(fp) + deleted += 1 + log.write(f"DIRECT DELETE (not in Lidarr): {fp}\n") + except OSError as e: + print(f" ERROR: {os.path.basename(fp)}: {e}") + log.write(f"ERROR deleting {fp}: {e}\n") + + # Remove empty folder (but never the mount root) + if folder != input_folder and os.path.isdir(folder): + try: + remaining_entries = os.listdir(folder) + if not remaining_entries: + os.rmdir(folder) + log.write(f"REMOVED EMPTY FOLDER: {folder}\n") + except OSError: + pass + + print(f" -> {deleted} files deleted\n") + return (1, deleted, 0, album_ids) + + +def _load_corrupt_file_list(corrupt_list_path, log_dir): + """Load and deduplicate corrupt files, group by folder.""" + with open(corrupt_list_path, 'r', encoding='utf-8') as f: + all_paths = [line.strip() for line in f if line.strip()] + + corrupt_details = _load_corrupt_details(log_dir) + + seen = set() + files = [] + for fp in all_paths: + if fp not in seen: + seen.add(fp) + files.append(fp) + + folders = {} + for fp in files: + folder = os.path.dirname(fp) + if folder not in folders: + folders[folder] = [] + folders[folder].append(fp) + + return files, folders, corrupt_details + + +def _run_interactive_delete(folders, total_folders, corrupt_details, + log_file, files, corrupt_list_path, + input_folder=None, lidarr_url=None, + lidarr_api_key=None, lidarr_blocklist=False): + """Interactive per-folder delete loop. + Returns list of affected album IDs (for Lidarr search queueing).""" + print("\nFor each folder:") + print(" [y] delete entire folder (album) [f] delete corrupt files only") + print(" [n] skip [a] delete all remaining folders") + print(" [q] quit\n") + + folders_deleted = 0 + files_deleted = 0 + skipped_folders = 0 + missing_files = 0 + delete_all = False + all_album_ids = set() + + with open(log_file, 'a', encoding='utf-8') as log: + log.write(f"\n{'='*60}\n") + log.write(f"Interactive delete started: " + f"{time.strftime('%Y-%m-%d %H:%M:%S')}\n") + log.write(f"{'='*60}\n") + log.flush() + + for i, (folder, corrupt_files) in enumerate(folders.items(), 1): + existing = [f for f in corrupt_files if os.path.exists(f)] + gone = len(corrupt_files) - len(existing) + if gone: + missing_files += gone + + if not existing and not os.path.isdir(folder): + print(f" [{i}/{total_folders}] GONE: {folder}") + log.write(f"MISSING FOLDER: {folder}\n") + continue + + print(f" [{i}/{total_folders}] {folder}/") + _display_folder_files(corrupt_files, corrupt_details) + + try: + entries = os.listdir(folder) + total_in_folder = sum( + 1 for e in entries + if os.path.isfile(os.path.join(folder, e))) + print(f" ({len(existing)} corrupt / " + f"{total_in_folder} total files in folder)") + except OSError: + pass + + if delete_all: + choice = 'y' + else: + try: + choice = input( + " Action? [y/f/n/a/q] ").strip().lower() + except EOFError: + print("\nNo input available. Run with: docker run -it ...") + break + + if choice == 'q': + print("\nQuitting.") + break + if choice == 'a': + delete_all = True + choice = 'y' + + fd, fid, sk, aids = _handle_folder_action( + choice, folder, existing, log, + input_folder, corrupt_details, + lidarr_url, lidarr_api_key, lidarr_blocklist) + folders_deleted += fd + files_deleted += fid + skipped_folders += sk + all_album_ids.update(aids) + + summary = ( + f"\nDelete summary:\n" + f" Folders deleted: {folders_deleted}\n" + f" Files deleted: {files_deleted}\n" + f" Folders skipped: {skipped_folders}\n" + f" Already missing: {missing_files}\n" + ) + print(summary) + log.write(summary) + + remaining = [f for f in files if os.path.exists(f)] + with open(corrupt_list_path, 'w', encoding='utf-8') as f: + for fp in remaining: + f.write(fp + "\n") + if remaining: + print(f"Updated {corrupt_list_path} ({len(remaining)} files remaining)") + else: + print(f"All corrupt files handled. {corrupt_list_path} cleared.") + + return list(all_album_ids) + + +def run_mass_delete(files, log_file, log_dir, corrupt_details=None, + lidarr_url=None, lidarr_api_key=None, + lidarr_blocklist=False): + """Delete all corrupt files without prompts. + Routes through Lidarr API when configured.""" + corrupt_list_path = os.path.join(log_dir, "corrupt.txt") + existing = [f for f in files if os.path.exists(f)] + + if not existing: + print("All files already deleted.") + with open(corrupt_list_path, 'w') as f: + pass + return + + total_size = 0 + for f in existing: + try: + total_size += os.path.getsize(f) + except OSError: + pass + + try: + confirm = input( + f"\nConfirm: delete {len(existing)} files " + f"({format_size(total_size)})? [yes/no] " + ).strip().lower() + except EOFError: + print("\nNo input available.") + return + + if confirm != "yes": + print("Cancelled.") + return + + print() + + if lidarr_url and lidarr_api_key and corrupt_details: + result = _lidarr_delete_corrupt( + lidarr_url, lidarr_api_key, existing, log_file, + log_dir=log_dir, blocklist=lidarr_blocklist) + if result is not None: + deleted, album_ids = result + print(f"\n{deleted}/{len(existing)} files deleted") + remaining = [f for f in files if os.path.exists(f)] + with open(corrupt_list_path, 'w', encoding='utf-8') as f: + for fp in remaining: + f.write(fp + "\n") + if not remaining: + print("corrupt.txt cleared.") + return album_ids + + # Fallback: direct filesystem delete + deleted = 0 + with open(log_file, 'a', encoding='utf-8') as log: + log.write(f"\n{'='*60}\n") + log.write(f"Mass delete: {time.strftime('%Y-%m-%d %H:%M:%S')}\n") + log.write(f"{'='*60}\n") + log.flush() + + for fp in existing: + try: + os.remove(fp) + deleted += 1 + log.write(f"DELETED: {fp}\n") + log.flush() + print(f" Deleted: {fp}") + except OSError as e: + log.write(f"ERROR: {fp} - {e}\n") + log.flush() + print(f" ERROR: {fp} - {e}") + + log.write(f"Mass delete complete: " + f"{deleted}/{len(existing)} deleted\n") + log.flush() + + print(f"\n{deleted}/{len(existing)} files deleted") + + remaining = [f for f in files if os.path.exists(f)] + with open(corrupt_list_path, 'w', encoding='utf-8') as f: + for fp in remaining: + f.write(fp + "\n") + if not remaining: + print("corrupt.txt cleared.") + return None + + +def run_delete_mode(corrupt_list_path, log_file, log_dir, + input_folder=None, lidarr_url=None, + lidarr_api_key=None, lidarr_blocklist=False): + """Interactive delete mode. Groups corrupt files by album folder and prompts.""" + lock_path = os.path.join(log_dir, ".scanning") + if os.path.exists(lock_path): + logger.info("A scan is currently running. Waiting for it to finish...") + _wait_for_scan_lock(log_dir) + if shutdown_requested: + return + logger.info("Scan finished. Starting delete mode.") + + if not os.path.exists(corrupt_list_path): + logger.error("No corrupt file list found at %s", corrupt_list_path) + logger.info("Run a scan first with MODE=report") + sys.exit(1) + + files, folders, corrupt_details = _load_corrupt_file_list( + corrupt_list_path, log_dir) + + if not files: + logger.info("corrupt.txt is empty — no corrupt files found.") + return + + total_files = len(files) + total_folders = len(folders) + total_corrupt_size = 0 + for f in files: + try: + total_corrupt_size += os.path.getsize(f) + except OSError: + pass + + print(f"Found {total_files} corrupt files across " + f"{total_folders} folders ({format_size(total_corrupt_size)})\n") + + try: + action = input( + " [a] delete ALL corrupt files now\n" + " [i] interactive (decide per folder)\n" + " [q] quit\n\n" + " Choice: " + ).strip().lower() + except EOFError: + print("\nNo input available. Run with: " + "docker exec -it BeatsCheck /app/delete.sh") + return + + if action == 'q': + return + if action == 'a': + run_mass_delete(files, log_file, log_dir, corrupt_details, + lidarr_url, lidarr_api_key, lidarr_blocklist) + return + if action != 'i': + print("Invalid choice.") + return + + _run_interactive_delete( + folders, total_folders, corrupt_details, + log_file, files, corrupt_list_path, + input_folder, lidarr_url, lidarr_api_key, lidarr_blocklist) + + +# --- Auto-delete --- + +def _post_auto_delete_lidarr(log_dir, album_ids, lidarr_url, + lidarr_api_key, lidarr_search): + """After auto-delete, record pending re-downloads and queue + explicit searches for unmonitored albums.""" + if not album_ids: + return + _record_pending_redownloads( + log_dir, album_ids, lidarr_url, lidarr_api_key) + if not lidarr_search: + return + unmonitored = [] + for aid in album_ids: + album = _lidarr_get_album(lidarr_url, lidarr_api_key, aid) + if album and not album.get("monitored", True): + unmonitored.append(aid) + if unmonitored: + _search_queue_add(log_dir, unmonitored) + logger.info(" Queued search for %d unmonitored albums", + len(unmonitored)) + + +def _reverify_before_delete(to_delete, tracking, log_dir, log_file): + """Re-decode every auto-delete candidate and drop the ones that are now + clean (a verdict recorded during a scan that overlapped an in-place tag + rewrite can be a stale false positive). Files that cannot be verified are + kept on disk this run — never delete on uncertainty.""" + still_corrupt = [] + healed = [] + for path in to_delete: + try: + _, is_corrupt, _, _ = check_audio_file(path) + except Exception: + logger.exception("Re-verify failed for %s — skipping delete", path) + continue + if is_corrupt: + still_corrupt.append(path) + else: + healed.append(path) + + if healed: + logger.info("%d file(s) re-verified clean — clearing corrupt status " + "instead of deleting", len(healed)) + with open(log_file, 'a', encoding='utf-8') as log: + for path in healed: + log.write(f"HEALED: {path} - re-verified clean at auto-delete\n") + details_path = os.path.join(log_dir, "corrupt_details.json") + details = _load_json(details_path) + for path in healed: + tracking.pop(path, None) + details.pop(path, None) + write_json_atomic(details_path, details) + + return still_corrupt + + +def run_auto_delete(log_dir, log_file, delete_after_days, max_deletes=50, + lidarr_url=None, lidarr_api_key=None, + lidarr_search=False, lidarr_blocklist=False): + """Auto-delete corrupt files that have been known for longer than DELETE_AFTER days. + Aborts if more than max_deletes files would be removed (safety threshold).""" + tracking_path = os.path.join(log_dir, "corrupt_tracking.json") + corrupt_list_path = os.path.join(log_dir, "corrupt.txt") + + tracking = _load_json(tracking_path) + + now = time.strftime('%Y-%m-%dT%H:%M:%S') + corrupt_set = _load_lines_as_set(corrupt_list_path) + for path in corrupt_set: + if path not in tracking: + tracking[path] = now + + # Drop entries that left corrupt.txt (healed) or left the disk — a healed + # file must not keep its old first-seen countdown if it ever re-flags. + tracking = {p: t for p, t in tracking.items() + if os.path.exists(p) and p in corrupt_set} + + if not tracking: + write_json_atomic(tracking_path, tracking) + return + + threshold = time.time() - (delete_after_days * 86400) + to_delete = [] + to_keep = [] + for path, first_seen in tracking.items(): + try: + seen_time = time.mktime(time.strptime(first_seen, '%Y-%m-%dT%H:%M:%S')) + except ValueError: + to_keep.append(path) + continue + if seen_time < threshold: + to_delete.append(path) + else: + to_keep.append(path) + + if not to_delete: + write_json_atomic(tracking_path, tracking) + if to_keep: + logger.info("%d corrupt files still within %d-day review window", + len(to_keep), delete_after_days) + return + + if max_deletes > 0 and len(to_delete) > max_deletes: + logger.warning( + "Auto-delete aborted — %d files exceed safety threshold of %d", + len(to_delete), max_deletes + ) + logger.warning("This may indicate a filesystem issue. Review corrupt.txt manually.") + logger.warning("Adjust MAX_AUTO_DELETE to increase the threshold if this is expected.") + with open(log_file, 'a', encoding='utf-8') as log: + log.write( + f"AUTO-DELETE ABORTED: {len(to_delete)} files exceed " + f"threshold of {max_deletes}\n" + ) + write_json_atomic(tracking_path, tracking) + return + + # Confirm the evidence before the destructive step: re-decode candidates + # so a stale false positive is healed, never deleted or blocklisted. + to_delete = _reverify_before_delete(to_delete, tracking, log_dir, log_file) + + if not to_delete: + write_json_atomic(tracking_path, tracking) + with open(corrupt_list_path, 'w', encoding='utf-8') as f: + for path in tracking: + f.write(path + "\n") + return + + logger.info("Auto-deleting %d corrupt files (older than %d days):", + len(to_delete), delete_after_days) + deleted = 0 + direct_delete_paths = [] + + if lidarr_url and lidarr_api_key: + logger.info(" Using Lidarr API for deletion") + result = _lidarr_delete_corrupt( + lidarr_url, lidarr_api_key, to_delete, log_file, + log_dir=log_dir, blocklist=lidarr_blocklist) + if result is not None: + deleted, album_ids = result + for path in to_delete: + if not os.path.exists(path): + tracking.pop(path, None) + _post_auto_delete_lidarr( + log_dir, album_ids, lidarr_url, lidarr_api_key, + lidarr_search) + else: + logger.error(" Lidarr API failed — auto-delete aborted. " + "Files will be retried next run.") + else: + direct_delete_paths = to_delete + + if direct_delete_paths: + with open(log_file, 'a', encoding='utf-8') as log: + log.write(f"\nAuto-delete ({delete_after_days}d threshold): " + f"{len(direct_delete_paths)} files\n") + log.flush() + for path in direct_delete_paths: + try: + os.remove(path) + deleted += 1 + tracking.pop(path, None) + log.write(f"AUTO-DELETED: {path}\n") + log.flush() + logger.info(" Deleted: %s", path) + except OSError as e: + log.write(f"ERROR auto-deleting {path}: {e}\n") + log.flush() + logger.error(" ERROR: %s - %s", path, e) + + logger.info(" %d/%d files deleted", deleted, len(to_delete)) + if to_keep: + logger.info(" %d files still within review window", len(to_keep)) + + write_json_atomic(tracking_path, tracking) + with open(corrupt_list_path, 'w', encoding='utf-8') as f: + for path in tracking: + f.write(path + "\n") + + +# --- Lidarr --- + +class _NoRedirectHandler(urllib.request.HTTPRedirectHandler): + """Block redirects to prevent API key leaking to a redirected host.""" + def redirect_request(self, req, fp, code, msg, headers, newurl): + raise urllib.error.HTTPError( + newurl, code, "Redirect blocked for security", headers, fp) + + +def _lidarr_request(url, api_key, method="GET", data=None, timeout=30): + """Make an authenticated request to the Lidarr API.""" + headers = { + "X-Api-Key": api_key, + "Content-Type": "application/json", + } + body = json.dumps(data).encode("utf-8") if data else None + req = urllib.request.Request(url, data=body, headers=headers, method=method) + opener = urllib.request.build_opener(_NoRedirectHandler) + # Extract just the API path for logging (never log the full URL or host) + api_path = url.split("/api/", 1)[-1] if "/api/" in url else "?" + try: + with opener.open(req, timeout=timeout) as resp: + raw = resp.read() + return json.loads(raw) if raw else {} + except urllib.error.HTTPError as e: + logger.error("Lidarr API %s /api/%s -> HTTP %d", + method, api_path, e.code) + return None + except json.JSONDecodeError: + logger.error("Lidarr API %s /api/%s -> non-JSON response", + method, api_path) + return None + except (urllib.error.URLError, OSError) as e: + # Log error type only — exception may contain hostname + logger.error("Lidarr API connection failed (%s /api/%s): %s", + method, api_path, type(e).__name__) + return None + + +def _load_lidarr_api_key(): + """Load Lidarr API key from env var or Docker secret. Never log it.""" + key = os.environ.get("LIDARR_API_KEY", "") + if not key: + secret_path = "/run/secrets/lidarr_api_key" + if os.path.exists(secret_path): + with open(secret_path, 'r') as f: + key = f.read().strip() + return key + + +def _build_lidarr_index(base_url, api_key): + """Pre-fetch all Lidarr trackfiles into a basename-keyed index. + + Returns ``{basename: [{id, albumId, artistId, path}, ...]}``. + Queries per-artist — Lidarr's trackfile endpoint requires a filter + (artistId/albumId) and rejects unfiltered requests with HTTP 400. + Returns ``None`` if the artist list cannot be fetched. + """ + try: + artists = _lidarr_get_artists(base_url, api_key) + if not artists: + return None + index = {} + for artist in artists: + for tf in _lidarr_get_trackfiles( + base_url, api_key, artist["id"]): + index.setdefault( + os.path.basename(tf["path"]), []).append({ + "id": tf["id"], + "albumId": tf["albumId"], + "artistId": artist["id"], + "path": tf["path"], + }) + logger.info(" Lidarr: indexed %d trackfiles from %d artists", + sum(len(v) for v in index.values()), len(artists)) + return index + except Exception: + logger.warning("Lidarr index build failed — " + "IDs will be resolved at scan end") + return None + + +def _apply_lidarr_match(detail, match): + """Copy ID fields from a Lidarr trackfile match into a detail dict.""" + detail["trackfileId"] = match["id"] + detail["albumId"] = match["albumId"] + detail["artistId"] = match["artistId"] + detail["lidarrPath"] = match["path"] + + +def _resolve_single_lidarr_id(file_path, corrupt_details, lidarr_index): + """Resolve Lidarr IDs for one corrupt file using the pre-built index.""" + candidates = lidarr_index.get(os.path.basename(file_path)) or [] + match = _best_suffix_match(file_path, candidates, min_score=1) + if match and file_path in corrupt_details: + _apply_lidarr_match(corrupt_details[file_path], match) + + +def _suffix_match_path(container_path, lidarr_path): + """Number of matching path components from the right. 0 = no match.""" + c_parts = container_path.replace("\\", "/").split("/") + l_parts = lidarr_path.replace("\\", "/").split("/") + matches = 0 + for cp, lp in zip(reversed(c_parts), reversed(l_parts)): + if cp == lp: + matches += 1 + else: + break + return matches + + +def _best_suffix_match(container_path, candidates, min_score=1): + """Pick the best-scoring candidate for a container path. + *candidates* is an iterable of dicts with a "path" key. + Returns the winning candidate, or None if no match meets *min_score* + or if the top score is ambiguous (tied between multiple candidates).""" + best = None + best_score = 0 + ambiguous = False + for c in candidates: + score = _suffix_match_path(container_path, c["path"]) + if score > best_score: + best = c + best_score = score + ambiguous = False + elif score == best_score and score >= 1: + ambiguous = True + if best and best_score >= min_score and not ambiguous: + return best + return None + + +def _resolve_lidarr_ids(corrupt_details, base_url, api_key): + """Post-scan: enrich corrupt_details with Lidarr trackfileId and albumId. + Tries a strict filename+folder match first, then a looser filename-only + match. Handles different container mount paths via suffix matching.""" + unresolved = [p for p, v in corrupt_details.items() + if "trackfileId" not in v] + if not unresolved: + return + + index = _build_lidarr_index(base_url, api_key) + if index is None: + logger.warning("Lidarr: could not fetch trackfiles for ID resolution") + return + + # Two passes: strict (>=2 components) then loose (>=1 unambiguous). + # Strict avoids wrong matches when many files share a filename. + matched = 0 + for min_score in (2, 1): + for path in list(unresolved): + candidates = index.get(os.path.basename(path)) or [] + match = _best_suffix_match(path, candidates, min_score=min_score) + if match: + _apply_lidarr_match(corrupt_details[path], match) + unresolved.remove(path) + matched += 1 + + if matched: + logger.info(" Lidarr: resolved %d corrupt files to trackfile IDs", + matched) + if unresolved: + logger.info(" Lidarr: %d corrupt files could not be matched", + len(unresolved)) + + +def _lidarr_get_artists(base_url, api_key): + """Fetch all artists. Returns list of {id, path} dicts.""" + result = _lidarr_request(f"{base_url}/api/v1/artist", api_key) + if result is None: + return [] + return [{"id": a["id"], "path": a["path"]} for a in result] + + +def _lidarr_get_trackfiles(base_url, api_key, artist_id): + """Fetch all track files for an artist.""" + url = f"{base_url}/api/v1/trackfile?artistId={artist_id}" + result = _lidarr_request(url, api_key) + if result is None: + return [] + return [{"id": tf["id"], "path": tf["path"], + "albumId": tf.get("albumId", 0)} for tf in result] + + +def _lidarr_get_trackfiles_by_album(base_url, api_key, album_id): + """Fetch all track files for an album.""" + url = f"{base_url}/api/v1/trackfile?albumId={album_id}" + result = _lidarr_request(url, api_key) + if result is None: + return [] + return [{"id": tf["id"], "path": tf["path"], + "albumId": tf.get("albumId", 0)} for tf in result] + + +def _lidarr_get_album(base_url, api_key, album_id): + """Fetch album details. Returns dict with 'monitored' field, or None.""" + url = f"{base_url}/api/v1/album/{album_id}" + return _lidarr_request(url, api_key) + + +def _lidarr_delete_trackfiles_bulk(base_url, api_key, track_file_ids): + """Bulk delete track files via Lidarr API. + Falls back to individual deletes if bulk fails (e.g. stale IDs + cause Lidarr to reject the entire batch).""" + url = f"{base_url}/api/v1/trackfile/bulk" + data = {"trackFileIds": track_file_ids} + result = _lidarr_request(url, api_key, method="DELETE", data=data) + if result is not None: + return result + + # Bulk failed — try deleting one at a time so valid IDs + # still get removed even if some are stale + if len(track_file_ids) <= 1: + return None + logger.warning(" Bulk delete failed — retrying %d " + "trackfiles individually", len(track_file_ids)) + succeeded = 0 + for tf_id in track_file_ids: + r = _lidarr_request( + f"{base_url}/api/v1/trackfile/{tf_id}", + api_key, method="DELETE") + if r is not None: + succeeded += 1 + else: + logger.debug(" Trackfile %d: delete failed (stale ID?)", + tf_id) + if succeeded: + logger.info(" Individual deletes: %d/%d succeeded", + succeeded, len(track_file_ids)) + return {} + return None + + +def _lidarr_wait_for_search(base_url, api_key, log_dir=None, + since_id=0, max_wait=None): + """Wait for new AlbumSearch commands (id > since_id) to complete. + Called after each album deletion so Lidarr finishes searching + before the next album is deleted. When *max_wait* is set (seconds), + caps both the total timeout and the 'search appeared' deadline at + that value. Otherwise polls every 10s, 5 min timeout, with a 30s + appear deadline. Returns the completion message from the last + search command, or None if none appeared / completed in time.""" + heartbeat_path = os.path.join(log_dir, ".heartbeat") if log_dir else None + last_message = None + total_budget = max_wait if max_wait is not None else 300 + appear_budget = max_wait if max_wait is not None else 30 + timeout = time.time() + total_budget + appear_deadline = time.time() + appear_budget + found_new = False + poll_interval = 5 if max_wait is not None and max_wait <= 60 else 10 + while time.time() < timeout and not shutdown_requested: + result = _lidarr_request( + f"{base_url}/api/v1/command", api_key) + if result is None: + return last_message + search_cmds = [c for c in result + if c.get("name") == "AlbumSearch" + and c.get("id", 0) > since_id] + active = [c for c in search_cmds + if c.get("status", "").lower() + in ("queued", "started")] + if search_cmds: + found_new = True + if found_new and not active: + # Capture completion message from finished searches + for c in search_cmds: + msg = c.get("message", "") + if msg: + last_message = msg + return last_message + if not found_new and time.time() > appear_deadline: + return None # search never appeared — move on + if heartbeat_path: + _write_heartbeat(heartbeat_path) + time.sleep(poll_interval) + return last_message + + +def _resolve_fresh_tf_ids(base_url, api_key, album_id, entries, prefix): + """Re-resolve trackfile IDs from Lidarr's current state. + Returns updated list of trackfile IDs, falling back to stored IDs + when re-resolution finds no match.""" + tf_ids = [e[1] for e in entries] + current_tfs = _lidarr_get_trackfiles_by_album( + base_url, api_key, album_id) + if not current_tfs: + return tf_ids + refreshed = [] + for cpath, old_id in entries: + matched_id = None + for ctf in current_tfs: + if _suffix_match_path(cpath, ctf["path"]) >= 2: + matched_id = ctf["id"] + break + refreshed.append(matched_id or old_id) + if refreshed != tf_ids: + changed = sum(1 for r, o in zip(refreshed, tf_ids) if r != o) + logger.debug("%s: re-resolved %d/%d trackfile IDs", + prefix, changed, len(tf_ids)) + return refreshed + + +def _lidarr_delete_album_files(base_url, api_key, tf_ids, + paths_in_album, log, prefix, + search, log_dir, label): + """Delete one album's trackfiles via Lidarr API. + Returns (deleted_count, success_bool).""" + result = _lidarr_delete_trackfiles_bulk( + base_url, api_key, tf_ids) + if result is not None: + log.write(f"LIDARR DELETE: {label} — " + f"{len(tf_ids)} track files\n") + log.flush() + return (len(tf_ids), True) + + logger.error("%s: Lidarr API delete failed — skipping", prefix) + log.write(f"ERROR: Lidarr delete failed for {label}\n") + log.flush() + return (0, False) + + +def _log_delete_and_search(base_url, api_key, album_info, log_dir, + prefix, count, search, max_search_id, + max_wait=None): + """Log delete result and optionally wait for Lidarr search. + When *max_wait* is set (seconds), caps the search-wait at that + many seconds.""" + if not search: + logger.info("%s: deleted %d trackfiles", prefix, count) + return + monitored = (album_info.get("monitored", True) + if album_info else True) + if monitored: + logger.info("%s: deleted %d trackfiles — waiting for search", + prefix, count) + msg = _lidarr_wait_for_search( + base_url, api_key, log_dir, + since_id=max_search_id, max_wait=max_wait) + if msg: + logger.info("%s: %s", prefix, msg) + else: + logger.info("%s: no release found within " + "%ds", prefix, max_wait or 30) + else: + logger.info( + "%s: deleted %d trackfiles " + "(unmonitored — Lidarr will not re-download)", + prefix, count) + + +def _lidarr_delete_corrupt(base_url, api_key, corrupt_paths, log_file, + log_dir=None, blocklist=False, search=True, + progress_cb=None, cancel_cb=None, + search_wait_seconds=30): + """Delete corrupt files via Lidarr API, one album at a time. + Reads IDs from corrupt_details.json (resolved at scan time). + Re-resolves IDs before each delete to handle stale entries. + Falls back to direct filesystem delete when Lidarr API fails. + Sequential processing prevents flooding indexers with searches. + The per-album search wait (capped at *search_wait_seconds*) both + paces indexer requests and gives Lidarr time to log whether it + grabbed a replacement release. + Returns (deleted_count, affected_album_ids).""" + corrupt_details = _load_corrupt_details( + log_dir or os.path.dirname(log_file)) + + album_to_entries = {} + non_lidarr_paths = [] + + for path in corrupt_paths: + detail = corrupt_details.get(path, {}) + if "trackfileId" in detail: + aid = detail.get("albumId", 0) + if aid not in album_to_entries: + album_to_entries[aid] = [] + album_to_entries[aid].append( + (path, detail["trackfileId"])) + else: + non_lidarr_paths.append(path) + + if not album_to_entries and not non_lidarr_paths: + return (0, []) + + deleted = 0 + affected_albums = [] + failed_albums = [] + total_albums = len(album_to_entries) + + with open(log_file, 'a', encoding='utf-8') as log: + # Process one album at a time + for i, (album_id, entries) in enumerate( + album_to_entries.items(), 1): + paths_in_album = [e[0] for e in entries] + + # Fetch album details for logging + album_info = _lidarr_get_album( + base_url, api_key, album_id) + album_title = (album_info.get("title", "") + if album_info else "") + artist_obj = (album_info.get("artist", {}) + if album_info else {}) + artist_name = (artist_obj.get("artistName", "") + if artist_obj else "") + label = (f"{artist_name} — {album_title}" + if artist_name and album_title + else album_title or str(album_id)) + prefix = f" [{i}/{total_albums}] {label}" + + if cancel_cb and cancel_cb(): + log.write(f"CANCELLED at album {i}/{total_albums}\n") + log.flush() + break + + if progress_cb: + progress_cb(i, total_albums, label, "deleting") + + # Blocklist before deletion + if blocklist: + bl_ok, bl_fail = _lidarr_blocklist_albums( + base_url, api_key, [album_id]) + if bl_fail: + logger.error("%s: blocklist failed — skipping", + prefix) + log.write(f"ERROR: Blocklist failed for album " + f"{album_id} ({label}) — skipped\n") + log.flush() + continue + if bl_ok: + logger.debug("%s: blocklisted", prefix) + + # Re-resolve trackfile IDs from Lidarr's current state + tf_ids = _resolve_fresh_tf_ids( + base_url, api_key, album_id, entries, prefix) + + # Snapshot max AlbumSearch command ID before delete + max_search_id = 0 + if search: + cmds = _lidarr_request( + f"{base_url}/api/v1/command", api_key) + if cmds: + for c in cmds: + if (c.get("name") == "AlbumSearch" + and c.get("id", 0) + > max_search_id): + max_search_id = c["id"] + + count, via_api = _lidarr_delete_album_files( + base_url, api_key, tf_ids, paths_in_album, + log, prefix, search, log_dir, label) + if count: + deleted += count + affected_albums.append(album_id) + else: + failed_albums.append((label, paths_in_album)) + + if via_api: + _log_delete_and_search( + base_url, api_key, album_info, log_dir, + prefix, len(tf_ids), search, max_search_id, + max_wait=search_wait_seconds) + + # Direct delete for files not tracked by Lidarr + for path in non_lidarr_paths: + try: + os.remove(path) + deleted += 1 + log.write(f"DIRECT DELETE (not in Lidarr): {path}\n") + log.flush() + logger.info(" Deleted (not in Lidarr): %s", path) + except OSError as e: + log.write(f"ERROR deleting {path}: {e}\n") + log.flush() + logger.error(" ERROR: %s - %s", path, e) + + # Log completion summary + if deleted: + logger.info(" Lidarr delete complete: %d files deleted " + "across %d albums", deleted, len(affected_albums)) + if failed_albums: + logger.error(" %d album(s) failed — files must be " + "deleted manually:", len(failed_albums)) + for label, paths in failed_albums: + logger.error(" %s", label) + for fp in paths: + logger.error(" %s", fp) + elif not deleted and album_to_entries: + logger.error(" Lidarr delete complete: all albums failed") + + return (deleted, affected_albums) + + +def _lidarr_get_album_history(base_url, api_key, album_id): + """Fetch the most recent grabbed history record for an album. + Returns list of history records (at most 1).""" + url = (f"{base_url}/api/v1/history" + f"?albumId={album_id}&eventType=1" + f"&sortKey=date&sortDirection=descending" + f"&pageSize=1&page=1") + result = _lidarr_request(url, api_key) + if result is None: + return [] + return result.get("records", []) + + +def _lidarr_mark_history_failed(base_url, api_key, history_id): + """Mark a history record as failed, creating a blocklist entry. + Lidarr auto-blocklists the release on DownloadFailedEvent.""" + url = f"{base_url}/api/v1/history/failed/{history_id}" + return _lidarr_request(url, api_key, method="POST") + + +def _lidarr_blocklist_albums(base_url, api_key, album_ids): + """Blocklist the most recent grab for each album by marking it as failed. + Returns (blocklisted_count, failed_count). A failed_count > 0 means + some albums could not be blocklisted (API error, not missing history).""" + blocklisted = 0 + failed = 0 + for album_id in album_ids: + history = _lidarr_get_album_history(base_url, api_key, album_id) + if not history: + logger.debug(" Blocklist: no grab history for album %d " + "— may have been imported manually", album_id) + continue + record = history[0] + history_id = record.get("id") + if not history_id: + logger.debug(" Blocklist: no history ID for album %d", + album_id) + continue + result = _lidarr_mark_history_failed(base_url, api_key, history_id) + if result is not None: + blocklisted += 1 + logger.debug(" Blocklist: marked history %d as failed " + "(album %d)", history_id, album_id) + else: + failed += 1 + logger.error(" Blocklist: failed to mark history %d " + "for album %d", history_id, album_id) + if blocklisted: + logger.info(" Lidarr: blocklisted %d/%d albums", + blocklisted, len(album_ids)) + return blocklisted, failed + + +def _log_lidarr_status(lidarr_url, lidarr_api_key, lidarr_search, + lidarr_blocklist=False): + """Log Lidarr integration status without exposing credentials.""" + if lidarr_url and not lidarr_url.startswith(("http://", "https://")): + logger.error("LIDARR_URL must start with http:// or https://") + sys.exit(1) + if lidarr_url and lidarr_api_key: + logger.info(" Lidarr integration: enabled") + if lidarr_search: + logger.info(" Lidarr search: enabled (5/hour)") + if lidarr_blocklist: + logger.info(" Lidarr blocklist: enabled") + masked = re.sub(r'(https?://)(.+)', r'\1****', lidarr_url) + logger.debug(" Lidarr URL: %s", masked) + elif lidarr_url: + logger.warning(" Lidarr URL set but API key missing — disabled") + + +def _search_queue_path(log_dir): + return os.path.join(log_dir, "search_queue.json") + + +def _search_queue_add(log_dir, album_ids): + """Append album IDs to the search queue, deduplicating.""" + path = _search_queue_path(log_dir) + queue = _load_json(path, default=[]) + if not isinstance(queue, list): + logger.warning("Corrupt search_queue.json — resetting to empty list") + queue = [] + existing = set(queue) + added = 0 + for aid in album_ids: + if aid not in existing: + queue.append(aid) + existing.add(aid) + added += 1 + if added: + write_json_atomic(path, queue) + logger.info(" Queued Lidarr search for %d albums " + "(%d total in queue)", added, len(queue)) + + +def _search_queue_drain_one(log_dir, base_url, api_key): + """Process one album from the search queue. Sends AlbumSearch command, + polls until complete. Returns True if a search was attempted.""" + path = _search_queue_path(log_dir) + queue = _load_json(path, default=[]) + if not isinstance(queue, list) or not queue: + return False + + album_id = queue[0] + logger.info(" Lidarr search: album %d (%d in queue)", album_id, + len(queue)) + + # Trigger the search + result = _lidarr_request( + f"{base_url}/api/v1/command", api_key, method="POST", + data={"name": "AlbumSearch", "albumIds": [album_id]}) + + if result is None: + logger.warning(" Lidarr search: failed to start for album %d", + album_id) + return True # attempted but failed — don't remove, retry next cycle + + command_id = result.get("id") + if not command_id: + logger.warning(" Lidarr search: no command ID returned for album %d", + album_id) + queue.pop(0) + write_json_atomic(path, queue) + return True + + # Poll until complete (30s intervals, 10 min timeout) + # Write heartbeat during poll to prevent healthcheck failure + heartbeat_path = os.path.join(log_dir, ".heartbeat") + poll_failures = 0 + timeout = time.time() + 600 + while time.time() < timeout and not shutdown_requested: + time.sleep(30) + _write_heartbeat(heartbeat_path) + status = _lidarr_request( + f"{base_url}/api/v1/command/{command_id}", api_key) + if status is None: + poll_failures += 1 + # Command may have been cleaned from DB (5 min retention) + if poll_failures >= 3: + logger.warning(" Lidarr search: lost track of command " + "for album %d, assuming complete", album_id) + queue.pop(0) + write_json_atomic(path, queue) + return True + continue + poll_failures = 0 + state = status.get("status", "").lower() + if state == "completed": + queue.pop(0) + write_json_atomic(path, queue) + logger.info(" Lidarr search: album %d complete " + "(%d remaining)", album_id, len(queue)) + return True + if state in ("failed", "aborted", "cancelled", "orphaned"): + queue.pop(0) + write_json_atomic(path, queue) + logger.warning(" Lidarr search: album %d %s " + "(%d remaining)", album_id, state, len(queue)) + return True + + if shutdown_requested: + logger.info(" Lidarr search: interrupted by shutdown") + else: + logger.warning(" Lidarr search: album %d timed out, " + "keeping in queue", album_id) + return True + + +# --- Config --- + +def setup_logging(log_level, log_file=None): + """Configure logging with console handler and optional file handler.""" + level = getattr(logging, log_level.upper(), logging.INFO) + + root = logging.getLogger() + root.setLevel(level) + + console = logging.StreamHandler(sys.stdout) + console.setLevel(level) + console_fmt = logging.Formatter( + "%(asctime)s | %(levelname)-8s | %(message)s", + datefmt="%Y-%m-%d %H:%M:%S" + ) + console.setFormatter(console_fmt) + root.addHandler(console) + + if log_file: + file_handler = logging.handlers.WatchedFileHandler( + log_file, encoding='utf-8') + file_handler.setLevel(level) + file_handler.setFormatter(console_fmt) + root.addHandler(file_handler) + + +_DEFAULT_CONFIG = """\ +####################################################### +## BeatsCheck Configuration ## +####################################################### +## ## +## Change the values below to configure BeatsCheck. ## +## Lines starting with # are ignored — remove the ## +## # to enable a setting. ## +## ## +## Settings can also be changed from the WebUI ## +## (enable at the bottom of this file). ## +## ## +## https://github.com/chodeus/BeatsCheck ## +####################################################### + + +##----- Scanning --------------------------------------- +## How and when BeatsCheck scans your library. + +## music_dir — folder to scan for audio files +## Defaults to /data. Set a subfolder if your music +## is under e.g. /data/media/music +# music_dir = /data + +## mode — what happens when BeatsCheck runs +## setup = sit idle, don't scan (default) +## report = scan and log corrupt files (nothing deleted) +## move = move corrupt files to a quarantine folder +mode = setup + +## workers — files checked in parallel +## More = faster scans but more CPU. 2-4 recommended. +workers = 4 + +## run_interval — hours between automatic scans +## 0 = scan once then wait (trigger manually or via WebUI) +## 24 = daily +## 168 = weekly +run_interval = 0 + +## min_file_age — skip files modified in the last N minutes +## Avoids flagging files being actively downloaded. +min_file_age = 30 + + +##----- When Corrupt Files Are Found ------------------- +## By default, corrupt files are only logged — nothing +## is deleted unless you configure auto-delete here or +## use the Corrupt Files page in the WebUI. + +## delete_after — auto-delete corrupt files after N days +## Gives you time to review before anything is removed. +## 0 = never auto-delete (delete manually via WebUI) +## 7 = delete after 7 days +delete_after = 0 + +## max_auto_delete — safety limit +## Abort if more than N files would be auto-deleted +## in one run. Prevents mass deletion from filesystem +## issues. 0 = no limit. +max_auto_delete = 50 + +## output_dir — quarantine folder (move mode only) +## Must be within a mounted volume (e.g. /data/corrupted) +# output_dir = /data/corrupted + + +##----- Lidarr (Automatic Re-download) ---------------- +## Connect to Lidarr so deleted corrupt files are +## automatically re-downloaded as clean copies. +## +## To enable: remove the # from lidarr_url and +## lidarr_api_key below and fill in your values. +## +## How it works: +## 1. Corrupt files are deleted via the Lidarr API +## 2. Monitored albums are automatically re-searched +## by Lidarr (no extra config needed) +## 3. Blocklist prevents re-downloading the same +## bad release +## +## Your API key is stored securely here — it won't +## appear in docker inspect or process listings. + +## lidarr_url — your Lidarr address +## e.g. http://lidarr:8686 or http://192.168.1.100:8686 +# lidarr_url = http://lidarr:8686 + +## lidarr_api_key — find in Lidarr: Settings > General +# lidarr_api_key = + +## lidarr_blocklist — blocklist the corrupt release so +## Lidarr downloads a different copy +## true = blocklist before deleting, false = just delete +# lidarr_blocklist = false + +## lidarr_search — search for unmonitored albums after +## auto-delete. Monitored albums are searched +## automatically by Lidarr — this is only for albums +## you've stopped monitoring. +# lidarr_search = false + + +##----- Logging ---------------------------------------- + +## log_level — how much detail to log +## INFO = normal, DEBUG = verbose, WARNING/ERROR = quiet +log_level = INFO + +## max_log_mb — rotate log at this size (MB) +## Rotation triggers a fresh full rescan. 0 = never. +max_log_mb = 50 + + +##----- Web Interface ---------------------------------- +## Enable the WebUI for monitoring, config, and managing +## corrupt files from your browser. +## +## Also publish the port in Docker (e.g. ports: 8484:8484) +## On first visit you'll create a username and password. + +## webui — enable or disable +## true = start web server, false = no web interface +webui = false + +## webui_port — port number +webui_port = 8484 +""" + +# Maps config-file keys (lowercase) to environment variable names. +_CONFIG_KEY_MAP = { + 'music_dir': 'MUSIC_DIR', + 'output_dir': 'OUTPUT_DIR', + 'mode': 'MODE', + 'workers': 'WORKERS', + 'run_interval': 'RUN_INTERVAL', + 'delete_after': 'DELETE_AFTER', + 'max_auto_delete': 'MAX_AUTO_DELETE', + 'min_file_age': 'MIN_FILE_AGE', + 'log_level': 'LOG_LEVEL', + 'max_log_mb': 'MAX_LOG_MB', + 'lidarr_url': 'LIDARR_URL', + 'lidarr_api_key': 'LIDARR_API_KEY', + 'lidarr_search': 'LIDARR_SEARCH', + 'lidarr_blocklist': 'LIDARR_BLOCKLIST', + 'webui': 'WEBUI', + 'webui_port': 'WEBUI_PORT', +} + + +def _write_default_config(config_dir): + """Write the default beatscheck.conf template if it doesn't exist.""" + path = os.path.join(config_dir, "beatscheck.conf") + try: + with open(path, 'x') as f: + f.write(_DEFAULT_CONFIG) + print(f"Created default config: {path}") + except FileExistsError: + pass + except OSError as e: + print(f"Warning: could not write default config {path}: {e}") + + +# Track env vars set by Docker (before _apply_config_file). +# Used by _reload_config to know which values should not be +# overridden by the config file. +_docker_env_vars = set() + + +def _snapshot_docker_env(): + """Record which config-related env vars were set by Docker.""" + for env_name in _CONFIG_KEY_MAP.values(): + if env_name in os.environ: + _docker_env_vars.add(env_name) + + +def _parse_config_lines(path): + """Parse beatscheck.conf into {key: value} dict. + Handles inline comments and surrounding quotes.""" + result = {} + if not os.path.isfile(path): + return result + with open(path, 'r') as f: + for line in f: + line = line.strip() + if not line or line.startswith('#'): + continue + if '=' not in line: + continue + key, _, value = line.partition('=') + key = key.strip().lower() + value = value.strip() + if ' #' in value and not ( + len(value) >= 2 and value[0] == value[-1] + and value[0] in ('"', "'")): + value = value[:value.index(' #')].rstrip() + if (len(value) >= 2 and value[0] == value[-1] + and value[0] in ('"', "'")): + value = value[1:-1] + result[key] = value + return result + + +def _apply_config_file(config_dir): + """Load beatscheck.conf and set env vars for any values not already + set in the environment. This gives env vars priority over the file.""" + path = os.path.join(config_dir, "beatscheck.conf") + try: + for key, value in _parse_config_lines(path).items(): + env_name = _CONFIG_KEY_MAP.get(key) + if env_name and env_name not in os.environ: + os.environ[env_name] = value + except OSError as e: + print(f"Warning: could not read config file {path}: {e}") + + +def _env(name, default, cast, strict=False, label=None): + """Read and cast an environment variable. + On cast failure: sys.exit(1) if *strict*, else return *default*.""" + raw = os.environ.get(name) + if raw is None: + return default + try: + return cast(raw) + except (ValueError, TypeError): + if strict: + print(f"Invalid {name} value" + f"{' (' + label + ')' if label else ''}.") + sys.exit(1) + return default + + +def _parse_env_bool(name, default=False): + """Parse a boolean environment variable (true/false/1/0/yes/no).""" + val = os.environ.get(name, str(default)).lower().strip() + return val in ("true", "1", "yes") + + +def _load_config(): + """Load configuration from CLI args, config file, or environment. + + Priority: env vars > beatscheck.conf > defaults. + """ + if len(sys.argv) == 4: + input_folder = sys.argv[1].rstrip("/") + output_folder = sys.argv[2].rstrip("/") + log_file = sys.argv[3] + log_dir = os.path.dirname(log_file) + else: + log_dir = os.environ.get("CONFIG_DIR", "/config").rstrip("/") + # Write default config template on first run, then load values. + # Config file values fill in for any env vars not already set. + _write_default_config(log_dir) + _snapshot_docker_env() + _apply_config_file(log_dir) + input_folder = os.environ.get("MUSIC_DIR", "/data").rstrip("/") + output_folder = os.environ.get("OUTPUT_DIR", "/data/corrupted").rstrip("/") + log_file = os.path.join(log_dir, "beats_check.log") + + mode = (os.environ.get("MODE") or "setup").lower() + if mode not in ("report", "move", "delete", "setup"): + print(f"Invalid MODE '{mode}'. Must be: report, move, delete, setup") + sys.exit(1) + + workers = _env("WORKERS", 4, int, strict=True) + if workers < 1: + print("Invalid WORKERS value. Must be a positive integer.") + sys.exit(1) + + return types.SimpleNamespace( + input_folder=input_folder, + output_folder=output_folder, + log_dir=log_dir, + log_file=log_file, + mode=mode, + log_level=os.environ.get("LOG_LEVEL", "INFO"), + workers=workers, + run_interval=_env("RUN_INTERVAL", 0.0, float, strict=True, label="hours"), + delete_after=_env("DELETE_AFTER", 0.0, float, strict=True, label="days"), + max_auto_delete=_env("MAX_AUTO_DELETE", 50, int, strict=True), + min_age_minutes=_env("MIN_FILE_AGE", 30, int, strict=True, label="minutes"), + max_log_mb=_env("MAX_LOG_MB", 50, int, strict=True, label="MB"), + lidarr_url=os.environ.get("LIDARR_URL", "").rstrip("/"), + lidarr_api_key=_load_lidarr_api_key(), + lidarr_search=_parse_env_bool("LIDARR_SEARCH", False), + lidarr_blocklist=_parse_env_bool("LIDARR_BLOCKLIST", False), + webui=_parse_env_bool("WEBUI", False), + webui_port=_env("WEBUI_PORT", 8484, int, strict=True), + fresh_rescan=False, + ) + + +def _reload_config(cfg): + """Re-read beatscheck.conf and update cfg for the next scan. + + Docker env vars (set before startup) always win. Config file values + update the os.environ entries that _apply_config_file originally set, + then we refresh the cfg namespace from os.environ. + """ + path = os.path.join(cfg.log_dir, "beatscheck.conf") + try: + file_vals = {} + for key, value in _parse_config_lines(path).items(): + env_name = _CONFIG_KEY_MAP.get(key) + if env_name: + file_vals[env_name] = value + except OSError: + return + + # Update env vars from config file (skip Docker-set vars) + for env_name, value in file_vals.items(): + if env_name not in _docker_env_vars: + os.environ[env_name] = value + + # Refresh cfg attributes from env vars + cfg.input_folder = os.environ.get( + "MUSIC_DIR", cfg.input_folder).rstrip("/") + cfg.output_folder = os.environ.get( + "OUTPUT_DIR", cfg.output_folder).rstrip("/") + cfg.mode = (os.environ.get("MODE") or cfg.mode).lower() + cfg.workers = max(1, _env("WORKERS", cfg.workers, int)) + cfg.run_interval = _env("RUN_INTERVAL", cfg.run_interval, float) + cfg.delete_after = _env("DELETE_AFTER", cfg.delete_after, float) + cfg.max_auto_delete = _env("MAX_AUTO_DELETE", cfg.max_auto_delete, int) + cfg.min_age_minutes = _env("MIN_FILE_AGE", cfg.min_age_minutes, int) + cfg.max_log_mb = _env("MAX_LOG_MB", cfg.max_log_mb, int) + cfg.lidarr_url = os.environ.get( + "LIDARR_URL", cfg.lidarr_url).rstrip("/") + cfg.lidarr_api_key = _load_lidarr_api_key() + cfg.lidarr_search = _parse_env_bool( + "LIDARR_SEARCH", cfg.lidarr_search) + cfg.lidarr_blocklist = _parse_env_bool( + "LIDARR_BLOCKLIST", cfg.lidarr_blocklist) + + +def _run_setup_idle(log_dir, lidarr_url=None, lidarr_api_key=None): + """Setup mode — sit idle until rescan with a mode is triggered. + Bare 'rescan' (no mode) defaults to report. + Drains the Lidarr search queue during idle if configured.""" + logger.info("Setup mode — container is idle. " + "Start scanning with: rescan report") + trigger = _idle_wait(log_dir, None, lidarr_url, lidarr_api_key) + if trigger is None: + return None + stripped = trigger[len("fresh:"):] if trigger.startswith("fresh:") else trigger + return stripped if stripped in ("report", "move") else "report" + + +class _NullAppState: + """No-op stand-in used when the WebUI is disabled.""" + def update(self, **kwargs): + pass + + def snapshot(self): + return {} + + def get(self, key=None): + return None if key else {} + + +_webui_app_state = _NullAppState() + + +def _webui_progress(current, total, corrupted, current_file): + """Update WebUI scan progress. Called from scan loop.""" + _webui_app_state.update( + scan_progress={ + "current": current, "total": total, + "file": current_file}, + corrupt_count=corrupted, + total_scanned=current, + ) + + +# --- Main helpers --- + + +def _start_webui(cfg): + """Start optional WebUI server in a daemon thread.""" + global _webui_app_state + try: + from webui import start_webui, app_state + _webui_app_state = app_state + app_state.update(version=__version__, mode=cfg.mode, + status="starting", workers=cfg.workers) + start_webui(cfg.log_dir, cfg.webui_port) + except Exception: + logger.exception("WebUI failed to start") + + +def _maybe_rotate_logs(cfg): + """Rotate log and processed.txt if log exceeds max size.""" + if cfg.max_log_mb <= 0 or not os.path.exists(cfg.log_file): + return + try: + log_size = os.path.getsize(cfg.log_file) + if log_size > cfg.max_log_mb * 1024 * 1024: + _rotate_file(cfg.log_file, keep=3) + processed = os.path.join(cfg.log_dir, "processed.txt") + if os.path.exists(processed): + _rotate_file(processed, keep=3) + logger.info( + "Log rotated (%s > %dMB limit). " + "Starting fresh full scan.", + format_size(log_size), cfg.max_log_mb) + except OSError: + pass + + +def _validate_move_mode(cfg): + """Validate output_dir for move mode. Falls back to report if invalid. + Returns True if move mode is valid and ready.""" + if not cfg.output_folder: + logger.error("Move mode requires output_dir to be " + "configured. Falling back to report mode.") + return False + if not os.path.isdir( + os.path.dirname(cfg.output_folder.rstrip("/"))): + logger.error( + "Output directory parent does not exist: %s. " + "Check your volume mount. Falling back to " + "report mode.", cfg.output_folder) + return False + os.makedirs(cfg.output_folder, exist_ok=True) + return True + + +def _wait_after_cancel(cfg): + """After a cancelled scan, go idle and wait for next rescan trigger. + Returns True if shutdown requested (caller should break).""" + logger.info("Scan cancelled. Container is idle.") + _webui_app_state.update(status="idle", scan_progress=None) + trigger = _idle_wait( + cfg.log_dir, None, cfg.lidarr_url, cfg.lidarr_api_key) + if trigger is None: + return True + if _apply_rescan_trigger(trigger, cfg): + logger.info("Mode changed to: %s", cfg.mode) + return False + + +def _post_scan_wait(cfg): + """Wait for next scan after a completed or failed cycle. + Returns True if shutdown requested (caller should break).""" + if cfg.run_interval <= 0: + logger.info("Scan complete. Container is idle. " + "Rescan with: rescan [report|move]") + trigger = _idle_wait( + cfg.log_dir, None, cfg.lidarr_url, cfg.lidarr_api_key) + if trigger is None: + return True + if _apply_rescan_trigger(trigger, cfg): + logger.info("Mode changed to: %s", cfg.mode) + return False + + next_run = time.strftime( + '%Y-%m-%d %H:%M:%S', + time.localtime(time.time() + cfg.run_interval * 3600) + ) + logger.info( + "Next scan at %s (%sh interval). Waiting...", + next_run, cfg.run_interval + ) + trigger = _idle_wait(cfg.log_dir, cfg.run_interval * 3600, + cfg.lidarr_url, cfg.lidarr_api_key) + if trigger is None: + if shutdown_requested: + return True + logger.info("Scheduled scan starting.") + elif _apply_rescan_trigger(trigger, cfg): + logger.info("Mode changed to: %s", cfg.mode) + return False + + +# --- Main --- + +def main(): + signal.signal(signal.SIGTERM, handle_shutdown) + signal.signal(signal.SIGINT, handle_shutdown) + + try: + os.nice(10) + except OSError: + pass + + cfg = _load_config() + + os.makedirs(cfg.log_dir, exist_ok=True) + + setup_logging(cfg.log_level, log_file=cfg.log_file) + + logger.info("BeatsCheck v%s starting", __version__) + + if cfg.webui: + _start_webui(cfg) + + _log_lidarr_status(cfg.lidarr_url, cfg.lidarr_api_key, cfg.lidarr_search, + cfg.lidarr_blocklist) + + queue = _load_json(_search_queue_path(cfg.log_dir), default=[]) + if isinstance(queue, list) and queue: + logger.info(" Search queue: %d albums pending", len(queue)) + + if cfg.mode == "setup": + _webui_app_state.update(status="setup", mode="setup") + new_mode = _run_setup_idle(cfg.log_dir, cfg.lidarr_url, + cfg.lidarr_api_key) + if new_mode: + cfg.mode = new_mode + logger.info("Mode changed to: %s", cfg.mode) + else: + return + + if cfg.mode == "delete": + corrupt_list_path = os.path.join(cfg.log_dir, "corrupt.txt") + run_delete_mode(corrupt_list_path, cfg.log_file, cfg.log_dir, + cfg.input_folder, cfg.lidarr_url, + cfg.lidarr_api_key, cfg.lidarr_blocklist) + return + + if not os.path.isdir(cfg.input_folder): + logger.error("Music directory not found: %s", cfg.input_folder) + logger.error("Check your volume mount. Container will stay idle.") + cfg.mode = "setup" + + while True: + global scan_cancelled + scan_cancelled = False + _reload_config(cfg) + _maybe_rotate_logs(cfg) + _poll_pending_redownloads(cfg) + + if not os.path.isdir(cfg.input_folder): + logger.error("Music directory not found: %s", + cfg.input_folder) + _webui_app_state.update(status="idle", mode=cfg.mode) + trigger = _idle_wait( + cfg.log_dir, None, cfg.lidarr_url, + cfg.lidarr_api_key) + if trigger is None: + break + _apply_rescan_trigger(trigger, cfg) + continue + + if cfg.mode == "move" and not _validate_move_mode(cfg): + cfg.mode = "report" + _webui_app_state.update(mode="report") + + _clear_resume_if_fresh(cfg) + _webui_app_state.update( + status="scanning", mode=cfg.mode, scan_progress=None, + workers=cfg.workers) + scan_failed = False + try: + run_scan(cfg.input_folder, cfg.output_folder, cfg.log_file, + cfg.log_dir, cfg.mode, cfg.workers, cfg.min_age_minutes, + cfg.lidarr_url, cfg.lidarr_api_key) + except Exception: + logger.exception("Scan failed unexpectedly") + scan_failed = True + + if scan_cancelled: + if _wait_after_cancel(cfg): + break + continue + + _webui_app_state.update(status="idle", scan_progress=None) + + if not scan_failed and cfg.delete_after > 0: + try: + run_auto_delete( + cfg.log_dir, cfg.log_file, cfg.delete_after, + cfg.max_auto_delete, cfg.lidarr_url, cfg.lidarr_api_key, + cfg.lidarr_search, cfg.lidarr_blocklist) + except Exception: + logger.exception("Auto-delete failed unexpectedly") + + if shutdown_requested: + break + + if _post_scan_wait(cfg): + break + + +if __name__ == "__main__": + # Register as 'main' so that ``from main import …`` in webui.py + # finds THIS running instance instead of re-importing the file as + # a second module with separate global state. + sys.modules.setdefault("main", sys.modules[__name__]) + main() diff --git a/app/static/app.js b/app/static/app.js new file mode 100644 index 0000000..470ded3 --- /dev/null +++ b/app/static/app.js @@ -0,0 +1,1664 @@ +/* ============================================================ + BeatsCheck WebUI — Frontend Application + Patterns adapted from DAPS experimental frontend + ============================================================ */ + +// --- State --- +let currentPage = 'dashboard'; +let corruptFiles = []; +let pollTimer = null; +let logTimer = null; +let sortColumn = localStorage.getItem('beatscheck-sort-col') || null; +let sortDirection = localStorage.getItem('beatscheck-sort-dir') || 'asc'; +let configSnapshot = null; // tracks unsaved changes +let scanStartTime = null; +let scanStartCount = 0; +let logRawLines = []; // unfiltered log lines for client-side filtering +let isAuthenticated = false; +let corruptView = localStorage.getItem('beatscheck-corrupt-view') || 'files'; // 'files' or 'albums' +let corruptLoadId = 0; // monotonic ID to discard stale loadCorrupt() responses + +// --- Config metadata for form rendering --- +const CONFIG_SCHEMA = [ + { section: 'Scanning', + help: 'How and when BeatsCheck scans your music library for corruption.' }, + { key: 'music_dir', label: 'Scan Location', type: 'path', default: '/data', + desc: 'Folder to scan for audio files' }, + { key: 'mode', label: 'Scan Mode', type: 'select', options: ['setup','report','move'], default: 'setup', + desc: 'setup = idle (no scanning), report = scan and log only, move = quarantine corrupt files' }, + { key: 'workers', label: 'Workers', type: 'number', default: '4', + desc: 'Number of files checked in parallel. More = faster but uses more CPU (2-4 recommended)' }, + { key: 'run_interval', label: 'Scan Interval', type: 'number', default: '0', + desc: 'Hours between automatic scans. 0 = scan once then wait. 24 = daily. 168 = weekly' }, + { key: 'min_file_age', label: 'Min File Age', type: 'number', default: '30', + desc: 'Skip files modified in the last N minutes (avoids flagging active downloads)' }, + + { section: 'When Corrupt Files Are Found', + help: 'What happens after a scan finds corrupt files. By default, corrupt files are only logged — nothing is deleted unless you configure it here or use the Corrupt Files page.' }, + { key: 'delete_after', label: 'Auto-Delete After',type: 'number', default: '0', + desc: 'Automatically delete corrupt files after this many days. 0 = never (use Corrupt Files page to delete manually). 7 = one week review window' }, + { key: 'max_auto_delete', label: 'Safety Limit', type: 'number', default: '50', + desc: 'Abort auto-delete if more than this many files would be removed in one run. Prevents mass deletion from filesystem issues. 0 = no limit' }, + { key: 'output_dir', label: 'Quarantine Folder', type: 'path', default: '/data/corrupted', + desc: 'Only for move mode — corrupt files are moved here instead of deleted' }, + + { section: 'Lidarr (Automatic Re-download)', + help: 'Connect to Lidarr so deleted corrupt files are automatically re-downloaded. Monitored albums are re-searched by Lidarr after deletion — no extra config needed.' }, + { key: 'lidarr_url', label: 'Lidarr URL', type: 'text', default: '', + desc: 'Your Lidarr address, e.g. http://lidarr:8686 or http://192.168.1.100:8686' }, + { key: 'lidarr_api_key', label: 'API Key', type: 'password', default: '', + desc: 'Find this in Lidarr under Settings > General > API Key' }, + { key: 'lidarr_blocklist', label: 'Blocklist', type: 'select', options: ['false','true'], default: 'false', + desc: 'Blocklist the corrupt release before deleting so Lidarr downloads a different copy' }, + { key: 'lidarr_search', label: 'Search Unmonitored', type: 'select', options: ['false','true'], default: 'false', + desc: 'Queue search for unmonitored albums after auto-delete. Monitored albums are searched automatically by Lidarr' }, + + { section: 'Logging' }, + { key: 'log_level', label: 'Log Level', type: 'select', options: ['DEBUG','INFO','WARNING','ERROR'], default: 'INFO', + desc: 'INFO = normal. DEBUG = verbose (for troubleshooting). WARNING/ERROR = quiet' }, + { key: 'max_log_mb', label: 'Max Log Size (MB)', type: 'number', default: '50', + desc: 'Rotate log when it exceeds this size. Triggers a fresh full rescan. 0 = never rotate' }, + + { section: 'Web Interface' }, + { key: 'webui', label: 'Enabled', type: 'select', options: ['false','true'], default: 'false', + desc: 'Enable the web interface (requires container restart to take effect)' }, + { key: 'webui_port', label: 'Port', type: 'number', default: '8484', + desc: 'Port number for the web interface (requires container restart)' }, +]; + +// --- API helpers --- +async function api(path, opts = {}) { + try { + const res = await fetch('/api/' + path, { + headers: { 'Content-Type': 'application/json' }, + ...opts, + }); + if (res.status === 401) { + // Session expired — redirect to login + isAuthenticated = false; + showAuthPage(); + return null; + } + if (!res.ok) { + const data = await res.json().catch(() => ({})); + console.error('API error:', res.status, data.error || res.statusText); + return null; + } + return await res.json(); + } catch (e) { + console.error('API error:', e); + return null; + } +} + +function apiPost(path, body) { + return api(path, { method: 'POST', body: JSON.stringify(body) }); +} + +// --- Authentication --- +async function checkAuth() { + try { + const res = await fetch('/api/auth-status'); + const data = await res.json(); + if (data.setup_required) { + showPage('setup'); + return; + } + if (!data.authenticated) { + showPage('login'); + return; + } + isAuthenticated = true; + showApp(); + } catch (e) { + showPage('login'); + } +} + +function showPage(page) { + document.querySelectorAll('.page').forEach(p => p.classList.remove('active')); + const el = document.getElementById('page-' + page); + if (el) el.classList.add('active'); + const title = document.getElementById('screen-title'); + if (title) title.textContent = SCREEN_TITLES[page] || 'BeatsCheck'; + // Hide/show app chrome for auth pages + const sidebar = document.getElementById('sidebar'); + const logoutBtn = document.getElementById('logout-btn'); + if (page === 'login' || page === 'setup') { + stopStatusPoll(); + sidebar.style.display = 'none'; + if (logoutBtn) logoutBtn.style.display = 'none'; + document.body.classList.add('auth-view'); + } else { + sidebar.style.display = ''; + if (logoutBtn) logoutBtn.style.display = ''; + document.body.classList.remove('auth-view'); + } +} + +function showApp() { + document.body.classList.remove('auth-view'); + const sidebar = document.getElementById('sidebar'); + sidebar.style.display = ''; + const logoutBtn = document.getElementById('logout-btn'); + if (logoutBtn) logoutBtn.style.display = ''; + initRouter(); +} + +function showAuthPage() { + stopStatusPoll(); + stopLogPoll(); + checkAuth(); +} + +async function submitAuth(e, endpoint, body, errorEl, failLabel) { + e.preventDefault(); + errorEl.textContent = ''; + const btn = e.target.querySelector('[type="submit"]'); + btn.disabled = true; + try { + const res = await fetch('/api/' + endpoint, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + }); + const data = await res.json(); + if (res.ok && data.ok) { + isAuthenticated = true; + showApp(); + } else { + errorEl.textContent = data.error || failLabel; + } + } catch (err) { + errorEl.textContent = 'Connection error'; + } + btn.disabled = false; +} + +function doSetup(e) { + const username = document.getElementById('setup-username').value.trim(); + const password = document.getElementById('setup-password').value; + const confirm = document.getElementById('setup-confirm').value; + const error = document.getElementById('setup-error'); + if (!username) { e.preventDefault(); error.textContent = 'Username is required'; return; } + if (password.length < 8) { e.preventDefault(); error.textContent = 'Password must be at least 8 characters'; return; } + if (password !== confirm) { e.preventDefault(); error.textContent = 'Passwords do not match'; return; } + return submitAuth(e, 'setup', { username, password }, error, 'Setup failed'); +} + +function doLogin(e) { + const username = document.getElementById('login-username').value.trim(); + const password = document.getElementById('login-password').value; + const error = document.getElementById('login-error'); + if (!username || !password) { + e.preventDefault(); + error.textContent = 'Username and password required'; + return; + } + return submitAuth(e, 'login', { username, password }, error, 'Login failed'); +} + +async function doLogout() { + await apiPost('logout', {}); + isAuthenticated = false; + stopStatusPoll(); + stopLogPoll(); + showPage('login'); + // Clear form fields + const fields = ['login-username', 'login-password']; + fields.forEach(id => { const el = document.getElementById(id); if (el) el.value = ''; }); +} + +// --- Theme --- +function initTheme() { + const saved = localStorage.getItem('beatscheck-theme') || 'dark'; + document.documentElement.setAttribute('data-theme', saved); + updateThemeIcon(saved); +} + +function toggleTheme() { + const current = document.documentElement.getAttribute('data-theme'); + const next = current === 'dark' ? 'light' : 'dark'; + document.documentElement.setAttribute('data-theme', next); + localStorage.setItem('beatscheck-theme', next); + updateThemeIcon(next); +} + +function updateThemeIcon(theme) { + const btn = document.getElementById('theme-toggle'); + // Header toggle is labeled with the *other* theme's name (per design). + btn.textContent = theme === 'dark' ? 'Light' : 'Dark'; +} + +// --- Navigation --- +const SCREEN_TITLES = { + dashboard: 'Dashboard', corrupt: 'Corrupt files', config: 'Configuration', + logs: 'Logs', login: 'Login', setup: 'Setup wizard', +}; + +function navigate(page) { + if (currentPage === 'config' && page !== 'config' && hasUnsavedConfig()) { + if (!confirm('You have unsaved configuration changes. Leave anyway?')) return; + } + + currentPage = page; + document.querySelectorAll('.page').forEach(p => p.classList.remove('active')); + document.querySelectorAll('.nav-link').forEach(n => n.classList.remove('active')); + const el = document.getElementById('page-' + page); + if (el) el.classList.add('active'); + const nav = document.querySelector(`[data-page="${page}"]`); + if (nav) nav.classList.add('active'); + + const title = document.getElementById('screen-title'); + if (title) title.textContent = SCREEN_TITLES[page] || 'BeatsCheck'; + + closeSidebar(); + + // Status drives the header pill + sidebar card on every screen, so the + // poll runs continuously while authenticated (not just on the dashboard). + startStatusPoll(); + + if (page === 'dashboard') loadRecentFlagged(); + if (page === 'corrupt') loadCorrupt(); + if (page === 'config') loadConfig(); + if (page === 'logs') { refreshLogs(); startLogPoll(); } + else stopLogPoll(); +} + +function initRouter() { + window.addEventListener('hashchange', () => { + const page = location.hash.slice(1) || 'dashboard'; + navigate(page); + }); + const initial = location.hash.slice(1) || 'dashboard'; + navigate(initial); +} + +// --- Mobile sidebar --- +function initSidebar() { + const toggle = document.getElementById('menu-toggle'); + toggle.addEventListener('click', () => { + document.getElementById('sidebar').classList.toggle('open'); + getOrCreateOverlay().classList.toggle('visible'); + }); + + const sidebar = document.getElementById('sidebar'); + sidebar.addEventListener('keydown', (e) => { + const links = Array.from(sidebar.querySelectorAll('.nav-link')); + const idx = links.indexOf(document.activeElement); + if (idx === -1) return; + if (e.key === 'ArrowDown') { + e.preventDefault(); + links[(idx + 1) % links.length].focus(); + } else if (e.key === 'ArrowUp') { + e.preventDefault(); + links[(idx - 1 + links.length) % links.length].focus(); + } + }); +} + +function closeSidebar() { + document.getElementById('sidebar').classList.remove('open'); + const overlay = document.querySelector('.sidebar-overlay'); + if (overlay) overlay.classList.remove('visible'); +} + +function getOrCreateOverlay() { + let overlay = document.querySelector('.sidebar-overlay'); + if (!overlay) { + overlay = document.createElement('div'); + overlay.className = 'sidebar-overlay'; + overlay.addEventListener('click', closeSidebar); + document.body.appendChild(overlay); + } + return overlay; +} + +// --- Dashboard --- +function formatUptime(secs) { + if (!secs && secs !== 0) return '--'; + const d = Math.floor(secs / 86400); + const h = Math.floor((secs % 86400) / 3600); + const m = Math.floor((secs % 3600) / 60); + if (d > 0) return d + 'd ' + h + 'h'; + if (h > 0) return h + 'h ' + m + 'm'; + return m + 'm'; +} + +function formatSize(bytes) { + if (!bytes) return '0 B'; + if (bytes >= 1024**4) return (bytes / 1024**4).toFixed(1) + ' TB'; + if (bytes >= 1024**3) return (bytes / 1024**3).toFixed(1) + ' GB'; + if (bytes >= 1024**2) return (bytes / 1024**2).toFixed(1) + ' MB'; + if (bytes >= 1024) return (bytes / 1024).toFixed(1) + ' KB'; + return bytes + ' B'; +} + +let prevCardValues = {}; +let lastDashCorruptCount = null; +let wasScanning = false; + +function formatNumber(n) { + if (n == null || isNaN(n)) return '--'; + return Number(n).toLocaleString('en-US'); +} + +function capitalize(s) { return s ? s.charAt(0).toUpperCase() + s.slice(1) : s; } + +async function refreshDashboard() { + const data = await api('status'); + if (!data) return; + + const version = 'v' + (data.version || '?'); + document.getElementById('version-badge').textContent = version; + const logoVer = document.getElementById('logo-version'); + if (logoVer) logoVer.textContent = version + ' \u00b7 integrity'; + + const status = data.status || 'unknown'; + const isScanning = status === 'scanning'; + const statusWord = capitalize(status); + const summary = data.summary || {}; + const prog = data.scan_progress; + const pct = (prog && prog.total > 0) ? Math.round((prog.current / prog.total) * 100) : 0; + + // --- Header status pill --- + const dot = document.getElementById('status-indicator'); + dot.className = 'status-dot ' + status; + setText('status-text', isScanning ? ('Scanning \u00b7 ' + pct + '%') : statusWord); + + // --- Sidebar status card --- + const sideDot = document.getElementById('sidebar-status-dot'); + if (sideDot) sideDot.className = 'status-dot ' + status; + setText('sidebar-status-word', statusWord); + const workers = data.workers != null ? data.workers : null; + const sideMeta = document.getElementById('sidebar-status-meta'); + if (sideMeta) { + const line1 = (workers != null ? workers + ' workers' : 'idle') + ' \u00b7 nice(10)'; + const line2 = (data.mode ? data.mode : 'setup') + ' mode'; + sideMeta.innerHTML = escHtml(line1) + '
' + escHtml(line2); + } + + // --- Stat cards --- + const dashStatusIcon = document.getElementById('dash-status-icon'); + if (dashStatusIcon) dashStatusIcon.className = 'status-dot ' + status; + setCardValue('dash-status', statusWord); + setCardValue('dash-mode', capitalize(data.mode || '--')); + setCardValue('dash-workers', workers != null ? workers : '--'); + setCardValue('dash-uptime', formatUptime(data.uptime)); + + // live corrupt count wins during a scan; otherwise the finished summary + const corruptNum = isScanning && data.corrupt_count != null + ? data.corrupt_count + : (summary.corrupted != null ? summary.corrupted : null); + setCardValue('dash-corrupt', corruptNum != null ? formatNumber(corruptNum) : '--'); + setText('dash-corrupt-sub', summary.corrupt_size_human + ? ('flagged \u00b7 ' + summary.corrupt_size_human) : 'flagged'); + + setCardValue('dash-library', summary.library_files != null + ? formatNumber(summary.library_files) + : (summary.library_size_human || '--')); + setText('dash-library-sub', summary.library_size_human + ? ('files \u00b7 ' + summary.library_size_human) : ''); + + // --- Sidebar nav badge --- + updateNavCorruptBadge(corruptNum); + + // --- Hero: scanning spectrogram vs idle card --- + const scanHero = document.getElementById('scan-hero'); + const idleHero = document.getElementById('idle-hero'); + if (isScanning && prog) { + if (idleHero) idleHero.style.display = 'none'; + if (scanHero) scanHero.style.display = ''; + renderScanHero(prog, pct, corruptNum || 0, workers); + } else if (status === 'idle') { + if (scanHero) scanHero.style.display = 'none'; + if (idleHero) idleHero.style.display = ''; + renderIdleHero(summary, corruptNum); + scanStartTime = null; + } else { + // starting / setup / unknown \u2014 no hero + if (scanHero) scanHero.style.display = 'none'; + if (idleHero) idleHero.style.display = 'none'; + scanStartTime = null; + } + + // --- Action buttons --- + document.querySelectorAll('.action-bar .btn:not(#cancel-scan-btn)').forEach(b => { + b.disabled = isScanning; + }); + const cancelBtn = document.getElementById('cancel-scan-btn'); + if (cancelBtn) { + cancelBtn.style.display = isScanning ? '' : 'none'; + cancelBtn.disabled = false; + } + + // Refresh the recently-flagged list when corrupt count changes or a scan + // just finished. + if (currentPage === 'dashboard') { + if (corruptNum !== lastDashCorruptCount || (wasScanning && !isScanning)) { + loadRecentFlagged(); + } + } + lastDashCorruptCount = corruptNum; + wasScanning = isScanning; +} + +function updateNavCorruptBadge(count) { + const badge = document.getElementById('nav-corrupt-badge'); + if (!badge) return; + const n = count || 0; + badge.textContent = n; + badge.classList.toggle('zero', n === 0); +} + +function renderIdleHero(summary, corruptNum) { + const meta = document.getElementById('idle-hero-meta'); + if (!meta) return; + const parts = []; + if (summary.finished) parts.push('Last scan ' + summary.finished); + if (summary.library_files != null) parts.push(formatNumber(summary.library_files) + ' files'); + let html = parts.map(escHtml).join(' \u00b7 '); + if (corruptNum != null) { + html += (html ? ' \u00b7 ' : '') + '' + formatNumber(corruptNum) + ' flagged'; + } + meta.innerHTML = html || 'No scan run yet'; +} + +function renderScanHero(prog, pct, corruptNum, workers) { + setText('scan-hero-pct', pct + '%'); + const checked = formatNumber(prog.current) + ' / ' + formatNumber(prog.total); + setText('scan-hero-checked', checked); + setText('shero-checked', checked); + setText('scan-hero-file', prog.file || ''); + setText('shero-corrupt', (corruptNum || 0) + ' corrupt found'); + setText('shero-workers', (workers != null ? workers : '?') + ' workers active'); + + // ETA \u2014 same rate model as before (reset if a fresh scan rewinds the count) + if (!scanStartTime || scanStartCount > prog.current) { + scanStartTime = Date.now(); + scanStartCount = prog.current; + } + const elapsed = (Date.now() - scanStartTime) / 1000; + const done = prog.current - scanStartCount; + let etaText; + if (prog.current >= prog.total) { + etaText = 'Finalizing\u2026'; + } else if (done > 10 && elapsed > 0) { + const rate = done / elapsed; + const remaining = (prog.total - prog.current) / rate; + etaText = 'ETA ' + formatUptime(Math.round(remaining)); + } else { + etaText = 'ETA calculating\u2026'; + } + setText('shero-eta', etaText); + + updateSpectrogram(pct / 100, corruptNum || 0); +} + +// --- Spectrogram visualization --- +const SPEC_N = 150; +const SPEC_SPECTRUM = ['#6c5cff', '#4b86ff', '#34e0a0', '#9ad94a', '#f5c24a', '#ff7a4d']; +const SPEC_CORRUPT_IDX = [11, 22, 38, 47, 61, 74, 89, 103, 118, 131]; +let specBars = []; + +function buildSpectrogram() { + const container = document.getElementById('spectrogram'); + if (!container || specBars.length) return; + const playhead = document.getElementById('spectrogram-playhead'); + let seed = 7; + const rnd = () => { seed = (seed * 1103515245 + 12345) & 0x7fffffff; return ((seed >> 8) / 0x7fffff) % 1; }; + specBars = []; + for (let i = 0; i < SPEC_N; i++) { + let h = Math.max(8, Math.min(98, 18 + Math.abs(Math.sin(i * 0.19)) * 42 + rnd() * 36)); + const color = SPEC_SPECTRUM[Math.min(5, Math.floor(h / 17))]; + const el = document.createElement('div'); + el.className = 'spec-bar'; + el.style.height = h + '%'; + el.style.background = color; + el.style.opacity = '0.15'; + container.insertBefore(el, playhead); + specBars.push({ el, h, color, corruptSlot: SPEC_CORRUPT_IDX.indexOf(i) }); + } +} + +function updateSpectrogram(prog, corruptCount) { + buildSpectrogram(); + const playhead = document.getElementById('spectrogram-playhead'); + const wash = document.getElementById('spectrogram-wash'); + const leftPct = Math.max(0, Math.min(100, prog * 100)); + if (playhead) playhead.style.left = leftPct + '%'; + if (wash) wash.style.width = leftPct + '%'; + const spec = document.getElementById('spectrogram'); + if (spec) spec.setAttribute('aria-valuenow', Math.round(leftPct)); + + const scannedCount = Math.floor(prog * SPEC_N); + specBars.forEach((b, i) => { + const scanned = i < scannedCount; + // mark a corrupt bar if its slot index is within the discovered count + const isCorrupt = b.corruptSlot >= 0 && b.corruptSlot < corruptCount && scanned; + if (isCorrupt) { + b.el.style.background = 'var(--dg, #ff4d6d)'; + b.el.style.height = Math.min(100, b.h + 16) + '%'; + b.el.style.opacity = '1'; + b.el.style.boxShadow = '0 0 8px rgba(255,77,109,.85)'; + } else { + b.el.style.background = b.color; + b.el.style.height = b.h + '%'; + b.el.style.opacity = scanned ? '0.92' : '0.15'; + b.el.style.boxShadow = 'none'; + } + }); +} + +// --- Recently flagged (dashboard) --- +async function loadRecentFlagged() { + const list = document.getElementById('recent-list'); + if (!list) return; + const data = await api('corrupt'); + if (!data) return; + const files = data.files || []; + if (files.length === 0) { + list.innerHTML = '
No corrupt files \u2014 library is clean.
'; + return; + } + // corrupt.txt is append-ordered (newest last) \u2014 show the most recent first. + const recent = files.slice(-6).reverse(); + list.innerHTML = recent.map(f => { + const name = f.path.split('/').pop(); + return `
+
+
+
${escHtml(name)}
+
${escHtml(f.reason || 'Corrupt')}
+
+
${f.missing ? 'N/A' : formatSize(f.size)}
+
`; + }).join(''); +} + +function setCardValue(id, val) { + const el = document.getElementById(id); + if (!el) return; + const strVal = String(val); + if (prevCardValues[id] !== undefined && prevCardValues[id] !== strVal) { + el.classList.add('changed'); + setTimeout(() => el.classList.remove('changed'), 600); + } + prevCardValues[id] = strVal; + el.textContent = val; +} + +function setText(id, val) { + const el = document.getElementById(id); + if (el) el.textContent = val; +} + +// --- Corrupt Files --- +async function loadCorrupt() { + const thisLoad = ++corruptLoadId; + const data = await api('corrupt'); + if (thisLoad !== corruptLoadId) return; // stale response + if (!data) { + document.getElementById('corrupt-tbody').innerHTML = + 'Failed to load data'; + return; + } + corruptFiles = data.files || []; + document.getElementById('corrupt-count').textContent = corruptFiles.length; + updateNavCorruptBadge(corruptFiles.length); + const viewBtn = document.getElementById('view-toggle-btn'); + if (viewBtn) viewBtn.textContent = corruptView === 'files' ? 'Group by album' : 'Flat view'; + applyCorruptFilters(); + + // Check if scanning — disable deletes and show banner + const status = await api('status'); + const scanning = status && status.status === 'scanning'; + const banner = document.getElementById('corrupt-scan-banner'); + if (banner) banner.style.display = scanning ? '' : 'none'; + document.querySelectorAll('#page-corrupt .btn-danger, #page-corrupt .btn-primary').forEach(b => b.disabled = scanning); + document.querySelectorAll('#page-corrupt .file-check, #page-corrupt .album-check, #select-all').forEach(c => c.disabled = scanning); + + // Show Lidarr info banner if any files have Lidarr IDs + const hasAnyLidarr = corruptFiles.some(f => f.has_lidarr_id); + const infoBanner = document.getElementById('corrupt-info-banner'); + if (infoBanner) { + if (hasAnyLidarr) { + const cfgData = await api('config'); + const cfgEntries = cfgData && cfgData.config ? cfgData.config : []; + const cfgVal = key => { const e = cfgEntries.find(c => c.key === key); return e ? e.value : ''; }; + const blocklist = cfgVal('lidarr_blocklist') === 'true'; + const searchUnmon = cfgVal('lidarr_search') === 'true'; + const blTag = blocklist + ? 'Enabled' + : 'Disabled'; + const collapsed = localStorage.getItem('beatscheck-info-collapsed') === '1'; + const lines = []; + lines.push('Delete — removes corrupt files. Lidarr-tracked files are deleted via the Lidarr API' + + (blocklist ? ', the bad release is blocklisted,' : '') + + ' and BeatsCheck waits for each album to finish searching before moving to the next.'); + lines.push('Delete Album — removes the entire album folder (same Lidarr flow for tracked files).'); + lines.push('Monitored albums are automatically re-searched by Lidarr.' + + (searchUnmon ? ' Unmonitored albums will also be searched.' : ' Unmonitored albums will not be re-downloaded.')); + lines.push('Successful re-downloads are logged to beats_check.log on the next scan cycle.'); + lines.push('Blocklist: ' + blTag + + (blocklist ? ' — bad releases will be blocklisted to prevent re-grabbing the same corrupt version.' : ' — Lidarr may re-grab the same release. Enable in Settings to prevent this.')); + infoBanner.innerHTML = '
' + + 'Lidarr Delete Info' + + '' + + '
' + + '
' + lines.join('
') + '
'; + infoBanner.style.display = ''; + } else { + infoBanner.style.display = 'none'; + } + } +} + +function toggleInfoBanner() { + const body = document.querySelector('.info-banner-body'); + const toggle = document.querySelector('.info-banner-toggle'); + if (!body || !toggle) return; + const collapsed = !body.classList.contains('collapsed'); + body.classList.toggle('collapsed', collapsed); + toggle.innerHTML = collapsed ? '▶' : '▼'; + localStorage.setItem('beatscheck-info-collapsed', collapsed ? '1' : '0'); +} + +function toggleCorruptView() { + corruptView = corruptView === 'files' ? 'albums' : 'files'; + localStorage.setItem('beatscheck-corrupt-view', corruptView); + const btn = document.getElementById('view-toggle-btn'); + if (btn) btn.textContent = corruptView === 'files' ? 'Group by album' : 'Flat view'; + applyCorruptFilters(); + updateAlbumHelperVisibility(); +} + +function applyCorruptFilters() { + const q = (document.getElementById('corrupt-search').value || '').toLowerCase(); + let filtered = corruptFiles; + if (q) { + filtered = filtered.filter(f => + f.path.toLowerCase().includes(q) || (f.reason || '').toLowerCase().includes(q) + ); + } + if (corruptView === 'albums') { + renderCorruptAlbums(filtered); + } else { + if (sortColumn) { + filtered = [...filtered].sort((a, b) => { + let va, vb; + if (sortColumn === 'path') { va = a.path; vb = b.path; } + else if (sortColumn === 'reason') { va = a.reason || ''; vb = b.reason || ''; } + else if (sortColumn === 'size') { va = a.size || 0; vb = b.size || 0; } + else return 0; + if (typeof va === 'string') { + const cmp = va.localeCompare(vb); + return sortDirection === 'asc' ? cmp : -cmp; + } + return sortDirection === 'asc' ? va - vb : vb - va; + }); + } + renderCorruptTable(filtered); + updateSortIndicators(); + } +} + +function renderCorruptAlbums(files) { + const tbody = document.getElementById('corrupt-tbody'); + if (files.length === 0) { + tbody.innerHTML = 'No corrupt files — library is clean.'; + return; + } + // Group by parent folder + const albums = {}; + files.forEach(f => { + const dir = f.path.split('/').slice(0, -1).join('/'); + if (!albums[dir]) albums[dir] = []; + albums[dir].push(f); + }); + const sorted = Object.entries(albums).sort((a, b) => a[0].localeCompare(b[0])); + let html = ''; + sorted.forEach(([dir, tracks]) => { + const albumName = dir.split('/').slice(-2).join(' / '); + const totalSize = tracks.reduce((s, f) => s + (f.size || 0), 0); + const allMissing = tracks.every(f => f.missing); + const albumTotal = tracks[0]?.album_total || tracks.length; + const allCorrupt = tracks.length >= albumTotal; + const safeDir = escHtml(dir); + const countLabel = allCorrupt + ? `All ${tracks.length} files corrupt` + : `${tracks.length} of ${albumTotal} files corrupt`; + html += ` + + ${escHtml(albumName)}
${countLabel} + ${formatSize(totalSize)} + + + + + + `; + tracks.forEach(f => { + const name = f.path.split('/').pop(); + const safePath = escHtml(f.path); + const cls = f.missing ? 'file-missing' : ''; + html += ` + + ${escHtml(name)} + ${escHtml(f.reason || '')} + ${f.missing ? 'N/A' : formatSize(f.size)} + + `; + }); + }); + tbody.innerHTML = html; + updateAlbumHelperVisibility(); +} + +function toggleAlbumExpand(row) { + const dir = row.querySelector('.album-check')?.dataset.dir; + if (!dir) return; + const files = document.querySelectorAll(`tr.album-file[data-album="${dir}"]`); + const visible = files[0]?.style.display !== 'none'; + files.forEach(f => f.style.display = visible ? 'none' : ''); + row.classList.toggle('expanded', !visible); +} + +function toggleAlbumSelect(checkbox) { + const dir = checkbox.dataset.dir; + const checked = checkbox.checked; + document.querySelectorAll(`tr.album-file[data-album="${dir}"] .file-check`).forEach(c => c.checked = checked); + updateDeleteBtn(); +} + +function updateAlbumHelperVisibility() { + const helper = document.getElementById('album-select-helper'); + const bulkBtn = document.getElementById('delete-albums-btn'); + if (!helper || !bulkBtn) return; + const isAlbums = corruptView === 'albums'; + helper.style.display = isAlbums ? '' : 'none'; + bulkBtn.style.display = isAlbums ? '' : 'none'; +} + +function selectNAlbums() { + const input = document.getElementById('select-n-input'); + let n = parseInt(input.value, 10); + if (isNaN(n) || n < 1) n = 1; + if (n > 50) n = 50; + input.value = n; + const boxes = document.querySelectorAll('tr.album-header:not([style*="display: none"]) .album-check'); + let selected = 0; + boxes.forEach(b => { + if (selected < n) { + if (!b.checked) { + b.checked = true; + toggleAlbumSelect(b); + } + selected++; + } + }); + if (selected < n) { + showToast(`Selected ${selected} album(s) (fewer than requested — only ${selected} visible)`, 'info'); + } else { + showToast(`Selected ${selected} album(s)`, 'success'); + } +} + +async function deleteAlbumWhole(dir, totalFiles) { + const msg = `Delete the entire album folder?\n\n${dir}\n\nThis will delete ALL ${totalFiles || '?'} file(s) in the folder, not just the corrupt ones.`; + if (!confirm(msg)) return; + const res = await startDeleteJob([dir], 'whole'); + if (res) openDeleteProgress(res.job_id, res.total, 'whole'); +} + +async function deleteSelectedAlbums() { + const boxes = document.querySelectorAll('.album-check:checked'); + const dirs = Array.from(boxes).map(b => b.dataset.dir).filter(Boolean); + if (dirs.length === 0) return; + if (dirs.length > 50) { + showToast('Maximum 50 albums per bulk delete', 'error'); + return; + } + const estMin = Math.ceil((dirs.length * 35) / 60); + const msg = `Delete ${dirs.length} whole album folder(s)?\n\n` + + `Albums are processed one at a time with a 30s delay between each to avoid flooding Lidarr and indexers.\n\n` + + `Estimated time: ~${estMin} minute(s).\n\nThis cannot be undone.`; + if (!confirm(msg)) return; + const res = await startDeleteJob(dirs, 'whole'); + if (res) openDeleteProgress(res.job_id, res.total, 'whole'); +} + +async function startDeleteJob(dirs, mode) { + try { + const r = await fetch('/api/delete-albums', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ folders: dirs, mode }), + }); + if (r.status === 202) { + return await r.json(); + } + const err = await r.json().catch(() => ({})); + showToast('Delete failed: ' + (err.error || ('HTTP ' + r.status)), 'error'); + return null; + } catch (e) { + showToast('Delete request failed: ' + e.message, 'error'); + return null; + } +} + +let deleteProgressInterval = null; +let currentDeleteJobId = null; + +function openDeleteProgress(jobId, total, mode) { + currentDeleteJobId = jobId; + const modal = document.getElementById('delete-progress-modal'); + const title = document.getElementById('delete-progress-title'); + if (title) title.textContent = mode === 'whole' ? 'Deleting Albums' : 'Deleting Files'; + document.getElementById('delete-progress-text').textContent = `0 / ${total}`; + document.getElementById('delete-progress-phase').textContent = 'Starting...'; + document.getElementById('delete-progress-current').textContent = ''; + document.getElementById('delete-progress-errors').style.display = 'none'; + document.getElementById('delete-progress-cancel').style.display = ''; + document.getElementById('delete-progress-cancel').disabled = false; + document.getElementById('delete-progress-close').style.display = 'none'; + document.getElementById('delete-progress-fill').style.width = '0%'; + modal.style.display = 'flex'; + if (deleteProgressInterval) clearInterval(deleteProgressInterval); + deleteProgressInterval = setInterval(() => pollDeleteJob(jobId), 2000); + pollDeleteJob(jobId); +} + +async function pollDeleteJob(jobId) { + const r = await fetch('/api/delete-job-status?id=' + encodeURIComponent(jobId)); + if (!r.ok) return; + const job = await r.json(); + const total = job.total || 1; + const done = job.done || 0; + const pct = Math.min(100, Math.round((done / total) * 100)); + document.getElementById('delete-progress-fill').style.width = pct + '%'; + document.getElementById('delete-progress-text').textContent = `${done} / ${total}`; + let phaseLabel = job.phase; + if (job.phase === 'running') phaseLabel = 'Running'; + else if (job.phase === 'waiting') phaseLabel = 'Waiting 30s before next album...'; + else if (job.phase === 'deleting') phaseLabel = 'Deleting current album...'; + else if (job.phase === 'done') phaseLabel = 'Complete'; + else if (job.phase === 'cancelled') phaseLabel = 'Cancelled'; + else if (job.phase === 'error') phaseLabel = 'Error'; + document.getElementById('delete-progress-phase').textContent = phaseLabel; + document.getElementById('delete-progress-current').textContent = job.current ? job.current : ''; + if (job.errors && job.errors.length) { + const errBox = document.getElementById('delete-progress-errors'); + errBox.style.display = ''; + errBox.innerHTML = job.errors.slice(0, 10) + .map(e => `${escHtml(e.folder || e.path || '')} — ${escHtml(e.error || '')}`) + .join('
'); + } + if (job.finished) { + clearInterval(deleteProgressInterval); + deleteProgressInterval = null; + document.getElementById('delete-progress-cancel').style.display = 'none'; + document.getElementById('delete-progress-close').style.display = ''; + const verb = job.cancelled ? 'Cancelled' : 'Done'; + showToast(`${verb} — ${job.deleted || 0} file(s) deleted`, job.cancelled ? 'info' : 'success'); + loadCorrupt(); + refreshDashboard(); + } +} + +async function cancelDeleteJob() { + if (!currentDeleteJobId) return; + const btn = document.getElementById('delete-progress-cancel'); + btn.disabled = true; + btn.textContent = 'Cancelling...'; + try { + await fetch('/api/delete-job-cancel?id=' + encodeURIComponent(currentDeleteJobId), { method: 'POST' }); + } catch (e) { + showToast('Cancel request failed', 'error'); + } +} + +function closeDeleteProgress() { + const modal = document.getElementById('delete-progress-modal'); + modal.style.display = 'none'; + currentDeleteJobId = null; + if (deleteProgressInterval) { + clearInterval(deleteProgressInterval); + deleteProgressInterval = null; + } + const btn = document.getElementById('delete-progress-cancel'); + if (btn) btn.textContent = 'Cancel'; +} + +async function deleteAlbum(dir) { + const files = document.querySelectorAll(`tr.album-file[data-album="${dir}"] .file-check`); + const paths = Array.from(files).map(c => c.dataset.path).filter(Boolean); + if (!paths.length || !confirm('Permanently delete ' + paths.length + ' corrupt file(s) from this album?')) return; + const btns = document.querySelectorAll(`tr.album-header .btn`); + btns.forEach(b => b.disabled = true); + const res = await startDeleteFilesJob(paths); + btns.forEach(b => b.disabled = false); + if (res) openDeleteProgress(res.job_id, res.total, 'files'); +} + +async function startDeleteFilesJob(paths) { + try { + const r = await fetch('/api/delete-files', { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({files: paths}) + }); + if (!r.ok) { + const err = await r.json().catch(() => ({})); + showToast('Delete failed: ' + (err.error || ('HTTP ' + r.status)), 'error'); + return null; + } + return await r.json(); + } catch (e) { + showToast('Delete request failed: ' + e.message, 'error'); + return null; + } +} + +async function clearCorruptList() { + const paths = corruptFiles.map(f => f.path).filter(Boolean); + if (!paths.length) { + showToast('No corrupt files to clear', 'info'); + return; + } + if (!confirm('Clear all ' + paths.length + ' file(s) from the corrupt list?\n\nThey will reappear if found corrupt on the next scan.')) return; + const res = await apiPost('ignore', { files: paths }); + if (res && res.ok) { + showToast('Corrupt list cleared', 'success'); + loadCorrupt(); + } else { + showToast('Clear failed', 'error'); + } +} + +async function ignoreAlbum(dir) { + // Remove all files in this album from corrupt.txt (hide until next scan) + const files = document.querySelectorAll(`tr.album-file[data-album="${dir}"] .file-check`); + const paths = Array.from(files).map(c => c.dataset.path).filter(Boolean); + if (!paths.length) return; + const res = await apiPost('ignore', { files: paths }); + if (res && res.ok) { + showToast('Album ignored — will reappear if found corrupt on next scan', 'info'); + loadCorrupt(); + } else { + showToast('Ignore failed', 'error'); + } +} + +function sortTable(col) { + if (sortColumn === col) { + sortDirection = sortDirection === 'asc' ? 'desc' : 'asc'; + } else { + sortColumn = col; + sortDirection = 'asc'; + } + localStorage.setItem('beatscheck-sort-col', sortColumn); + localStorage.setItem('beatscheck-sort-dir', sortDirection); + applyCorruptFilters(); +} + +function updateSortIndicators() { + document.querySelectorAll('thead th.sortable').forEach(th => { + th.classList.remove('sort-asc', 'sort-desc'); + if (th.dataset.sort === sortColumn) { + th.classList.add('sort-' + sortDirection); + th.setAttribute('aria-sort', sortDirection === 'asc' ? 'ascending' : 'descending'); + } else { + th.setAttribute('aria-sort', 'none'); + } + }); +} + +function renderCorruptTable(files) { + const tbody = document.getElementById('corrupt-tbody'); + if (files.length === 0) { + tbody.innerHTML = 'No corrupt files — library is clean.'; + return; + } + tbody.innerHTML = files.map(f => { + const cls = f.missing ? 'file-missing' : ''; + const name = f.path.split('/').pop(); + const dir = f.path.split('/').slice(0, -1).join('/'); + const safePath = escHtml(f.path); + const reason = f.reason ? `
${escHtml(f.reason)}` : ''; + return ` + +
${escHtml(name)}${reason}
${escHtml(dir)}
+ ${f.missing ? 'N/A' : formatSize(f.size)} + + `; + }).join(''); +} + +function escHtml(s) { + // Escapes for BOTH text and double-quoted attribute contexts. The + // textContent trick escaped &<> but not " or ', letting a file/folder + // name break out of data-*/title/aria-label attributes (XSS) or a + // single-quoted inline handler. + return String(s) + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + +// Search filter +document.addEventListener('DOMContentLoaded', () => { + const search = document.getElementById('corrupt-search'); + if (search) { + let debounceTimer; + search.addEventListener('input', () => { + clearTimeout(debounceTimer); + debounceTimer = setTimeout(applyCorruptFilters, 150); + }); + } +}); + +function toggleSelectAll(el) { + document.querySelectorAll('.file-check').forEach(c => c.checked = el.checked); + document.querySelectorAll('.album-check').forEach(c => c.checked = el.checked); + updateDeleteBtn(); +} + +function updateDeleteBtn() { + const checked = document.querySelectorAll('.file-check:checked'); + const any = checked.length > 0; + document.getElementById('delete-selected-btn').disabled = !any; + const albumsBtn = document.getElementById('delete-albums-btn'); + if (albumsBtn) { + const checkedAlbums = document.querySelectorAll('.album-check:checked').length; + albumsBtn.disabled = checkedAlbums === 0 || checkedAlbums > 50; + albumsBtn.textContent = checkedAlbums > 0 + ? `Delete ${checkedAlbums} Album${checkedAlbums === 1 ? '' : 's'} (Whole)` + : 'Delete Albums (Whole)'; + } +} + +async function deleteSingle(el) { + const path = el.dataset.path; + if (!path || !confirm('Delete ' + path + '?')) return; + el.disabled = true; + const res = await startDeleteFilesJob([path]); + el.disabled = false; + if (res) openDeleteProgress(res.job_id, res.total, 'files'); +} + +async function deleteSelected() { + const checks = document.querySelectorAll('.file-check:checked'); + const paths = Array.from(checks).map(c => c.dataset.path).filter(Boolean); + if (paths.length === 0) return; + if (!confirm('Delete ' + paths.length + ' file(s)?')) return; + const btn = document.getElementById('delete-selected-btn'); + btn.disabled = true; + const res = await startDeleteFilesJob(paths); + btn.disabled = false; + if (res) { + document.getElementById('select-all').checked = false; + openDeleteProgress(res.job_id, res.total, 'files'); + } +} + + +// --- Folder Picker --- +function createFolderPicker(inputId, currentVal) { + const wrapper = document.createElement('div'); + wrapper.className = 'folder-picker'; + + // Display current value + browse button + const display = document.createElement('input'); + display.type = 'text'; + display.className = 'input folder-picker-value'; + display.id = inputId; + display.name = inputId.replace('cfg-', ''); + display.value = currentVal || '/data'; + display.addEventListener('input', updateUnsavedIndicator); + + const browseBtn = document.createElement('button'); + browseBtn.type = 'button'; + browseBtn.className = 'btn btn-outline btn-sm'; + browseBtn.textContent = 'Browse'; + browseBtn.onclick = () => toggleFolderBrowser(wrapper, display); + + const row = document.createElement('div'); + row.className = 'folder-picker-row'; + row.appendChild(display); + row.appendChild(browseBtn); + wrapper.appendChild(row); + + // Browser panel (hidden by default) + const browser = document.createElement('div'); + browser.className = 'folder-browser'; + browser.style.display = 'none'; + wrapper.appendChild(browser); + + return wrapper; +} + +async function toggleFolderBrowser(wrapper, display) { + const browser = wrapper.querySelector('.folder-browser'); + if (browser.style.display !== 'none') { + browser.style.display = 'none'; + return; + } + browser.style.display = ''; + await loadFolderLevel(browser, '/data', display); +} + +async function loadFolderLevel(browser, dir, display) { + browser.innerHTML = '
Loading...
'; + const data = await api('paths?dir=' + encodeURIComponent(dir)); + if (!data) { + browser.innerHTML = '
Failed to load
'; + return; + } + + let html = ''; + // Back button (if not at root) + if (dir !== '/data') { + const parent = dir.split('/').slice(0, -1).join('/') || '/data'; + html += `
.. (back)
`; + } + // Current dir — select button + html += `
+ ${escHtml(dir)} + +
`; + // Children + if (data.children && data.children.length > 0) { + data.children.forEach(c => { + const name = c.split('/').pop(); + html += `
+ 📁 ${escHtml(name)} +
`; + }); + } else { + html += '
No subfolders
'; + } + browser.innerHTML = html; + + // Wire up clicks + browser.querySelectorAll('.folder-child').forEach(el => { + el.onclick = () => loadFolderLevel(browser, el.dataset.path, display); + }); + browser.querySelectorAll('.folder-back').forEach(el => { + el.onclick = () => loadFolderLevel(browser, el.dataset.path, display); + }); + browser.querySelectorAll('.folder-select-btn').forEach(btn => { + btn.onclick = (e) => { + e.stopPropagation(); + display.value = btn.closest('.folder-current').dataset.path; + browser.style.display = 'none'; + updateUnsavedIndicator(); + }; + }); +} + +// --- Configuration --- +function buildConfigSnapshot() { + const form = document.getElementById('config-form'); + if (!form) return null; + const fd = new FormData(form); + const obj = {}; + for (const [k, v] of fd.entries()) obj[k] = String(v); + return JSON.stringify(obj); +} + +async function loadConfig() { + const data = await api('config'); + if (!data) { + document.getElementById('config-fields').innerHTML = + '
Failed to load configuration
'; + return; + } + const values = {}; + (data.config || []).forEach(e => { values[e.key] = e.value; }); + renderConfigForm(values); + configSnapshot = buildConfigSnapshot(); + updateUnsavedIndicator(); +} + +function isBooleanField(item) { + return item.type === 'select' && Array.isArray(item.options) + && item.options.length === 2 + && item.options.includes('true') && item.options.includes('false'); +} + +// Custom toggle switch backed by a hidden input so FormData (save + +// unsaved-change detection) keeps working unchanged. +function createConfigSwitch(key, on) { + const wrap = document.createElement('div'); + const hidden = document.createElement('input'); + hidden.type = 'hidden'; + hidden.id = 'cfg-' + key; + hidden.name = key; + hidden.value = on ? 'true' : 'false'; + + const sw = document.createElement('button'); + sw.type = 'button'; + sw.className = 'switch' + (on ? ' on' : ''); + sw.setAttribute('role', 'switch'); + sw.setAttribute('aria-checked', on ? 'true' : 'false'); + sw.innerHTML = ''; + sw.addEventListener('click', () => { + const next = hidden.value !== 'true'; + hidden.value = next ? 'true' : 'false'; + sw.classList.toggle('on', next); + sw.setAttribute('aria-checked', next ? 'true' : 'false'); + updateUnsavedIndicator(); + }); + + wrap.appendChild(hidden); + wrap.appendChild(sw); + return wrap; +} + +function renderConfigForm(values) { + const container = document.getElementById('config-fields'); + container.innerHTML = ''; + let card = null; // current section's field card + + CONFIG_SCHEMA.forEach(item => { + if (item.section) { + const title = document.createElement('div'); + title.className = 'config-section-title'; + title.textContent = item.section; + container.appendChild(title); + if (item.help) { + const help = document.createElement('p'); + help.className = 'config-section-help'; + help.textContent = item.help; + container.appendChild(help); + } + card = document.createElement('div'); + card.className = 'config-card'; + container.appendChild(card); + return; + } + if (!card) { card = document.createElement('div'); card.className = 'config-card'; container.appendChild(card); } + + const group = document.createElement('div'); + group.className = 'config-group'; + + const keyCol = document.createElement('div'); + keyCol.style.minWidth = '0'; + const keyEl = document.createElement('div'); + keyEl.className = 'config-key'; + keyEl.textContent = item.label || item.key; + keyEl.title = item.key; + keyCol.appendChild(keyEl); + const keyName = document.createElement('span'); + keyName.className = 'config-keyname'; + keyName.textContent = item.key; + keyCol.appendChild(keyName); + if (item.desc) { + const desc = document.createElement('span'); + desc.className = 'config-desc'; + desc.textContent = item.desc; + keyCol.appendChild(desc); + } + group.appendChild(keyCol); + + const control = document.createElement('div'); + control.className = 'config-control'; + const curVal = item.key in values ? values[item.key] : (item.default || ''); + + if (item.type === 'path') { + control.appendChild(createFolderPicker('cfg-' + item.key, curVal)); + } else if (isBooleanField(item)) { + control.appendChild(createConfigSwitch(item.key, String(curVal) === 'true')); + } else if (item.type === 'select') { + const sel = document.createElement('select'); + (item.options || []).forEach(opt => { + const o = document.createElement('option'); + o.value = opt; o.textContent = opt; + sel.appendChild(o); + }); + sel.id = 'cfg-' + item.key; + sel.name = item.key; + sel.value = curVal; + sel.addEventListener('change', updateUnsavedIndicator); + control.appendChild(sel); + } else { + const input = document.createElement('input'); + input.type = item.type || 'text'; + if (item.type === 'number') { input.step = 'any'; input.min = '0'; } + input.id = 'cfg-' + item.key; + input.name = item.key; + input.value = curVal; + input.addEventListener('input', updateUnsavedIndicator); + input.addEventListener('change', updateUnsavedIndicator); + control.appendChild(input); + } + group.appendChild(control); + card.appendChild(group); + }); +} + +function hasUnsavedConfig() { + if (!configSnapshot) return false; + return buildConfigSnapshot() !== configSnapshot; +} + +function updateUnsavedIndicator() { + const navLink = document.querySelector('[data-page="config"]'); + if (!navLink) return; + let dot = navLink.querySelector('.unsaved-dot'); + if (hasUnsavedConfig()) { + if (!dot) { + dot = document.createElement('span'); + dot.className = 'unsaved-dot'; + dot.title = 'Unsaved changes'; + navLink.appendChild(dot); + } + } else { + if (dot) dot.remove(); + } +} + +async function saveConfig(e) { + e.preventDefault(); + const form = document.getElementById('config-form'); + const formData = new FormData(form); + const config = {}; + for (const [key, val] of formData.entries()) { + config[key] = val; + } + const status = document.getElementById('config-status'); + const submitBtn = form.querySelector('[type="submit"]'); + submitBtn.disabled = true; + const res = await apiPost('config', { config }); + submitBtn.disabled = false; + if (res && res.ok) { + status.textContent = 'Saved!'; + status.className = 'form-status'; + showToast('Configuration saved', 'success'); + configSnapshot = buildConfigSnapshot(); + updateUnsavedIndicator(); + } else { + status.textContent = 'Save failed'; + status.className = 'form-status error'; + showToast('Save failed' + (res && res.error ? ': ' + res.error : ''), 'error'); + } + setTimeout(() => { status.textContent = ''; }, 3000); +} + +// --- Logs --- +const LOG_PATTERNS = [ + { regex: /\b(CORRUPT:)/g, cls: 'log-corrupt' }, + { regex: /\b(CRITICAL)\b/g, cls: 'log-level-critical' }, + { regex: /\b(ERROR)\b/g, cls: 'log-level-error' }, + { regex: /\b(WARNING)\b/g, cls: 'log-level-warning' }, + { regex: /\b(INFO)\b/g, cls: 'log-level-info' }, + { regex: /\b(DEBUG)\b/g, cls: 'log-level-debug' }, + { regex: /(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}(?:[.,]\d+)?)/g, cls: 'log-timestamp' }, + { regex: /(https?:\/\/\S+)/g, cls: 'log-url' }, + { regex: /(\/data\/[^\n]+?\.\w{2,5})(?=\s|$)/g, cls: 'log-path' }, + { regex: /(\/config\/[\w./-]+)/g, cls: 'log-path' }, + { regex: /\b(\d+(?:\.\d+)?)\s*(?:files?|MB|GB|KB|TB|bytes?|%|ms|seconds?|minutes?|hours?)\b/g, cls: 'log-number' }, +]; + +// Message-only highlight patterns (level + timestamp are rendered as their +// own columns in the terminal layout, so they're excluded here). +const LOG_MSG_PATTERNS = [ + { regex: /\b(CORRUPT)\b/g, cls: 'log-corrupt' }, + { regex: /(https?:\/\/\S+)/g, cls: 'log-url' }, + { regex: /(\/data\/[^\n]+?\.\w{2,5})(?=\s|$)/g, cls: 'log-path' }, + { regex: /(\/config\/[\w./-]+)/g, cls: 'log-path' }, + { regex: /\b(\d+(?:[.,]\d+)?)\s*(?:files?|MB|GB|KB|TB|bytes?|%|ms|seconds?|minutes?|hours?)\b/g, cls: 'log-number' }, +]; + +function highlightLogMessage(msg) { + let html = escHtml(msg); + LOG_MSG_PATTERNS.forEach(p => { + html = html.replace(p.regex, '$1'); + }); + return html; +} + +function highlightLogLine(line, isSearchMatch) { + let html = escHtml(line); + LOG_PATTERNS.forEach(p => { + html = html.replace(p.regex, '$1'); + }); + if (isSearchMatch) { + html = '' + html + ''; + } + return html; +} + +// Backend log format: "YYYY-MM-DD HH:MM:SS | LEVEL | message" +const LOG_LINE_RE = /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s*\|\s*(\w+)\s*\|\s*([\s\S]*)$/; +const LOG_LEVELS = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG']; + +function renderLogLine(line, isSearchMatch) { + const m = LOG_LINE_RE.exec(line); + if (m && LOG_LEVELS.includes(m[2])) { + const ts = m[1]; + const lv = m[2]; + const msgHtml = highlightLogMessage(m[3]); + const hl = isSearchMatch ? ' log-highlight-line' : ''; + return '
' + + '' + escHtml(ts) + '' + + '' + lv + '' + + '' + msgHtml + '' + + '
'; + } + // Non-standard line (banners, multi-line output) — full highlight fallback + return '
' + highlightLogLine(line, false) + '
'; +} + +let logLastMtime = 0; + +async function refreshLogs() { + const lines = document.getElementById('log-lines').value || '500'; + const query = 'log?lines=' + encodeURIComponent(lines) + '&since=' + logLastMtime; + const data = await api(query); + if (!data) { + document.getElementById('log-output').innerHTML = '(failed to load logs)'; + return; + } + if (data.unchanged) return; + logLastMtime = data.mtime || 0; + logRawLines = (data.log || '').split('\n'); + renderLogOutput(); +} + +function renderLogOutput() { + const viewer = document.getElementById('log-output'); + const levelFilter = document.getElementById('log-level-filter').value; + const searchTerm = document.getElementById('log-search').value.trim(); + + let lines = logRawLines; + + if (levelFilter) { + lines = lines.filter(line => line.includes(levelFilter)); + } + + const hasSearch = searchTerm.length > 0; + const searchLower = searchTerm.toLowerCase(); + if (hasSearch) { + lines = lines.filter(line => line.toLowerCase().includes(searchLower)); + } + + viewer.innerHTML = lines.map(line => renderLogLine(line, hasSearch)).join(''); + + if (document.getElementById('log-autoscroll').checked) { + viewer.scrollTop = viewer.scrollHeight; + } +} + +function startLogPoll() { + stopLogPoll(); + logTimer = setInterval(refreshLogs, 5000); +} + +function stopLogPoll() { + if (logTimer) { clearInterval(logTimer); logTimer = null; } +} + +function copyLogs() { + const text = logRawLines.join('\n'); + navigator.clipboard.writeText(text).then( + () => showToast('Logs copied to clipboard', 'success'), + () => showToast('Failed to copy logs', 'error') + ); +} + +function downloadLogs() { + const text = logRawLines.join('\n'); + const blob = new Blob([text], { type: 'text/plain' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = 'beatscheck-' + new Date().toISOString().slice(0, 10) + '.log'; + a.click(); + URL.revokeObjectURL(url); + showToast('Log file downloaded', 'info'); +} + +// --- Rescan --- +async function triggerRescan(mode) { + const fresh = document.getElementById('fresh-scan-check').checked; + // Warn early if output_dir looks unconfigured — the backend is + // authoritative and will fall back to report mode if invalid. + if (mode === 'move') { + const cfg = await api('config'); + if (cfg) { + const outputDir = (cfg.config || []).find(e => e.key === 'output_dir'); + if (!outputDir || !outputDir.value) { + showToast('Move mode requires an Output Directory. Configure it in Settings first.', 'warning'); + return; + } + } + } + const btns = document.querySelectorAll('.action-bar .btn'); + btns.forEach(b => b.disabled = true); + const res = await apiPost('rescan', { mode, fresh }); + btns.forEach(b => b.disabled = false); + if (res && res.ok) { + showToast('Scan triggered (' + mode + (fresh ? ', fresh' : '') + ')', 'success'); + setTimeout(refreshDashboard, 1000); + } else { + showToast('Scan failed', 'error'); + } +} + +async function cancelScan() { + const btn = document.getElementById('cancel-scan-btn'); + btn.disabled = true; + const res = await apiPost('cancel', {}); + btn.disabled = false; + if (res && res.ok) { + showToast('Scan cancel requested — finishing current files...', 'warning'); + } else { + showToast('Cancel failed', 'error'); + } +} + +// --- Toast notifications --- +const TOAST_DURATIONS = { success: 4000, error: 6000, warning: 5000, info: 4000 }; + +function showToast(message, type) { + const container = document.getElementById('toast-container'); + const duration = TOAST_DURATIONS[type] || 4000; + + const t = document.createElement('div'); + t.className = 'toast ' + (type || ''); + t.setAttribute('aria-atomic', 'true'); + + const span = document.createElement('span'); + span.textContent = message; + t.appendChild(span); + + const close = document.createElement('button'); + close.className = 'toast-close'; + close.innerHTML = '×'; + close.setAttribute('aria-label', 'Dismiss notification'); + close.onclick = () => { if (t.parentNode) t.remove(); }; + t.appendChild(close); + + const prefersReduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches; + const prog = document.createElement('div'); + prog.className = 'toast-progress'; + prog.style.width = '100%'; + t.appendChild(prog); + + container.appendChild(t); + + requestAnimationFrame(() => { + prog.style.transitionDuration = prefersReduced ? '0ms' : duration + 'ms'; + prog.style.width = '0%'; + }); + + const timer = setTimeout(() => { if (t.parentNode) t.remove(); }, duration); + + t.addEventListener('mouseenter', () => { + clearTimeout(timer); + prog.style.transitionDuration = '0ms'; + }); + t.addEventListener('mouseleave', () => { + const remaining = (parseFloat(getComputedStyle(prog).width) / t.offsetWidth) * duration; + prog.style.transitionDuration = remaining + 'ms'; + prog.style.width = '0%'; + setTimeout(() => { if (t.parentNode) t.remove(); }, remaining); + }); + + while (container.children.length > 5) { + container.removeChild(container.firstChild); + } +} + +// --- Polling --- +function startStatusPoll() { + stopStatusPoll(); + refreshDashboard(); + pollTimer = setInterval(refreshDashboard, 5000); +} + +function stopStatusPoll() { + if (pollTimer) { clearInterval(pollTimer); pollTimer = null; } +} + +// --- Init --- +document.addEventListener('DOMContentLoaded', () => { + initTheme(); + initSidebar(); + + document.getElementById('theme-toggle').addEventListener('click', toggleTheme); + + // Auth forms + const setupForm = document.getElementById('setup-form'); + if (setupForm) setupForm.addEventListener('submit', doSetup); + const loginForm = document.getElementById('login-form'); + if (loginForm) loginForm.addEventListener('submit', doLogin); + const logoutBtn = document.getElementById('logout-btn'); + if (logoutBtn) logoutBtn.addEventListener('click', doLogout); + + // Log controls + const logLines = document.getElementById('log-lines'); + const logLevel = document.getElementById('log-level-filter'); + const logSearch = document.getElementById('log-search'); + if (logLines) logLines.addEventListener('change', () => { + logLastMtime = 0; + if (currentPage === 'logs') refreshLogs(); + }); + if (logLevel) logLevel.addEventListener('change', renderLogOutput); + if (logSearch) { + let debounce; + logSearch.addEventListener('input', () => { + clearTimeout(debounce); + debounce = setTimeout(renderLogOutput, 200); + }); + } + + // Start auth check + checkAuth(); +}); diff --git a/app/static/favicon.png b/app/static/favicon.png new file mode 100644 index 0000000..be3575a Binary files /dev/null and b/app/static/favicon.png differ diff --git a/app/static/index.html b/app/static/index.html new file mode 100644 index 0000000..7bd6f88 --- /dev/null +++ b/app/static/index.html @@ -0,0 +1,325 @@ + + + + + + BeatsCheck + + + + + + + + + + +
+ +
+
+ +

Dashboard

+ v… +
+
+
+ + Connecting… +
+ + +
+
+ +
+ + +
+
+
+
+ +
Create your credentials
+

First-run setup. These are stored hashed in the container.

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+

Credentials are stored securely in the container. To reset, run:
docker exec beatscheck reset-webui-password

+
+
+
+ + +
+
+
+
+ +
Welcome back
+

Sign in to BeatsCheck

+
+
+
+ + +
+
+ + +
+
+ +
+

Forgot your password?
docker exec beatscheck reset-webui-password

+
+
+
+ + +
+

Dashboard

+
+
+
+
Status
+
--
+
+
+
Mode
+
--
+
+
+
Library
+
--
+
+
+
+
Corrupt
+
--
+
+
+
+
Workers
+
--
+
+
+
Uptime
+
--
+
+
+ + + + + + + + +
+

Actions

+
+ + + +
+ +
+ + +
Recently flagged most recent
+
+
No corrupt files — library is clean.
+
+
+
+ + +
+

Corrupt Files 0

+
+ + +
+ + + + + + +
+
+ + + + + + + + + + + + +
File Size Actions
No corrupt files — library is clean.
+
+
+
+ + +
+

Configuration

+
+

Changes save to beatscheck.conf and apply on the next scan.

+
+
+ + +
+
+
+
+
+ + +
+

Logs

+
+ + + + +
+ + + +
+
+
+
+ +
+
+ +
+ + + + + + + diff --git a/app/static/style.css b/app/static/style.css new file mode 100644 index 0000000..9874d52 --- /dev/null +++ b/app/static/style.css @@ -0,0 +1,1295 @@ +/* ============================================================ + BeatsCheck WebUI — Redesign + Dark "audio-tool" dashboard · signal-green accent · light mode + Design tokens & component styles per design_handoff redesign. + ============================================================ */ + +@import url('https://fonts.googleapis.com/css2?family=Hanken+Grotesk:wght@400;500;600;700;800&family=IBM+Plex+Mono:wght@400;500;600&display=swap'); + +/* ---------- Theme tokens ---------- */ +:root, +[data-theme="dark"] { + --bg: #0a0b0e; + --bg1: #121319; + --bg2: #1a1c23; + --bgh: #22242d; + --bd: #23252e; + --bds: #30333d; + --tx: #ecedf1; + --txm: #9396a3; + --txd: #5f626d; + --term-bg: #08090c; + + --dg: #ff4d6d; + --dgd: rgba(255, 77, 109, .13); + --wn: #f5b94a; + --inf: #5a9cf0; + + /* log chips */ + --log-info: #5a9cf0; --log-info-bg: rgba(90,156,240,.14); + --log-warn: #f5b94a; --log-warn-bg: rgba(245,185,74,.14); + --log-err: #ff5775; --log-err-bg: rgba(255,87,117,.15); + --log-dbg: #6b6f7d; --log-dbg-bg: rgba(107,111,125,.18); + --log-crit: #ff3868; --log-crit-bg: rgba(255,56,104,.16); + + --spectrum: linear-gradient(180deg,#6c5cff,#4b86ff,#34e0a0,#9ad94a,#f5c24a,#ff7a4d); + + --cardpad: 18px; + --rowpad: 13px; + --gap: 14px; + + /* legacy aliases — kept so inline styles emitted by app.js still resolve */ + --text-primary: var(--tx); + --text-muted: var(--txm); + --text-dim: var(--txd); + --danger: var(--dg); + --accent: var(--ac); + + color-scheme: dark; +} + +[data-theme="light"] { + --bg: #eef0f4; + --bg1: #ffffff; + --bg2: #f3f4f8; + --bgh: #e7e9f0; + --bd: #e3e5ed; + --bds: #d2d5df; + --tx: #16171c; + --txm: #5b5f6b; + --txd: #9094a0; + --term-bg: #0c0d12; + + --dg: #d6294e; + --dgd: rgba(214,41,78,.1); + --wn: #b9831a; + --inf: #2f72d6; + + color-scheme: light; +} + +/* ---------- Accent themes (swap --ac/--acd/--acft together) ---------- */ +:root, +[data-accent="signal"] { --ac: #34e0a0; --acd: rgba(52,224,160,.14); --acft: #06120d; } +[data-theme="light"][data-accent="signal"] { --ac: #0f9d6e; } + +[data-accent="amber"] { --ac: #f5a623; --acd: rgba(245,166,35,.15); --acft: #06120d; } +[data-theme="light"][data-accent="amber"] { --ac: #bf7d0c; } + +[data-accent="indigo"] { --ac: #6c7bff; --acd: rgba(108,123,255,.16); --acft: #ffffff; } +[data-theme="light"][data-accent="indigo"] { --ac: #4a59e6; } + +/* ---------- Base ---------- */ +* { box-sizing: border-box; } + +html, body { margin: 0; padding: 0; } + +body { + font-family: 'Hanken Grotesk', system-ui, -apple-system, sans-serif; + -webkit-font-smoothing: antialiased; + background: var(--bg); + color: var(--tx); + display: flex; + height: 100vh; + overflow: hidden; /* the right-hand
is the scroll container */ + width: 100%; +} + +a { color: inherit; text-decoration: none; } + +code, .mono { font-family: 'IBM Plex Mono', monospace; } + +::-webkit-scrollbar { width: 9px; height: 9px; } +::-webkit-scrollbar-thumb { background: var(--bds); border-radius: 5px; } +::-webkit-scrollbar-track { background: transparent; } + +.skip-link { + position: absolute; + left: -9999px; + top: 0; + background: var(--ac); + color: var(--acft); + padding: 8px 14px; + border-radius: 8px; + z-index: 100; + font-weight: 700; +} +.skip-link:focus { left: 12px; top: 12px; } + +/* ---------- Keyframes ---------- */ +@keyframes bc-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .28; } } +@keyframes bc-sweep { 0%, 100% { opacity: .55; } 50% { opacity: 1; } } +@keyframes bc-changed { 0% { color: var(--ac); } 100% { color: inherit; } } + +/* ============================================================ + Sidebar + ============================================================ */ +#sidebar { + width: 238px; + flex-shrink: 0; + background: var(--bg1); + border-right: 1px solid var(--bd); + display: flex; + flex-direction: column; + height: 100vh; + position: sticky; + top: 0; + z-index: 30; +} + +.sidebar-logo { + display: flex; + align-items: center; + gap: 12px; + padding: 20px 20px 16px; +} +.logo-chip { + width: 36px; + height: 36px; + border-radius: 11px; + background: radial-gradient(circle at 50% 38%, #1d1f28, #08090c); + border: 1px solid var(--bds); + display: flex; + align-items: center; + justify-content: center; + position: relative; + flex-shrink: 0; +} +.logo-chip .logo-bar { + width: 4px; + height: 15px; + border-radius: 2px; + background: var(--spectrum); +} +.logo-chip .logo-dot { + width: 4px; + height: 4px; + border-radius: 50%; + background: #ff4d6d; + position: absolute; + bottom: 7px; +} +.logo-word { + font-size: 16px; + font-weight: 800; + letter-spacing: -.02em; + color: var(--tx); + line-height: 1; +} +.logo-word b { color: var(--ac); font-weight: 800; } +.logo-sub { + font-family: 'IBM Plex Mono', monospace; + font-size: 9.5px; + color: var(--txd); + letter-spacing: .04em; + margin-top: 3px; +} + +.sidebar-divider { + height: 1px; + background: var(--bd); + margin: 4px 12px 10px; +} + +.nav-section { + font-family: 'IBM Plex Mono', monospace; + font-size: 9.5px; + letter-spacing: .14em; + color: var(--txd); + padding: 2px 22px 8px; +} +.nav-section.spaced { padding-top: 18px; } + +.nav-link { + display: flex; + align-items: center; + gap: 11px; + padding: 9px 14px; + margin: 1px 12px; + border-radius: 9px; + color: var(--txm); + font-size: 13.5px; + font-weight: 600; + cursor: pointer; + transition: background .15s, color .15s; +} +.nav-link:hover { background: var(--bgh); } +.nav-link.active { color: var(--ac); background: var(--acd); } +.nav-link.active:hover { background: var(--acd); } + +.nav-index { + font-family: 'IBM Plex Mono', monospace; + font-size: 10.5px; + color: var(--txd); + width: 15px; + flex-shrink: 0; +} +.nav-link.active .nav-index { color: var(--ac); opacity: .7; } + +.nav-badge { + margin-left: auto; + font-family: 'IBM Plex Mono', monospace; + font-size: 10px; + font-weight: 700; + color: var(--dg); + background: var(--dgd); + padding: 1px 7px; + border-radius: 999px; +} +.nav-badge:empty, +.nav-badge.zero { display: none; } + +.unsaved-dot { + margin-left: auto; + width: 7px; + height: 7px; + border-radius: 50%; + background: var(--wn); + box-shadow: 0 0 6px var(--wn); +} +.nav-link .nav-badge + .unsaved-dot { margin-left: 8px; } + +.sidebar-status { + margin-top: auto; + padding: 16px 18px; +} +.status-card { + background: var(--bg2); + border: 1px solid var(--bd); + border-radius: 11px; + padding: 12px 13px; +} +.status-card-head { + display: flex; + align-items: center; + gap: 8px; + margin-bottom: 7px; +} +.status-card-word { + font-size: 11.5px; + font-weight: 700; + color: var(--tx); +} +.status-card-meta { + font-family: 'IBM Plex Mono', monospace; + font-size: 10px; + color: var(--txd); + line-height: 1.5; +} + +/* ============================================================ + Main column + header + ============================================================ */ +.main-col { + flex: 1; + display: flex; + flex-direction: column; + min-width: 0; + background: var(--bg); +} + +header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + padding: 15px 30px; + border-bottom: 1px solid var(--bd); + background: var(--bg1); + position: sticky; + top: 0; + z-index: 20; +} +.header-left { display: flex; align-items: center; gap: 12px; min-width: 0; } +.header-right { display: flex; align-items: center; gap: 11px; } +#screen-title { + font-size: 18px; + font-weight: 700; + letter-spacing: -.015em; + color: var(--tx); + margin: 0; +} + +.status-pill { + display: flex; + align-items: center; + gap: 8px; + padding: 6px 13px; + border-radius: 999px; + background: var(--bg2); + border: 1px solid var(--bd); +} +.status-pill #status-text { + font-size: 12.5px; + color: var(--txm); + font-weight: 600; +} + +/* Status dot — idle (accent + glow), scanning (amber + pulse) */ +.status-dot { + width: 8px; + height: 8px; + border-radius: 50%; + flex-shrink: 0; + display: inline-block; + background: var(--ac); + box-shadow: 0 0 8px var(--ac); +} +.status-dot.scanning { background: var(--wn); box-shadow: 0 0 8px var(--wn); animation: bc-pulse 1.4s ease-in-out infinite; } +.status-dot.idle { background: var(--ac); box-shadow: 0 0 8px var(--ac); } +.status-dot.setup, +.status-dot.starting, +.status-dot.unknown { background: var(--txd); box-shadow: none; } + +main#content { + flex: 1; + min-height: 0; /* allow the flex child to shrink so it scrolls */ + overflow: auto; + padding: 26px 30px; +} + +.page { display: none; } +.page.active { display: block; } + +.page-narrow { max-width: 1080px; } +.page-config-wrap { max-width: 840px; } + +/* legacy page

headings — title is shown in the header instead */ +.page > h2 { + position: absolute; + width: 1px; height: 1px; + padding: 0; margin: -1px; + overflow: hidden; clip: rect(0,0,0,0); + white-space: nowrap; border: 0; +} +.subtitle { + color: var(--txm); + font-size: 13px; + margin: 0 0 22px; + line-height: 1.5; +} +.subtitle code, code { + font-family: 'IBM Plex Mono', monospace; + background: var(--bg2); + padding: 2px 7px; + border-radius: 5px; + font-size: 12px; + color: var(--tx); +} + +.badge { + font-family: 'IBM Plex Mono', monospace; + font-size: 10px; + color: var(--txd); +} + +/* ============================================================ + Buttons + ============================================================ */ +.btn { + font-family: inherit; + font-size: 13px; + font-weight: 700; + padding: 10px 17px; + border-radius: 10px; + border: 1px solid transparent; + cursor: pointer; + transition: background .15s, opacity .15s, border-color .15s, filter .15s; + white-space: nowrap; +} +.btn:disabled { opacity: .4; cursor: not-allowed; } + +.btn-primary { background: var(--ac); color: var(--acft); border-color: var(--ac); } +.btn-primary:hover:not(:disabled) { filter: brightness(1.06); } + +.btn-secondary { background: var(--bg2); color: var(--tx); border-color: var(--bd); font-weight: 600; } +.btn-secondary:hover:not(:disabled) { background: var(--bgh); } + +.btn-danger { background: var(--dg); color: #fff; border-color: var(--dg); } +.btn-danger:hover:not(:disabled) { filter: brightness(1.06); } + +.btn-outline { background: transparent; color: var(--txm); border-color: var(--bd); font-weight: 600; } +.btn-outline:hover:not(:disabled) { background: var(--bgh); color: var(--tx); } + +.btn-sm { padding: 5px 11px; font-size: 11.5px; border-radius: 7px; font-weight: 600; } + +/* row-level outline delete buttons (corrupt table) */ +.col-actions .btn-danger.btn-sm { + background: transparent; + color: var(--dg); + border: 1px solid var(--bd); + font-weight: 600; +} +.col-actions .btn-danger.btn-sm:hover:not(:disabled) { background: var(--dgd); } + +.icon-btn { + background: transparent; + border: 1px solid var(--bd); + color: var(--txm); + font-size: 12.5px; + font-weight: 600; + padding: 7px 13px; + border-radius: 8px; + cursor: pointer; + line-height: 1; +} +.icon-btn:hover { background: var(--bgh); color: var(--tx); } + +/* ============================================================ + Inputs / selects + ============================================================ */ +.input, +input[type="text"], +input[type="password"], +input[type="number"], +select { + font-family: inherit; + font-size: 13px; + padding: 9px 13px; + border-radius: 9px; + border: 1px solid var(--bd); + background: var(--bg2); + color: var(--tx); + outline: none; +} +.input:focus, +input:focus, +select:focus { border-color: var(--ac); } + +select { + font-family: 'IBM Plex Mono', monospace; + font-size: 12.5px; + cursor: pointer; +} + +input::placeholder { color: var(--txd); } + +/* ============================================================ + Dashboard + ============================================================ */ +.card-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); + gap: var(--gap); + margin-bottom: 20px; +} +.card { + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 14px; + padding: var(--cardpad); +} +.card-label { + font-family: 'IBM Plex Mono', monospace; + font-size: 9.5px; + letter-spacing: .1em; + color: var(--txm); + text-transform: uppercase; + margin-bottom: 10px; + font-weight: 600; + display: flex; + align-items: center; + gap: 7px; +} +.card-icon { display: none; } /* design uses no icons in stat cards */ +.card-value { + font-size: 23px; + font-weight: 700; + letter-spacing: -.02em; + color: var(--tx); + display: flex; + align-items: center; + gap: 9px; +} +.card-value.changed { animation: bc-changed .6s ease-out; } +.card-value.danger { color: var(--dg); } +.card-sub { + font-family: 'IBM Plex Mono', monospace; + font-size: 10.5px; + color: var(--txd); + margin-top: 3px; +} + +/* Scan hero (scanning) */ +.scan-hero { + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 16px; + padding: 20px 22px; + margin-bottom: 20px; + position: relative; + overflow: hidden; +} +.scan-hero-top { + display: flex; + justify-content: space-between; + align-items: flex-start; + gap: 16px; + flex-wrap: wrap; +} +.scan-hero-title { font-size: 13px; font-weight: 700; color: var(--tx); margin-bottom: 4px; } +.scan-hero-file { + font-family: 'IBM Plex Mono', monospace; + font-size: 11px; + color: var(--txd); + max-width: 540px; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.scan-hero-pct-wrap { text-align: right; flex-shrink: 0; } +.scan-hero-pct { + font-size: 30px; + font-weight: 800; + letter-spacing: -.03em; + color: var(--ac); + line-height: 1; +} +.scan-hero-checked { + font-family: 'IBM Plex Mono', monospace; + font-size: 10.5px; + color: var(--txm); + margin-top: 4px; +} + +.spectrogram { + position: relative; + height: 118px; + display: flex; + align-items: flex-end; + gap: 2px; + margin: 20px 0 14px; +} +.spectrogram-wash { + position: absolute; + top: 0; bottom: 0; left: 0; + background: linear-gradient(90deg, var(--acd), transparent); + z-index: 0; + pointer-events: none; + border-radius: 6px; +} +.spec-bar { + flex: 1 1 0; + border-radius: 2px; + align-self: flex-end; + transition: opacity .3s; + z-index: 1; +} +.spectrogram-playhead { + position: absolute; + top: -4px; bottom: -4px; + width: 2px; + background: var(--ac); + box-shadow: 0 0 14px var(--ac); + z-index: 3; + animation: bc-sweep 1.6s ease-in-out infinite; +} +.spectrogram-playhead::before { + content: ''; + position: absolute; + top: 0; left: -3px; + width: 8px; height: 8px; + border-radius: 50%; + background: var(--ac); + box-shadow: 0 0 10px var(--ac); +} +.scan-hero-stats { + display: flex; + gap: 24px; + flex-wrap: wrap; + font-size: 12.5px; + align-items: center; +} +.scan-hero-stats span { color: var(--txm); } +.scan-hero-stats b { color: var(--tx); } +.scan-hero-stats .stat-corrupt { color: var(--dg); font-weight: 600; } +.scan-hero-stats .stat-eta { color: var(--ac); font-weight: 600; } + +/* Idle hero */ +.idle-hero { + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 16px; + padding: 22px; + margin-bottom: 20px; + display: flex; + gap: 18px; + align-items: center; + flex-wrap: wrap; +} +.idle-hero-tile { + width: 52px; + height: 52px; + border-radius: 14px; + background: var(--acd); + display: flex; + align-items: center; + justify-content: center; + flex-shrink: 0; + color: var(--ac); + font-size: 24px; + font-weight: 700; +} +.idle-hero-title { font-size: 17px; font-weight: 700; color: var(--tx); letter-spacing: -.01em; } +.idle-hero-meta { font-size: 12.5px; color: var(--txm); margin-top: 3px; } +.idle-hero-meta b { color: var(--dg); } +.idle-hero .btn { margin-left: auto; } + +/* Action row */ +.action-bar { + display: flex; + align-items: center; + gap: 10px; + flex-wrap: wrap; + margin-bottom: 22px; +} +.action-bar h3 { + position: absolute; + width: 1px; height: 1px; padding: 0; margin: -1px; + overflow: hidden; clip: rect(0,0,0,0); border: 0; +} +.btn-group { display: contents; } +.fresh-scan-toggle { + display: flex; + align-items: center; + gap: 7px; + font-size: 12.5px; + color: var(--txm); + cursor: pointer; + margin-left: 6px; +} +.fresh-scan-toggle input { accent-color: var(--ac); } + +/* legacy progress section — hidden; the spectrogram hero replaces it */ +#scan-progress-section { display: none !important; } + +/* Recently flagged */ +.section-heading { + font-size: 13px; + font-weight: 700; + color: var(--tx); + margin-bottom: 12px; + display: flex; + align-items: center; + gap: 9px; +} +.section-heading .meta { + font-family: 'IBM Plex Mono', monospace; + font-size: 10.5px; + color: var(--txd); + font-weight: 500; +} +.recent-list { + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 14px; + overflow: hidden; +} +.recent-row { + display: flex; + align-items: center; + gap: 14px; + padding: 13px 16px; + border-bottom: 1px solid var(--bd); +} +.recent-row:last-child { border-bottom: none; } +.recent-dot { + width: 6px; height: 6px; + border-radius: 50%; + background: var(--dg); + flex-shrink: 0; + box-shadow: 0 0 7px var(--dg); +} +.recent-main { min-width: 0; flex: 1; } +.recent-name { + font-family: 'IBM Plex Mono', monospace; + font-size: 12px; + color: var(--tx); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} +.recent-error { + font-size: 11px; + color: var(--dg); + margin-top: 3px; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} +.recent-size { + font-family: 'IBM Plex Mono', monospace; + font-size: 11.5px; + color: var(--txm); + flex-shrink: 0; +} +.recent-empty { + padding: 22px 16px; + text-align: center; + color: var(--txd); + font-size: 12.5px; +} + +/* ============================================================ + Corrupt files + ============================================================ */ +.scan-banner { + background: var(--dgd); + border: 1px solid var(--dg); + color: var(--dg); + border-radius: 10px; + padding: 10px 14px; + font-size: 12.5px; + margin-bottom: 14px; +} +.info-banner { + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 12px; + padding: 12px 14px; + font-size: 12.5px; + color: var(--txm); + line-height: 1.55; + margin-bottom: 14px; +} +.info-banner code { font-size: 11px; } +.info-banner-header { + display: flex; + align-items: center; + justify-content: space-between; + cursor: pointer; + color: var(--tx); + font-size: 13px; +} +.info-banner-toggle { + background: none; + border: none; + color: var(--txm); + cursor: pointer; + font-size: 11px; +} +.info-banner-body { margin-top: 8px; } +.info-banner-body.collapsed { display: none; } +.info-tag { + font-family: 'IBM Plex Mono', monospace; + font-size: 10.5px; + font-weight: 700; + padding: 1px 7px; + border-radius: 999px; +} +.info-tag.on { color: var(--ac); background: var(--acd); } +.info-tag.off { color: var(--txd); background: var(--bg2); } + +.table-toolbar { + display: flex; + align-items: center; + gap: 11px; + flex-wrap: wrap; + margin-bottom: 16px; +} +.table-toolbar > .input { + flex: 1; + min-width: 200px; + background: var(--bg1); +} +.select-n-group { + display: flex; + align-items: center; + gap: 6px; +} +.select-n-label { font-size: 12px; color: var(--txm); } +.select-n-input { width: 64px; } + +.table-wrap { + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 14px; + overflow: hidden; +} +table#corrupt-table { + width: 100%; + border-collapse: collapse; +} +#corrupt-table thead th { + font-family: 'IBM Plex Mono', monospace; + font-size: 9.5px; + letter-spacing: .08em; + color: var(--txm); + text-transform: uppercase; + font-weight: 600; + text-align: left; + padding: 11px 16px; + border-bottom: 1px solid var(--bd); + background: var(--bg1); +} +#corrupt-table th.col-size { text-align: right; } +#corrupt-table th.col-actions { text-align: right; } +#corrupt-table th.sortable { cursor: pointer; user-select: none; } +#corrupt-table th.sortable:hover { color: var(--tx); } +.sort-indicator { color: var(--ac); } +th.sort-asc .sort-indicator::after { content: ' ▲'; font-size: 9px; } +th.sort-desc .sort-indicator::after { content: ' ▼'; font-size: 9px; } + +#corrupt-table tbody td { + padding: var(--rowpad) 16px; + border-bottom: 1px solid var(--bd); + font-size: 12.5px; + vertical-align: middle; +} +#corrupt-table tbody tr:last-child td { border-bottom: none; } +#corrupt-table .col-check { width: 34px; } +#corrupt-table .col-size { + text-align: right; + font-family: 'IBM Plex Mono', monospace; + font-size: 12px; + color: var(--txm); + white-space: nowrap; +} +#corrupt-table .col-actions { text-align: right; white-space: nowrap; } +#corrupt-table .file-check, +#corrupt-table .album-check, +#select-all { + width: 15px; height: 15px; + accent-color: var(--ac); + cursor: pointer; +} +#corrupt-table tbody tr:has(.file-check:checked) td { background: var(--acd); } + +.file-path { min-width: 0; } +.file-path strong { + font-family: 'IBM Plex Mono', monospace; + font-size: 12.5px; + font-weight: 500; + color: var(--tx); +} + +#corrupt-table tr.album-header td { background: var(--bg2); } +#corrupt-table tr.album-header { cursor: pointer; } +#corrupt-table tr.album-header strong { font-size: 13px; font-weight: 700; color: var(--tx); } +.album-count { + font-family: 'IBM Plex Mono', monospace; + font-size: 10.5px; + color: var(--dg); + font-weight: 600; +} +#corrupt-table tr.album-header.all-corrupt strong { color: var(--dg); } +.album-actions { display: flex; gap: 6px; justify-content: flex-end; } +#corrupt-table tr.file-missing { opacity: .5; } + +.empty-state { + padding: 40px !important; + text-align: center; + color: var(--txd); + font-size: 13px; +} + +/* ============================================================ + Configuration + ============================================================ */ +.config-section-title { + font-size: 14px; + font-weight: 700; + color: var(--ac); + margin: 26px 0 3px; + letter-spacing: -.01em; +} +#config-fields > .config-section-title:first-child { margin-top: 0; } +.config-section-help { + font-size: 12px; + color: var(--txm); + margin: 0 0 13px; + line-height: 1.45; +} +.config-card { + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 14px; + overflow: hidden; + margin-bottom: 6px; +} +.config-group { + display: grid; + grid-template-columns: 210px 1fr; + gap: 16px; + align-items: center; + padding: 14px 18px; + border-bottom: 1px solid var(--bd); +} +.config-card .config-group:last-child { border-bottom: none; } +.config-key { + font-size: 13px; + color: var(--tx); + font-weight: 600; + letter-spacing: -.01em; +} +.config-keyname { + display: block; + font-family: 'IBM Plex Mono', monospace; + font-size: 10px; + color: var(--txd); + margin-top: 2px; + letter-spacing: .02em; +} +.config-desc { + font-size: 10.5px; + color: var(--txd); + margin-top: 4px; + line-height: 1.45; + display: block; +} +.config-control { + display: flex; + justify-content: flex-end; + align-items: center; +} +.config-control select, +.config-control input { + min-width: 210px; + max-width: 320px; +} + +/* toggle switch */ +.switch { + width: 40px; + height: 22px; + border-radius: 999px; + background: var(--bg2); + border: 1px solid var(--bds); + position: relative; + cursor: pointer; + flex-shrink: 0; + transition: background .2s, border-color .2s; + padding: 0; +} +.switch.on { background: var(--ac); border-color: transparent; } +.switch .switch-knob { + position: absolute; + top: 2px; left: 2px; + width: 16px; height: 16px; + border-radius: 50%; + background: #fff; + transition: left .2s; + box-shadow: 0 1px 2px rgba(0,0,0,.3); +} +.switch.on .switch-knob { left: 20px; } + +.form-actions { + display: flex; + align-items: center; + gap: 14px; + margin-top: 4px; +} +/* Save bar pinned to the top of the config screen, stays in view while + scrolling the long form. */ +.form-actions-top { + position: sticky; + top: 0; + z-index: 10; + margin-top: 0; + margin-bottom: 18px; + padding: 12px 0 14px; + background: var(--bg); + border-bottom: 1px solid var(--bd); +} +.form-status { font-size: 12.5px; color: var(--ac); } +.form-status.error { color: var(--dg); } + +/* folder picker */ +.folder-picker { width: 100%; max-width: 320px; } +.folder-picker-row { display: flex; gap: 8px; } +.folder-picker-value { flex: 1; min-width: 0; } +.folder-browser { + margin-top: 8px; + border: 1px solid var(--bd); + border-radius: 9px; + background: var(--bg2); + max-height: 240px; + overflow: auto; +} +.folder-item { + padding: 7px 11px; + font-size: 12.5px; + cursor: pointer; + border-bottom: 1px solid var(--bd); + display: flex; + align-items: center; + gap: 7px; +} +.folder-item:last-child { border-bottom: none; } +.folder-item:hover { background: var(--bgh); } +.folder-current { justify-content: space-between; } +.folder-current strong { font-family: 'IBM Plex Mono', monospace; font-size: 12px; } +.folder-icon { color: var(--txm); } + +/* ============================================================ + Logs + ============================================================ */ +.log-toolbar { + display: flex; + align-items: center; + gap: 10px; + flex-wrap: wrap; + margin-bottom: 14px; +} +.log-toolbar label { + display: flex; + align-items: center; + gap: 7px; + font-size: 12.5px; + color: var(--txm); + cursor: pointer; +} +.log-toolbar label input { accent-color: var(--ac); } +.log-search-input { flex: 1; min-width: 180px; background: var(--bg1); } +.log-lines-select { max-width: 150px; } +.log-toolbar-actions { display: flex; gap: 8px; margin-left: auto; } + +.log-viewer { + background: var(--term-bg); + border: 1px solid var(--bd); + border-radius: 14px; + padding: 14px 16px; + font-family: 'IBM Plex Mono', monospace; + font-size: 12px; + line-height: 1.75; + max-height: 64vh; + overflow: auto; +} +.log-line { display: flex; gap: 11px; align-items: baseline; padding: 1px 0; } +.log-line .log-ts { color: #5a5d68; flex-shrink: 0; } +.log-chip { + display: inline-block; + min-width: 60px; + text-align: center; + padding: 1px 6px; + border-radius: 5px; + font-size: 10px; + font-weight: 700; + letter-spacing: .05em; + flex-shrink: 0; +} +.log-chip.INFO { color: var(--log-info); background: var(--log-info-bg); } +.log-chip.WARNING { color: var(--log-warn); background: var(--log-warn-bg); } +.log-chip.ERROR { color: var(--log-err); background: var(--log-err-bg); } +.log-chip.DEBUG { color: var(--log-dbg); background: var(--log-dbg-bg); } +.log-chip.CRITICAL { color: var(--log-crit); background: var(--log-crit-bg); } +.log-msg { color: #c9ccd6; opacity: .95; min-width: 0; word-break: break-word; } +.log-msg.ERROR, .log-msg.CRITICAL { color: #ff8095; } +.log-msg.DEBUG { opacity: .65; } +.log-line-plain { color: #c9ccd6; opacity: .9; word-break: break-word; } + +/* in-message syntax highlight tints (applied within .log-msg) */ +.log-highlight-line { background: var(--acd); border-radius: 3px; } +.log-corrupt { color: var(--dg); font-weight: 600; } +.log-level-critical { color: var(--log-crit); font-weight: 700; } +.log-level-error { color: var(--log-err); font-weight: 700; } +.log-level-warning { color: var(--log-warn); font-weight: 700; } +.log-level-info { color: var(--log-info); } +.log-level-debug { color: var(--log-dbg); } +.log-timestamp { color: #5a5d68; } +.log-url { color: var(--inf); text-decoration: underline; } +.log-path { color: var(--ac); } +.log-number { color: var(--wn); } + +/* ============================================================ + Auth (login / setup) + ============================================================ */ +.auth-container { + min-height: 62vh; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; +} +.auth-card { + width: 380px; + max-width: 100%; + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 18px; + padding: 32px 30px; +} +.auth-head { + display: flex; + flex-direction: column; + align-items: center; + text-align: center; + margin-bottom: 24px; +} +.auth-logo { + width: 46px; + height: 46px; + border-radius: 13px; + background: radial-gradient(circle at 50% 38%, #1d1f28, #08090c); + border: 1px solid var(--bds); + display: flex; + align-items: center; + justify-content: center; + position: relative; + margin-bottom: 14px; +} +.auth-logo .logo-bar { width: 5px; height: 18px; border-radius: 2px; background: var(--spectrum); } +.auth-logo .logo-dot { width: 5px; height: 5px; border-radius: 50%; background: #ff4d6d; position: absolute; bottom: 8px; } +.auth-title { font-size: 19px; font-weight: 800; letter-spacing: -.02em; color: var(--tx); } +.auth-card .subtitle { font-size: 12.5px; color: var(--txm); margin: 4px 0 0; text-align: center; line-height: 1.5; } +.auth-form { margin-top: 6px; } +.auth-field { margin-bottom: 14px; } +.auth-field label { + display: block; + font-size: 12px; + font-weight: 600; + color: var(--txm); + margin-bottom: 6px; +} +.auth-field .input { width: 100%; padding: 11px 13px; border-radius: 10px; font-size: 14px; } +.auth-error { color: var(--dg); font-size: 12.5px; min-height: 16px; margin-bottom: 8px; } +.auth-submit { width: 100%; padding: 12px; border-radius: 10px; font-size: 14px; margin-top: 6px; } +.auth-help { + font-family: 'IBM Plex Mono', monospace; + font-size: 10.5px; + color: var(--txd); + text-align: center; + margin-top: 18px; + line-height: 1.6; +} +.auth-help code { background: none; padding: 0; color: var(--txd); } + +/* ============================================================ + Toasts + ============================================================ */ +#toast-container { + position: fixed; + bottom: 22px; + right: 22px; + display: flex; + flex-direction: column; + gap: 10px; + z-index: 200; + max-width: 360px; +} +.toast { + background: var(--bg1); + border: 1px solid var(--bd); + border-left: 3px solid var(--txm); + border-radius: 10px; + padding: 12px 14px; + color: var(--tx); + font-size: 13px; + box-shadow: 0 8px 24px rgba(0,0,0,.35); + position: relative; + overflow: hidden; + display: flex; + align-items: flex-start; + gap: 10px; +} +.toast.success { border-left-color: var(--ac); } +.toast.error { border-left-color: var(--dg); } +.toast.warning { border-left-color: var(--wn); } +.toast.info { border-left-color: var(--inf); } +.toast span { flex: 1; } +.toast-close { + background: none; + border: none; + color: var(--txm); + cursor: pointer; + font-size: 16px; + line-height: 1; + padding: 0; +} +.toast-progress { + position: absolute; + bottom: 0; left: 0; + height: 2px; + background: var(--ac); + transition-property: width; + transition-timing-function: linear; +} +.toast.error .toast-progress { background: var(--dg); } +.toast.warning .toast-progress { background: var(--wn); } +.toast.info .toast-progress { background: var(--inf); } + +/* ============================================================ + Modal (bulk delete progress) + ============================================================ */ +.modal-backdrop { + position: fixed; + inset: 0; + background: rgba(0,0,0,.6); + display: flex; + align-items: center; + justify-content: center; + z-index: 300; +} +.modal { + background: var(--bg1); + border: 1px solid var(--bd); + border-radius: 16px; + padding: 24px; + width: 460px; + max-width: 92vw; +} +.modal h3 { margin: 0 0 16px; font-size: 16px; font-weight: 700; color: var(--tx); } +.progress-bar { + height: 8px; + background: var(--bg2); + border-radius: 999px; + overflow: hidden; + margin-bottom: 10px; +} +.progress-fill { + height: 100%; + width: 0; + background: var(--ac); + border-radius: 999px; + transition: width .3s; +} +.progress-text { font-family: 'IBM Plex Mono', monospace; font-size: 12.5px; color: var(--tx); } +.progress-corrupt { font-size: 12px; color: var(--dg); margin-top: 4px; } +.progress-eta { font-size: 12px; color: var(--txm); margin-top: 4px; } +.progress-file { + font-family: 'IBM Plex Mono', monospace; + font-size: 11px; + color: var(--txd); + margin-top: 4px; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +/* ============================================================ + Mobile + ============================================================ */ +.mobile-only { display: none; } +.sidebar-overlay { + display: none; + position: fixed; + inset: 0; + background: rgba(0,0,0,.5); + z-index: 25; +} +.sidebar-overlay.visible { display: block; } + +@media (max-width: 860px) { + #sidebar { + position: fixed; + left: 0; top: 0; + transform: translateX(-100%); + transition: transform .2s; + } + #sidebar.open { transform: translateX(0); } + .mobile-only { display: inline-flex; align-items: center; justify-content: center; } + header { padding: 12px 16px; } + main#content { padding: 18px 16px; } + .config-group { grid-template-columns: 1fr; gap: 10px; } + .config-control { justify-content: flex-start; } + .config-control select, .config-control input { min-width: 0; width: 100%; max-width: none; } + .log-toolbar-actions { margin-left: 0; } +} + +body.auth-view #sidebar { display: none; } +body.auth-view .status-pill, +body.auth-view #logout-btn { display: none !important; } + +@media (prefers-reduced-motion: reduce) { + * { animation: none !important; } +} diff --git a/app/webui.py b/app/webui.py new file mode 100644 index 0000000..3dac381 --- /dev/null +++ b/app/webui.py @@ -0,0 +1,1200 @@ +"""BeatsCheck WebUI — optional web interface served via Python stdlib.""" + +import hashlib +import hmac +import http.cookies +import json +import logging +import mimetypes +import os +import re +import secrets +import threading +import time +import urllib.parse +from http.server import HTTPServer, SimpleHTTPRequestHandler +from socketserver import ThreadingMixIn + +logger = logging.getLogger("beatscheck.webui") + +# --------------------------------------------------------------------------- +# Shared application state (thread-safe) +# --------------------------------------------------------------------------- + + +class AppState: + """Thread-safe container for state shared between scan loop and WebUI.""" + + def __init__(self): + self._lock = threading.Lock() + self._data = { + "status": "starting", + "mode": "setup", + "scan_progress": None, + "last_scan_time": None, + "last_scan_duration": None, + "corrupt_count": 0, + "total_scanned": 0, + "version": "unknown", + "uptime_start": time.time(), + } + + def get(self, key=None): + with self._lock: + if key is not None: + return self._data.get(key) + return dict(self._data) + + def update(self, **kwargs): + with self._lock: + self._data.update(kwargs) + + def snapshot(self): + """Return full state dict plus computed fields.""" + with self._lock: + d = dict(self._data) + d["uptime"] = int(time.time() - d["uptime_start"]) + return d + + +# Module-level singleton — set by main.main() +app_state = AppState() + +# --------------------------------------------------------------------------- +# Authentication +# --------------------------------------------------------------------------- + +_AUTH_FILE = "webui_auth.json" +_PBKDF2_ITERATIONS = 100_000 +_SESSION_MAX_AGE = 86400 # 24 hours +_MIN_PASSWORD_LENGTH = 8 + +# Sentinel returned by _load_auth() when the auth file EXISTS but cannot be +# read/parsed (corruption or PUID/PGID permission drift). Callers must fail +# closed on this — never treat it as "no credentials configured", which +# would re-open the first-run setup wizard to an attacker. +_AUTH_UNREADABLE = object() + +_sessions = {} +_sessions_lock = threading.Lock() + +# Failed-login throttle (per client IP). In-memory; resets on restart. +_LOGIN_MAX_ATTEMPTS = 5 +_LOGIN_LOCKOUT_SECONDS = 300 +_LOGIN_ATTEMPT_WINDOW = 900 +_login_attempts = {} +_login_attempts_lock = threading.Lock() + + +def _hash_password(password, salt=None): + """Hash password with PBKDF2-SHA256. Returns 'pbkdf2:salt_hex:hash_hex'.""" + if salt is None: + salt = secrets.token_bytes(16) + else: + salt = bytes.fromhex(salt) + dk = hashlib.pbkdf2_hmac( + 'sha256', password.encode('utf-8'), salt, _PBKDF2_ITERATIONS) + return f"pbkdf2:{salt.hex()}:{dk.hex()}" + + +def _verify_password(password, stored_hash): + """Verify password against stored hash.""" + try: + _, salt_hex, hash_hex = stored_hash.split(':') + except (ValueError, AttributeError): + return False + expected = _hash_password(password, salt=salt_hex) + return hmac.compare_digest(expected, stored_hash) + + +def _load_auth(config_dir): + """Load auth credentials from webui_auth.json. + + Returns None when the file is ABSENT (first-run setup allowed), the + credentials dict when present and valid, or the _AUTH_UNREADABLE + sentinel when the file exists but cannot be parsed / is missing + required fields. Callers must fail closed on the sentinel: mapping a + read error to None would let anyone re-run /api/setup and seize the + account (auth fail-open).""" + path = os.path.join(config_dir, _AUTH_FILE) + if not os.path.isfile(path): + return None + try: + with open(path, 'r', encoding='utf-8') as f: + data = json.load(f) + except (json.JSONDecodeError, OSError, ValueError): + logger.error( + "WebUI auth file %s exists but is unreadable/corrupt; " + "refusing setup and login (fail closed)", path) + return _AUTH_UNREADABLE + if data.get('username') and data.get('password_hash'): + return data + logger.error( + "WebUI auth file %s is present but missing username/" + "password_hash; refusing setup and login (fail closed)", path) + return _AUTH_UNREADABLE + + +def _save_auth(config_dir, username, password): + """Hash password and save auth credentials atomically.""" + path = os.path.join(config_dir, _AUTH_FILE) + tmp = path + ".tmp" + data = { + "username": username, + "password_hash": _hash_password(password), + } + with open(tmp, 'w', encoding='utf-8') as f: + json.dump(data, f, indent=2) + os.rename(tmp, path) + logger.info("WebUI credentials created for user '%s'", username) + + +def _create_session(username): + """Create a new session token with 24h expiry.""" + token = secrets.token_hex(32) + with _sessions_lock: + _sessions[token] = { + "username": username, + "expires": time.time() + _SESSION_MAX_AGE, + } + return token + + +def _validate_session(token): + """Check if session token is valid and not expired.""" + if not token: + return False + with _sessions_lock: + session = _sessions.get(token) + if not session: + return False + if time.time() > session["expires"]: + del _sessions[token] + return False + return True + + +def _invalidate_session(token): + """Remove a session token.""" + with _sessions_lock: + _sessions.pop(token, None) + + +def _cleanup_sessions(): + """Remove all expired sessions.""" + now = time.time() + with _sessions_lock: + expired = [t for t, s in _sessions.items() + if now > s["expires"]] + for t in expired: + del _sessions[t] + + +def _login_lockout_remaining(key): + """Return remaining lockout seconds (>0) for *key*, else 0. + Expired attempt windows are pruned as a side effect.""" + now = time.time() + with _login_attempts_lock: + rec = _login_attempts.get(key) + if not rec: + return 0 + if rec["locked_until"] > now: + return int(rec["locked_until"] - now) + 1 + if now - rec["first"] > _LOGIN_ATTEMPT_WINDOW: + _login_attempts.pop(key, None) + return 0 + + +def _login_record_failure(key): + """Record a failed login for *key*; lock it once the attempt cap is + exceeded within the sliding window.""" + now = time.time() + with _login_attempts_lock: + rec = _login_attempts.get(key) + if not rec or now - rec["first"] > _LOGIN_ATTEMPT_WINDOW: + rec = {"count": 0, "first": now, "locked_until": 0} + _login_attempts[key] = rec + rec["count"] += 1 + if rec["count"] >= _LOGIN_MAX_ATTEMPTS: + rec["locked_until"] = now + _LOGIN_LOCKOUT_SECONDS + rec["count"] = 0 + rec["first"] = now + + +def _login_record_success(key): + """Clear the failure counter for *key* on a successful login.""" + with _login_attempts_lock: + _login_attempts.pop(key, None) + + +# --------------------------------------------------------------------------- +# API helpers +# --------------------------------------------------------------------------- + +_API_KEY_MASK = "********" + +# The canonical config-key map lives in main.py; import it so the WebUI's +# allowed-keys list and parser stay in sync without duplication. +from main import ( # noqa: E402 + _CONFIG_KEY_MAP, _load_corrupt_details, _parse_config_lines, + write_json_atomic, write_text_atomic, +) +_ALLOWED_CONFIG_KEYS = frozenset(_CONFIG_KEY_MAP) + +_config_write_lock = threading.Lock() + + +def _read_config_entries(config_dir): + """Return [{key, value}] in file order for display in the WebUI.""" + return [ + {"key": k, "value": v} + for k, v in _parse_config_lines( + os.path.join(config_dir, "beatscheck.conf")).items() + ] + + +def _format_config_value(val): + """Quote a config value unless it's a plain number or boolean.""" + s = str(val) + if re.match(r'^(\d+(?:\.\d+)?|true|false)$', s): + return s + return f'"{s}"' + + +def _write_config_file(config_dir, updates): + """Update beatscheck.conf preserving comments and structure. + Thread-safe via _config_write_lock; atomic via tmp+rename.""" + with _config_write_lock: + path = os.path.join(config_dir, "beatscheck.conf") + if not os.path.isfile(path): + return False + + new_vals = {k.lower(): v for k, v in updates.items()} + written_keys = set() + + with open(path, 'r') as f: + lines = f.readlines() + + result = [] + for line in lines: + stripped = line.strip() + commented_match = re.match(r'^#\s*(\w+)\s*=', stripped) + active_match = re.match(r'^(\w+)\s*=', stripped) + match = active_match or commented_match + if match: + key = match.group(1).lower() + if key in new_vals: + result.append( + f"{key} = {_format_config_value(new_vals[key])}\n") + written_keys.add(key) + continue + result.append(line) + + for key, val in new_vals.items(): + if key not in written_keys: + result.append( + f"{key} = {_format_config_value(val)}\n") + + write_text_atomic(path, ''.join(result)) + return True + + +_corrupt_cache = {"key": None, "value": None} +_corrupt_cache_lock = threading.Lock() + + +def _read_corrupt_list(config_dir): + """Read corrupt.txt and corrupt_details.json, merge into list. + Results are cached per (corrupt.txt, corrupt_details.json) mtime pair + so repeated polls don't re-stat every corrupt file.""" + corrupt_path = os.path.join(config_dir, "corrupt.txt") + details_path = os.path.join(config_dir, "corrupt_details.json") + + def _mtime(p): + try: + return os.path.getmtime(p) + except OSError: + return 0 + + key = (_mtime(corrupt_path), _mtime(details_path)) + with _corrupt_cache_lock: + if _corrupt_cache["key"] == key and _corrupt_cache["value"] is not None: + return _corrupt_cache["value"] + + paths = [] + if os.path.isfile(corrupt_path): + with open(corrupt_path, 'r', encoding='utf-8') as f: + paths = [line.strip() for line in f if line.strip()] + + details = _load_corrupt_details(config_dir) + dir_totals = {} + + result = [] + for p in paths: + info = details.get(p, {}) + entry = {"path": p, "reason": info.get("reason", "")} + if "trackfileId" in info: + entry["has_lidarr_id"] = True + try: + entry["size"] = os.path.getsize(p) + except OSError: + entry["size"] = 0 + entry["missing"] = True + d = os.path.dirname(p) + if d not in dir_totals: + try: + dir_totals[d] = len([ + f for f in os.listdir(d) + if os.path.isfile(os.path.join(d, f))]) + except OSError: + dir_totals[d] = 0 + entry["album_total"] = dir_totals[d] + result.append(entry) + + with _corrupt_cache_lock: + _corrupt_cache["key"] = key + _corrupt_cache["value"] = result + return result + + +def _read_log_tail(config_dir, lines=200): + """Read last N lines of beats_check.log. Returns (text, mtime).""" + log_path = os.path.join(config_dir, "beats_check.log") + try: + st = os.stat(log_path) + except OSError: + return ("", 0) + try: + with open(log_path, 'rb') as f: + size = st.st_size + chunk = min(size, 256 * 1024) + f.seek(max(0, size - chunk)) + data = f.read().decode('utf-8', errors='replace') + all_lines = data.splitlines() + return ('\n'.join(all_lines[-lines:]), int(st.st_mtime)) + except OSError: + return ("", 0) + + +def _read_summary(config_dir): + """Read summary.json from last scan.""" + path = os.path.join(config_dir, "summary.json") + if not os.path.isfile(path): + return {} + try: + with open(path, 'r', encoding='utf-8') as f: + return json.load(f) + except (json.JSONDecodeError, ValueError): + return {} + + +def _prune_json(path, keys): + """Remove *keys* from a JSON-object file, atomic write on change.""" + if not os.path.isfile(path): + return + try: + with open(path, 'r', encoding='utf-8') as f: + data = json.load(f) + except (json.JSONDecodeError, OSError): + return + for k in keys: + data.pop(k, None) + try: + write_json_atomic(path, data) + except OSError: + pass + + +def _ignore_corrupt_files(config_dir, files): + """Remove files from corrupt.txt and the JSON state files without + deleting the actual files. They'll reappear on the next scan if + they're still corrupt.""" + corrupt_path = os.path.join(config_dir, "corrupt.txt") + ignore_set = set(files) + + if os.path.isfile(corrupt_path): + with open(corrupt_path, 'r', encoding='utf-8') as f: + remaining = [line.strip() for line in f + if line.strip() and line.strip() not in ignore_set] + write_text_atomic(corrupt_path, + ''.join(p + '\n' for p in remaining)) + + _prune_json(os.path.join(config_dir, "corrupt_details.json"), files) + _prune_json(os.path.join(config_dir, "corrupt_tracking.json"), files) + + +def _is_subpath(child, root): + """True if *child* is *root* itself or a path beneath it. + Both paths must already be resolved with os.path.realpath(). + Uses a separator-aware check so /data2 is NOT treated as a + child of /data.""" + return child == root or child.startswith(root + os.sep) + + +def _list_dir(path): + """List immediate subdirectories of a path. Used by folder picker.""" + real = os.path.realpath(path) + if not _is_subpath(real, os.path.realpath('/data')): + return [] + if not os.path.isdir(real): + return [] + try: + return sorted([ + os.path.join(path, n) for n in os.listdir(real) + if os.path.isdir(os.path.join(real, n)) + and not n.startswith('.')]) + except OSError: + return [] + + +# --------------------------------------------------------------------------- +# Bulk-delete job tracker (in-memory; survives only while WebUI is running) +# --------------------------------------------------------------------------- + +_delete_jobs = {} +_delete_jobs_lock = threading.Lock() + + +def _new_delete_job(total, mode): + """Register a new delete job and return its id.""" + job_id = secrets.token_hex(8) + with _delete_jobs_lock: + _delete_jobs[job_id] = { + "id": job_id, + "mode": mode, + "total": total, + "done": 0, + "current": "", + "phase": "queued", + "errors": [], + "deleted": 0, + "finished": False, + "cancelled": False, + "cancel_requested": False, + "started_at": time.time(), + } + return job_id + + +def _update_delete_job(job_id, **kwargs): + with _delete_jobs_lock: + job = _delete_jobs.get(job_id) + if job is not None: + job.update(kwargs) + + +def _get_delete_job(job_id): + with _delete_jobs_lock: + job = _delete_jobs.get(job_id) + if job is None: + return None + return dict(job) + + +def _cancel_delete_job(job_id): + with _delete_jobs_lock: + job = _delete_jobs.get(job_id) + if job is None: + return False + job["cancel_requested"] = True + return True + + +def _prune_delete_jobs(max_age=3600): + """Drop finished jobs older than *max_age* to keep the store bounded.""" + cutoff = time.time() - max_age + with _delete_jobs_lock: + stale = [jid for jid, j in _delete_jobs.items() + if j.get("finished") and j.get("started_at", 0) < cutoff] + for jid in stale: + _delete_jobs.pop(jid, None) + + +def _run_delete_job(job_id, folders, config_dir, music_dir, mode): + """Background worker that runs delete_album_folders under the scan + lock with progress + cancel wired to the in-memory job store.""" + try: + from main import ( + _acquire_scan_lock, delete_album_folders, + ) + except ImportError: + _update_delete_job( + job_id, finished=True, phase="error", + errors=[{"folder": "", "error": "delete not available"}]) + return + + def progress_cb(index, total, folder, phase): + _update_delete_job( + job_id, done=index, total=total, + current=folder, phase=phase) + + def cancel_cb(): + job = _get_delete_job(job_id) + return bool(job and job.get("cancel_requested")) + + lock_fd = None + try: + lock_fd = _acquire_scan_lock(config_dir) + except OSError: + _update_delete_job( + job_id, finished=True, phase="error", + errors=[{"folder": "", + "error": "could not acquire scan lock"}]) + return + + try: + _update_delete_job(job_id, phase="running") + result = delete_album_folders( + folders, config_dir, music_dir=music_dir, + mode=mode, progress_cb=progress_cb, cancel_cb=cancel_cb) + _update_delete_job( + job_id, + deleted=result.get("count", 0), + errors=result.get("errors", []), + cancelled=result.get("cancelled", False), + finished=True, + phase="cancelled" if result.get("cancelled") else "done", + ) + except Exception as e: # noqa: BLE001 + logger.exception("Bulk delete job %s failed", job_id) + _update_delete_job( + job_id, finished=True, phase="error", + errors=[{"folder": "", "error": str(e)}]) + finally: + try: + import fcntl as _fcntl + if lock_fd is not None: + _fcntl.flock(lock_fd.fileno(), _fcntl.LOCK_UN) + lock_fd.close() + try: + os.remove(os.path.join(config_dir, ".scanning")) + except OSError: + pass + except ImportError: + pass + _prune_delete_jobs() + + +def _run_delete_files_job(job_id, files, config_dir, music_dir): + """Background worker that runs delete_corrupt_files under the scan + lock with progress + cancel wired to the in-memory job store. Used + by /api/delete-files so multi-album file deletes don't block the + browser for the full 30s-per-album Lidarr search window.""" + try: + from main import _acquire_scan_lock, delete_corrupt_files + except ImportError: + _update_delete_job( + job_id, finished=True, phase="error", + errors=[{"path": "", "error": "delete not available"}]) + return + + def progress_cb(index, total, label, phase): + _update_delete_job( + job_id, done=index, total=total, + current=label, phase=phase) + + def cancel_cb(): + job = _get_delete_job(job_id) + return bool(job and job.get("cancel_requested")) + + lock_fd = None + try: + lock_fd = _acquire_scan_lock(config_dir) + except OSError: + _update_delete_job( + job_id, finished=True, phase="error", + errors=[{"path": "", + "error": "could not acquire scan lock"}]) + return + + try: + _update_delete_job(job_id, phase="running") + result = delete_corrupt_files( + files, config_dir, music_dir=music_dir, + progress_cb=progress_cb, cancel_cb=cancel_cb) + cancelled = bool( + _get_delete_job(job_id) and + _get_delete_job(job_id).get("cancel_requested")) + _update_delete_job( + job_id, + deleted=result.get("count", 0), + errors=result.get("errors", []), + cancelled=cancelled, + finished=True, + phase="cancelled" if cancelled else "done", + ) + except Exception as e: # noqa: BLE001 + logger.exception("Bulk file-delete job %s failed", job_id) + _update_delete_job( + job_id, finished=True, phase="error", + errors=[{"path": "", "error": str(e)}]) + finally: + try: + import fcntl as _fcntl + if lock_fd is not None: + _fcntl.flock(lock_fd.fileno(), _fcntl.LOCK_UN) + lock_fd.close() + try: + os.remove(os.path.join(config_dir, ".scanning")) + except OSError: + pass + except ImportError: + pass + _prune_delete_jobs() + + +def _trigger_rescan(config_dir, mode="report", fresh=False): + """Trigger a rescan by writing the .rescan file. + When *fresh* is True the trigger content is prefixed with ``fresh:`` + so the scanner clears the resume cache at scan start (not now), + avoiding a race where a running scan rebuilds processed.txt + between the delete and the next scan.""" + rescan_path = os.path.join(config_dir, ".rescan") + try: + content = ("fresh:" + mode) if fresh else mode + with open(rescan_path, 'w') as f: + f.write(content) + return True + except OSError: + return False + + +# --------------------------------------------------------------------------- +# HTTP Request Handler +# --------------------------------------------------------------------------- + +_MAX_BODY = 1_048_576 # 1 MB +_SESSION_COOKIE = "beatscheck_session" + + +class WebUIHandler(SimpleHTTPRequestHandler): + """Handles both static files and /api/* JSON endpoints.""" + + def log_message(self, format, *args): + pass + + def _json_response(self, data, status=200, cookies=None): + body = json.dumps(data).encode('utf-8') + try: + self.send_response(status) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', len(body)) + self.send_header('Cache-Control', 'no-cache') + if cookies: + for cookie in cookies: + self.send_header('Set-Cookie', cookie) + self.end_headers() + self.wfile.write(body) + except (ConnectionResetError, BrokenPipeError): + # Client disconnected before we could reply (common for + # long-running endpoints like /api/delete). Server-side + # work already completed; nothing to do. + pass + + def _read_body(self): + """Parse JSON request body. Returns dict or None on error.""" + try: + length = int(self.headers.get('Content-Length', 0)) + except (ValueError, TypeError): + self._json_response( + {"error": "invalid content-length"}, 400) + return None + if length <= 0 or length > _MAX_BODY: + self._json_response( + {"error": "body too large or empty"}, 400) + return None + try: + return json.loads(self.rfile.read(length)) + except ValueError: + self._json_response({"error": "invalid JSON"}, 400) + return None + + def _get_session_token(self): + """Extract session token from cookies.""" + jar = http.cookies.SimpleCookie() + jar.load(self.headers.get('Cookie', '')) + morsel = jar.get(_SESSION_COOKIE) + return morsel.value if morsel else None + + def _check_auth(self): + """Check if request is authenticated. Returns True or + sends 401 and returns False.""" + _cleanup_sessions() + if _validate_session(self._get_session_token()): + return True + self._json_response({"error": "unauthorized"}, 401) + return False + + def _client_ip(self): + """Best-effort client IP for rate-limiting. Trusts the first + X-Forwarded-For hop when present (the README's remote-access + deployment sits behind a reverse proxy), else the socket peer.""" + xff = self.headers.get('X-Forwarded-For', '') + if xff: + return xff.split(',')[0].strip() + return self.client_address[0] + + def _session_cookie(self, token, max_age=_SESSION_MAX_AGE): + """Build a Set-Cookie header value. The Secure attribute is added + when the request arrived over HTTPS (directly or via a reverse + proxy that sets X-Forwarded-Proto), or when explicitly forced via + WEBUI_FORCE_SECURE_COOKIE — so it hardens the documented HTTPS + deployment without breaking a plain-HTTP LAN install.""" + xfp = self.headers.get( + 'X-Forwarded-Proto', '').split(',')[0].strip().lower() + forced = os.environ.get( + 'WEBUI_FORCE_SECURE_COOKIE', '').strip().lower() \ + in ('1', 'true', 'yes', 'on') + secure = "Secure; " if (xfp == 'https' or forced) else "" + return (f"{_SESSION_COOKIE}={token}; " + f"HttpOnly; SameSite=Strict; {secure}Path=/; " + f"Max-Age={max_age}") + + def do_GET(self): + config_dir = self.server.config_dir + + # Auth-exempt: status check and static assets + if self.path == '/api/auth-status': + auth = _load_auth(config_dir) + token = self._get_session_token() + if auth is _AUTH_UNREADABLE: + # Fail closed: do NOT report setup_required (that would + # re-open the setup wizard). Existing valid sessions still + # work; a fresh client is sent to the login page. + self._json_response({ + "setup_required": False, + "authenticated": _validate_session(token), + "error": "auth store unreadable", + }, 503) + return + self._json_response({ + "setup_required": auth is None, + "authenticated": _validate_session(token), + }) + return + + # Static assets are always served (login page needs them) + if self.path == '/' or self.path == '/index.html': + self._serve_static('index.html') + return + if not self.path.startswith('/api/'): + self._serve_static(self.path.lstrip('/')) + return + + # All other API endpoints require auth + if not self._check_auth(): + return + + if self.path == '/api/status': + state = app_state.snapshot() + summary = _read_summary(config_dir) + # Merge live library size from scan into summary so the + # dashboard shows it immediately, not only after scan ends + for key in ('library_size_human', 'library_files'): + val = state.get(key) + if val is not None: + summary[key] = val + state["summary"] = summary + self._json_response(state) + + elif self.path == '/api/config': + entries = _read_config_entries(config_dir) + for entry in entries: + if entry["key"] == "lidarr_api_key" \ + and entry["value"]: + entry["value"] = _API_KEY_MASK + self._json_response({"config": entries}) + + elif self.path == '/api/corrupt': + files = _read_corrupt_list(config_dir) + self._json_response( + {"files": files, "count": len(files)}) + + elif self.path == '/api/log' or self.path.startswith('/api/log?'): + params = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query) + try: + lines = max(1, min( + int(params.get('lines', ['200'])[0]), 10000)) + except (ValueError, TypeError): + lines = 200 + since = params.get('since', ['0'])[0] + try: + since_mtime = int(since) + except (ValueError, TypeError): + since_mtime = 0 + text, mtime = _read_log_tail(config_dir, lines) + if mtime and mtime == since_mtime: + self._json_response({"log": None, "mtime": mtime, "unchanged": True}) + else: + self._json_response({"log": text, "mtime": mtime}) + + elif (self.path == '/api/delete-job-status' + or self.path.startswith('/api/delete-job-status?')): + params = urllib.parse.parse_qs( + urllib.parse.urlparse(self.path).query) + job_id = params.get('id', [''])[0] + job = _get_delete_job(job_id) + if job is None: + self._json_response({"error": "job not found"}, 404) + return + self._json_response(job) + + elif self.path == '/api/paths' or self.path.startswith('/api/paths?'): + params = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query) + parent = params.get('dir', ['/data'])[0] + real = os.path.realpath(parent) + if not _is_subpath(real, os.path.realpath('/data')): + self._json_response( + {"error": "path outside /data"}, 403) + return + self._json_response({ + "path": parent, + "children": _list_dir(parent), + }) + + else: + self._json_response({"error": "not found"}, 404) + + def _handle_setup(self, config_dir): + """Handle POST /api/setup — first-run credential creation.""" + auth = _load_auth(config_dir) + if auth is _AUTH_UNREADABLE: + self._json_response( + {"error": "auth store unreadable — refusing setup"}, + 503) + return + if auth is not None: + self._json_response( + {"error": "already configured"}, 400) + return + body = self._read_body() + if body is None: + return + username = (body.get('username') or '').strip() + password = body.get('password') or '' + if not username: + self._json_response( + {"error": "username required"}, 400) + return + if len(password) < _MIN_PASSWORD_LENGTH: + self._json_response( + {"error": f"password must be at least " + f"{_MIN_PASSWORD_LENGTH} characters"}, 400) + return + _save_auth(config_dir, username, password) + token = _create_session(username) + self._json_response( + {"ok": True}, + cookies=[self._session_cookie(token)]) + + def _handle_login(self, config_dir): + """Handle POST /api/login — credential validation.""" + key = self._client_ip() + locked = _login_lockout_remaining(key) + if locked: + self._json_response( + {"error": f"too many failed attempts — try again in " + f"{locked}s"}, 429) + return + body = self._read_body() + if body is None: + return + auth = _load_auth(config_dir) + if auth is _AUTH_UNREADABLE: + self._json_response( + {"error": "auth store unreadable"}, 503) + return + if auth is None: + self._json_response( + {"error": "setup required"}, 400) + return + username = (body.get('username') or '').strip() + password = body.get('password') or '' + if (username == auth['username'] + and _verify_password( + password, auth['password_hash'])): + _login_record_success(key) + token = _create_session(username) + self._json_response( + {"ok": True}, + cookies=[self._session_cookie(token)]) + else: + _login_record_failure(key) + self._json_response( + {"error": "invalid credentials"}, 401) + + def _handle_logout(self): + """Handle POST /api/logout — session invalidation.""" + token = self._get_session_token() + if token: + _invalidate_session(token) + self._json_response( + {"ok": True}, + cookies=[self._session_cookie("", max_age=0)]) + + def _handle_ignore(self, config_dir): + """Handle POST /api/ignore — remove files from corrupt list.""" + body = self._read_body() + if body is None: + return + files = body.get('files', []) + if not files: + self._json_response( + {"error": "no files specified"}, 400) + return + _ignore_corrupt_files(config_dir, files) + self._json_response({"ok": True}) + + def do_POST(self): + config_dir = self.server.config_dir + + # Auth endpoints (no session required) + if self.path == '/api/setup': + self._handle_setup(config_dir) + return + if self.path == '/api/login': + self._handle_login(config_dir) + return + if self.path == '/api/logout': + self._handle_logout() + return + + # Protected endpoints + if not self._check_auth(): + return + + if self.path == '/api/config': + body = self._read_body() + if body is None: + return + updates = body.get('config', {}) + if not updates: + self._json_response( + {"error": "no config provided"}, 400) + return + # Reject unknown config keys + rejected = set(updates) - _ALLOWED_CONFIG_KEYS + if rejected: + self._json_response( + {"error": "unknown keys: " + + ", ".join(sorted(rejected))}, 400) + return + # Don't overwrite real key with the mask sentinel + if updates.get('lidarr_api_key') == _API_KEY_MASK: + del updates['lidarr_api_key'] + ok = _write_config_file(config_dir, updates) + if ok: + # Sync updated values to os.environ so other + # operations (e.g. delete) see them immediately. + for key, val in updates.items(): + env_name = key.upper() + os.environ[env_name] = str(val) + self._json_response({"ok": True}) + else: + self._json_response( + {"error": "config file not found"}, 500) + + elif self.path == '/api/rescan': + body = self._read_body() + if body is None: + return + mode = body.get('mode', 'report') + fresh = body.get('fresh', False) + if mode not in ('report', 'move'): + self._json_response( + {"error": "mode must be report or move"}, + 400) + return + ok = _trigger_rescan(config_dir, mode, fresh) + self._json_response({"ok": ok}) + + elif self.path == '/api/cancel': + try: + from main import cancel_scan + cancel_scan() + self._json_response({"ok": True}) + except ImportError: + self._json_response( + {"error": "cancel not available"}, 500) + + elif self.path == '/api/delete': + body = self._read_body() + if body is None: + return + files = body.get('files', []) + if not files: + self._json_response( + {"error": "no files specified"}, 400) + return + try: + from main import delete_corrupt_files + except ImportError: + self._json_response( + {"error": "delete not available"}, 500) + return + music_dir = os.environ.get("MUSIC_DIR", "/data") + result = delete_corrupt_files( + files, config_dir, music_dir=music_dir) + self._json_response(result) + + elif self._dispatch_bulk_delete(config_dir): + return + + elif self.path == '/api/ignore': + self._handle_ignore(config_dir) + + else: + self._json_response({"error": "not found"}, 404) + + def _dispatch_bulk_delete(self, config_dir): + """Handle bulk-delete-related POSTs. Returns True if the path + matched (response already sent), False otherwise.""" + if self.path == '/api/delete-albums': + self._handle_delete_albums(config_dir) + return True + if self.path == '/api/delete-files': + self._handle_delete_files(config_dir) + return True + if (self.path == '/api/delete-job-cancel' + or self.path.startswith('/api/delete-job-cancel?')): + params = urllib.parse.parse_qs( + urllib.parse.urlparse(self.path).query) + ok = _cancel_delete_job(params.get('id', [''])[0]) + self._json_response({"ok": ok}) + return True + return False + + def _handle_delete_albums(self, config_dir): + """POST /api/delete-albums — fire-and-forget bulk delete. + Returns 202 + {job_id}; progress polled via + /api/delete-job-status?id=...""" + body = self._read_body() + if body is None: + return + folders = body.get('folders', []) + mode = body.get('mode', 'whole') + if not isinstance(folders, list) or not folders: + self._json_response( + {"error": "no folders specified"}, 400) + return + if mode not in ('whole', 'corrupt'): + self._json_response( + {"error": "mode must be 'whole' or 'corrupt'"}, 400) + return + try: + from main import _MAX_DELETE_ALBUMS + except ImportError: + _MAX_DELETE_ALBUMS = 50 + if len(folders) > _MAX_DELETE_ALBUMS: + self._json_response( + {"error": f"too many folders " + f"(max {_MAX_DELETE_ALBUMS})"}, 400) + return + # Refuse to start if a scan is running + lock_path = os.path.join(config_dir, ".scanning") + if os.path.exists(lock_path): + self._json_response( + {"error": "scan in progress — cannot delete"}, 409) + return + music_dir = os.environ.get("MUSIC_DIR", "/data") + job_id = _new_delete_job(len(folders), mode) + thread = threading.Thread( + target=_run_delete_job, + args=(job_id, folders, config_dir, music_dir, mode), + daemon=True, + ) + thread.start() + self._json_response( + {"job_id": job_id, "total": len(folders)}, 202) + + def _handle_delete_files(self, config_dir): + """POST /api/delete-files — fire-and-forget file-level delete. + Body: {"files": [...]}. Returns 202 + {job_id}; progress polled + via /api/delete-job-status?id=...""" + body = self._read_body() + if body is None: + return + files = body.get('files', []) + if not isinstance(files, list) or not files: + self._json_response( + {"error": "no files specified"}, 400) + return + if len(files) > 5000: + self._json_response( + {"error": "too many files (max 5000)"}, 400) + return + lock_path = os.path.join(config_dir, ".scanning") + if os.path.exists(lock_path): + self._json_response( + {"error": "scan in progress — cannot delete"}, 409) + return + music_dir = os.environ.get("MUSIC_DIR", "/data") + # Progress is measured per-album; Lidarr lookup happens inside + # the worker. Report total as file count for the initial display. + job_id = _new_delete_job(len(files), "files") + thread = threading.Thread( + target=_run_delete_files_job, + args=(job_id, files, config_dir, music_dir), + daemon=True, + ) + thread.start() + self._json_response( + {"job_id": job_id, "total": len(files)}, 202) + + def _serve_static(self, filename): + """Serve a file from the static directory.""" + static_dir = self.server.static_dir + filepath = os.path.join(static_dir, filename) + real = os.path.realpath(filepath) + if not _is_subpath(real, os.path.realpath(static_dir)): + self.send_error(403) + return + if not os.path.isfile(real): + self.send_error(404) + return + + ext = os.path.splitext(filename)[1].lower() + ct, _ = mimetypes.guess_type(filename) + ct = ct or 'application/octet-stream' + if ext in ('.html', '.css', '.js'): + ct += '; charset=utf-8' + + with open(real, 'rb') as f: # CodeQL[py/path-injection] false positive: real is validated by _is_subpath() above + body = f.read() + self.send_response(200) + self.send_header('Content-Type', ct) + self.send_header('Content-Length', len(body)) + if ext in ('.html', '.css', '.js'): + self.send_header('Cache-Control', 'no-cache') + self.end_headers() + self.wfile.write(body) + + +# --------------------------------------------------------------------------- +# Threaded HTTP Server +# --------------------------------------------------------------------------- + + +class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): + daemon_threads = True + + def __init__(self, addr, handler, config_dir, static_dir): + self.config_dir = config_dir + self.static_dir = static_dir + super().__init__(addr, handler) + + +def start_webui(config_dir, port=8484, static_dir=None): + """Start the WebUI HTTP server in a daemon thread.""" + if static_dir is None: + static_dir = os.path.join( + os.path.dirname(__file__), 'static') + + server = ThreadedHTTPServer( + ('0.0.0.0', port), WebUIHandler, config_dir, static_dir) + + thread = threading.Thread( + target=server.serve_forever, daemon=True) + thread.start() + logger.info("WebUI started on port %d", port) + return server diff --git a/beats-check.xml b/beats-check.xml new file mode 100644 index 0000000..9c147f0 --- /dev/null +++ b/beats-check.xml @@ -0,0 +1,32 @@ + + + BeatsCheck + ghcr.io/chodeus/beatscheck:latest + https://github.com/chodeus/BeatsCheck/pkgs/container/beatscheck + bridge + + sh + false + https://github.com/chodeus/BeatsCheck/issues + https://github.com/chodeus/BeatsCheck + https://github.com/chodeus/BeatsCheck/blob/main/README.md + Audio file integrity checker that decode-tests all audio files (FLAC, MP3, M4A, OGG, Opus, WAV, AAC, and more) using ffmpeg. Identifies corrupted files that cause playback errors.[br][br]The container scans on a schedule and stays running. Default: idle mode (no scanning until configured).[br][br]Settings are configured in /config/beatscheck.conf (auto-created on first run).[br]To review results: check /config/corrupt.txt[br]To delete corrupt files: docker exec -it BeatsCheck /app/delete.sh + MediaApp:Music Tools: + + + https://raw.githubusercontent.com/chodeus/BeatsCheck/main/icon.png + --restart unless-stopped + + + + + + /mnt/user/data + /mnt/user/appdata/beatscheck + 99 + 100 + 002 + UTC + 8484 + + diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..78a70e9 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,58 @@ +### BeatsCheck compose.yaml Example +### https://github.com/chodeus/BeatsCheck +### +### Usage: +### docker compose up -d # Start in background +### docker compose logs -f # Follow logs +### docker exec -it beatscheck delete # Interactive delete +### docker exec beatscheck rescan # Trigger rescan +### +services: + beatscheck: + image: ghcr.io/chodeus/beatscheck:latest + container_name: beatscheck + restart: unless-stopped + volumes: + ## Music/media library — same path you use in Lidarr/Sonarr. + ## Mount read-only for report mode, rw for move or delete. + - /mnt/user/data:/data:ro + ## Persistent config: logs, corrupt.txt, tracking data + - /path/to/config:/config + ## Auto-detect host timezone (no TZ env var needed if mounted) + - /etc/localtime:/etc/localtime:ro + environment: + ## Container user/group IDs and timezone + - PUID=1000 + - PGID=1000 + - TZ=America/New_York + - UMASK=002 + ## All other settings (mode, workers, Lidarr, etc.) are configured in + ## /config/beatscheck.conf — auto-created on first run. + ## WebUI: uncomment to enable the web interface. + ## Also set webui = true in /config/beatscheck.conf + ## On first access you'll create login credentials via a setup wizard. + ## To reset credentials: docker exec beatscheck reset-webui-password + ## WARNING: Do not expose the WebUI port directly to the internet. + ## Use a reverse proxy with HTTPS (Nginx, Caddy, Traefik) for remote access. + # ports: + # - "8484:8484" + ## Optional: limit CPU usage to avoid impacting other services + # cpus: 2 + ## Optional hardening: drop every capability, add back only the five + ## the entrypoint actually needs (PUID/PGID remap + chown + su-exec), + ## and block setuid escalation. Even during the brief root window at + ## startup, the container has no NET_RAW, SYS_ADMIN, or mount powers. + # cap_drop: + # - ALL + # cap_add: + # - CHOWN + # - SETUID + # - SETGID + # - FOWNER + # - DAC_OVERRIDE + # security_opt: + # - no-new-privileges:true + ## Fully rootless alternative: replace PUID/PGID env with `user:` and + ## pre-chown the host config dir (sudo chown -R 99:100 ./config). + ## The entrypoint detects non-root invocation and skips the chown/su-exec. + # user: "99:100" diff --git a/design_handoff_beatscheck_redesign/BeatsCheck.dc.html b/design_handoff_beatscheck_redesign/BeatsCheck.dc.html new file mode 100644 index 0000000..97f5419 --- /dev/null +++ b/design_handoff_beatscheck_redesign/BeatsCheck.dc.html @@ -0,0 +1,559 @@ + + + + + + + + + + + + + + + +
+ + + +
+ +
+
{{ screenTitle }}
+
+
+ + {{ statusText }} +
+ +
+
+ +
+ + +
+
+
+
Status
+
{{ statusWord }}
+
+
+
Mode
+
Report
+
+
+
Library
+
98,432
+
files · 2.8 TB
+
+
+
Corrupt
+
{{ corruptCount }}
+
flagged · 142.5 MB
+
+
+
Workers
+
6
+
+
+
Uptime
+
3d 4h
+
+
+ + +
+
+
+
Decoding library
+
{{ currentFile }}
+
+
+
62%
+
{{ checkedText }}
+
+
+
+
+
+
+
+
+
+
+ {{ checkedText }} checked + {{ corruptCount }} corrupt found + ETA 1h 24m + 6 workers active +
+
+
+ + +
+
✓
+
+
Scanner idle
+
Last scan 2h ago · 98,432 files · {{ corruptCount }} flagged
+
+ +
+
+ +
+ + + + +
+ +
Recently flagged last 24h
+
+ +
+
+
+
{{ f.name }}
+
{{ f.error }}
+
+
{{ f.size }}
+
+
+
+
+
+ + +
+
+ + + + +
+ +
+
+
File
Error
Size
+
+ + +
No corrupt files — library is clean.
+
+ + + +
+ +
+
{{ f.name }}
+
{{ f.folder }}
+
+
{{ f.error }}
+
{{ f.size }}
+ +
+
+
+ + + +
+
{{ al.name }}
+
{{ al.count }}
+
+ +
+ +
+
{{ f.name }}
+
{{ f.folder }}
+
+
{{ f.error }}
+
{{ f.size }}
+ +
+
+
+
+
+
+
+ + +
+

Changes save to beatscheck.conf and apply on the next scan.

+ +
+
{{ sec.title }}
+
{{ sec.help }}
+
+ +
+
+
{{ f.key }}
+
{{ f.desc }}
+
+
+
+ + +
+
+
+
+
+
+
+ + Last saved 2h ago +
+
+
+ + +
+
+ + + + + +
+
+ +
+ {{ l.t }} + {{ l.lv }} + {{ l.m }} +
+
+
+
+
+ + +
+
+
+
+
+
+
+
Welcome back
+
Sign in to BeatsCheck
+
+ + + + + +
Forgot your password?
docker exec beatscheck reset-webui-password
+
+
+
+ + +
+
+
+
+
+
+
+
Create your credentials
+
First-run setup. These are stored hashed in the container.
+
+ + + + + + + +
+
+
+ +
+
+
+
+ + + diff --git a/design_handoff_beatscheck_redesign/README.md b/design_handoff_beatscheck_redesign/README.md new file mode 100644 index 0000000..fb6c70e --- /dev/null +++ b/design_handoff_beatscheck_redesign/README.md @@ -0,0 +1,202 @@ +# Handoff: BeatsCheck WebUI Redesign + +## Overview +A full visual + interaction redesign of **BeatsCheck** — a self-hosted audio-library integrity checker (Docker/Unraid app that decodes every track with ffmpeg and flags corrupt files). This handoff covers the entire web interface: **Dashboard, Corrupt Files, Configuration, Logs, Login, and Setup**. The redesign keeps the project's spectrogram/audio-heat identity but sharpens it into a modern, dark "audio-tool" dashboard with a disciplined signal-green accent, a working light mode, and a signature spectrogram scan-progress visualization. + +The original app serves a static `index.html` + `app.js` from a FastAPI backend (`app/main.py`), polling JSON endpoints for status/progress/corrupt-list/logs. This redesign is a drop-in replacement for that front-end layer — same data, new presentation. + +## About the Design Files +The files in this bundle are **design references created in HTML** — a prototype showing the intended look and behavior, **not production code to copy directly**. The prototype is authored as a single "Design Component" (`BeatsCheck.dc.html`) that uses a small custom template runtime (`support.js`); **do not ship that runtime**. The `{{ }}` holes, ``, ``, and ``/`` tags are authoring conveniences, not part of the target stack. + +Your task is to **recreate these designs in BeatsCheck's existing front-end environment** — the app currently uses vanilla JS (`app/static/app.js`) talking to a FastAPI backend, so the most faithful path is plain HTML/CSS/JS (or a light framework like Preact/Alpine if you prefer) wired to the existing JSON endpoints. Reuse the backend's real data shapes; replace only the presentation. If you choose to introduce a framework, match the project's lightweight, dependency-light philosophy. + +## Fidelity +**High-fidelity (hifi).** Final colors, typography, spacing, radii, and interactions are all specified below and should be reproduced precisely. All sample data (file names, log lines, stats) is placeholder — wire the real backend data in its place. + +--- + +## Design Tokens + +### Color palette — Dark theme (default) +| Token | Hex | Use | +|---|---|---| +| `--bg` | `#0a0b0e` | App background (main content area) | +| `--bg1` | `#121319` | Surfaces: sidebar, header, cards, tables | +| `--bg2` | `#1a1c23` | Inset surfaces: inputs, selects, secondary buttons, album group headers | +| `--bgh` | `#22242d` | Hover surface | +| `--bd` | `#23252e` | Default borders / row dividers | +| `--bds` | `#30333d` | Stronger borders (logo chip, scrollbar) | +| `--tx` | `#ecedf1` | Primary text | +| `--txm` | `#9396a3` | Muted text (labels, secondary) | +| `--txd` | `#5f626d` | Dim text (meta, mono captions) | +| terminal bg | `#08090c` | Logs viewer background | + +### Color palette — Light theme +| Token | Hex | +|---|---| +| `--bg` | `#eef0f4` | +| `--bg1` | `#ffffff` | +| `--bg2` | `#f3f4f8` | +| `--bgh` | `#e7e9f0` | +| `--bd` | `#e3e5ed` | +| `--bds` | `#d2d5df` | +| `--tx` | `#16171c` | +| `--txm` | `#5b5f6b` | +| `--txd` | `#9094a0` | + +### Accent (default = "signal" green) +| Token | Dark | Light | Notes | +|---|---|---|---| +| `--ac` (accent) | `#34e0a0` | `#0f9d6e` | Primary buttons, active nav, status-OK, scan % | +| `--acd` (accent dim) | `rgba(52,224,160,.14)` | same | Active nav bg, selected row bg, accent-tinted panels | +| `--acft` (accent foreground) | `#06120d` | `#06120d` | Text on accent buttons (near-black) | + +Two alternate accents were built as theme options (swap `--ac`/`--acd`/`--acft` together): +- **amber**: dark `#f5a623` / light `#bf7d0c`, dim `rgba(245,166,35,.15)`, fg `#06120d` +- **indigo**: dark `#6c7bff` / light `#4a59e6`, dim `rgba(108,123,255,.16)`, fg `#ffffff` + +### Semantic colors +| Token | Dark | Light | Use | +|---|---|---|---| +| `--dg` (danger) | `#ff4d6d` | `#d6294e` | Corrupt counts, errors, delete buttons, corruption spikes | +| `--dgd` (danger dim) | `rgba(255,77,109,.13)` | `rgba(214,41,78,.1)` | Badge backgrounds | +| `--wn` (warning) | `#f5b94a` | `#b9831a` | Scanning status dot, WARNING log chips | +| `--inf` (info) | `#5a9cf0` | `#2f72d6` | INFO log chips | + +### Log-level chip colors (terminal) +| Level | Text | Tint bg | +|---|---|---| +| INFO | `#5a9cf0` | `rgba(90,156,240,.14)` | +| WARNING | `#f5b94a` | `rgba(245,185,74,.14)` | +| ERROR | `#ff5775` | `rgba(255,87,117,.15)` | +| DEBUG | `#6b6f7d` | `rgba(107,111,125,.18)` | +| CRITICAL | `#ff3868` | `rgba(255,56,104,.16)` | + +Log message body text: ERROR rows `#ff8095`; all others `#c9ccd6` (DEBUG at 0.65 opacity, others 0.95). Timestamp `#5a5d68`. + +### Spectrum (signature gradient) +`linear-gradient(180deg,#6c5cff,#4b86ff,#34e0a0,#9ad94a,#f5c24a,#ff7a4d)` — used for the logo mark and as the per-frequency color ramp in the scan visualization. Spectrogram bar color stops (by height bucket, low→high): `#6c5cff`, `#4b86ff`, `#34e0a0`, `#9ad94a`, `#f5c24a`, `#ff7a4d`. Corrupt bars override to `#ff4d6d` with glow. + +### Typography +- **Display / UI**: `'Hanken Grotesk', system-ui, -apple-system, sans-serif` — weights 400/500/600/700/800. `-webkit-font-smoothing: antialiased`. +- **Mono**: `'IBM Plex Mono', monospace` — weights 400/500/600. Used for file paths, log lines, stat captions, config keys, version string, nav numbers, section eyebrows. +- Google Fonts import: `Hanken+Grotesk:wght@400;500;600;700;800` and `IBM+Plex+Mono:wght@400;500;600`. + +Key sizes (px): page title 18/700; card stat value 21–23/700 (letter-spacing −.02em); card eyebrow 9.5 mono uppercase, letter-spacing .1em; scan % 30/800 (−.03em); nav item 13.5/600; body/secondary 12.5–13; mono captions 10.5–11; table mono cells 12–12.5; log lines 12/1.75. + +### Spacing / radii +- Card radius **14px**; large panels (scan hero, auth cards) **16–18px**; buttons **8–10px**; inputs **8–10px**; chips/badges **999px** (pills) or **5px** (log chips); logo chip **11–13px**. +- Main content padding `26px 30px`; header padding `15px 30px`; card padding `18px` comfortable / `13px` compact (token `--cardpad`). +- Table row padding `13px 16px` comfortable / `9px` compact (token `--rowpad`). +- Stat grid gap `14px` comfortable / `10px` compact (token `--gap`). +- Sidebar width **238px** (fixed, sticky, full height). + +### Shadows / glows +- Status dot glow: `0 0 8px `. +- Scan sweep line: `box-shadow: 0 0 14px var(--ac)`, with an 8px dot head glowing `0 0 10px var(--ac)`. +- Corrupt spectrogram bars: `box-shadow: 0 0 8px rgba(255,77,109,.85)`. +- Switch knob: `0 1px 2px rgba(0,0,0,.3)`. + +### Animations (keyframes) +- `bc-pulse` (1.4s ease-in-out infinite): `opacity 1 → .28 → 1` — scanning status dot. +- `bc-sweep` (1.6s ease-in-out infinite): `opacity .55 → 1 → .55` — scan playhead line. +- Row/selection/switch transitions: `.15s–.2s`. + +--- + +## Layout (app shell) +Two-column flex. **Left:** fixed 238px sidebar (`--bg1`, right border `--bd`, sticky, 100vh, flex-column). **Right:** flex-1 column = sticky header (`--bg1`, bottom border) + scrolling `
` (padding `26px 30px`). Most screens cap content at `max-width: 1080px` (config 840px, auth centered cards). + +### Sidebar +- **Logo block** (padding `20px 20px 16px`): 36px rounded chip — `radial-gradient(circle at 50% 38%, #1d1f28, #08090c)`, border `--bds` — containing a 4×15px spectrum-gradient bar + a 4px `#ff4d6d` dot near the bottom. Wordmark "Beats**Check**" (Check in `--ac`), 16/800, −.02em; sub-line mono 9.5 `--txd`: "v1.0.0 · integrity". +- **Sections** with mono eyebrows (9.5, letter-spacing .14em, `--txd`): "MONITOR" (Dashboard, Corrupt files, Configuration, Logs) and "ACCESS PREVIEW" (Login, Setup wizard). +- **Nav item**: flex, gap 11px, padding `9px 14px`, margin `1px 12px`, radius 9px, 13.5/600. Each item has a mono 2-digit index (`01`–`06`, color `--txd`, width 15px). Active item: text `--ac`, background `--acd`. Inactive text `--txm`. Corrupt-files item has a right-aligned pill badge (mono 10/700, color `--dg`, bg `--dgd`, padding `1px 7px`) showing the live corrupt count. +- **Status card** (bottom, margin-top auto): inset card (`--bg2`, border `--bd`, radius 11px) with a status dot + word ("Scanning"/"Idle") and mono meta: "6 workers · nice(10)" / "read-only mount · ro". + +### Header +Flex space-between. Left: screen title 18/700. Right: a status pill (`--bg2`, border `--bd`, radius 999px, padding `6px 13px`) with status dot + text ("Scanning · 62%" or "Idle"), and a **Light/Dark toggle** button (transparent, border `--bd`, `--txm`, padding `7px 13px`, radius 8px) labeled with the *other* theme's name. + +**Status dot**: 8px circle. Idle → `--ac` + green glow, no animation. Scanning → `--wn` (amber) + amber glow + `bc-pulse` animation. + +--- + +## Screens / Views + +### 1. Dashboard +**Purpose:** at-a-glance library health + start/monitor scans. +**Layout (max-width 1080):** +- **Stat grid**: `repeat(auto-fill, minmax(168px, 1fr))`, gap `--gap`. Six cards (`--bg1`, border `--bd`, radius 14, padding `--cardpad`), each = mono uppercase eyebrow + big value: + - Status (dot + "Idle"/"Scanning"), Mode ("Report"), Library ("98,432" + mono "files · 2.8 TB"), Corrupt (value in `--dg` + "flagged · 142.5 MB"), Workers ("6"), Uptime ("3d 4h"). +- **Scan hero** — conditionally shows one of: + - **Scanning state** (`--bg1`, border `--bd`, radius 16, padding `20px 22px`): top row = "Decoding library" 13/700 + mono current-file path (`--txd`, truncated) on the left; big "62%" in `--ac` 30/800 + mono "61,030 / 98,432" on the right. **Spectrogram** (height 118px, flex row of ~150 bars, `align-items:flex-end`, gap 2px): bars flex `1 1 0`, height 8–98%, radius 2px; color from the spectrum ramp by height; **scanned region** (left 62%) full opacity, unscanned region opacity .15; corrupt bars `#ff4d6d` + glow and taller. An accent gradient wash fills the scanned region behind bars (`linear-gradient(90deg, --acd, transparent)`). A 2px **playhead** at left:62% in `--ac`, glowing, `bc-sweep` animation, with a dot head. Footer stat row: "61,030 / 98,432 checked", "N corrupt found" (`--dg`), "ETA 1h 24m" (`--ac`), "6 workers active". + - **Idle state**: 52px rounded accent-dim tile with a `✓`, "Scanner idle" 17/700 + meta "Last scan 2h ago · 98,432 files · N flagged", and a right-aligned **Start scan** accent button. +- **Action row**: primary "Scan — Report" (accent), secondary "Scan — Move" (`--bg2`, border), "Cancel scan" (danger, only while scanning), and a "Fresh scan (ignore resume cache)" checkbox (accent-color `--ac`). +- **Recently flagged** list (`--bg1`, border, radius 14): rows with a glowing 6px `--dg` dot, mono filename, `--dg` error line, mono size. + +### 2. Corrupt Files +**Purpose:** review and remove flagged files. +**Layout (max-width 1080):** +- **Toolbar**: search input (flex-1, `--bg1`), "Group by album" / "Flat view" toggle (`--bg2`), "Delete selected (N)" (danger; disabled at 40% opacity + `not-allowed` when nothing selected), "Clear" (ghost). +- **Table** (`--bg1`, border, radius 14, overflow hidden). Column grid: `34px 1fr 230px 84px 74px` (checkbox · File · Error · Size · action). Mono uppercase header row (9.5, letter-spacing .08em). + - **Flat view**: one row per file — checkbox (accent-color), file block (mono name + `--txd` folder path, both truncated), error in `--dg` (truncated), mono right-aligned size, and a per-row outline "Delete" button (`--dg` text, border `--bd`, radius 7). **Selected rows** get `--acd` background. + - **Album view**: group header rows (`--bg2`, bold album name + mono `--dg` count like "2 files"), each followed by its file rows (same row component). + - Empty state: centered "No corrupt files — library is clean." (`--txd`). + +### 3. Configuration +**Purpose:** edit `beatscheck.conf` (applies next scan). +**Layout (max-width 840):** intro line referencing `beatscheck.conf` in a mono code chip. Four sections, each: section title 14/700 in `--ac`, help line `--txm` 12, then a card (`--bg1`, border, radius 14) of field rows. **Field row** grid `210px 1fr`, padding `14px 18px`, bottom border `--bd`: left = mono key (12.5/600) + `--txd` 10.5 description; right (justify-end) = the control. Controls: + - **Switch**: 40×22 pill track (`--ac` when on, else `--bg2` + border `--bds`), 16px white knob sliding left 2px→20px, `.2s`. + - **Select**: `--bg2`, border `--bd`, radius 8, mono 12.5, min-width 210. + - **Text/number**: same styling as select. + Sections & fields: **Scanning** (mode select [setup/report/delete/move], workers, run_interval, min_file_age, log_level select), **Deletion & quarantine** (delete_after, max_auto_delete, output_dir), **Lidarr integration** (lidarr_url, lidarr_api_key, lidarr_search switch, lidarr_blocklist switch), **Web interface** (webui switch, webui_port). Footer: "Save configuration" accent button + "Last saved 2h ago". + +### 4. Logs +**Purpose:** live tail of scanner output. +**Layout (full width):** toolbar = "Auto-scroll" checkbox, level filter `` filters by exact level; search filters message text; both compose. +- **Transitions**: status dot `bc-pulse` 1.4s; scan playhead `bc-sweep` 1.6s; row/selection/switch `.15–.2s`. (An optional page-enter fade `bc-page` exists in the prototype but was disabled — reintroduce only if desired.) +- **Responsive**: stat grid auto-fills; toolbars wrap (`flex-wrap`). The 238px sidebar is fixed — for narrow/mobile, collapse it to a drawer (not built in the prototype). + +## State Management +- `screen`: which view is active (`dashboard|corrupt|config|logs|login|setup`). +- `theme`: `dark|light`. +- `scanning`: boolean → drives status dot/text + dashboard hero. In production, derive from backend status/progress polling. +- `selected`: map of file-path → boolean (corrupt-files multi-select). +- `removed`: map of file-path → boolean (locally removed; replace with real backend mutations). +- `corruptSearch`, `logSearch`, `logLevel`: filter inputs. +- Tweakable design props in the prototype (map to build-time/user settings, not runtime UI): `accent` (signal/amber/indigo), `density` (comfortable/compact), `defaultTheme`, `scanState`. + +### Data the backend already provides (wire these in) +- **Status/progress**: mode, scanning flag, % complete, current file path, checked/total counts, corrupt count, ETA, worker count, uptime, library size. +- **Corrupt list**: per file → path, folder, filename, album (derive from path), human size, error/reason string. +- **Config**: the `beatscheck.conf` keys shown in §3. +- **Logs**: timestamp, level, message lines. + +## Assets +- **Logo**: built in pure CSS/markup (gradient chip + spectrum bar + red dot) — no image needed. A raster `icon.png` exists in the repo if a favicon/app icon is wanted. +- **Fonts**: Hanken Grotesk + IBM Plex Mono via Google Fonts (self-host for an offline/air-gapped Docker deploy). +- **Icons**: the design uses almost none — a single `✓` glyph on the idle hero. Status/indicator dots are CSS. If you add icons, keep them minimal and monochrome to match. +- No SVG illustrations; the spectrogram is generated from data (array of bars), not an asset. + +## Files +- `BeatsCheck.dc.html` — the full hi-fi prototype (all six screens, theme system, interactions). Authored in a custom HTML template runtime; **read it for exact values, don't ship it**. The template markup is the visual source of truth; the `class Component` logic block holds the sample data, the spectrogram bar generator (`buildBars`), config schema (`configData`), and log fixtures (`logData`). +- `support.js` — prototype runtime only. **Do not port.** +- `screenshots/` — reference renders (if included). +- `icon.png` — original app icon from the repo. diff --git a/design_handoff_beatscheck_redesign/icon.png b/design_handoff_beatscheck_redesign/icon.png new file mode 100644 index 0000000..be3575a Binary files /dev/null and b/design_handoff_beatscheck_redesign/icon.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/01-dashboard.png b/design_handoff_beatscheck_redesign/screenshots/01-dashboard.png new file mode 100644 index 0000000..e982a54 Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/01-dashboard.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/02-dashboard-scan.png b/design_handoff_beatscheck_redesign/screenshots/02-dashboard-scan.png new file mode 100644 index 0000000..e982a54 Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/02-dashboard-scan.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/03-corrupt-files.png b/design_handoff_beatscheck_redesign/screenshots/03-corrupt-files.png new file mode 100644 index 0000000..a8b6bd4 Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/03-corrupt-files.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/04-corrupt-albums.png b/design_handoff_beatscheck_redesign/screenshots/04-corrupt-albums.png new file mode 100644 index 0000000..a81acb9 Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/04-corrupt-albums.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/05-configuration.png b/design_handoff_beatscheck_redesign/screenshots/05-configuration.png new file mode 100644 index 0000000..65099e7 Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/05-configuration.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/06-logs.png b/design_handoff_beatscheck_redesign/screenshots/06-logs.png new file mode 100644 index 0000000..18143a2 Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/06-logs.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/07-login.png b/design_handoff_beatscheck_redesign/screenshots/07-login.png new file mode 100644 index 0000000..acf7328 Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/07-login.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/08-setup.png b/design_handoff_beatscheck_redesign/screenshots/08-setup.png new file mode 100644 index 0000000..d4826ee Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/08-setup.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/09-light-dashboard.png b/design_handoff_beatscheck_redesign/screenshots/09-light-dashboard.png new file mode 100644 index 0000000..f75bbce Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/09-light-dashboard.png differ diff --git a/design_handoff_beatscheck_redesign/screenshots/10-light-corrupt.png b/design_handoff_beatscheck_redesign/screenshots/10-light-corrupt.png new file mode 100644 index 0000000..a337bd0 Binary files /dev/null and b/design_handoff_beatscheck_redesign/screenshots/10-light-corrupt.png differ diff --git a/design_handoff_beatscheck_redesign/support.js b/design_handoff_beatscheck_redesign/support.js new file mode 100644 index 0000000..304d1fc --- /dev/null +++ b/design_handoff_beatscheck_redesign/support.js @@ -0,0 +1,1595 @@ +// GENERATED from dc-runtime/src/*.ts — do not edit. Rebuild with `cd dc-runtime && bun run build`. +"use strict"; +(() => { + var __defProp = Object.defineProperty; + var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; + var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); + + // src/react.ts + function getReact() { + const R = window.React; + if (!R) throw new Error("dc-runtime: window.React is not available yet"); + return R; + } + function getReactDOM() { + const RD = window.ReactDOM; + if (!RD) throw new Error("dc-runtime: window.ReactDOM is not available yet"); + return RD; + } + var h = ((...args) => getReact().createElement( + ...args + )); + + // src/parse.ts + function parseDcDocument(doc) { + const dc = doc.querySelector("x-dc"); + if (!dc) return null; + const scriptEl = doc.querySelector("script[data-dc-script]"); + const { props, preview } = parseDataProps( + scriptEl?.getAttribute("data-props") ?? null + ); + return { + template: dc.innerHTML, + js: scriptEl ? scriptEl.textContent || "" : "", + props, + preview + }; + } + function parseDcText(src) { + const openMatch = /]*)?>/.exec(src); + if (!openMatch) return null; + const close = src.lastIndexOf(""); + if (close === -1 || close < openMatch.index) return null; + const template = src.slice(openMatch.index + openMatch[0].length, close); + const doc = new DOMParser().parseFromString(src, "text/html"); + const scriptEl = doc.querySelector("script[data-dc-script]"); + const { props, preview } = parseDataProps( + scriptEl?.getAttribute("data-props") ?? null + ); + return { + template, + js: scriptEl ? scriptEl.textContent || "" : "", + props, + preview + }; + } + function parseDataProps(raw) { + if (!raw) return { props: null, preview: null }; + let parsed; + try { + parsed = JSON.parse(raw); + } catch { + return { props: null, preview: null }; + } + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { + return { props: null, preview: null }; + } + const obj = parsed; + const preview = obj.$preview && typeof obj.$preview === "object" ? obj.$preview : null; + const rest = {}; + for (const k of Object.keys(obj)) { + if (k[0] !== "$") rest[k] = obj[k]; + } + return { props: Object.keys(rest).length ? rest : null, preview }; + } + function dcNameFromPath(pathname) { + let p = pathname || ""; + try { + p = decodeURIComponent(p); + } catch { + } + const base = p.split("/").pop() || "Root"; + return base.replace(/\.dc\.html$/, "").replace(/\.html?$/, "") || "Root"; + } + + // src/boot.ts + var BASE_CSS = ` + .sc-placeholder{background:rgba(255,255,255,.3);border:1px solid rgba(0,0,0,.5); + border-radius:2px;box-sizing:border-box;overflow:hidden} + @keyframes sc-shine{0%{background-position:100% 50%}100%{background-position:0% 50%}} + html.sc-dc-streaming .sc-placeholder, + html.sc-dc-streaming .sc-interp.sc-missing{position:relative; + background:color-mix(in srgb,currentColor 5%,transparent); + border-color:transparent} + html.sc-dc-streaming .sc-placeholder::before, + html.sc-dc-streaming .sc-interp.sc-missing::before{content:''; + position:absolute;inset:0;pointer-events:none; + background:linear-gradient(90deg,rgba(217,119,87,0) 25%,rgba(247,225,211,.95) 37%,rgba(217,119,87,0) 63%); + background-size:400% 100%;animation:sc-shine 1.4s ease infinite} + html.sc-dc-streaming .sc-placeholder:nth-child(n+9 of .sc-placeholder)::before, + html.sc-dc-streaming .sc-interp.sc-missing:nth-child(n+9 of .sc-interp.sc-missing)::before{animation:none; + background:color-mix(in srgb,currentColor 8%,transparent)} + .sc-placeholder-error{padding:4px 8px;font:11px/1.4 ui-monospace,monospace; + color:rgba(0,0,0,.7);word-break:break-word} + .sc-interp.sc-missing{display:inline-block;width:2em;height:1em;overflow:hidden; + vertical-align:text-bottom;background:rgba(255,255,255,.3);border:1px solid rgba(0,0,0,.5); + border-radius:2px;box-sizing:border-box;color:transparent; + user-select:none} + .sc-interp.sc-unresolved{font-family:ui-monospace,monospace;font-size:.85em; + color:rgba(0,0,0,.5);background:rgba(0,0,0,.05);border-radius:3px; + padding:0 3px} + .sc-host.sc-has-error{position:relative} + .sc-logic-error{position:absolute;top:8px;left:8px;z-index:2147483647;max-width:60ch; + padding:6px 10px;background:#b00020;color:#fff;font:12px/1.4 ui-monospace,monospace; + border-radius:4px;white-space:pre-wrap;pointer-events:none} + /* Mirrors PRINT_BASELINE_CSS in apps/web deck-stage-export.ts \u2014 keep both + in sync until dc-runtime regains a build step. */ + @media print { + @page { margin: 0.5cm; } + figure, table { break-inside: avoid; } + #dc-root, #dc-root > .sc-host { height: auto; } + *, *::before, *::after { + print-color-adjust: exact; -webkit-print-color-adjust: exact; + backdrop-filter: none !important; -webkit-backdrop-filter: none !important; + animation-delay: -99s !important; animation-duration: .001s !important; + animation-iteration-count: 1 !important; animation-fill-mode: both !important; + animation-play-state: running !important; transition-duration: 0s !important; + } + } + `; + var FULL_PAGE_CSS = "html,body{height:100%;margin:0}#dc-root,#dc-root>.sc-host{height:100%}"; + function rootNameForDocument(doc, loc) { + let bootPath = loc.pathname || ""; + if (!/\.dc\.html?$/i.test(safeDecode(bootPath))) { + try { + bootPath = new URL(doc.baseURI || "/").pathname; + } catch { + } + } + return dcNameFromPath(bootPath); + } + function safeDecode(s) { + try { + return decodeURIComponent(s); + } catch { + return s; + } + } + function boot(runtime, doc = document) { + const parsed = parseDcDocument(doc); + if (!parsed) return null; + const React = getReact(); + const rootName = rootNameForDocument(doc, location); + runtime.markFetched(rootName); + runtime.setRootName(rootName); + runtime.adoptParsed(rootName, parsed); + fetch(location.href).then((res) => res.ok ? res.text() : "").then((t) => { + const raw = t ? parseDcText(t) : null; + if (raw?.template) runtime.updateHtml(rootName, raw.template); + }).catch(() => { + }); + const dc = doc.querySelector("x-dc"); + const hostEl = doc.createElement("div"); + hostEl.id = "dc-root"; + dc.replaceWith(hostEl); + if (!parsed.preview) { + const s = doc.createElement("style"); + s.textContent = FULL_PAGE_CSS; + doc.head.appendChild(s); + } + const Root = runtime.getDC(rootName); + const entry = runtime.registry.get(rootName); + function StandaloneRoot() { + const [, setTick] = React.useState(0); + React.useEffect(() => { + const sub = () => setTick((n) => n + 1); + entry.subs.add(sub); + return () => { + entry.subs.delete(sub); + }; + }, []); + return h(Root, entry.propOverrides || null); + } + const ReactDOM = getReactDOM(); + if (ReactDOM.createRoot) + ReactDOM.createRoot(hostEl).render(h(StandaloneRoot)); + else ReactDOM.render(h(StandaloneRoot), hostEl); + return rootName; + } + + // src/expr.ts + var IDENT_RE = /^[A-Za-z_$][A-Za-z0-9_$]*/; + var NUMBER_RE = /^-?\d+(\.\d+)?$/; + function resolve(vals, src) { + const expr = String(src).trim(); + if (!expr) return void 0; + if (expr[0] === "(" && expr[expr.length - 1] === ")" && parensWrapWhole(expr)) { + return resolve(vals, expr.slice(1, -1)); + } + const eq = findTopLevelEquality(expr); + if (eq) { + const lv = resolve(vals, expr.slice(0, eq.index)); + const rv = resolve(vals, expr.slice(eq.index + eq.op.length)); + switch (eq.op) { + case "===": + return lv === rv; + case "!==": + return lv !== rv; + case "==": + return lv == rv; + default: + return lv != rv; + } + } + if (expr[0] === "!") return !resolve(vals, expr.slice(1)); + if (expr === "true") return true; + if (expr === "false") return false; + if (expr === "null") return null; + if (expr === "undefined") return void 0; + if (NUMBER_RE.test(expr)) return Number(expr); + if (expr.length >= 2 && (expr[0] === '"' || expr[0] === "'") && expr[expr.length - 1] === expr[0]) { + return expr.slice(1, -1); + } + return resolvePath(vals, expr); + } + function parensWrapWhole(expr) { + let depth = 0; + for (let i = 0; i < expr.length - 1; i++) { + if (expr[i] === "(") depth++; + else if (expr[i] === ")") { + depth--; + if (depth === 0) return false; + } + } + return true; + } + function findTopLevelEquality(expr) { + let depth = 0; + for (let i = 0; i < expr.length; i++) { + const c = expr[i]; + if (c === "[" || c === "(") depth++; + else if (c === "]" || c === ")") depth--; + else if (depth === 0 && (c === "=" || c === "!") && expr[i + 1] === "=") { + if (i > 0 && (expr[i - 1] === "=" || expr[i - 1] === "!")) continue; + if (!expr.slice(0, i).trim()) continue; + const op = expr[i + 2] === "=" ? c + "==" : c + "="; + return { index: i, op }; + } + } + return null; + } + function resolvePath(vals, expr) { + const head = expr.match(IDENT_RE); + if (!head) return void 0; + let cur = vals == null ? void 0 : vals[head[0]]; + let i = head[0].length; + while (i < expr.length) { + if (expr[i] === ".") { + const m = expr.slice(i + 1).match(IDENT_RE) || expr.slice(i + 1).match(/^\d+/); + if (!m) return void 0; + cur = cur == null ? void 0 : cur[m[0]]; + i += 1 + m[0].length; + } else if (expr[i] === "[") { + let depth = 1; + let j = i + 1; + while (j < expr.length && depth > 0) { + if (expr[j] === "[") depth++; + else if (expr[j] === "]") { + depth--; + if (depth === 0) break; + } + j++; + } + if (depth !== 0) return void 0; + const key = resolve(vals, expr.slice(i + 1, j)); + cur = cur == null ? void 0 : cur[key]; + i = j + 1; + } else { + return void 0; + } + } + return cur; + } + + // src/encode.ts + var CAMEL_ATTR = "sc-camel-"; + var RAW_WRAP = { + select: "sc-raw-select", + table: "sc-raw-table", + tbody: "sc-raw-tbody", + thead: "sc-raw-thead", + tfoot: "sc-raw-tfoot", + tr: "sc-raw-tr", + td: "sc-raw-td", + th: "sc-raw-th", + caption: "sc-raw-caption" + }; + var RAW_UNWRAP = Object.fromEntries( + Object.entries(RAW_WRAP).map(([k, v]) => [v, k]) + ); + var EVENT_MAP = { + onclick: "onClick", + onchange: "onChange", + oninput: "onInput", + onsubmit: "onSubmit", + onkeydown: "onKeyDown", + onkeyup: "onKeyUp", + onkeypress: "onKeyPress", + onmousedown: "onMouseDown", + onmouseup: "onMouseUp", + onmouseenter: "onMouseEnter", + onmouseleave: "onMouseLeave", + onfocus: "onFocus", + onblur: "onBlur", + ondoubleclick: "onDoubleClick", + oncontextmenu: "onContextMenu" + }; + var ATTRS = `(?:[^>"']|"[^"]*"|'[^']*')*`; + var IMPORT_SELF_CLOSE_RE = new RegExp( + "<(x-import|dc-import)(" + ATTRS + ")/>", + "gi" + ); + var CAMEL_ATTR_RE = /(\s)([a-z]+[A-Z][A-Za-z0-9]*)(\s*=)/g; + function encodeCase(html) { + html = html.replace( + IMPORT_SELF_CLOSE_RE, + (_, t, a) => "<" + t + a + ">" + ); + html = html.replace(/)/gi, "/gi, ""); + html = html.replace( + CAMEL_ATTR_RE, + (_, sp, name, eq) => sp + CAMEL_ATTR + name.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase()) + eq + ); + for (const [real, alias] of Object.entries(RAW_WRAP)) { + html = html.replace( + new RegExp("(])", "gi"), + "$1" + alias + ); + } + return html; + } + function kebabToCamel(s) { + return s.replace(/-([a-z])/g, (_, c) => c.toUpperCase()); + } + function cssToObj(css) { + const o = {}; + for (const decl of css.split(";")) { + const i = decl.indexOf(":"); + if (i < 0) continue; + const prop = decl.slice(0, i).trim(); + o[prop.startsWith("--") ? prop : kebabToCamel(prop)] = decl.slice(i + 1).trim(); + } + return o; + } + function compileAttr(raw) { + const whole = raw.match(/^\s*\{\{([\s\S]+?)\}\}\s*$/); + if (whole) { + const path = whole[1]; + return (vals) => resolve(vals, path); + } + if (raw.includes("{{")) { + const parts = raw.split(/\{\{([\s\S]+?)\}\}/g); + return (vals) => parts.map((s, i) => i & 1 ? resolve(vals, s) ?? "" : s).join(""); + } + return () => raw; + } + + // src/compile.ts + function collectProps(node, kind, host) { + const propGetters = []; + const pseudoClasses = []; + let hintSize = null; + for (const { name, value } of [...node.attributes]) { + if (name === "sc-name" || name === "data-dc-tpl") continue; + let key = name; + if (key.startsWith(CAMEL_ATTR)) + key = kebabToCamel(key.slice(CAMEL_ATTR.length)); + if (key === "hint-size") { + hintSize = value; + continue; + } + if (key.startsWith("style-")) { + pseudoClasses.push(host.pseudoClass(key.slice(6), value)); + continue; + } + if (kind !== "dom") { + if (key.includes("-") && !(kind === "x-import" && (key.startsWith("aria-") || key.startsWith("data-")))) + key = kebabToCamel(key); + } else { + if (key === "class") key = "className"; + else if (key === "for") key = "htmlFor"; + else if (key.startsWith("on")) + key = EVENT_MAP[key] || "on" + key[2].toUpperCase() + key.slice(3); + } + propGetters.push([key, compileAttr(value)]); + } + return { propGetters, pseudoClasses, hintSize }; + } + var HOST_STYLE_PROPS = /* @__PURE__ */ new Set([ + "position", + "left", + "right", + "top", + "bottom", + "inset", + "width", + "height", + "z-index", + "transform" + ]); + function hostPositionStyle(style) { + const all = typeof style === "string" ? cssToObj(style) : style != null && typeof style === "object" ? style : null; + if (!all) return void 0; + const out = {}; + for (const [k, v] of Object.entries(all)) { + const kebab = k.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase()); + if (HOST_STYLE_PROPS.has(kebab)) out[k] = v; + } + return Object.keys(out).length ? out : void 0; + } + function compileTemplate(html, host) { + const tpl = document.createElement("template"); + //! nosemgrep: direct-inner-html-assignment + tpl.innerHTML = encodeCase(html); + let tplN = 0; + (function stamp(node) { + if (node.nodeType === Node.ELEMENT_NODE) { + node.setAttribute("data-dc-tpl", String(tplN++)); + } + for (const c of node.childNodes) stamp(c); + })(tpl.content); + const builders = walkChildren(tpl.content, host); + const render = ((vals, ctx) => builders.map((b, i) => b(vals || {}, ctx, i))); + render.__annotated = tpl.innerHTML; + return render; + } + function walkChildren(node, host) { + return [...node.childNodes].map((c) => walk(c, host)).filter((b) => b != null); + } + function walk(node, host) { + if (node.nodeType === Node.TEXT_NODE) return walkText(node); + if (node.nodeType !== Node.ELEMENT_NODE) return null; + const el = node; + const tag = el.tagName.toLowerCase(); + if (tag === "sc-for") return walkFor(el, host); + if (tag === "sc-if") return walkIf(el, host); + if (tag === "x-import") return walkXImport(el, host); + if (tag === "sc-helmet") return host.helmet(el); + if (tag === "dc-import") return walkComponent(el, host); + return walkElement(el, host); + } + var warnedHoles = /* @__PURE__ */ new Set(); + function warnUnresolved(ctx, what) { + const key = (ctx?.__name || "?") + "\0" + what; + if (warnedHoles.has(key)) return; + warnedHoles.add(key); + console.warn("[dc-runtime] " + (ctx?.__name || "template") + ": " + what); + } + function walkText(node) { + const txt = node.nodeValue ?? ""; + if (!txt.includes("{{")) { + if (!txt.trim() && !txt.includes(" ")) return null; + return () => txt; + } + const parts = txt.split(/\{\{([\s\S]+?)\}\}/g); + return (vals, ctx, key) => h( + getReact().Fragment, + { key }, + ...parts.map((p, i) => { + if (!(i & 1)) return p; + const v = resolve(vals, p); + if (v === void 0) { + if (!ctx?.__streamingNow) { + if (document.body?.hasAttribute("data-dc-editor-on")) { + return h( + "span", + { key: i, className: "sc-interp sc-unresolved" }, + "{{ " + p.trim() + " }}" + ); + } + warnUnresolved( + ctx, + "{{ " + p.trim() + " }} never resolved \u2014 rendered as empty" + ); + return null; + } + return h( + "span", + { key: i, className: "sc-interp sc-missing" }, + p.trim() + ); + } + if (getReact().isValidElement(v) || Array.isArray(v)) { + return h(getReact().Fragment, { key: i }, v); + } + if (v === null || typeof v === "boolean") return null; + return h("span", { key: i, className: "sc-interp" }, String(v)); + }) + ); + } + function walkFor(el, host) { + const listGet = compileAttr(el.getAttribute("list") || ""); + const asName = el.getAttribute("as") || "item"; + const hintN = parseInt(el.getAttribute("hint-placeholder-count") || "0", 10); + const kids = walkChildren(el, host); + const listSrc = el.getAttribute("list") || ""; + return (vals, ctx, key) => { + let list = listGet(vals); + if (!Array.isArray(list)) { + if (!ctx?.__streamingNow) { + if (list !== void 0 && list !== null) { + warnUnresolved( + ctx, + 'sc-for list="' + listSrc + '" is not an array (' + typeof list + ")" + ); + } + list = []; + } else { + list = hintN > 0 ? Array(hintN).fill(void 0) : []; + } + } + return h( + getReact().Fragment, + { key }, + list.map((item, i) => { + const sub = { ...vals, [asName]: item, $index: i }; + return h( + getReact().Fragment, + { key: i }, + kids.map((b, j) => b(sub, ctx, j)) + ); + }) + ); + }; + } + function walkIf(el, host) { + const valGet = compileAttr(el.getAttribute("value") || ""); + const hintRaw = el.getAttribute("hint-placeholder-val"); + const hintGet = hintRaw != null ? compileAttr(hintRaw) : null; + const kids = walkChildren(el, host); + return (vals, ctx, key) => { + let v = valGet(vals); + if (v === void 0 && hintGet && ctx?.__streamingNow) v = hintGet(vals); + return v ? h( + getReact().Fragment, + { key }, + kids.map((b, j) => b(vals, ctx, j)) + ) : null; + }; + } + function walkComponent(el, host) { + const name = el.getAttribute("name") || el.getAttribute("component") || ""; + el.removeAttribute("name"); + el.removeAttribute("component"); + const tplId = el.getAttribute("data-dc-tpl"); + const styleRaw = el.getAttribute("style"); + el.removeAttribute("style"); + const styleGet = styleRaw != null ? compileAttr(styleRaw) : null; + const { propGetters, hintSize } = collectProps(el, "dc-import", host); + const kids = walkChildren(el, host); + return (vals, ctx, key) => { + const props = { + key, + __hintSize: hintSize, + __tplId: tplId, + __hostStyle: styleGet ? hostPositionStyle(styleGet(vals)) : void 0 + }; + for (const [k, g] of propGetters) { + const v = g(vals); + if (k === "dcProps") { + if (v && typeof v === "object") Object.assign(props, v); + continue; + } + props[k] = v; + } + if (kids.length) props.children = kids.map((b, j) => b(vals, ctx, j)); + return h(host.component(name), props); + }; + } + function walkXImport(el, host) { + const globalNameGet = compileAttr( + el.getAttribute("component-from-global-scope") || "" + ); + const exportNameGet = compileAttr( + el.getAttribute("component") || el.getAttribute("name") || "" + ); + const fromRaw = el.getAttribute("from") || el.getAttribute("src") || el.getAttribute("import") || ""; + const urls = fromRaw.trim() ? fromRaw.trim().split(/\s+/) : []; + const url = urls.length ? urls[urls.length - 1] : ""; + const kindOf = (u) => /\.(jsx|tsx)(\?|#|$)/i.test(u) ? "jsx" : "js"; + const tplId = el.getAttribute("data-dc-tpl"); + const styleRaw = el.getAttribute("style"); + el.removeAttribute("style"); + const styleGet = styleRaw != null ? compileAttr(styleRaw) : null; + const wrap = tplId != null || styleGet != null; + const { propGetters, hintSize } = collectProps(el, "x-import", host); + const hasContent = el.children.length > 0 || !!(el.textContent || "").trim(); + const kids = hasContent ? walkChildren(el, host) : []; + const urlBindable = fromRaw.includes("{{"); + if (urls.length && !urlBindable) { + let prev; + for (const u of urls) prev = host.loadExternal(kindOf(u), u, prev); + } + const evalName = (g, vals) => { + const v = g(vals); + const s = v == null ? "" : String(v); + return s.includes("{{") ? "" : s; + }; + return (vals, ctx, key) => { + const globalName = evalName(globalNameGet, vals); + const name = globalName || evalName(exportNameGet, vals); + const C = !name || urlBindable ? null : globalName ? host.resolveExternalGlobal(url, globalName) : host.resolveExternal(url, name); + const hostStyle = styleGet ? hostPositionStyle(styleGet(vals)) : void 0; + const wrapper = wrap ? { + key, + className: "sc-host-x", + "data-dc-tpl": tplId, + style: hostStyle || { display: "contents" } + } : null; + if (!C) { + const error = urlBindable ? "x-import `from` cannot contain {{ \u2026 }} \u2014 module URLs are resolved at parse time; use a literal URL" : host.resolveExternalError(url, name); + const ph = host.placeholder({ + key: wrapper ? void 0 : key, + name, + hintSize, + error + }); + return wrapper ? h("div", wrapper, ph) : ph; + } + const props = wrapper ? {} : { key }; + let unresolvedHole = false; + for (const [k, g] of propGetters) { + if (k === "component" || k === "componentFromGlobalScope" || k === "from") { + continue; + } + const v = g(vals); + if (v === void 0) unresolvedHole = true; + if (k === "dcProps") { + if (v && typeof v === "object") Object.assign(props, v); + continue; + } + props[k] = v; + } + if (unresolvedHole && ctx?.__htmlStreamingNow) { + const ph = host.placeholder({ + key: wrapper ? void 0 : key, + name, + hintSize, + error: null + }); + return wrapper ? h("div", wrapper, ph) : ph; + } + if (kids.length) props.children = kids.map((b, j) => b(vals, ctx, j)); + return wrapper ? h("div", wrapper, h(C, props)) : h(C, props); + }; + } + function walkElement(el, host) { + const realTag = RAW_UNWRAP[el.localName] || el.localName; + const tplId = el.getAttribute("data-dc-tpl"); + const { propGetters, pseudoClasses } = collectProps(el, "dom", host); + const kids = walkChildren(el, host); + return (vals, ctx, key) => { + const props = { key, "data-dc-tpl": tplId }; + for (const [k, g] of propGetters) { + let v = g(vals); + if (k === "style" && typeof v === "string") v = cssToObj(v); + if ((k === "value" || k === "checked") && v === void 0) { + v = k === "checked" ? false : ""; + } + props[k] = v; + } + if (pseudoClasses.length) { + props.className = [props.className, ...pseudoClasses].filter(Boolean).join(" "); + } + return h(realTag, props, ...kids.map((b, j) => b(vals, ctx, j))); + }; + } + + // src/logic.ts + var StreamableLogic = class { + constructor(props) { + __publicField(this, "props"); + __publicField(this, "state", {}); + /** Back-pointer to the wrapper component, installed after construction. */ + __publicField(this, "__host"); + this.props = props || {}; + } + setState(update, cb) { + this.__host && this.__host.__setLogicState(update, cb); + } + forceUpdate() { + this.__host && this.__host.forceUpdate(); + } + componentDidMount() { + } + componentDidUpdate(_prevProps) { + } + componentWillUnmount() { + } + /** The flat object the template renders against (merged over props). */ + renderVals() { + return {}; + } + }; + function evalDcLogic(src) { + //! nosemgrep: eval-and-function-constructor + const fn = new Function( + "DCLogic", + "StreamableLogic", + "React", + src + '\n;return (typeof Component!=="undefined"&&Component)||undefined;' + ); + return fn(StreamableLogic, StreamableLogic, getReact()); + } + + // src/component.ts + function shallowEqual(a, b) { + if (!b) return false; + const ak = Object.keys(a).filter((k) => k !== "children"); + const bk = Object.keys(b).filter((k) => k !== "children"); + if (ak.length !== bk.length) return false; + for (const k of ak) if (a[k] !== b[k]) return false; + return true; + } + function Placeholder({ + name, + hintSize, + streaming, + error + }) { + const [w, hgt] = (hintSize || "100%,60px").split(","); + return h( + "div", + { + className: "sc-placeholder" + (streaming ? " sc-streaming" : ""), + style: { width: w.trim(), height: hgt && hgt.trim() }, + title: name + }, + error ? h( + "div", + { className: "sc-placeholder-error" }, + (name ? name + ": " : "") + error + ) : null + ); + } + function hintToMin(hint) { + if (!hint) return void 0; + const [w, hgt] = hint.split(","); + return { minWidth: w.trim(), minHeight: hgt && hgt.trim() }; + } + function createComponentFactory(registry, ensureFetched) { + const React = getReact(); + const AncestorContext = React.createContext([]); + class StreamableComponent extends React.Component { + constructor(props) { + super(props); + __publicField(this, "__name"); + __publicField(this, "__sub"); + __publicField(this, "__needsDidMount", false); + /** Snapshot of the registry's streaming flags taken at render time — + * builders read it off the RenderCtx (this) to pick placeholder vs + * render-nothing for unresolved values. */ + __publicField(this, "__streamingNow", false); + __publicField(this, "__htmlStreamingNow", false); + /** When a construct throws, remember the (class, registry.ver, props) + * triple so render-time reconcile doesn't re-attempt it on every parent + * re-render. A registry bump (new class, template, external module + * resolving via bumpAll) changes `ver` and breaks the memo so an + * env-dependent constructor can self-heal. */ + __publicField(this, "__failedLogic", null); + __publicField(this, "__failedUserProps", null); + __publicField(this, "__failedVer", -1); + /** Per-instance constructor error — kept here (not on the registry entry) + * so one instance's successful construct can't hide a sibling's failure, + * and a construct can never wipe an eval error `updateJs` recorded on + * `r.logicError`. */ + __publicField(this, "__ctorError", null); + __publicField(this, "logic"); + this.__name = props.__name; + this.state = { __v: 0, __err: null }; + this.__sub = () => { + if (this.state.__err) this.setState({ __err: null }); + this.forceUpdate(); + }; + this.__makeLogic(registry.get(this.__name).Logic, null); + ensureFetched(this.__name); + } + /** Error-boundary hook: a render crash anywhere in this DC's subtree + * (its own template, an x-import'd component, a child DC without its + * own deeper boundary) lands here instead of unmounting the page. */ + static getDerivedStateFromError(e) { + return { __err: e instanceof Error && e.message ? e.message : String(e) }; + } + componentDidCatch(e, info) { + console.error( + "[dc-runtime] render error in <" + this.__name + ">:", + e, + info?.componentStack || "" + ); + } + /** Instantiate the logic class (or the no-op base) and adopt `prevState` + * over its initial state — used both at mount and on hot-swap. */ + __makeLogic(Logic, prevState) { + const L = Logic || StreamableLogic; + try { + this.logic = new L(this.__userProps()); + this.__failedLogic = null; + this.__failedUserProps = null; + this.__ctorError = null; + } catch (e) { + console.error(e); + this.__failedLogic = Logic; + this.__failedUserProps = this.__userProps(); + this.__failedVer = registry.get(this.__name).ver; + this.__ctorError = this.__name + ": " + (e instanceof Error && e.message ? e.message : String(e)); + this.logic = new StreamableLogic( + this.__userProps() + ); + } + this.logic.__host = this; + if (prevState) + this.logic.state = { ...this.logic.state || {}, ...prevState }; + } + /** The props the author's logic + template see — internal __-prefixed + * wiring stripped. */ + __userProps() { + const { __name, __hintSize, __tplId, __hostStyle, ...rest } = this.props; + return rest; + } + __setLogicState(update, cb) { + const prev = this.logic.state; + const patch = typeof update === "function" ? update(prev) : update; + this.logic.state = { ...prev, ...patch }; + this.setState((s) => ({ __v: s.__v + 1 }), cb); + } + /** Swap the logic instance when the registry's Logic class changed + * (streaming completion, hot reload). State carries over; didMount + * re-fires after the swap commits so refs exist. */ + __reconcileLogic() { + const r = registry.get(this.__name); + const Next = r.Logic; + const Cur = this.logic.constructor; + if (Next === Cur || !Next && Cur === StreamableLogic || Next === this.__failedLogic && r.ver === this.__failedVer && shallowEqual(this.__userProps(), this.__failedUserProps)) { + return; + } + if (!this.__needsDidMount) { + try { + this.logic.componentWillUnmount(); + } catch (e) { + console.error(e); + } + } + this.__makeLogic(Next, this.logic.state); + this.__needsDidMount = true; + } + componentDidMount() { + registry.get(this.__name).subs.add(this.__sub); + try { + this.logic.componentDidMount(); + } catch (e) { + console.error(e); + } + } + componentDidUpdate(prevProps) { + this.logic.props = this.__userProps(); + if (this.__needsDidMount) { + if (this.state.__err || !registry.get(this.__name).tpl) return; + this.__needsDidMount = false; + try { + this.logic.componentDidMount(); + } catch (e) { + console.error(e); + } + } else { + try { + this.logic.componentDidUpdate(prevProps); + } catch (e) { + console.error(e); + } + } + } + componentWillUnmount() { + registry.get(this.__name).subs.delete(this.__sub); + if (!this.__needsDidMount) { + try { + this.logic.componentWillUnmount(); + } catch (e) { + console.error(e); + } + } + } + render() { + const r = registry.get(this.__name); + const cls = "sc-host" + (r.htmlStreaming ? " sc-streaming-html" : "") + (r.jsStreaming ? " sc-streaming-js" : ""); + const hintStyle = r.htmlStreaming ? hintToMin(this.props.__hintSize) : void 0; + const hostStyle = this.props.__hostStyle || hintStyle ? { ...hintStyle || {}, ...this.props.__hostStyle || {} } : void 0; + const hostBase = { + className: cls, + style: hostStyle, + "data-sc-name": this.__name, + "data-dc-tpl": this.props.__tplId + }; + const chain = Array.isArray(this.context) ? this.context : []; + if (chain.includes(this.__name)) { + const cycle = [ + ...chain.slice(chain.indexOf(this.__name)), + this.__name + ].join(" \u2192 "); + return h( + "div", + { ...hostBase, className: cls + " sc-has-error" }, + h(Placeholder, { + name: this.__name, + hintSize: this.props.__hintSize, + error: "circular import: " + cycle + }) + ); + } + if (this.state.__err) { + return h( + "div", + { ...hostBase, className: cls + " sc-has-error" }, + h( + "div", + { className: "sc-logic-error", "data-omelette-chrome": "" }, + this.__name + ": " + this.state.__err + ), + h(Placeholder, { + name: this.__name, + hintSize: this.props.__hintSize, + error: this.state.__err + }) + ); + } + this.__reconcileLogic(); + if (!r.tpl) { + return h( + "div", + hostBase, + h(Placeholder, { name: this.__name, hintSize: this.props.__hintSize }) + ); + } + const userProps = this.__userProps(); + this.logic.props = userProps; + let vals = userProps; + let renderErr = r.logicError || this.__ctorError; + try { + vals = { ...userProps, ...this.logic.renderVals() || {} }; + } catch (e) { + console.error(e); + renderErr = this.__name + ".renderVals(): " + (e instanceof Error && e.message ? e.message : String(e)); + } + this.__streamingNow = !!(r.htmlStreaming || r.jsStreaming); + this.__htmlStreamingNow = !!r.htmlStreaming; + return h( + "div", + { ...hostBase, className: cls + (renderErr ? " sc-has-error" : "") }, + renderErr && h( + "div", + { className: "sc-logic-error", "data-omelette-chrome": "" }, + renderErr + ), + h( + AncestorContext.Provider, + { value: [...chain, this.__name] }, + r.tpl(vals, this) + ) + ); + } + } + __publicField(StreamableComponent, "contextType", AncestorContext); + const named = /* @__PURE__ */ new Map(); + function getDC(name) { + const hit = named.get(name); + if (hit) return hit; + function Dispatcher(p) { + const [, setTick] = React.useState(0); + React.useEffect(() => { + const sub = () => setTick((n) => n + 1); + registry.get(name).subs.add(sub); + return () => { + registry.get(name).subs.delete(sub); + }; + }, []); + ensureFetched(name); + return h(StreamableComponent, { ...p, __name: name }); + } + Dispatcher.displayName = name; + named.set(name, Dispatcher); + return Dispatcher; + } + return { + getDC, + StreamableComponent + }; + } + + // src/external.ts + var isCustomElementName = (n) => !n.includes(".") && n.includes("-"); + function isRenderableType(g) { + if (typeof g === "function") return !isElementClass(g); + return typeof g === "object" && g !== null && typeof g.$$typeof === "symbol"; + } + function resolveDottedPath(root, name) { + let cur = root; + for (const seg of name.split(".")) { + if (cur == null) return void 0; + cur = cur[seg]; + } + return cur; + } + var BABEL_URL = "https://unpkg.com/@babel/standalone@7.26.4/babel.min.js"; + var GLOBAL_POLL_INTERVAL_MS = 50; + var GLOBAL_POLL_TIMEOUT_MS = 3e4; + function createExternalModules(onResolved) { + const cache = /* @__PURE__ */ new Map(); + let babelLoading = null; + const reportedMissing = /* @__PURE__ */ new Map(); + const polling = /* @__PURE__ */ new Set(); + function ensureBabel() { + if (window.Babel) return Promise.resolve(); + if (babelLoading) return babelLoading; + babelLoading = new Promise((res, rej) => { + const s = document.createElement("script"); + s.src = BABEL_URL; + s.crossOrigin = "anonymous"; + s.onload = () => res(); + s.onerror = rej; + document.head.appendChild(s); + }); + return babelLoading; + } + const pending = /* @__PURE__ */ new Map(); + function load(kind, url, after) { + const existing = pending.get(url); + if (existing) return existing; + cache.set(url, null); + console.info("[dc-runtime] x-import: loading", url, "(" + kind + ")"); + const ready = Promise.all([ + kind === "jsx" ? ensureBabel() : Promise.resolve(), + after ?? Promise.resolve() + ]); + const p = ready.then(() => fetch(url)).then((r) => { + if (!r.ok) throw new Error("HTTP " + r.status); + return r.text(); + }).then((src) => { + const code = kind === "jsx" ? window.Babel.transform(src, { + filename: url, + presets: ["react", "typescript"] + }).code : src; + const module = { exports: {} }; + const before = new Set(Object.keys(window)); + //! nosemgrep: eval-and-function-constructor + new Function("React", "module", "exports", "require", code)( + getReact(), + module, + module.exports, + () => ({}) + ); + const globals = {}; + for (const k of Object.keys(window)) { + if (!before.has(k) && typeof window[k] === "function") { + globals[k] = window[k]; + } + } + cache.set(url, { mod: module.exports, globals }); + console.info( + "[dc-runtime] x-import: loaded", + url, + "\u2014 exports:", + Object.keys(module.exports), + "window globals:", + Object.keys(globals) + ); + onResolved(); + }).catch((e) => { + cache.set(url, { + mod: {}, + globals: {}, + error: "failed to load: " + (e instanceof Error && e.message ? e.message : String(e)) + }); + console.error( + "[dc-runtime] x-import: FAILED to load", + url, + "(" + kind + ")", + e + ); + onResolved(); + }); + pending.set(url, p); + return p; + } + function resolve2(url, name) { + const entry = cache.get(url); + if (!entry) return null; + const { mod, globals } = entry; + const C = mod && mod[name] || globals && globals[name] || typeof window !== "undefined" && window[name] || mod && mod.default; + if (typeof C === "function") return C; + const key = url + "\0" + name; + if (!reportedMissing.has(key)) { + reportedMissing.set( + key, + entry.error || 'no export named "' + name + '" (has: ' + Object.keys(mod).join(", ") + ")" + ); + console.error( + "[dc-runtime] x-import: module", + url, + "loaded but has no component named", + JSON.stringify(name), + "\u2014 available exports:", + Object.keys(mod), + "window globals:", + Object.keys(globals), + ". The module must `module.exports = {" + name + "}` or set `window." + name + "`." + ); + } + return null; + } + function waitForGlobal(name) { + if (polling.has(name)) return; + polling.add(name); + const started = Date.now(); + const isCE = isCustomElementName(name); + const tick = () => { + const found = isCE ? customElements.get(name) : isRenderableType(resolveDottedPath(window, name)); + if (found) { + polling.delete(name); + onResolved(); + return; + } + if (Date.now() - started >= GLOBAL_POLL_TIMEOUT_MS) { + console.warn( + "[dc-runtime] x-import: global", + JSON.stringify(name), + "never appeared on window after " + GLOBAL_POLL_TIMEOUT_MS + "ms" + ); + return; + } + setTimeout(tick, GLOBAL_POLL_INTERVAL_MS); + }; + setTimeout(tick, GLOBAL_POLL_INTERVAL_MS); + } + function resolveGlobal(url, name) { + const isCE = isCustomElementName(name); + if (!url) { + if (isCE) { + if (customElements.get(name)) return name; + waitForGlobal(name); + return null; + } + const g2 = resolveDottedPath(window, name); + if (isRenderableType(g2)) return g2; + waitForGlobal(name); + return null; + } + const entry = cache.get(url); + if (!entry) return null; + if (isCE && customElements.get(name)) return name; + const g = entry.globals[name] ?? resolveDottedPath(window, name); + if (isRenderableType(g)) return g; + if (name.includes(".")) return null; + const key = url + "\0global\0" + name; + if (!reportedMissing.has(key)) { + reportedMissing.set(key, null); + if (isCE && !customElements.get(name)) { + console.warn( + "[dc-runtime] x-import:", + url, + "loaded but no custom element", + JSON.stringify(name), + "is registered and window." + name + " is not a function \u2014 rendering <" + name + "> as an unknown element." + ); + } + } + return name; + } + function getError(url, name) { + const entry = cache.get(url); + if (entry?.error) return entry.error; + return reportedMissing.get(url + "\0" + name) || null; + } + return { load, resolve: resolve2, resolveGlobal, getError }; + } + function isElementClass(g) { + try { + return typeof g === "function" && typeof HTMLElement !== "undefined" && g.prototype instanceof HTMLElement; + } catch { + return false; + } + } + + // src/atomics.ts + var ATOMIC_CSS = ( + // layout + ".fx{display:flex}.col{display:flex;flex-direction:column}.grid{display:grid}.ac{align-items:center}.jc{justify-content:center}.jb{justify-content:space-between}.f1{flex:1}.noshrink{flex-shrink:0}.wrap{flex-wrap:wrap}.fw5{font-weight:500}.fw6{font-weight:600}.fw7{font-weight:700}.fw8{font-weight:800}.fs11{font-size:11px}.fs12{font-size:12px}.fs13{font-size:13px}.fs14{font-size:14px}.fs15{font-size:15px}.fs16{font-size:16px}.fs20{font-size:20px}.fs22{font-size:22px}.upper{text-transform:uppercase}.tc{text-align:center}.nowrap{white-space:nowrap}.gap8{gap:8px}.gap10{gap:10px}.gap12{gap:12px}.gap16{gap:16px}.gap24{gap:24px}.m0{margin:0}.mt8{margin-top:8px}.mt12{margin-top:12px}.mt16{margin-top:16px}.mb8{margin-bottom:8px}.mb12{margin-bottom:12px}.mb16{margin-bottom:16px}.posrel{position:relative}.posabs{position:absolute}.round{border-radius:50%}.ohide{overflow:hidden}.bbox{box-sizing:border-box}.pointer{cursor:pointer}.w100{width:100%}.b0{border:none}" + ); + + // src/helmet.ts + var DESIGN_DOC_MODE_RE = /]*\bname\s*=\s*["']design_doc_mode["'][^>]*\b(?:content|value)\s*=\s*["'](\w+)["']/i; + var CANVAS_BG = "#f0eee9"; + function createHelmetManager(doc, isStreaming) { + const mounted = /* @__PURE__ */ new Set(); + const live = /* @__PURE__ */ new Map(); + let designDocMode = null; + let canvasStyleEl = null; + function postDesignMode(mode) { + if (window.parent === window) return; + try { + window.parent.postMessage({ type: "__dc_design_mode", mode }, "*"); + } catch { + } + } + function setDesignDocMode(mode) { + if (mode === designDocMode) return; + designDocMode = mode; + postDesignMode(mode); + if (mode === "canvas") { + doc.documentElement.setAttribute("data-dc-canvas", ""); + canvasStyleEl = doc.createElement("style"); + canvasStyleEl.setAttribute("data-dc-canvas", ""); + canvasStyleEl.textContent = `html,body{background:${CANVAS_BG}}#dc-root>.sc-host{position:relative}`; + doc.head.appendChild(canvasStyleEl); + } else { + doc.documentElement.removeAttribute("data-dc-canvas"); + canvasStyleEl?.remove(); + canvasStyleEl = null; + } + } + window.addEventListener("message", (e) => { + if (!designDocMode || (e.data && e.data.type) !== "__dc_probe") return; + postDesignMode(designDocMode); + }); + function compile(node) { + const raw = [...node.children]; + const helmetClosed = node.nextSibling != null || node.parentNode?.nextSibling != null; + if (node.hasAttribute("data-dc-atomics") && !mounted.has("__dc-atomics")) { + mounted.add("__dc-atomics"); + const el = doc.createElement("style"); + el.id = "__dc-atomics"; + el.textContent = ATOMIC_CSS; + doc.head.appendChild(el); + } + return (_vals, ctx) => { + const name = ctx && ctx.__name || ""; + const streaming = !!(name && isStreaming(name)); + for (let i = 0; i < raw.length; i++) { + const child = raw[i]; + const tag = child.tagName; + const mayBePartial = streaming && !helmetClosed && i === raw.length - 1; + if (tag === "SCRIPT") { + if (mayBePartial) continue; + const key = "SCRIPT|" + (child.getAttribute("src") || child.textContent || ""); + if (mounted.has(key)) continue; + mounted.add(key); + const el = doc.createElement("script"); + for (const { name: an, value } of [...child.attributes]) + el.setAttribute(an, value); + if (child.textContent) el.textContent = child.textContent; + doc.head.appendChild(el); + } else if (tag === "LINK" || tag === "META") { + if (mayBePartial) continue; + const key = tag + "|" + (child.getAttribute("href") || child.getAttribute("src") || child.outerHTML); + if (mounted.has(key)) continue; + mounted.add(key); + doc.head.appendChild(child.cloneNode(true)); + } else { + const key = name + "|" + i; + let el = live.get(key); + if (!el || el.tagName !== tag) { + if (el) el.remove(); + el = doc.createElement(tag.toLowerCase()); + live.set(key, el); + doc.head.appendChild(el); + } + for (const { name: an, value } of [...child.attributes]) { + if (el.getAttribute(an) !== value) el.setAttribute(an, value); + } + if (el.textContent !== child.textContent) + el.textContent = child.textContent; + } + } + return null; + }; + } + return { compile, setDesignDocMode }; + } + + // src/pseudo.ts + function createPseudoSheet(doc) { + let el = null; + const cache = /* @__PURE__ */ new Map(); + let n = 0; + return (pseudo, css) => { + const k = pseudo + "|" + css; + const hit = cache.get(k); + if (hit) return hit; + if (!el) { + el = doc.createElement("style"); + doc.head.appendChild(el); + } + const cls = "scp" + (n++).toString(36); + const sel = pseudo === "before" || pseudo === "after" ? "." + cls + "::" + pseudo : "." + cls + ":" + pseudo; + el.sheet.insertRule(sel + "{" + css + "}", el.sheet.cssRules.length); + cache.set(k, cls); + return cls; + }; + } + + // src/registry.ts + function createRegistry() { + const entries = /* @__PURE__ */ Object.create(null); + function get(name) { + return entries[name] || (entries[name] = { + html: "", + tpl: null, + Logic: null, + jsStreaming: false, + htmlStreaming: false, + ver: 0, + subs: /* @__PURE__ */ new Set(), + fetched: false + }); + } + function bump(name) { + const r = get(name); + r.ver++; + for (const fn of r.subs) fn(); + } + return { + entries, + get, + bump, + bumpAll() { + for (const n in entries) bump(n); + } + }; + } + + // src/runtime.ts + var COMPONENT_DIR = "."; + function createRuntime(doc = document) { + const registry = createRegistry(); + const pseudoClass = createPseudoSheet(doc); + const helmet = createHelmetManager( + doc, + (name) => registry.get(name).htmlStreaming + ); + const external = createExternalModules(() => registry.bumpAll()); + const factory = createComponentFactory(registry, ensureFetched); + const host = { + component: (name) => factory.getDC(name), + placeholder: (props) => h(Placeholder, props), + helmet: (node) => helmet.compile(node), + loadExternal: (kind, url, after) => external.load(kind, url, after), + resolveExternal: (url, name) => external.resolve(url, name), + resolveExternalGlobal: (url, name) => external.resolveGlobal(url, name), + resolveExternalError: (url, name) => external.getError(url, name), + pseudoClass + }; + function ensureFetched(name) { + const r = registry.get(name); + if (r.fetched) return; + r.fetched = true; + const url = COMPONENT_DIR + "/" + encodeURIComponent(name) + ".dc.html"; + fetch(url).then((res) => { + if (!res.ok) { + console.error( + "[dc-runtime] sibling fetch for <" + name + "/> failed:", + url, + "returned", + res.status, + "\u2014 the reference renders as an empty placeholder." + ); + return ""; + } + return res.text(); + }).then((t) => { + if (!t) return; + const parsed = parseDcText(t); + if (!parsed) { + console.error( + "[dc-runtime] sibling fetch for <" + name + "/>:", + url, + "has no block \u2014 not a Design Component." + ); + return; + } + if (parsed.props) r.propsMeta = parsed.props; + if (parsed.preview) r.preview = parsed.preview; + if (parsed.template && !r.html) updateHtml(name, parsed.template); + if (parsed.js && !r.Logic) updateJs(name, parsed.js); + }).catch( + (e) => console.error( + "[dc-runtime] sibling fetch for <" + name + "/> threw:", + url, + e + ) + ); + } + let rootName = null; + function updateHtml(name, html) { + const r = registry.get(name); + r.html = html; + if (name === rootName) { + const mode = DESIGN_DOC_MODE_RE.exec(html)?.[1] ?? null; + if (mode || !r.htmlStreaming) helmet.setDesignDocMode(mode); + } + try { + r.tpl = compileTemplate(html, host); + } catch (e) { + console.error("[dc-runtime] template compile FAILED for", name, e); + } + registry.bump(name); + } + function updateJs(name, src) { + const r = registry.get(name); + const seq = r.jsSeq = (r.jsSeq || 0) + 1; + try { + const Cls = evalDcLogic(src); + if (r.jsSeq !== seq) return; + if (typeof Cls !== "function") { + r.logicError = name + ".dc.html: