diff --git a/lib/hal/HalStorage.cpp b/lib/hal/HalStorage.cpp new file mode 100644 index 00000000..928061e8 --- /dev/null +++ b/lib/hal/HalStorage.cpp @@ -0,0 +1,63 @@ +#include "HalStorage.h" + +#include + +HalStorage HalStorage::instance; + +HalStorage::HalStorage() {} + +bool HalStorage::begin() { return SdMan.begin(); } + +bool HalStorage::ready() const { return SdMan.ready(); } + +std::vector HalStorage::listFiles(const char* path, int maxFiles) { return SdMan.listFiles(path, maxFiles); } + +String HalStorage::readFile(const char* path) { return SdMan.readFile(path); } + +bool HalStorage::readFileToStream(const char* path, Print& out, size_t chunkSize) { + return SdMan.readFileToStream(path, out, chunkSize); +} + +size_t HalStorage::readFileToBuffer(const char* path, char* buffer, size_t bufferSize, size_t maxBytes) { + return SdMan.readFileToBuffer(path, buffer, bufferSize, maxBytes); +} + +bool HalStorage::writeFile(const char* path, const String& content) { return SdMan.writeFile(path, content); } + +bool HalStorage::ensureDirectoryExists(const char* path) { return SdMan.ensureDirectoryExists(path); } + +File HalStorage::open(const char* path, const oflag_t oflag) { return SdMan.open(path, oflag); } + +bool HalStorage::mkdir(const char* path, const bool pFlag) { return SdMan.mkdir(path, pFlag); } + +bool HalStorage::exists(const char* path) { return SdMan.exists(path); } + +bool HalStorage::remove(const char* path) { return SdMan.remove(path); } + +bool HalStorage::rmdir(const char* path) { return SdMan.rmdir(path); } + +bool HalStorage::openFileForRead(const char* moduleName, const char* path, File& file) { + return SdMan.openFileForRead(moduleName, path, file); +} + +bool HalStorage::openFileForRead(const char* moduleName, const std::string& path, File& file) { + return openFileForRead(moduleName, path.c_str(), file); +} + +bool HalStorage::openFileForRead(const char* moduleName, const String& path, File& file) { + return openFileForRead(moduleName, path.c_str(), file); +} + +bool HalStorage::openFileForWrite(const char* moduleName, const char* path, File& file) { + return SdMan.openFileForWrite(moduleName, path, file); +} + +bool HalStorage::openFileForWrite(const char* moduleName, const std::string& path, File& file) { + return openFileForWrite(moduleName, path.c_str(), file); +} + +bool HalStorage::openFileForWrite(const char* moduleName, const String& path, File& file) { + return openFileForWrite(moduleName, path.c_str(), file); +} + +bool HalStorage::removeDir(const char* path) { return SdMan.removeDir(path); } \ No newline at end of file diff --git a/lib/hal/HalStorage.h b/lib/hal/HalStorage.h new file mode 100644 index 00000000..34f3a984 --- /dev/null +++ b/lib/hal/HalStorage.h @@ -0,0 +1,51 @@ +#pragma once + +#include + +#include + +using File = FsFile; + +class HalStorage { + public: + HalStorage(); + bool begin(); + bool ready() const; + std::vector listFiles(const char* path = "/", int maxFiles = 200); + // Read the entire file at `path` into a String. Returns empty string on failure. + String readFile(const char* path); + // Low-memory helpers: + // Stream the file contents to a `Print` (e.g. `Serial`, or any `Print`-derived object). + // Returns true on success, false on failure. + bool readFileToStream(const char* path, Print& out, size_t chunkSize = 256); + // Read up to `bufferSize-1` bytes into `buffer`, null-terminating it. Returns bytes read. + size_t readFileToBuffer(const char* path, char* buffer, size_t bufferSize, size_t maxBytes = 0); + // Write a string to `path` on the SD card. Overwrites existing file. + // Returns true on success. + bool writeFile(const char* path, const String& content); + // Ensure a directory exists, creating it if necessary. Returns true on success. + bool ensureDirectoryExists(const char* path); + + File open(const char* path, const oflag_t oflag = O_RDONLY); + bool mkdir(const char* path, const bool pFlag = true); + bool exists(const char* path); + bool remove(const char* path); + bool rmdir(const char* path); + + bool openFileForRead(const char* moduleName, const char* path, File& file); + bool openFileForRead(const char* moduleName, const std::string& path, File& file); + bool openFileForRead(const char* moduleName, const String& path, File& file); + bool openFileForWrite(const char* moduleName, const char* path, File& file); + bool openFileForWrite(const char* moduleName, const std::string& path, File& file); + bool openFileForWrite(const char* moduleName, const String& path, File& file); + bool removeDir(const char* path); + + static HalStorage& getInstance() { return instance; } + + private: + static HalStorage instance; + + bool initialized = false; +}; + +#define Storage HalStorage::getInstance() diff --git a/src/CrossPointSettings.cpp b/src/CrossPointSettings.cpp index 232c7c57..5a2a361e 100644 --- a/src/CrossPointSettings.cpp +++ b/src/CrossPointSettings.cpp @@ -1,7 +1,7 @@ #include "CrossPointSettings.h" +#include #include -#include #include #include @@ -11,7 +11,7 @@ // Initialize the static instance CrossPointSettings CrossPointSettings::instance; -void readAndValidate(FsFile& file, uint8_t& member, const uint8_t maxValue) { +void readAndValidate(File& file, uint8_t& member, const uint8_t maxValue) { uint8_t tempValue; serialization::readPod(file, tempValue); if (tempValue < maxValue) { @@ -28,10 +28,10 @@ constexpr char SETTINGS_FILE[] = "/.crosspoint/settings.bin"; bool CrossPointSettings::saveToFile() const { // Make sure the directory exists - SdMan.mkdir("/.crosspoint"); + Storage.mkdir("/.crosspoint"); - FsFile outputFile; - if (!SdMan.openFileForWrite("CPS", SETTINGS_FILE, outputFile)) { + File outputFile; + if (!Storage.openFileForWrite("CPS", SETTINGS_FILE, outputFile)) { return false; } @@ -68,8 +68,8 @@ bool CrossPointSettings::saveToFile() const { } bool CrossPointSettings::loadFromFile() { - FsFile inputFile; - if (!SdMan.openFileForRead("CPS", SETTINGS_FILE, inputFile)) { + File inputFile; + if (!Storage.openFileForRead("CPS", SETTINGS_FILE, inputFile)) { return false; } diff --git a/src/CrossPointState.cpp b/src/CrossPointState.cpp index 91aa2536..7415b30f 100644 --- a/src/CrossPointState.cpp +++ b/src/CrossPointState.cpp @@ -1,7 +1,7 @@ #include "CrossPointState.h" +#include #include -#include #include namespace { @@ -12,8 +12,8 @@ constexpr char STATE_FILE[] = "/.crosspoint/state.bin"; CrossPointState CrossPointState::instance; bool CrossPointState::saveToFile() const { - FsFile outputFile; - if (!SdMan.openFileForWrite("CPS", STATE_FILE, outputFile)) { + File outputFile; + if (!Storage.openFileForWrite("CPS", STATE_FILE, outputFile)) { return false; } @@ -25,8 +25,8 @@ bool CrossPointState::saveToFile() const { } bool CrossPointState::loadFromFile() { - FsFile inputFile; - if (!SdMan.openFileForRead("CPS", STATE_FILE, inputFile)) { + File inputFile; + if (!Storage.openFileForRead("CPS", STATE_FILE, inputFile)) { return false; } diff --git a/src/RecentBooksStore.cpp b/src/RecentBooksStore.cpp index 5932de36..2bdc253c 100644 --- a/src/RecentBooksStore.cpp +++ b/src/RecentBooksStore.cpp @@ -1,7 +1,7 @@ #include "RecentBooksStore.h" +#include #include -#include #include #include @@ -35,10 +35,10 @@ void RecentBooksStore::addBook(const std::string& path, const std::string& title bool RecentBooksStore::saveToFile() const { // Make sure the directory exists - SdMan.mkdir("/.crosspoint"); + Storage.mkdir("/.crosspoint"); - FsFile outputFile; - if (!SdMan.openFileForWrite("RBS", RECENT_BOOKS_FILE, outputFile)) { + File outputFile; + if (!Storage.openFileForWrite("RBS", RECENT_BOOKS_FILE, outputFile)) { return false; } @@ -58,8 +58,8 @@ bool RecentBooksStore::saveToFile() const { } bool RecentBooksStore::loadFromFile() { - FsFile inputFile; - if (!SdMan.openFileForRead("RBS", RECENT_BOOKS_FILE, inputFile)) { + File inputFile; + if (!Storage.openFileForRead("RBS", RECENT_BOOKS_FILE, inputFile)) { return false; } diff --git a/src/WifiCredentialStore.cpp b/src/WifiCredentialStore.cpp index be865b86..821777d0 100644 --- a/src/WifiCredentialStore.cpp +++ b/src/WifiCredentialStore.cpp @@ -1,7 +1,7 @@ #include "WifiCredentialStore.h" +#include #include -#include #include // Initialize the static instance @@ -29,10 +29,10 @@ void WifiCredentialStore::obfuscate(std::string& data) const { bool WifiCredentialStore::saveToFile() const { // Make sure the directory exists - SdMan.mkdir("/.crosspoint"); + Storage.mkdir("/.crosspoint"); - FsFile file; - if (!SdMan.openFileForWrite("WCS", WIFI_FILE, file)) { + File file; + if (!Storage.openFileForWrite("WCS", WIFI_FILE, file)) { return false; } @@ -59,8 +59,8 @@ bool WifiCredentialStore::saveToFile() const { } bool WifiCredentialStore::loadFromFile() { - FsFile file; - if (!SdMan.openFileForRead("WCS", WIFI_FILE, file)) { + File file; + if (!Storage.openFileForRead("WCS", WIFI_FILE, file)) { return false; } diff --git a/src/activities/boot_sleep/SleepActivity.cpp b/src/activities/boot_sleep/SleepActivity.cpp index 7ffc5851..71e475d8 100644 --- a/src/activities/boot_sleep/SleepActivity.cpp +++ b/src/activities/boot_sleep/SleepActivity.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -35,7 +35,7 @@ void SleepActivity::onEnter() { void SleepActivity::renderCustomSleepScreen() const { // Check if we have a /sleep directory - auto dir = SdMan.open("/sleep"); + auto dir = Storage.open("/sleep"); if (dir && dir.isDirectory()) { std::vector files; char name[500]; @@ -77,8 +77,8 @@ void SleepActivity::renderCustomSleepScreen() const { APP_STATE.lastSleepImage = randomFileIndex; APP_STATE.saveToFile(); const auto filename = "/sleep/" + files[randomFileIndex]; - FsFile file; - if (SdMan.openFileForRead("SLP", filename, file)) { + File file; + if (Storage.openFileForRead("SLP", filename, file)) { Serial.printf("[%lu] [SLP] Randomly loading: /sleep/%s\n", millis(), files[randomFileIndex].c_str()); delay(100); Bitmap bitmap(file, true); @@ -94,8 +94,8 @@ void SleepActivity::renderCustomSleepScreen() const { // Look for sleep.bmp on the root of the sd card to determine if we should // render a custom sleep screen instead of the default. - FsFile file; - if (SdMan.openFileForRead("SLP", "/sleep.bmp", file)) { + File file; + if (Storage.openFileForRead("SLP", "/sleep.bmp", file)) { Bitmap bitmap(file, true); if (bitmap.parseHeaders() == BmpReaderError::Ok) { Serial.printf("[%lu] [SLP] Loading: /sleep.bmp\n", millis()); @@ -253,8 +253,8 @@ void SleepActivity::renderCoverSleepScreen() const { return renderDefaultSleepScreen(); } - FsFile file; - if (SdMan.openFileForRead("SLP", coverBmpPath, file)) { + File file; + if (Storage.openFileForRead("SLP", coverBmpPath, file)) { Bitmap bitmap(file); if (bitmap.parseHeaders() == BmpReaderError::Ok) { Serial.printf("[SLP] Rendering sleep cover: %s\n", coverBmpPath); diff --git a/src/activities/home/HomeActivity.cpp b/src/activities/home/HomeActivity.cpp index 678af7cb..4d796a12 100644 --- a/src/activities/home/HomeActivity.cpp +++ b/src/activities/home/HomeActivity.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -36,7 +36,7 @@ void HomeActivity::onEnter() { renderingMutex = xSemaphoreCreateMutex(); // Check if we have a book to continue reading - hasContinueReading = !APP_STATE.openEpubPath.empty() && SdMan.exists(APP_STATE.openEpubPath.c_str()); + hasContinueReading = !APP_STATE.openEpubPath.empty() && Storage.exists(APP_STATE.openEpubPath.c_str()); // Check if OPDS browser URL is configured hasOpdsUrl = strlen(SETTINGS.opdsServerUrl) > 0; @@ -242,8 +242,8 @@ void HomeActivity::render() { // Only load from SD on first render, then use stored buffer if (hasContinueReading && hasCoverImage && !coverBmpPath.empty() && !coverRendered) { // First time: load cover from SD and render - FsFile file; - if (SdMan.openFileForRead("HOME", coverBmpPath, file)) { + File file; + if (Storage.openFileForRead("HOME", coverBmpPath, file)) { Bitmap bitmap(file); if (bitmap.parseHeaders() == BmpReaderError::Ok) { // Calculate position to center image within the book card diff --git a/src/activities/home/MyLibraryActivity.cpp b/src/activities/home/MyLibraryActivity.cpp index 29c6ea73..2d18f94c 100644 --- a/src/activities/home/MyLibraryActivity.cpp +++ b/src/activities/home/MyLibraryActivity.cpp @@ -1,7 +1,7 @@ #include "MyLibraryActivity.h" #include -#include +#include #include @@ -72,7 +72,7 @@ void MyLibraryActivity::loadRecentBooks() { for (const auto& book : books) { // Skip if file no longer exists - if (!SdMan.exists(book.path.c_str())) { + if (!Storage.exists(book.path.c_str())) { continue; } recentBooks.push_back(book); @@ -82,7 +82,7 @@ void MyLibraryActivity::loadRecentBooks() { void MyLibraryActivity::loadFiles() { files.clear(); - auto root = SdMan.open(basepath.c_str()); + auto root = Storage.open(basepath.c_str()); if (!root || !root.isDirectory()) { if (root) root.close(); return; diff --git a/src/activities/reader/EpubReaderActivity.cpp b/src/activities/reader/EpubReaderActivity.cpp index 5ccfb4fe..0402beb1 100644 --- a/src/activities/reader/EpubReaderActivity.cpp +++ b/src/activities/reader/EpubReaderActivity.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include "CrossPointSettings.h" #include "CrossPointState.h" @@ -56,8 +56,8 @@ void EpubReaderActivity::onEnter() { epub->setupCacheDir(); - FsFile f; - if (SdMan.openFileForRead("ERS", epub->getCachePath() + "/progress.bin", f)) { + File f; + if (Storage.openFileForRead("ERS", epub->getCachePath() + "/progress.bin", f)) { uint8_t data[6]; int dataSize = f.read(data, 6); if (dataSize == 4 || dataSize == 6) { @@ -435,7 +435,7 @@ void EpubReaderActivity::renderScreen() { void EpubReaderActivity::saveProgress(int spineIndex, int currentPage, int pageCount) { FsFile f; - if (SdMan.openFileForWrite("ERS", epub->getCachePath() + "/progress.bin", f)) { + if (Storage.openFileForWrite("ERS", epub->getCachePath() + "/progress.bin", f)) { uint8_t data[6]; data[0] = currentSpineIndex & 0xFF; data[1] = (currentSpineIndex >> 8) & 0xFF; diff --git a/src/activities/reader/ReaderActivity.cpp b/src/activities/reader/ReaderActivity.cpp index 04240b3c..270130ae 100644 --- a/src/activities/reader/ReaderActivity.cpp +++ b/src/activities/reader/ReaderActivity.cpp @@ -1,5 +1,7 @@ #include "ReaderActivity.h" +#include + #include "Epub.h" #include "EpubReaderActivity.h" #include "Txt.h" @@ -27,7 +29,7 @@ bool ReaderActivity::isTxtFile(const std::string& path) { } std::unique_ptr ReaderActivity::loadEpub(const std::string& path) { - if (!SdMan.exists(path.c_str())) { + if (!Storage.exists(path.c_str())) { Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str()); return nullptr; } @@ -42,7 +44,7 @@ std::unique_ptr ReaderActivity::loadEpub(const std::string& path) { } std::unique_ptr ReaderActivity::loadXtc(const std::string& path) { - if (!SdMan.exists(path.c_str())) { + if (!Storage.exists(path.c_str())) { Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str()); return nullptr; } @@ -57,7 +59,7 @@ std::unique_ptr ReaderActivity::loadXtc(const std::string& path) { } std::unique_ptr ReaderActivity::loadTxt(const std::string& path) { - if (!SdMan.exists(path.c_str())) { + if (!Storage.exists(path.c_str())) { Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str()); return nullptr; } diff --git a/src/activities/reader/TxtReaderActivity.cpp b/src/activities/reader/TxtReaderActivity.cpp index eb1a9eef..aeaa1a89 100644 --- a/src/activities/reader/TxtReaderActivity.cpp +++ b/src/activities/reader/TxtReaderActivity.cpp @@ -1,7 +1,7 @@ #include "TxtReaderActivity.h" #include -#include +#include #include #include @@ -543,8 +543,8 @@ void TxtReaderActivity::renderStatusBar(const int orientedMarginRight, const int } void TxtReaderActivity::saveProgress() const { - FsFile f; - if (SdMan.openFileForWrite("TRS", txt->getCachePath() + "/progress.bin", f)) { + File f; + if (Storage.openFileForWrite("TRS", txt->getCachePath() + "/progress.bin", f)) { uint8_t data[4]; data[0] = currentPage & 0xFF; data[1] = (currentPage >> 8) & 0xFF; @@ -556,8 +556,8 @@ void TxtReaderActivity::saveProgress() const { } void TxtReaderActivity::loadProgress() { - FsFile f; - if (SdMan.openFileForRead("TRS", txt->getCachePath() + "/progress.bin", f)) { + File f; + if (Storage.openFileForRead("TRS", txt->getCachePath() + "/progress.bin", f)) { uint8_t data[4]; if (f.read(data, 4) == 4) { currentPage = data[0] + (data[1] << 8); @@ -587,8 +587,8 @@ bool TxtReaderActivity::loadPageIndexCache() { // - N * uint32_t: page offsets std::string cachePath = txt->getCachePath() + "/index.bin"; - FsFile f; - if (!SdMan.openFileForRead("TRS", cachePath, f)) { + File f; + if (!Storage.openFileForRead("TRS", cachePath, f)) { Serial.printf("[%lu] [TRS] No page index cache found\n", millis()); return false; } @@ -679,8 +679,8 @@ bool TxtReaderActivity::loadPageIndexCache() { void TxtReaderActivity::savePageIndexCache() const { std::string cachePath = txt->getCachePath() + "/index.bin"; - FsFile f; - if (!SdMan.openFileForWrite("TRS", cachePath, f)) { + File f; + if (!Storage.openFileForWrite("TRS", cachePath, f)) { Serial.printf("[%lu] [TRS] Failed to save page index cache\n", millis()); return; } diff --git a/src/activities/reader/XtcReaderActivity.cpp b/src/activities/reader/XtcReaderActivity.cpp index f579abcd..93c18c4d 100644 --- a/src/activities/reader/XtcReaderActivity.cpp +++ b/src/activities/reader/XtcReaderActivity.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include "CrossPointSettings.h" #include "CrossPointState.h" @@ -368,8 +368,8 @@ void XtcReaderActivity::renderPage() { } void XtcReaderActivity::saveProgress() const { - FsFile f; - if (SdMan.openFileForWrite("XTR", xtc->getCachePath() + "/progress.bin", f)) { + File f; + if (Storage.openFileForWrite("XTR", xtc->getCachePath() + "/progress.bin", f)) { uint8_t data[4]; data[0] = currentPage & 0xFF; data[1] = (currentPage >> 8) & 0xFF; @@ -381,8 +381,8 @@ void XtcReaderActivity::saveProgress() const { } void XtcReaderActivity::loadProgress() { - FsFile f; - if (SdMan.openFileForRead("XTR", xtc->getCachePath() + "/progress.bin", f)) { + File f; + if (Storage.openFileForRead("XTR", xtc->getCachePath() + "/progress.bin", f)) { uint8_t data[4]; if (f.read(data, 4) == 4) { currentPage = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); diff --git a/src/activities/settings/ClearCacheActivity.cpp b/src/activities/settings/ClearCacheActivity.cpp index 1e10c14b..b831aeaf 100644 --- a/src/activities/settings/ClearCacheActivity.cpp +++ b/src/activities/settings/ClearCacheActivity.cpp @@ -1,8 +1,8 @@ #include "ClearCacheActivity.h" #include +#include #include -#include #include "MappedInputManager.h" #include "fontIds.h" @@ -106,7 +106,7 @@ void ClearCacheActivity::clearCache() { Serial.printf("[%lu] [CLEAR_CACHE] Clearing cache...\n", millis()); // Open .crosspoint directory - auto root = SdMan.open("/.crosspoint"); + auto root = Storage.open("/.crosspoint"); if (!root || !root.isDirectory()) { Serial.printf("[%lu] [CLEAR_CACHE] Failed to open cache directory\n", millis()); if (root) root.close(); @@ -131,7 +131,7 @@ void ClearCacheActivity::clearCache() { file.close(); // Close before attempting to delete - if (SdMan.removeDir(fullPath.c_str())) { + if (Storage.removeDir(fullPath.c_str())) { clearedCount++; } else { Serial.printf("[%lu] [CLEAR_CACHE] Failed to remove: %s\n", millis(), fullPath.c_str()); diff --git a/src/main.cpp b/src/main.cpp index 89c4e13c..de9025ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -283,7 +283,7 @@ void setup() { // SD Card Initialization // We need 6 open files concurrently when parsing a new chapter - if (!SdMan.begin()) { + if (!Storage.begin()) { Serial.printf("[%lu] [ ] SD card initialization failed\n", millis()); setupDisplayAndFonts(); exitActivity(); diff --git a/src/network/CrossPointWebServer.cpp b/src/network/CrossPointWebServer.cpp index a135c9f0..cfd6db2e 100644 --- a/src/network/CrossPointWebServer.cpp +++ b/src/network/CrossPointWebServer.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -25,7 +25,7 @@ constexpr uint16_t LOCAL_UDP_PORT = 8134; CrossPointWebServer* wsInstance = nullptr; // WebSocket upload state -FsFile wsUploadFile; +File wsUploadFile; String wsUploadFileName; String wsUploadPath; size_t wsUploadSize = 0; @@ -280,7 +280,7 @@ void CrossPointWebServer::handleStatus() const { } void CrossPointWebServer::scanFiles(const char* path, const std::function& callback) const { - FsFile root = SdMan.open(path); + File root = Storage.open(path); if (!root) { Serial.printf("[%lu] [WEB] Failed to open directory: %s\n", millis(), path); return; @@ -294,7 +294,7 @@ void CrossPointWebServer::scanFiles(const char* path, const std::functionsend(404, "text/plain", "Item not found"); return; } - FsFile file = SdMan.open(itemPath.c_str()); + File file = Storage.open(itemPath.c_str()); if (!file) { server->send(500, "text/plain", "Failed to open file"); return; @@ -459,7 +459,7 @@ void CrossPointWebServer::handleDownload() const { } // Static variables for upload handling -static FsFile uploadFile; +static File uploadFile; static String uploadFileName; static String uploadPath = "/"; static size_t uploadSize = 0; @@ -553,15 +553,15 @@ void CrossPointWebServer::handleUpload() const { // Check if file already exists - SD operations can be slow esp_task_wdt_reset(); - if (SdMan.exists(filePath.c_str())) { + if (Storage.exists(filePath.c_str())) { Serial.printf("[%lu] [WEB] [UPLOAD] Overwriting existing file: %s\n", millis(), filePath.c_str()); esp_task_wdt_reset(); - SdMan.remove(filePath.c_str()); + Storage.remove(filePath.c_str()); } // Open file for writing - this can be slow due to FAT cluster allocation esp_task_wdt_reset(); - if (!SdMan.openFileForWrite("WEB", filePath, uploadFile)) { + if (!Storage.openFileForWrite("WEB", filePath, uploadFile)) { uploadError = "Failed to create file on SD card"; Serial.printf("[%lu] [WEB] [UPLOAD] FAILED to create file: %s\n", millis(), filePath.c_str()); return; @@ -639,7 +639,7 @@ void CrossPointWebServer::handleUpload() const { String filePath = uploadPath; if (!filePath.endsWith("/")) filePath += "/"; filePath += uploadFileName; - SdMan.remove(filePath.c_str()); + Storage.remove(filePath.c_str()); } uploadError = "Upload aborted"; Serial.printf("[%lu] [WEB] Upload aborted\n", millis()); @@ -690,13 +690,13 @@ void CrossPointWebServer::handleCreateFolder() const { Serial.printf("[%lu] [WEB] Creating folder: %s\n", millis(), folderPath.c_str()); // Check if already exists - if (SdMan.exists(folderPath.c_str())) { + if (Storage.exists(folderPath.c_str())) { server->send(400, "text/plain", "Folder already exists"); return; } // Create the folder - if (SdMan.mkdir(folderPath.c_str())) { + if (Storage.mkdir(folderPath.c_str())) { Serial.printf("[%lu] [WEB] Folder created successfully: %s\n", millis(), folderPath.c_str()); server->send(200, "text/plain", "Folder created: " + folderName); } else { @@ -746,7 +746,7 @@ void CrossPointWebServer::handleDelete() const { } // Check if item exists - if (!SdMan.exists(itemPath.c_str())) { + if (!Storage.exists(itemPath.c_str())) { Serial.printf("[%lu] [WEB] Delete failed - item not found: %s\n", millis(), itemPath.c_str()); server->send(404, "text/plain", "Item not found"); return; @@ -758,10 +758,10 @@ void CrossPointWebServer::handleDelete() const { if (itemType == "folder") { // For folders, try to remove (will fail if not empty) - FsFile dir = SdMan.open(itemPath.c_str()); + File dir = Storage.open(itemPath.c_str()); if (dir && dir.isDirectory()) { // Check if folder is empty - FsFile entry = dir.openNextFile(); + File entry = dir.openNextFile(); if (entry) { // Folder is not empty entry.close(); @@ -772,10 +772,10 @@ void CrossPointWebServer::handleDelete() const { } dir.close(); } - success = SdMan.rmdir(itemPath.c_str()); + success = Storage.rmdir(itemPath.c_str()); } else { // For files, use remove - success = SdMan.remove(itemPath.c_str()); + success = Storage.remove(itemPath.c_str()); } if (success) { @@ -811,7 +811,7 @@ void CrossPointWebServer::onWebSocketEvent(uint8_t num, WStype_t type, uint8_t* String filePath = wsUploadPath; if (!filePath.endsWith("/")) filePath += "/"; filePath += wsUploadFileName; - SdMan.remove(filePath.c_str()); + Storage.remove(filePath.c_str()); Serial.printf("[%lu] [WS] Deleted incomplete upload: %s\n", millis(), filePath.c_str()); } wsUploadInProgress = false; @@ -855,13 +855,13 @@ void CrossPointWebServer::onWebSocketEvent(uint8_t num, WStype_t type, uint8_t* // Check if file exists and remove it esp_task_wdt_reset(); - if (SdMan.exists(filePath.c_str())) { - SdMan.remove(filePath.c_str()); + if (Storage.exists(filePath.c_str())) { + Storage.remove(filePath.c_str()); } // Open file for writing esp_task_wdt_reset(); - if (!SdMan.openFileForWrite("WS", filePath, wsUploadFile)) { + if (!Storage.openFileForWrite("WS", filePath, wsUploadFile)) { wsServer->sendTXT(num, "ERROR:Failed to create file"); wsUploadInProgress = false; return; diff --git a/src/network/HttpDownloader.cpp b/src/network/HttpDownloader.cpp index b7718c2d..29c7866a 100644 --- a/src/network/HttpDownloader.cpp +++ b/src/network/HttpDownloader.cpp @@ -100,13 +100,13 @@ HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& Serial.printf("[%lu] [HTTP] Content-Length: %zu\n", millis(), contentLength); // Remove existing file if present - if (SdMan.exists(destPath.c_str())) { - SdMan.remove(destPath.c_str()); + if (Storage.exists(destPath.c_str())) { + Storage.remove(destPath.c_str()); } // Open file for writing - FsFile file; - if (!SdMan.openFileForWrite("HTTP", destPath.c_str(), file)) { + File file; + if (!Storage.openFileForWrite("HTTP", destPath.c_str(), file)) { Serial.printf("[%lu] [HTTP] Failed to open file for writing\n", millis()); http.end(); return FILE_ERROR; @@ -117,7 +117,7 @@ HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& if (!stream) { Serial.printf("[%lu] [HTTP] Failed to get stream\n", millis()); file.close(); - SdMan.remove(destPath.c_str()); + Storage.remove(destPath.c_str()); http.end(); return HTTP_ERROR; } @@ -145,7 +145,7 @@ HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& if (written != bytesRead) { Serial.printf("[%lu] [HTTP] Write failed: wrote %zu of %zu bytes\n", millis(), written, bytesRead); file.close(); - SdMan.remove(destPath.c_str()); + Storage.remove(destPath.c_str()); http.end(); return FILE_ERROR; } @@ -165,7 +165,7 @@ HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& // Verify download size if known if (contentLength > 0 && downloaded != contentLength) { Serial.printf("[%lu] [HTTP] Size mismatch: got %zu, expected %zu\n", millis(), downloaded, contentLength); - SdMan.remove(destPath.c_str()); + Storage.remove(destPath.c_str()); return HTTP_ERROR; } diff --git a/src/network/HttpDownloader.h b/src/network/HttpDownloader.h index ac520a42..fd18dd4c 100644 --- a/src/network/HttpDownloader.h +++ b/src/network/HttpDownloader.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #include #include