From 78167ecc20ecd400163be6afabdcadfa4ae7a74a Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Mon, 28 Jul 2025 17:37:28 +0200 Subject: [PATCH] fixed absolute / relative song name handling --- src/DirectoryNode.cpp | 64 +++++++++++++++++++++++-------------------- src/DirectoryNode.h | 2 +- src/main.cpp | 6 +++- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/DirectoryNode.cpp b/src/DirectoryNode.cpp index ae46e78..e616160 100644 --- a/src/DirectoryNode.cpp +++ b/src/DirectoryNode.cpp @@ -130,6 +130,8 @@ void DirectoryNode::buildDirectoryTree(const char *currentPath) rootDir.close(); } + + void DirectoryNode::printDirectoryTree(int level) const { for (int i = 0; i < level; i++) @@ -220,61 +222,60 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id) return nullptr; } -DirectoryNode *DirectoryNode::advanceToMP3(const String *currentGlobal) + +DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) { - if (currentGlobal == nullptr) { - Serial.println("advanceToMP3: currentGlobal is null"); + if (songName == nullptr) { + Serial.println("advanceToMP3: songName is null"); return nullptr; } // Check if the input is an absolute path (starts with '/') or just a filename - bool isAbsolutePath = currentGlobal->startsWith("/"); - - // First, check MP3 files in this directory + bool isAbsolutePath = songName->startsWith("/"); + + // First, search in the current directory's MP3 files for (size_t i = 0; i < mp3Files.size(); i++) { if (isAbsolutePath) { - // For absolute paths, do exact match - if (*currentGlobal == mp3Files[i]) - { - setCurrentPlaying(&mp3Files[i]); - return this; - } - } else { - // For filenames, extract filename from full path and compare - String filename = mp3Files[i]; - int lastSlash = filename.lastIndexOf('/'); - if (lastSlash != -1) { - filename = filename.substring(lastSlash + 1); - } - if (*currentGlobal == filename) - { + if (*songName == mp3Files[i]) { setCurrentPlaying(&mp3Files[i]); return this; } + } else if (mp3Files[i].endsWith(*songName)) + { + setCurrentPlaying(&mp3Files[i]); + return this; } } - // Recursively search subdirectories + // Then search in subdirectories for (auto subdir : subdirectories) { - // Check if the string matches a directory name (only for non-absolute paths) - if (!isAbsolutePath && subdir->getName() == *currentGlobal) + if (!isAbsolutePath && subdir->getName() == *songName) { subdir->advanceToFirstMP3InThisNode(); return subdir; } - // Recursively search in subdirectory - DirectoryNode* result = subdir->advanceToMP3(currentGlobal); - if (result != nullptr && result->getCurrentPlaying() != nullptr) - { - return result; + // Search all files within subdir: + for (size_t i = 0; i < subdir->mp3Files.size(); i++) + { + + if (isAbsolutePath) { + if (*songName == subdir->mp3Files[i]) { + subdir->setCurrentPlaying(&subdir->mp3Files[i]); + return subdir; + } + } else if (subdir->mp3Files[i].endsWith(*songName)) + { + subdir->setCurrentPlaying(&subdir->mp3Files[i]); + return subdir; + } } } // If we get here, no matching song was found - Serial.println("advanceToMP3: No song found for: " + *currentGlobal); + Serial.println("advanceToMP3: No song found for: " + *songName); return nullptr; } @@ -448,6 +449,9 @@ DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal) return this; } + + + String DirectoryNode::getDirectoryStructureHTML() const { String html; diff --git a/src/DirectoryNode.h b/src/DirectoryNode.h index e8be759..683a2a5 100644 --- a/src/DirectoryNode.h +++ b/src/DirectoryNode.h @@ -47,7 +47,7 @@ public: void addMP3File(const String& mp3File); void buildDirectoryTree(const char* currentPath); void printDirectoryTree(int level = 0) const; - DirectoryNode* advanceToMP3(const String* currentGlobal); + DirectoryNode* advanceToMP3(const String* songName); DirectoryNode* advanceToNextMP3(const String* currentGlobal); DirectoryNode* goToPreviousMP3(uint32_t thresholdSeconds = 3); DirectoryNode* findPreviousMP3Globally(const String* currentGlobal); diff --git a/src/main.cpp b/src/main.cpp index c301a5c..83c1757 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -555,7 +555,7 @@ void saveMappingToFile(const String filename) { File file = SD.open(filename, FILE_WRITE); if (file) { for (const auto &pair : rfid_map) { - file.println(pair.first + "=" + pair.second); + file.println(pair.first+ "=" + pair.second); } file.close(); Serial.println("Mapping saved to file."); @@ -569,6 +569,8 @@ void editMapping(AsyncWebServerRequest *request) { if (request->hasParam("rfid", true) && request->hasParam("song", true)) { String rfid = request->getParam("rfid", true)->value(); String song = request->getParam("song", true)->value(); + rfid.trim(); + song.trim(); rfid_map[rfid] = song; saveMappingToFile(getSysDir(mapping_file)); request->send(200, "text/plain", "Mapping updated"); @@ -590,6 +592,8 @@ std::map readDataFromFile(String filename) { // Extract key and value String key = line.substring(0, separatorIndex).c_str(); String value = line.substring(separatorIndex + 1).c_str(); + key.trim(); + value.trim(); Serial.println("found rfid mapping for "+value); // Add key-value pair to the map rfid_map[key] = value;