diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 244618b6..ba27e1a2 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -891,33 +891,47 @@ void webHandleFirmwareUpload() { upload = &webServer.upload(); - if(upload->status == UPLOAD_FILE_START) { - if(!httpIsAuthenticated(F("update"))) return; - LOG_TRACE(TAG_HTTP, F("Update: %s"), upload->filename.c_str()); - haspProgressMsg(upload->filename.c_str()); - // WiFiUDP::stopAll(); - uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; - // if(!Update.begin(UPDATE_SIZE_UNKNOWN)) { // start with max available size - if(!Update.begin(maxSketchSpace)) { // start with max available size - webUpdatePrintError(); + switch(upload->status) { + + case UPLOAD_FILE_START: { + if(!httpIsAuthenticated(F("update"))) return; + LOG_TRACE(TAG_HTTP, F("Update: %s"), upload->filename.c_str()); + haspProgressMsg(upload->filename.c_str()); + // WiFiUDP::stopAll(); + + int command = webServer.arg(F("cmd")).toInt(); + size_t size; + if(command == U_FLASH) { + size = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; + } else if(command == U_SPIFFS) { + size = UPDATE_SIZE_UNKNOWN; + } + // if(!Update.begin(UPDATE_SIZE_UNKNOWN)) { // start with max available size + if(!Update.begin(size, command, -1, 0U, "spiffs")) { // start with max available size + webUpdatePrintError(); + } + break; } - } else if(upload->status == UPLOAD_FILE_WRITE) { - // flashing firmware to ESP - if(Update.write(upload->buf, upload->currentSize) != upload->currentSize) { - webUpdatePrintError(); - } else { - webUploadProgress(); - } + case UPLOAD_FILE_WRITE: // flashing firmware to ESP + if(Update.write(upload->buf, upload->currentSize) != upload->currentSize) { + webUpdatePrintError(); + } else { + webUploadProgress(); + } + break; - } else if(upload->status == UPLOAD_FILE_END) { - haspProgressVal(100); - if(Update.end(true)) { // true to set the size to the current progress - haspProgressMsg(F(D_OTA_UPDATE_APPLY)); - webUpdateReboot(); - } else { - webUpdatePrintError(); - } + case UPLOAD_FILE_END: + haspProgressVal(100); + if(Update.end(true)) { // true to set the size to the current progress + haspProgressMsg(F(D_OTA_UPDATE_APPLY)); + webUpdateReboot(); + } else { + webUpdatePrintError(); + } + break; + + default:; } } #endif @@ -932,13 +946,9 @@ int handleFileRead(String path) if(path.endsWith("/")) { path += F("index.htm"); } + String pathWithGz = path + F(".gz"); if(HASP_FS.exists(pathWithGz) || HASP_FS.exists(path)) { - if(HASP_FS.exists(pathWithGz)) path += F(".gz"); - File file = HASP_FS.open(path, "r"); - - String configFile((char*)0); - configFile = String(FPSTR(FP_HASP_CONFIG_FILE)); String contentType((char*)0); if(webServer.hasArg(F("download"))) @@ -946,26 +956,29 @@ int handleFileRead(String path) else contentType = getContentType(path); + if(!HASP_FS.exists(path) && HASP_FS.exists(pathWithGz)) + path = pathWithGz; // Only use .gz if normal file doesn't exist + File file = HASP_FS.open(path, "r"); + + String configFile((char*)0); // Verify if the file is config.json + configFile = String(FPSTR(FP_HASP_CONFIG_FILE)); + if(!strncasecmp(file.name(), configFile.c_str(), configFile.length())) { file.close(); DynamicJsonDocument settings(8 * 256); DeserializationError error = configParseFile(configFile, settings); - if(!error) { - // LOG_TRACE(TAG_CONF, F(D_FILE_LOADING), configFile.c_str()); - configMaskPasswords(settings); // Output settings in log with masked passwords - char buffer[1024]; - size_t len = serializeJson(settings, buffer, sizeof(buffer)); - // LOG_VERBOSE(TAG_CONF, buffer); + if(error) return 500; // Internal Server Error - webServer.setContentLength(len); - webServer.send(200, contentType, buffer); - } else { - return 500; // Internal Server error - } + configMaskPasswords(settings); // Output settings to the client with masked passwords! + char buffer[1024]; + size_t len = serializeJson(settings, buffer, sizeof(buffer)); + webServer.setContentLength(len); + webServer.send(200, contentType, buffer); } else { + // Stream other files directly from filesystem webServer.streamFile(file, contentType); file.close(); } @@ -1444,7 +1457,8 @@ void webHandleHttpConfig() // httpMessage += F(D_PASSWORD_MASK); // } // httpMessage += - // F("'>
"); + // F("'>"); // httpMessage += PSTR("