melody.uid is never actually populated anywhere in MelodyForm.jsx, so keying local .bsm storage on it (as the previous commit did) would silently break for every existing melody. pid is the correct key anyway: it identifies the underlying archetype binary, and multiple melodies legitimately share one pid (each remaps the same note sequence to different bells/speed/duration via its own settings). Deletion is now share-aware — a melody's binary is only removed from disk once no other melody still references its pid. Also adds backend/scripts/migrate_melody_binaries_to_local.py to backfill existing melodies from their old Firebase URLs to local storage, with --dry-run support and a warning list for pids whose melodies point at different source files (a pre-existing data issue, flagged for manual review via each melody's playback button rather than silently resolved). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
6.6 KiB
Melody Binary Serving (Plain HTTP)
Why this exists
ESP32 devices download .bsm melody files directly, using a URL supplied through the
Android app. The download previously pointed at a Firebase Storage public URL (HTTPS
only). ESP32's TLS client needs 40KB+ of RAM to open an HTTPS connection, which these
devices can't reliably spare. To fix this, melody .bsm binaries are now stored and
served by our own backend over plain HTTP, instead of Firebase Storage.
This does not affect the audio preview file (information.previewURL) — that's
only ever fetched by the browser admin UI, which is already HTTPS, so it stays on
Firebase Storage unchanged.
PID vs UID — why storage is keyed on pid, not uid
pid("Playback ID") identifies the underlying archetype binary — the raw note sequence (A, B, C...) baked into a.bsmfile. Multiple melodies can legitimately share onepid: each melody remaps those notes to actual bells via its ownnoteAssignments, speed, and duration settings. One binary can back many different "remix" melodies. Duplicate PIDs across melodies are correct and expected, not a bug.uidis meant to be each melody's own unique database identity. It is not currently populated anywhere inMelodyForm.jsx— this is a pre-existing, separate bug, deferred as its own task. Because of this, local binary storage is keyed onpid, notuid.
What changed
- Storage:
.bsmbinaries are written to./data/melody_binaries/{pid}.bsmon the host (mounted into the backend container at/app/storage/melody_binaries, same pattern as./data/firmwareand./data/built_melodies). Because storage is keyed onpid, multiple melodies sharing apidshare one file — saving from any of them overwrites the same file, which is expected. - Upload path:
SelectArchetypeModal/BuildOnTheFlyModalstill callPOST /api/melodies/{melodyId}/upload/binaryexactly as before. The backend (backend/melodies/service.py::save_binary_for_melody) now writes the bytes to local disk instead of uploading to Firebase Storage, and returns a URL likehttp://melodies.bellsystems.net/download/{pid}. The melody must have apidset before uploading a binary (the endpoint returns 400 if not). - Stored URL: the melody's
urlfield (Firestore + SQLite) now holds that plain-HTTP URL instead of a Firebase public URL. No schema change —urlwas already a plain string field. - Download route:
GET /api/melodies/download/{pid}(backend/melodies/router.py) is unauthenticated (devices have no login token) and resolvespiddirectly to{pid}.bsmon disk, same pattern as the existing firmware download route (backend/firmware/router.py::download_firmware). - Deletion is share-aware: deleting a melody or its binary
(
service.delete_file/service._delete_storage_files) only removes the local.bsmfile if no other melody still references the samepid(service._pid_used_by_other_melody). This prevents one melody's deletion from breaking playback for other melodies sharing its archetype binary.
Migrating existing melodies
Melodies created before this change still have a Firebase Storage URL in their url
field — deploying this code does not touch existing data. They keep working exactly
as before (still HTTPS to Firebase) until migrated.
Two ways to move a melody to the new plain-HTTP URL:
-
Per-melody, via the UI: open the melody and re-run "Select Archetype" or "Build on the Fly" — this naturally re-uploads through the same endpoint, which now writes to local disk and updates
url. -
Bulk, via script:
backend/scripts/migrate_melody_binaries_to_local.pydownloads each melody's current Firebase binary and writes it to local disk under itspid, then updatesurl(SQLite + Firestore if published). Run with--dry-runfirst:docker exec -it bellsystems-backend python scripts/migrate_melody_binaries_to_local.py --dry-run docker exec -it bellsystems-backend python scripts/migrate_melody_binaries_to_local.pyKnown caveat: some existing melodies share a
piddespite their Firebase URLs pointing at different source files (observed on real data — e.g. voice-count variant filenames like1N_/2N_/3N_/4N_all filed under onepid). The script downloads only the first melody's file for eachpidand reuses it for every other melody sharing thatpid— it does not attempt to detect which file is "correct". It prints a warning list of everypidwhere this happened; use each melody's playback button afterward to verify the right archetype plays, and manually re-assign the correct one via "Select Archetype" if it's wrong.
Infrastructure (outside this repo)
The plain-HTTP requirement is handled by keeping melodies.bellsystems.net on a
separate hostname from console.bellsystems.net, so the console's NPM proxy host
can stay HTTPS-only with no per-path exceptions.
Required, one-time setup outside this repository:
- DNS: add an A/CNAME record for
melodies.bellsystems.netpointing at the same host asconsole.bellsystems.net. - NPM (Nginx Proxy Manager) proxy host: create a new proxy host for
melodies.bellsystems.netforwarding to thenginxcontainer's exposed port (90perdocker-compose.yml). Do not force SSL / do not enable the "Force SSL" redirect on this proxy host — it must remain reachable over plain HTTP.
The in-repo nginx/nginx.conf already has a dedicated server_name melodies.bellsystems.net block that maps /download/{pid} to the backend's
/api/melodies/download/{pid} route, so no further nginx changes are needed once
the NPM proxy host exists.
Verification
- Build/select an archetype for a melody with a
pidset → confirm a file appears at./data/melody_binaries/{pid}.bsmand the melody'surlbecomeshttp://melodies.bellsystems.net/download/{pid}. curl http://localhost:8000/api/melodies/download/{pid}(direct to backend, bypassing nginx) → returns the.bsmbytes, no auth header needed.curl http://localhost:90/api/melodies/download/{pid}(through nginx on the console server block) → same result.- Once NPM is configured,
curl http://melodies.bellsystems.net/download/{pid}→ same result, over plain HTTP. - Delete a melody whose
pidis not shared with any other melody → confirm the local.bsmfile is removed. Delete one of two melodies sharing apid→ confirm the file survives until the last one is deleted. - Upload a preview audio file → confirm it still lands in Firebase Storage and
previewURLstill populates (regression check).