refactor(FileManager): drop HTTPS support — melody downloads over plain HTTP only

The ESP32 mbedTLS TLS handshake requires ~40KB of contiguous heap, which is
not reliably available after runtime heap fragmentation. Melody .bsm files
carry no credentials so plain HTTP is acceptable.

Removed: WiFiClientSecure, esp_task_wdt WDT disable/re-enable around the GET,
SSL heap guard, and all secureClient cleanup paths. HTTPS URLs are now rejected
immediately with a clear error log (F-027).

Also fixed: ensureDirectoryExists now strips trailing slashes instead of
appending one — SD.exists() returns false for directories when a trailing
slash is present.

Docs: feature-catalog.md F-027 added, FileManager README updated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 10:42:46 +03:00
co-authored by Claude Sonnet 4.6
parent 9f33f90ea9
commit 9e415de7bc
4 changed files with 32 additions and 47 deletions
+12 -40
View File
@@ -1,5 +1,4 @@
#include "FileManager.hpp"
#include <esp_task_wdt.h>
#define TAG "FileManager"
#include "../BuiltInMelodies/BuiltInMelodies.hpp"
@@ -50,68 +49,44 @@ bool FileManager::ensureDirectoryExists(const String& dirPath) {
return false;
}
// Ensure the directory ends with '/'
// Strip trailing slash — SD.exists() returns false for directories when a
// trailing slash is present, even if the directory exists.
String normalizedPath = dirPath;
if (!normalizedPath.endsWith("/")) {
normalizedPath += "/";
while (normalizedPath.endsWith("/")) {
normalizedPath.remove(normalizedPath.length() - 1);
}
// Check if directory already exists
if (SD.exists(normalizedPath.c_str())) {
return true; // Directory already exists, success
return true;
}
// Create directory if it doesn't exist
return SD.mkdir(normalizedPath.c_str());
}
bool FileManager::downloadFile(const String& url, const String& directory, const String& filename) {
LOG_INFO(TAG, "Starting download from: %s", url.c_str());
// Check if URL is HTTPS
bool isHttps = url.startsWith("https://");
HTTPClient http;
WiFiClientSecure* secureClient = nullptr;
// Configure HTTP client based on protocol
if (isHttps) {
size_t largestBlock = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
LOG_DEBUG(TAG, "Largest free heap block before SSL alloc: %u bytes", largestBlock);
if (largestBlock < 40000) {
LOG_ERROR(TAG, "Insufficient contiguous heap for SSL context (%u bytes free, need ~40KB)", largestBlock);
return false;
}
secureClient = new WiFiClientSecure();
secureClient->setInsecure(); // Skip certificate validation for Firebase
secureClient->setTimeout(15); // 15 second timeout for TLS operations
http.begin(*secureClient, url);
LOG_DEBUG(TAG, "Using HTTPS with secure client");
} else {
http.begin(url);
LOG_DEBUG(TAG, "Using HTTP");
if (url.startsWith("https://")) {
LOG_ERROR(TAG, "HTTPS URLs are not supported — serve melody files over HTTP");
return false;
}
http.setTimeout(30000); // 30 second timeout for large files
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); // Follow redirects automatically
HTTPClient http;
http.begin(url);
http.setTimeout(30000);
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
// Remove this task from the WDT while the TLS handshake blocks.
// The handshake can take several seconds and would otherwise starve async_tcp.
esp_task_wdt_delete(NULL);
LOG_DEBUG(TAG, "Sending HTTP GET request...");
int httpCode = http.GET();
esp_task_wdt_add(NULL);
if (httpCode != HTTP_CODE_OK && httpCode != HTTP_CODE_MOVED_PERMANENTLY && httpCode != HTTP_CODE_FOUND) {
LOG_ERROR(TAG, "HTTP GET failed, code: %d, error: %s", httpCode, http.errorToString(httpCode).c_str());
http.end();
if (secureClient) delete secureClient;
return false;
}
if (!initializeSD()) {
http.end();
if (secureClient) delete secureClient;
return false;
}
@@ -119,7 +94,6 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
if (!ensureDirectoryExists(directory)) {
LOG_ERROR(TAG, "Failed to create directory: %s", directory.c_str());
http.end();
if (secureClient) delete secureClient;
return false;
}
@@ -132,7 +106,6 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
if (!file) {
LOG_ERROR(TAG, "Failed to open file for writing: %s", fullPath.c_str());
http.end();
if (secureClient) delete secureClient;
return false;
}
@@ -189,7 +162,6 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
file.close();
http.end();
if (secureClient) delete secureClient;
LOG_INFO(TAG, "Download complete, file saved to: %s (%u bytes)", fullPath.c_str(), totalBytes);
return true;
}