From a13e83e1e69666b19946ec9e3285af65d04aee82 Mon Sep 17 00:00:00 2001 From: Benjamin Nestler <101095581+benjaminnestler@users.noreply.github.com> Date: Fri, 12 Jan 2024 10:08:05 +0100 Subject: [PATCH] FIX: Reset the 'upload_error' variable after web file upload error (#20340) * FIX: Reset the 'upload_error' variable after signaling the error to enable the next upload action. * ADD: Introduce HandleUploadUFSDone() to display information messages for uploaded files in UFS and handle errors. ADD: Include '?fsz=' web-argument (filesize) in the upload button click function. --- .../xdrv_01_9_webserver.ino | 22 +++++++--- .../xdrv_50_filesystem.ino | 43 +++++++++++++++++-- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino b/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino index b87aec505..69bdaddb2 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino @@ -2777,7 +2777,10 @@ void HandleUploadLoop(void) { if (UPLOAD_FILE_START == upload.status) { Web.upload_error = 0; upload_error_signalled = false; - upload_size = 0; + char tmp[16]; + + WebGetArg("fsz", tmp, sizeof(tmp)); // filesize + upload_size = (!strlen(tmp)) ? 0 : atoi(tmp); UploadServices(0); @@ -2785,18 +2788,25 @@ void HandleUploadLoop(void) { Web.upload_error = 1; // No file selected return; } - SettingsSave(1); // Free flash for upload - AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UPLOAD D_FILE " %s"), upload.filename.c_str()); + AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_UPLOAD D_FILE " %s (%d bytes)"), upload.filename.c_str(), upload_size); #ifdef USE_UFILESYS if (UPL_UFSFILE == Web.upload_file_type) { + const uint32_t freeBytes = (UfsFree() * 1024); + if (upload_size > freeBytes) { + Web.upload_error = 9; // File too large + return; + } + if (!UfsUploadFileOpen(upload.filename.c_str())) { - Web.upload_error = 2; + Web.upload_error = 2; // Not enough space return; } } #endif // USE_UFILESYS + + SettingsSave(1); // Free flash for upload } // ***** Step2: Write upload file @@ -2890,7 +2900,7 @@ void HandleUploadLoop(void) { Web.config_block_count++; } #ifdef USE_UFILESYS - else if (UPL_UFSFILE == Web.upload_file_type) { + else if (!Web.upload_error && UPL_UFSFILE == Web.upload_file_type) { if (!UfsUploadFileWrite(upload.buf, upload.currentSize)) { Web.upload_error = 9; // File too large return; @@ -2925,7 +2935,7 @@ void HandleUploadLoop(void) { } } #ifdef USE_UFILESYS - else if (UPL_UFSFILE == Web.upload_file_type) { + else if (!Web.upload_error && UPL_UFSFILE == Web.upload_file_type) { UfsUploadFileClose(); } #endif // USE_UFILESYS diff --git a/tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino b/tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino index f577d5904..df3dd7260 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino @@ -803,9 +803,11 @@ const char UFS_FORM_FILE_UPGc2[] PROGMEM = ""; const char UFS_FORM_FILE_UPG[] PROGMEM = - "
" + "" "

" - "
" + "
" "

"; const char UFS_FORM_SDC_DIRa[] PROGMEM = "
"; @@ -858,6 +860,40 @@ const char HTTP_EDITOR_FORM_END[] PROGMEM = #endif // #ifdef GUI_EDIT_FILE +void HandleUploadUFSDone(void) { + if (!HttpCheckPriviledgedAccess()) { return; } + + HTTPUpload& upload = Webserver->upload(); + + AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_UPLOAD_DONE)); + + WifiConfigCounter(); + UploadServices(1); + + WSContentStart_P(PSTR(D_INFORMATION)); + + WSContentSendStyle(); + WSContentSend_P(PSTR("
" D_UPLOAD " " D_FAILED "

"), WebColor(COL_TEXT_WARNING)); + char error[100]; + if (Web.upload_error < 10) { + GetTextIndexed(error, sizeof(error), Web.upload_error -1, kUploadErrors); + } else { + snprintf_P(error, sizeof(error), PSTR(D_UPLOAD_ERROR_CODE " %d"), Web.upload_error); + } + WSContentSend_P(error); + Web.upload_error = 0; + } else { + WSContentSend_P(PSTR("%06x'>" D_SUCCESSFUL "
"), WebColor(COL_TEXT_SUCCESS)); + } + WSContentSend_P(PSTR("

")); + + XdrvCall(FUNC_WEB_ADD_MANAGEMENT_BUTTON); + + WSContentStop(); +} + void UfsDirectory(void) { if (!HttpCheckPriviledgedAccess()) { return; } @@ -1457,7 +1493,8 @@ bool Xdrv50(uint32_t function) { // Webserver->on(F("/ufsu"), HTTP_POST,[](){Webserver->sendHeader(F("Location"),F("/ufsu"));Webserver->send(303);}, HandleUploadLoop); Webserver->on("/ufsd", UfsDirectory); Webserver->on("/ufsu", HTTP_GET, UfsDirectory); - Webserver->on("/ufsu", HTTP_POST,[](){Webserver->sendHeader(F("Location"),F("/ufsu"));Webserver->send(303);}, HandleUploadLoop); + //Webserver->on("/ufsu", HTTP_POST,[](){Webserver->sendHeader(F("Location"),F("/ufsu"));Webserver->send(303);}, HandleUploadLoop); + Webserver->on("/ufsu", HTTP_POST, HandleUploadUFSDone, HandleUploadLoop); #ifdef GUI_EDIT_FILE Webserver->on("/ufse", HTTP_GET, UfsEditor); Webserver->on("/ufse", HTTP_POST, UfsEditorUpload);