fixed absolute / relative song name handling

This commit is contained in:
2025-07-28 17:37:28 +02:00
parent a1d486dd2d
commit 78167ecc20
3 changed files with 40 additions and 32 deletions

View File

@@ -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;