diff --git a/src/network/CrossPointWebServer.cpp b/src/network/CrossPointWebServer.cpp index b869398..90dfed7 100644 --- a/src/network/CrossPointWebServer.cpp +++ b/src/network/CrossPointWebServer.cpp @@ -11,6 +11,7 @@ #include "html/FilesPageHtml.generated.h" #include "html/HomePageHtml.generated.h" +#include "util/StringUtils.h" namespace { // Folders/files to hide from the web interface file browser @@ -33,9 +34,8 @@ bool wsUploadInProgress = false; // Helper function to clear epub cache after upload void clearEpubCacheIfNeeded(const String& filePath) { // Only clear cache for .epub files - if (filePath.endsWith(".epub") || filePath.endsWith(".EPUB")) { - Epub epub(filePath.c_str(), "/.crosspoint"); - epub.clearCache(); + if (StringUtils::checkFileExtension(filePath, ".epub")) { + Epub(filePath.c_str(), "/.crosspoint").clearCache(); Serial.printf("[%lu] [WEB] Cleared epub cache for: %s\n", millis(), filePath.c_str()); } } diff --git a/src/util/StringUtils.cpp b/src/util/StringUtils.cpp index e56bc9d..1c5c499 100644 --- a/src/util/StringUtils.cpp +++ b/src/util/StringUtils.cpp @@ -49,6 +49,19 @@ bool checkFileExtension(const std::string& fileName, const char* extension) { return true; } +bool checkFileExtension(const String& fileName, const char* extension) { + if (fileName.length() < strlen(extension)) { + return false; + } + + String localFile(fileName); + String localExtension(extension); + localFile.toLowerCase(); + localExtension.toLowerCase(); + return localFile.endsWith(localExtension); +} + + size_t utf8RemoveLastChar(std::string& str) { if (str.empty()) return 0; size_t pos = str.size() - 1; diff --git a/src/util/StringUtils.h b/src/util/StringUtils.h index e001d7b..8d75125 100644 --- a/src/util/StringUtils.h +++ b/src/util/StringUtils.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace StringUtils { @@ -15,6 +16,7 @@ std::string sanitizeFilename(const std::string& name, size_t maxLength = 100); * Check if the given filename ends with the specified extension (case-insensitive). */ bool checkFileExtension(const std::string& fileName, const char* extension); +bool checkFileExtension(const String& fileName, const char* extension); // UTF-8 safe string truncation - removes one character from the end // Returns the new size after removing one UTF-8 character