Cleanup extension checking

This commit is contained in:
Dave Allie 2026-01-21 23:53:26 +11:00
parent d05f3f4681
commit a46f41f934
No known key found for this signature in database
GPG Key ID: F2FDDB3AD8D0276F
3 changed files with 18 additions and 3 deletions

View File

@ -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());
}
}

View File

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

View File

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <WString.h>
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