Skip to content

Commit

Permalink
Fix for playing files via Web-UI for Arduino >=2.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
tueddy committed Mar 13, 2023
1 parent c578f88 commit 27afd7b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Expand Up @@ -16,7 +16,7 @@ default_envs = lolin_d32_pro_sdmmc_pe
board_build.flash_mode = qio
board_build.bootloader = dio
platform = espressif32@<=3.5.0
;platform = espressif32@<=6.0.1
;platform = espressif32@<=6.1.0
;platform = espressif32
;platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
framework = arduino
Expand Down
8 changes: 7 additions & 1 deletion src/Common.h
Expand Up @@ -48,7 +48,13 @@ inline bool endsWith(const char *str, const char *suf) {
return b == suf && *a == *b;
}

inline void convertUtf8ToAscii(String utf8String, char *asciiString) {
inline void convertFilenameToAscii(String utf8String, char *asciiString) {
#if ESP_ARDUINO_VERSION_MAJOR >= 2
// Arduino >= 2.0.5 filenames are already unicode, copy to result here without UTF-8 decoding
strncpy(asciiString, (char *) utf8String.c_str(), utf8String.length() / sizeof(asciiString[0]));
asciiString[utf8String.length()] = 0;
return;
#endif

int k = 0;
bool f_C3_seen = false;
Expand Down
18 changes: 9 additions & 9 deletions src/Web.cpp
Expand Up @@ -578,7 +578,7 @@ bool processJsonRequest(char *_serialJson) {
} else if (doc.containsKey("rfidAssign")) {
const char *_rfidIdAssinId = object["rfidAssign"]["rfidIdMusic"];
char _fileOrUrlAscii[MAX_FILEPATH_LENTGH];
convertUtf8ToAscii(object["rfidAssign"]["fileOrUrl"], _fileOrUrlAscii);
convertFilenameToAscii(object["rfidAssign"]["fileOrUrl"], _fileOrUrlAscii);
uint8_t _playMode = object["rfidAssign"]["playMode"];
char rfidString[275];
snprintf(rfidString, sizeof(rfidString) / sizeof(rfidString[0]), "%s%s%s0%s%u%s0", stringDelimiter, _fileOrUrlAscii, stringDelimiter, stringDelimiter, _playMode, stringDelimiter);
Expand Down Expand Up @@ -756,7 +756,7 @@ void explorerHandleFileUpload(AsyncWebServerRequest *request, String filename, s
utf8FilePath = "/" + filename;
}

convertUtf8ToAscii(utf8FilePath, filePath);
convertFilenameToAscii(utf8FilePath, filePath);

snprintf(Log_Buffer, Log_BufferLength, "%s: %s", (char *)FPSTR (writingFile), utf8FilePath.c_str());
Log_Println(Log_Buffer, LOGLEVEL_INFO);
Expand Down Expand Up @@ -912,7 +912,7 @@ void explorerHandleListRequest(AsyncWebServerRequest *request) {
File root;
if (request->hasParam("path")) {
param = request->getParam("path");
convertUtf8ToAscii(param->value(), filePath);
convertFilenameToAscii(param->value(), filePath);
root = gFSystem.open(filePath);
} else {
root = gFSystem.open("/");
Expand Down Expand Up @@ -1030,7 +1030,7 @@ void explorerHandleDownloadRequest(AsyncWebServerRequest *request) {
}
// check file exists on SD card
param = request->getParam("path");
convertUtf8ToAscii(param->value(), filePath);
convertFilenameToAscii(param->value(), filePath);
if (!gFSystem.exists(filePath)) {
snprintf(Log_Buffer, Log_BufferLength, "DOWNLOAD: File not found on SD card: %s", param->value().c_str());
Log_Println(Log_Buffer, LOGLEVEL_ERROR);
Expand Down Expand Up @@ -1079,7 +1079,7 @@ void explorerHandleDeleteRequest(AsyncWebServerRequest *request) {
char filePath[MAX_FILEPATH_LENTGH];
if (request->hasParam("path")) {
param = request->getParam("path");
convertUtf8ToAscii(param->value(), filePath);
convertFilenameToAscii(param->value(), filePath);
if (gFSystem.exists(filePath)) {
file = gFSystem.open(filePath);
if (file.isDirectory()) {
Expand Down Expand Up @@ -1118,7 +1118,7 @@ void explorerHandleCreateRequest(AsyncWebServerRequest *request) {
char filePath[MAX_FILEPATH_LENTGH];
if (request->hasParam("path")) {
param = request->getParam("path");
convertUtf8ToAscii(param->value(), filePath);
convertFilenameToAscii(param->value(), filePath);
if (gFSystem.mkdir(filePath)) {
snprintf(Log_Buffer, Log_BufferLength, "CREATE: %s created", param->value().c_str());
Log_Println(Log_Buffer, LOGLEVEL_INFO);
Expand All @@ -1143,8 +1143,8 @@ void explorerHandleRenameRequest(AsyncWebServerRequest *request) {
if (request->hasParam("srcpath") && request->hasParam("dstpath")) {
srcPath = request->getParam("srcpath");
dstPath = request->getParam("dstpath");
convertUtf8ToAscii(srcPath->value(), srcFullFilePath);
convertUtf8ToAscii(dstPath->value(), dstFullFilePath);
convertFilenameToAscii(srcPath->value(), srcFullFilePath);
convertFilenameToAscii(dstPath->value(), dstFullFilePath);
if (gFSystem.exists(srcFullFilePath)) {
if (gFSystem.rename(srcFullFilePath, dstFullFilePath)) {
snprintf(Log_Buffer, Log_BufferLength, "RENAME: %s renamed to %s", srcPath->value().c_str(), dstPath->value().c_str());
Expand Down Expand Up @@ -1175,7 +1175,7 @@ void explorerHandleAudioRequest(AsyncWebServerRequest *request) {
char filePath[MAX_FILEPATH_LENTGH];
if (request->hasParam("path") && request->hasParam("playmode")) {
param = request->getParam("path");
convertUtf8ToAscii(param->value(), filePath);
convertFilenameToAscii(param->value(), filePath);
param = request->getParam("playmode");
playModeString = param->value();

Expand Down
8 changes: 7 additions & 1 deletion src/settings.h
Expand Up @@ -191,7 +191,13 @@

// Where to store the backup-file for NVS-records
constexpr const char backupFile[] PROGMEM = "/backup.txt"; // File is written every time a (new) RFID-assignment via GUI is done
constexpr const char playlistCacheFile[] PROGMEM = "playlistcache.csv"; // Filename that is used for caching playlists
// Filename that is used for caching playlists
// Arduino 1.0.6 uses ANSI / Arduino >=2.0.5 UTF-8 encoding. Use different filenames to avoid incompabilities
#if ESP_ARDUINO_VERSION_MAJOR >= 2
constexpr const char playlistCacheFile[] PROGMEM = "playlistcache2.csv";
#else
constexpr const char playlistCacheFile[] PROGMEM = "playlistcache.csv";
#endif

//#################### Settings for optional Modules##############################
// (optinal) Neopixel
Expand Down

0 comments on commit 27afd7b

Please sign in to comment.