Tweak file manager upload

This commit is contained in:
Theo Arends 2021-01-08 18:32:36 +01:00
parent 1843b585a9
commit cdce47e92e

View File

@ -486,7 +486,6 @@ void UfsDirectory(void) {
WSContentSend_P(UFS_FORM_FILE_UPGb); WSContentSend_P(UFS_FORM_FILE_UPGb);
WSContentSpaceButton(BUTTON_CONFIGURATION); WSContentSpaceButton(BUTTON_CONFIGURATION);
WSContentStop(); WSContentStop();
Web.upload_error = 0;
} }
void UfsListDir(char *path, uint8_t depth) { void UfsListDir(char *path, uint8_t depth) {
@ -623,27 +622,48 @@ uint8_t UfsDownloadFile(char *file) {
} }
void UfsUpload(void) { void UfsUpload(void) {
bool _serialoutput = (LOG_LEVEL_DEBUG <= TasmotaGlobal.seriallog_level);
HTTPUpload& upload = Webserver->upload(); HTTPUpload& upload = Webserver->upload();
if (upload.status == UPLOAD_FILE_START) { if (upload.status == UPLOAD_FILE_START) {
Web.upload_error = 0;
char npath[48]; char npath[48];
sprintf(npath, "%s/%s", ufs_path, upload.filename.c_str()); 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); dfsp->remove(npath);
ufs_upload_file = dfsp->open(npath, UFS_FILE_WRITE); ufs_upload_file = dfsp->open(npath, UFS_FILE_WRITE);
if (!ufs_upload_file) { Web.upload_error = 1; } if (!ufs_upload_file) {
Web.upload_error = 1;
}
Web.upload_progress_dot_count = 0;
} }
else if (upload.status == UPLOAD_FILE_WRITE) { else if (upload.status == UPLOAD_FILE_WRITE) {
if (ufs_upload_file) { if (ufs_upload_file) {
ufs_upload_file.write(upload.buf, upload.currentSize); ufs_upload_file.write(upload.buf, upload.currentSize);
if (_serialoutput) {
Serial.printf(".");
Web.upload_progress_dot_count++;
if (!(Web.upload_progress_dot_count % 80)) { Serial.println(); }
}
} else {
Web.upload_error = 2;
} }
} }
else if (upload.status == UPLOAD_FILE_END) { else if (upload.status == UPLOAD_FILE_END) {
if (ufs_upload_file) { ufs_upload_file.close(); } if (_serialoutput && (Web.upload_progress_dot_count % 80)) {
if (Web.upload_error) { Serial.println();
AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload error")); }
if (ufs_upload_file) {
ufs_upload_file.close();
} else {
Web.upload_error = 3;
} }
} else { } else {
Web.upload_error = 1; Web.upload_error = 4;
WSSend(500, CT_PLAIN, F("500: couldn't create file")); WSSend(500, CT_PLAIN, F("500: Couldn't create file"));
}
if (Web.upload_error) {
AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: Upload error %d"), Web.upload_error);
} }
} }
@ -669,7 +689,7 @@ bool Xdrv50(uint8_t function) {
case FUNC_WEB_ADD_HANDLER: case FUNC_WEB_ADD_HANDLER:
Webserver->on("/ufsd", UfsDirectory); Webserver->on("/ufsd", UfsDirectory);
Webserver->on("/ufsu", HTTP_GET, 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);}, UfsUpload);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }