Filemanager uses standard upload process

This commit is contained in:
Theo Arends 2021-01-09 18:19:15 +01:00
parent 6a6454d8ab
commit 30d52acce0
2 changed files with 56 additions and 58 deletions

View File

@ -2337,7 +2337,7 @@ void HandleUploadDone(void) {
void HandleUploadLoop(void) {
// Based on ESP8266HTTPUpdateServer.cpp uses ESP8266WebServer Parsing.cpp and Cores Updater.cpp (Update)
static uint32_t upload_size = 0;
static uint32_t upload_size;
bool _serialoutput = (LOG_LEVEL_DEBUG <= TasmotaGlobal.seriallog_level);
if (HTTP_USER == Web.state) { return; }
@ -2351,6 +2351,7 @@ void HandleUploadLoop(void) {
// ***** Step1: Start upload file
if (UPLOAD_FILE_START == upload.status) {
Web.upload_error = 0;
upload_size = 0;
TasmotaGlobal.restart_flag = 60;
if (0 == upload.filename.c_str()[0]) {
Web.upload_error = 1; // No file selected
@ -2365,7 +2366,17 @@ void HandleUploadLoop(void) {
Web.upload_error = 2; // Not enough space
return;
}
} else {
}
#ifdef USE_UFILESYS
else if (UPL_UFSFILE == Web.upload_file_type) {
if (!UfsUploadFileOpen(upload.filename.c_str())) {
Web.upload_error = 2;
return;
}
TasmotaGlobal.restart_flag = 0;
}
#endif // USE_UFILESYS
else {
MqttRetryCounter(60);
#ifdef USE_COUNTER
CounterInterruptDisable(true); // Prevent OTA failures on 100Hz counter interrupts
@ -2425,11 +2436,11 @@ void HandleUploadLoop(void) {
}
#endif // USE_ZIGBEE_EZSP
#endif // USE_WEB_FW_UPGRADE
else if ((upload.buf[0] != 0xE9) && (upload.buf[0] != 0x1F)) { // 0x1F is gzipped 0xE9
Web.upload_error = 3; // Invalid file signature - Magic byte is not 0xE9
return;
}
if (UPL_TASMOTA == Web.upload_file_type) {
else if (UPL_TASMOTA == Web.upload_file_type) {
if ((upload.buf[0] != 0xE9) && (upload.buf[0] != 0x1F)) { // 0x1F is gzipped 0xE9
Web.upload_error = 3; // Invalid file signature - Magic byte is not 0xE9
return;
}
if (0xE9 == upload.buf[0]) {
uint32_t bin_flash_size = ESP.magicFlashChipSize((upload.buf[3] & 0xf0) >> 4);
if (bin_flash_size > ESP.getFlashChipRealSize()) {
@ -2455,6 +2466,14 @@ void HandleUploadLoop(void) {
memcpy(settings_buffer + (Web.config_block_count * HTTP_UPLOAD_BUFLEN), upload.buf, upload.currentSize);
Web.config_block_count++;
}
#ifdef USE_UFILESYS
else if (UPL_UFSFILE == Web.upload_file_type) {
if (!UfsUploadFileWrite(upload.buf, upload.currentSize)) {
Web.upload_error = 9; // File too large
return;
}
}
#endif // USE_UFILESYS
#ifdef USE_WEB_FW_UPGRADE
else if (BUpload.active) {
// Write a block
@ -2523,6 +2542,11 @@ void HandleUploadLoop(void) {
return;
}
}
#ifdef USE_UFILESYS
else if (UPL_UFSFILE == Web.upload_file_type) {
UfsUploadFileClose();
}
#endif // USE_UFILESYS
#ifdef USE_WEB_FW_UPGRADE
else if (BUpload.active) {
// Done writing the data to SPI flash
@ -2588,7 +2612,7 @@ void HandleUploadLoop(void) {
if (UPL_TASMOTA == Web.upload_file_type) { Update.end(); }
}
delay(0);
OsWatchLoop(); // Feed OsWatch timer to prevent restart
OsWatchLoop(); // Feed OsWatch timer to prevent restart on long uploads
}
/*-------------------------------------------------------------------------------------------*/

View File

@ -422,7 +422,7 @@ const char UFS_FORM_FILE_UPGc1[] PROGMEM =
" &nbsp;&nbsp;<a href='http://%s/ufsd?dir=%d'>%s</a>";
const char UFS_FORM_FILE_UPGc2[] PROGMEM =
"</div>";
"</div>";
const char UFS_FORM_FILE_UPG[] PROGMEM =
"<form method='post' action='ufsu' enctype='multipart/form-data'>"
@ -451,10 +451,13 @@ const char UFS_FORM_SDC_HREFdel[] PROGMEM =
void UfsDirectory(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_MANAGE_FILE_SYSTEM));
uint8_t depth = 0;
strcpy(ufs_path, "/");
if (!HttpCheckPriviledgedAccess()) { return; }
if (Webserver->hasArg("download")) {
String stmp = Webserver->arg("download");
@ -509,6 +512,8 @@ void UfsDirectory(void) {
WSContentSend_P(UFS_FORM_FILE_UPGb);
WSContentSpaceButton(BUTTON_CONFIGURATION);
WSContentStop();
Web.upload_file_type = UPL_UFSFILE;
}
void UfsListDir(char *path, uint8_t depth) {
@ -651,56 +656,25 @@ uint8_t UfsDownloadFile(char *file) {
return 0;
}
void UfsUpload(void) {
static uint32_t upload_size = 0;
bool _serialoutput = (LOG_LEVEL_DEBUG <= TasmotaGlobal.seriallog_level);
bool UfsUploadFileOpen(const char* upload_filename) {
char npath[48];
snprintf_P(npath, sizeof(npath), PSTR("%s/%s"), ufs_path, upload_filename);
dfsp->remove(npath);
ufs_upload_file = dfsp->open(npath, UFS_FILE_WRITE);
return (ufs_upload_file);
}
HTTPUpload& upload = Webserver->upload();
if (upload.status == UPLOAD_FILE_START) {
Web.upload_error = 0;
char npath[48];
snprintf_P(npath, sizeof(npath), PSTR("%s/%s"), ufs_path, upload.filename.c_str());
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_UPLOAD D_FILE " %s ..."), npath);
dfsp->remove(npath);
ufs_upload_file = dfsp->open(npath, UFS_FILE_WRITE);
if (!ufs_upload_file) {
Web.upload_error = 1;
}
Web.upload_progress_dot_count = 0;
}
else if (upload.status == UPLOAD_FILE_WRITE) {
if (ufs_upload_file) {
ufs_upload_file.write(upload.buf, upload.currentSize);
if (_serialoutput) {
upload_size += upload.currentSize;
Serial.printf(".");
Web.upload_progress_dot_count++;
if (!(Web.upload_progress_dot_count % 50)) { // Assuming core HTTP_UPLOAD_BUFLEN=2048
Serial.printf("%5dkB\n", upload_size / 1024);
}
}
} else {
Web.upload_error = 2;
}
}
else if (upload.status == UPLOAD_FILE_END) {
if (_serialoutput && (Web.upload_progress_dot_count % 50)) {
Serial.printf("%5dkB\n", upload_size / 1024);
}
if (ufs_upload_file) {
ufs_upload_file.close();
} else {
Web.upload_error = 3;
}
bool UfsUploadFileWrite(uint8_t *upload_buf, size_t current_size) {
if (ufs_upload_file) {
ufs_upload_file.write(upload_buf, current_size);
} else {
Web.upload_error = 4;
WSSend(500, CT_PLAIN, F("500: Couldn't create file"));
return false;
}
if (Web.upload_error) {
AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: Upload error %d"), Web.upload_error);
}
delay(0);
OsWatchLoop(); // Feed OsWatch timer to prevent restart
return true;
}
void UfsUploadFileClose(void) {
ufs_upload_file.close();
}
#endif // USE_WEBSERVER
@ -730,7 +704,7 @@ bool Xdrv50(uint8_t function) {
case FUNC_WEB_ADD_HANDLER:
Webserver->on("/ufsd", UfsDirectory);
Webserver->on("/ufsu", HTTP_GET, UfsDirectory);
Webserver->on("/ufsu", HTTP_POST,[](){Webserver->sendHeader("Location","/ufsu");Webserver->send(303);}, UfsUpload);
Webserver->on("/ufsu", HTTP_POST,[](){Webserver->sendHeader("Location","/ufsu");Webserver->send(303);}, HandleUploadLoop);
break;
#endif // USE_WEBSERVER
}