Skip to content

Fix firmware OTA URL to use correct bucket public URL - #11

Merged
JamesSmartCell merged 1 commit into
masterfrom
cursor/fix-firmware-ota-url-0ab8
Nov 17, 2026
Merged

JamesSmartCell merged 1 commit into
masterfrom
cursor/fix-firmware-ota-url-0ab8

Conversation

@JamesSmartCell

Copy link
Copy Markdown
Owner

Problem

Firmware binaries are uploaded to a separate Cloudflare R2 bucket (R2_FIRMWARE_BUCKET_NAME, e.g. esp-firmware), but src/service/CloudflareR2.ts builds the returned public URL from R2_PUBLIC_URL only, which is the camera bucket's public r2.dev URL. This causes firmware download URLs to point at the wrong bucket, resulting in 404 errors on OTA updates.

Solution

This PR adds support for a new environment variable R2_FIRMWARE_PUBLIC_URL to specify the public URL for the firmware bucket.

Changes

  1. Added R2_FIRMWARE_PUBLIC_URL support in CloudflareR2.ts:

    • Firmware uploads now use R2_FIRMWARE_PUBLIC_URL when available
    • Falls back to R2_PUBLIC_URL if firmware-specific URL is not set
    • Falls back to the existing r2.cloudflarestorage.com form as a last resort
    • Camera uploads continue to use R2_PUBLIC_URL (unchanged behavior)
  2. Added startup warning:

    • Logs a clear ASCII-only warning if R2_FIRMWARE_BUCKET_NAME is set but neither R2_FIRMWARE_PUBLIC_URL nor R2_PUBLIC_URL is configured
    • Warning is logged only once at first firmware upload to avoid spam
  3. Documentation updates:

    • Added R2_FIRMWARE_PUBLIC_URL to .env.example with inline comment
    • Updated docs/firmware-ota.md with configuration section explaining the new variable and fallback behavior
  4. Comprehensive unit tests:

    • Added 7 new tests in src/__tests__/cloudflare-r2-url.test.ts covering:
      • Firmware uses R2_FIRMWARE_PUBLIC_URL when set
      • Fallback to R2_PUBLIC_URL when firmware URL is not set
      • Fallback to cloudflarestorage.com when no public URL is configured
      • Trailing slash trimming
      • Camera uploads remain unaffected
      • Warning logged once when needed
      • No warning when firmware and camera use same bucket

Test Results

All 222 tests pass:

  • 7 new CloudflareR2 URL selection tests ✅
  • 215 existing tests ✅

Backward Compatibility

✅ Fully backward compatible. When R2_FIRMWARE_PUBLIC_URL is not set, the system falls back to R2_PUBLIC_URL, then to the existing r2.cloudflarestorage.com form. Existing deployments will continue to work unchanged.

Open in Web Open in Cursor 

- Add R2_FIRMWARE_PUBLIC_URL env var for firmware bucket public URL
- Build firmware URLs from R2_FIRMWARE_PUBLIC_URL with fallback to R2_PUBLIC_URL
- Add optional publicBaseUrlOverride parameter to uploadToR2
- Log warning when firmware bucket is set but no public URL configured
- Update .env.example with R2_FIRMWARE_PUBLIC_URL documentation
- Update docs/firmware-ota.md with configuration details
- Add comprehensive unit tests for URL selection logic
- All 222 tests pass

Co-authored-by: James Brown <JamesSmartCell@users.noreply.github.com>
@JamesSmartCell
JamesSmartCell merged commit 0ef08e1 into master Nov 17, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants