Read normal file over .gz if both exist. Allow filesystem upload

This commit is contained in:
fvanroie 2021-05-02 04:38:42 +02:00
parent 517268426d
commit 326c099c46

View File

@ -891,33 +891,47 @@ void webHandleFirmwareUpload()
{ {
upload = &webServer.upload(); upload = &webServer.upload();
if(upload->status == UPLOAD_FILE_START) { switch(upload->status) {
if(!httpIsAuthenticated(F("update"))) return;
LOG_TRACE(TAG_HTTP, F("Update: %s"), upload->filename.c_str()); case UPLOAD_FILE_START: {
haspProgressMsg(upload->filename.c_str()); if(!httpIsAuthenticated(F("update"))) return;
// WiFiUDP::stopAll(); LOG_TRACE(TAG_HTTP, F("Update: %s"), upload->filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; haspProgressMsg(upload->filename.c_str());
// if(!Update.begin(UPDATE_SIZE_UNKNOWN)) { // start with max available size // WiFiUDP::stopAll();
if(!Update.begin(maxSketchSpace)) { // start with max available size
webUpdatePrintError(); 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) { case UPLOAD_FILE_WRITE: // flashing firmware to ESP
// flashing firmware to ESP if(Update.write(upload->buf, upload->currentSize) != upload->currentSize) {
if(Update.write(upload->buf, upload->currentSize) != upload->currentSize) { webUpdatePrintError();
webUpdatePrintError(); } else {
} else { webUploadProgress();
webUploadProgress(); }
} break;
} else if(upload->status == UPLOAD_FILE_END) { case UPLOAD_FILE_END:
haspProgressVal(100); haspProgressVal(100);
if(Update.end(true)) { // true to set the size to the current progress if(Update.end(true)) { // true to set the size to the current progress
haspProgressMsg(F(D_OTA_UPDATE_APPLY)); haspProgressMsg(F(D_OTA_UPDATE_APPLY));
webUpdateReboot(); webUpdateReboot();
} else { } else {
webUpdatePrintError(); webUpdatePrintError();
} }
break;
default:;
} }
} }
#endif #endif
@ -932,13 +946,9 @@ int handleFileRead(String path)
if(path.endsWith("/")) { if(path.endsWith("/")) {
path += F("index.htm"); path += F("index.htm");
} }
String pathWithGz = path + F(".gz"); String pathWithGz = path + F(".gz");
if(HASP_FS.exists(pathWithGz) || HASP_FS.exists(path)) { 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); String contentType((char*)0);
if(webServer.hasArg(F("download"))) if(webServer.hasArg(F("download")))
@ -946,26 +956,29 @@ int handleFileRead(String path)
else else
contentType = getContentType(path); 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())) { if(!strncasecmp(file.name(), configFile.c_str(), configFile.length())) {
file.close(); file.close();
DynamicJsonDocument settings(8 * 256); DynamicJsonDocument settings(8 * 256);
DeserializationError error = configParseFile(configFile, settings); DeserializationError error = configParseFile(configFile, settings);
if(!error) { if(error) return 500; // Internal Server 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);
webServer.setContentLength(len); configMaskPasswords(settings); // Output settings to the client with masked passwords!
webServer.send(200, contentType, buffer); char buffer[1024];
} else { size_t len = serializeJson(settings, buffer, sizeof(buffer));
return 500; // Internal Server error webServer.setContentLength(len);
} webServer.send(200, contentType, buffer);
} else { } else {
// Stream other files directly from filesystem
webServer.streamFile(file, contentType); webServer.streamFile(file, contentType);
file.close(); file.close();
} }
@ -1444,7 +1457,8 @@ void webHandleHttpConfig()
// httpMessage += F(D_PASSWORD_MASK); // httpMessage += F(D_PASSWORD_MASK);
// } // }
// httpMessage += // httpMessage +=
// F("'><p><button type='submit' name='save' value='http'>" D_HTTP_SAVE_SETTINGS "</button></p></form>"); // F("'><p><button type='submit' name='save' value='http'>" D_HTTP_SAVE_SETTINGS
// "</button></p></form>");
// httpMessage += PSTR("<p><form method='GET' action='/config'><button type='submit'>&#8617; " // httpMessage += PSTR("<p><form method='GET' action='/config'><button type='submit'>&#8617; "
// D_HTTP_CONFIGURATION // D_HTTP_CONFIGURATION
@ -1571,12 +1585,6 @@ void webHandleGpioConfig()
httpMessage += F("</td><td>"); httpMessage += F("</td><td>");
httpMessage += conf.group; httpMessage += conf.group;
httpMessage += F("</td><td>"); httpMessage += F("</td><td>");
// bool inverted = (conf.type == HASP_GPIO_BUTTON_INVERTED) ||
// (conf.type == HASP_GPIO_SWITCH_INVERTED) || (conf.type == HASP_GPIO_LED_INVERTED)
// || (conf.type == HASP_GPIO_RELAY_INVERTED) || (conf.type ==
// HASP_GPIO_PWM_INVERTED);
httpMessage += (conf.type & 0x1) ? F("High") : F("Low"); httpMessage += (conf.type & 0x1) ? F("High") : F("Low");
httpMessage += F("</td><td><a href='/config/gpio/options?id="); httpMessage += F("</td><td><a href='/config/gpio/options?id=");
@ -1826,10 +1834,10 @@ void webHandleHaspConfig()
httpMessage += getOption(7, F("Template"), themeid == 7); httpMessage += getOption(7, F("Template"), themeid == 7);
#endif #endif
httpMessage += F("</select></br>"); httpMessage += F("</select></br>");
httpMessage += httpMessage += F("<b>Hue</b><div style='width:100%;background-image:linear-gradient(to "
F("<b>Hue</b><div style='width:100%;background-image:linear-gradient(to " "right,red,orange,yellow,green,blue,indigo,violet);'><input "
"right,red,orange,yellow,green,blue,indigo,violet);'><input style='align:center;padding:0px;width:100%;' " "style='align:center;padding:0px;width:100%;' "
"name='hue' type='range' min='0' max='360' value='"); "name='hue' type='range' min='0' max='360' value='");
httpMessage += settings[FPSTR(FP_CONFIG_HUE)].as<String>(); httpMessage += settings[FPSTR(FP_CONFIG_HUE)].as<String>();
httpMessage += F("'></div></p>"); httpMessage += F("'></div></p>");
httpMessage += F("<p><b>Default Font</b><select id='font' name='font'><option value=''>None</option>"); httpMessage += F("<p><b>Default Font</b><select id='font' name='font'><option value=''>None</option>");
@ -1892,7 +1900,6 @@ void httpHandleNotFound()
{ // webServer 404 { // webServer 404
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
int statuscode = handleFileRead(webServer.uri()); int statuscode = handleFileRead(webServer.uri());
if(statuscode == 200) return;
#else #else
int statuscode = 404; int statuscode = 404;
#endif #endif
@ -1904,6 +1911,8 @@ void httpHandleNotFound()
// LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"), String(webServer.client().remoteIP()).c_str()); // LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"), String(webServer.client().remoteIP()).c_str());
#endif #endif
if(statuscode == 200) return; // OK
String httpMessage((char*)0); String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE); httpMessage.reserve(HTTP_PAGE_SIZE);
@ -1939,9 +1948,16 @@ void webHandleFirmware()
httpMessage += F("<p><form action='/update' method='POST' enctype='multipart/form-data'><input type='file' " httpMessage += F("<p><form action='/update' method='POST' enctype='multipart/form-data'><input type='file' "
"name='filename' accept='.bin'>"); "name='filename' accept='.bin'>");
httpMessage += F("<button type='submit'>" D_HTTP_UPDATE_FIRMWARE "</button></form></p>"); // httpMessage += F("<button type='submit'>" D_HTTP_UPDATE_FIRMWARE "</button></form></p>");
// httpMessage += F("<p><form action='/update' method='POST' enctype='multipart/form-data'><input type='file' " httpMessage += F("<input id='cmd' name='cmd' type='radio' value='0' checked>Firmware &nbsp; "
"<input id='cmd' name='cmd' type='radio' value='100'>Filesystem");
add_button(httpMessage, F(D_HTTP_UPDATE_FIRMWARE), F(""));
httpMessage += F("</form></p>");
// httpMessage += F("<p><form action='/update' method='POST' enctype='multipart/form-data'><input
// type='file' "
// "name='filename' accept='.spiffs'>"); // "name='filename' accept='.spiffs'>");
// httpMessage += F("<button type='submit'>Replace Filesystem Image</button></form></p>"); // httpMessage += F("<button type='submit'>Replace Filesystem Image</button></form></p>");
@ -2021,7 +2037,8 @@ void httpHandleResetConfig()
} else { } else {
httpMessage += httpMessage +=
F("<h2>Warning</h2><b>This process will reset all settings to the default values. The internal flash " F("<h2>Warning</h2><b>This process will reset all settings to the default values. The internal flash "
"will be erased and the device is restarted. You may need to connect to the WiFi AP displayed on the " "will be erased and the device is restarted. You may need to connect to the WiFi AP displayed on "
"the "
"panel to re-configure the device before accessing it again. ALL FILES WILL BE LOST!</b>" "panel to re-configure the device before accessing it again. ALL FILES WILL BE LOST!</b>"
"<br/><hr><br/><form method='GET' action='resetConfig'>"); "<br/><hr><br/><form method='GET' action='resetConfig'>");
@ -2119,7 +2136,7 @@ void httpSetup()
webServer.on(F("/list"), HTTP_GET, handleFileList); webServer.on(F("/list"), HTTP_GET, handleFileList);
// load editor // load editor
webServer.on(F("/edit"), HTTP_GET, []() { webServer.on(F("/edit"), HTTP_GET, []() {
if(!handleFileRead("/edit.htm")) { if(handleFileRead("/edit.htm") != 200) {
char mimetype[16]; char mimetype[16];
snprintf_P(mimetype, sizeof(mimetype), PSTR("text/plain")); snprintf_P(mimetype, sizeof(mimetype), PSTR("text/plain"));
webServer.send_P(404, mimetype, PSTR("FileNotFound")); webServer.send_P(404, mimetype, PSTR("FileNotFound"));