From 9caddc80fc537f96b9c2fe479b5f6cd4c49a5b2a Mon Sep 17 00:00:00 2001 From: grch101 Date: Tue, 2 Feb 2021 21:32:13 +0100 Subject: [PATCH] Filebrowser: Move to CP437 encoding support --- html/management.html | 10 ++-- platformio.ini | 2 +- src/HTMLmanagement.h | 11 ++-- src/main.cpp | 134 ++++++++++++++++++++++++++++--------------- 4 files changed, 98 insertions(+), 59 deletions(-) diff --git a/html/management.html b/html/management.html index 26660be..4dd34f7 100644 --- a/html/management.html +++ b/html/management.html @@ -156,7 +156,7 @@
-
+
@@ -485,9 +485,9 @@ function doRest(path, callback, obj) { obj.url = path; obj.dataType = "json"; - obj.contentType= "application/json;charset=iso-8859-1", - obj.scriptCharset= "iso-8859-1", - obj.success = function(data, textStatus, jqXHR) { + obj.contentType= "application/json;charset=IBM437", + obj.scriptCharset= "IBM437", + obj.success = function(data, textStatus, jqXHR) { if (callback) { callback(data); } @@ -771,7 +771,7 @@ /* Delete */ items.delete = { - label: "Loeschen", + label: "Löschen", action: function(x) { handleDeleteData(nodeId); refreshNode(ref.get_parent(nodeId)); diff --git a/platformio.ini b/platformio.ini index 14a44cb..f2d4545 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,7 @@ lib_deps_builtin = SPI Wire lib_deps_external = - https://github.com/schreibfaul1/ESP32-audioI2S.git#a816a19 ; currently not master-version as commit a816a19 not compatible with latest stable ESP32-Arduino + https://github.com/grch87/ESP32-audioI2S.git https://github.com/madhephaestus/ESP32Encoder.git https://github.com/knolleary/pubsubclient.git https://github.com/biologist79/ESP32FTPServer diff --git a/src/HTMLmanagement.h b/src/HTMLmanagement.h index 03e505e..9e1ee51 100644 --- a/src/HTMLmanagement.h +++ b/src/HTMLmanagement.h @@ -156,11 +156,10 @@ static const char management_HTML[] PROGMEM = "\
\
\
\ -
\ + \ \ \
\ -\
\ \ \ @@ -485,9 +484,9 @@ static const char management_HTML[] PROGMEM = "\ function doRest(path, callback, obj) {\ obj.url = path;\ obj.dataType = \"json\";\ - obj.contentType= \"application/json;charset=iso-8859-1\",\ - obj.scriptCharset= \"iso-8859-1\",\ - obj.success = function(data, textStatus, jqXHR) {\ + obj.contentType= \"application/json;charset=IBM437\",\ + obj.scriptCharset= \"IBM437\",\ + obj.success = function(data, textStatus, jqXHR) {\ if (callback) {\ callback(data);\ }\ @@ -771,7 +770,7 @@ static const char management_HTML[] PROGMEM = "\ \ /* Delete */\ items.delete = {\ - label: \"Loeschen\",\ + label: \"Löschen\",\ action: function(x) {\ handleDeleteData(nodeId);\ refreshNode(ref.get_parent(nodeId));\ diff --git a/src/main.cpp b/src/main.cpp index f0b9aca..83ce44c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -362,6 +362,7 @@ void freeMultiCharArray(char **arr, const uint32_t cnt); uint8_t getRepeatMode(void); bool getWifiEnableStatusFromNVS(void); void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final); +void convertAsciiToUtf8(String asciiString, char *utf8String); void convertUtf8ToAscii(String utf8String, char *asciiString); void explorerHandleFileUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final); void explorerHandleFileStorageTask(void *parameter); @@ -3593,25 +3594,62 @@ void webserverStart(void) { return true; } -// Conversion routine for http UTF-8 strings +// Conversion routine +void convertAsciiToUtf8(String asciiString, char *utf8String) { + + int k=0; + + for (int i=0; i 128 && asciiString[j] < 189 && f_C3_seen == true) { - asciiString[j] = asciiString[j] + 64; // found a related ASCII sign - f_C3_seen = false; + int k=0; + bool f_C3_seen = false; + + for (int i=0; ihasParam("path")) { AsyncWebParameter *param = request->getParam("path"); utf8FilePath = param->value() + "/" + filename; @@ -3632,7 +3670,11 @@ void explorerHandleFileUpload(AsyncWebServerRequest *request, String filename, s } else { utf8FilePath = "/" + filename; } - convertUtf8ToAscii(utf8FilePath, asciiFilePath); + + convertUtf8ToAscii(utf8FilePath, filePath); + + snprintf(logBuf, serialLoglength, "%s: %s", (char *) FPSTR(writingFile), utf8FilePath.c_str()); + loggerNl(serialDebug, logBuf, LOGLEVEL_INFO); // Create Ringbuffer for upload if(explorerFileUploadRingBuffer == NULL) { @@ -3649,7 +3691,7 @@ void explorerHandleFileUpload(AsyncWebServerRequest *request, String filename, s explorerHandleFileStorageTask, /* Function to implement the task */ "fileStorageTask", /* Name of the task */ 4000, /* Stack size in words */ - asciiFilePath, /* Task input parameter */ + filePath, /* Task input parameter */ 2 | portPRIVILEGE_BIT, /* Priority of the task */ &fileStorageTaskHandle /* Task handle. */ ); @@ -3685,9 +3727,6 @@ void explorerHandleFileStorageTask(void *parameter) { uploadFile = FSystem.open((char *)parameter, "w"); - snprintf(logBuf, serialLoglength, "%s: %s", (char *) FPSTR(writingFile), (char *) parameter); - loggerNl(serialDebug, logBuf, LOGLEVEL_INFO); - for(;;) { esp_task_wdt_reset(); @@ -3718,13 +3757,13 @@ void explorerHandleListRequest(AsyncWebServerRequest *request) { //StaticJsonDocument<4096> jsonBuffer; String serializedJsonString; AsyncWebParameter *param; - char asciiFilePath[256]; + char filePath[256]; JsonArray obj = jsonBuffer.createNestedArray(); File root; if(request->hasParam("path")){ param = request->getParam("path"); - convertUtf8ToAscii(param->value(), asciiFilePath); - root = FSystem.open(asciiFilePath); + convertUtf8ToAscii(param->value(), filePath); + root = FSystem.open(filePath); } else { root = FSystem.open("/"); } @@ -3745,7 +3784,8 @@ void explorerHandleListRequest(AsyncWebServerRequest *request) { while(file) { JsonObject entry = obj.createNestedObject(); - std::string path = file.name(); + convertAsciiToUtf8(file.name(), filePath); + std::string path = filePath; std::string fileName = path.substr(path.find_last_of("/") + 1); entry["name"] = fileName; @@ -3758,7 +3798,7 @@ void explorerHandleListRequest(AsyncWebServerRequest *request) { } serializeJson(obj, serializedJsonString); - request->send(200, "application/json; charset=iso-8859-1", serializedJsonString); + request->send(200, "application/json; charset=utf-8", serializedJsonString); } bool explorerDeleteDirectory(File dir) { @@ -3786,31 +3826,31 @@ bool explorerDeleteDirectory(File dir) { void explorerHandleDeleteRequest(AsyncWebServerRequest *request) { File file; AsyncWebParameter *param; - char asciiFilePath[256]; + char filePath[256]; if(request->hasParam("path")){ param = request->getParam("path"); - convertUtf8ToAscii(param->value(), asciiFilePath); - if(FSystem.exists(asciiFilePath)) { - file = FSystem.open(asciiFilePath); + convertUtf8ToAscii(param->value(), filePath); + if(FSystem.exists(filePath)) { + file = FSystem.open(filePath); if(file.isDirectory()) { if(explorerDeleteDirectory(file)) { - snprintf(logBuf, serialLoglength, "DELETE: %s deleted", asciiFilePath); + snprintf(logBuf, serialLoglength, "DELETE: %s deleted", param->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_INFO); } else { - snprintf(logBuf, serialLoglength, "DELETE: Cannot delete %s", asciiFilePath); + snprintf(logBuf, serialLoglength, "DELETE: Cannot delete %s", param->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_ERROR); } } else { - if(FSystem.remove(asciiFilePath)) { - snprintf(logBuf, serialLoglength, "DELETE: %s deleted", asciiFilePath); + if(FSystem.remove(filePath)) { + snprintf(logBuf, serialLoglength, "DELETE: %s deleted", param->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_INFO); } else { - snprintf(logBuf, serialLoglength, "DELETE: Cannot delete %s", asciiFilePath); + snprintf(logBuf, serialLoglength, "DELETE: Cannot delete %s", param->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_ERROR); } } } else { - snprintf(logBuf, serialLoglength, "DELETE: Path %s does not exist", asciiFilePath); + snprintf(logBuf, serialLoglength, "DELETE: Path %s does not exist", param->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_ERROR); } } else { @@ -3824,15 +3864,15 @@ void explorerHandleDeleteRequest(AsyncWebServerRequest *request) { // requires a GET parameter path to the new directory void explorerHandleCreateRequest(AsyncWebServerRequest *request) { AsyncWebParameter *param; - char asciiFilePath[256]; + char filePath[256]; if(request->hasParam("path")){ param = request->getParam("path"); - convertUtf8ToAscii(param->value(), asciiFilePath); - if(FSystem.mkdir(asciiFilePath)) { - snprintf(logBuf, serialLoglength, "CREATE: %s created", asciiFilePath); + convertUtf8ToAscii(param->value(), filePath); + if(FSystem.mkdir(filePath)) { + snprintf(logBuf, serialLoglength, "CREATE: %s created", param->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_INFO); } else { - snprintf(logBuf, serialLoglength, "CREATE: Cannot create %s", asciiFilePath); + snprintf(logBuf, serialLoglength, "CREATE: Cannot create %s", param->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_ERROR); } } else { @@ -3856,14 +3896,14 @@ void explorerHandleRenameRequest(AsyncWebServerRequest *request) { convertUtf8ToAscii(dstPath->value(), dstFullFilePath); if(FSystem.exists(srcFullFilePath)) { if(FSystem.rename(srcFullFilePath, dstFullFilePath)) { - snprintf(logBuf, serialLoglength, "RENAME: %s renamed to %s", srcFullFilePath, dstFullFilePath); + snprintf(logBuf, serialLoglength, "RENAME: %s renamed to %s", srcPath->value().c_str(), dstPath->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_INFO); } else { - snprintf(logBuf, serialLoglength, "RENAME: Cannot rename %s", srcFullFilePath); + snprintf(logBuf, serialLoglength, "RENAME: Cannot rename %s", srcPath->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_ERROR); } } else { - snprintf(logBuf, serialLoglength, "RENAME: Path %s does not exist", srcFullFilePath); + snprintf(logBuf, serialLoglength, "RENAME: Path %s does not exist", srcPath->value().c_str()); loggerNl(serialDebug, logBuf, LOGLEVEL_ERROR); } } else { @@ -3880,15 +3920,15 @@ void explorerHandleAudioRequest(AsyncWebServerRequest *request) { AsyncWebParameter *param; String playModeString; uint32_t playMode; - char asciiFilePath[256]; + char filePath[256]; if(request->hasParam("path") && request->hasParam("playmode")) { param = request->getParam("path"); - convertUtf8ToAscii(param->value(), asciiFilePath); + convertUtf8ToAscii(param->value(), filePath); param = request->getParam("playmode"); playModeString = param->value(); playMode = atoi(playModeString.c_str()); - trackQueueDispatcher(asciiFilePath,0,playMode,0); + trackQueueDispatcher(filePath,0,playMode,0); } else { loggerNl(serialDebug, "AUDIO: No path variable set", LOGLEVEL_ERROR); }