Add handler for SD

Fix quiet status on FS handler
This commit is contained in:
Luc 2019-10-26 09:26:13 +02:00
parent a733ed29e2
commit 5d018cfbf6
5 changed files with 383 additions and 6 deletions

View File

@ -0,0 +1,215 @@
/*
handle-SD-files.cpp - ESP3D http handle
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../../include/esp3d_config.h"
#if defined (HTTP_FEATURE) && defined(SD_DEVICE)
#include "../http_server.h"
#if defined (ARDUINO_ARCH_ESP32)
#include <WebServer.h>
#endif //ARDUINO_ARCH_ESP32
#if defined (ARDUINO_ARCH_ESP8266)
#include <ESP8266WebServer.h>
#endif //ARDUINO_ARCH_ESP8266
#include "../../filesystem/esp_sd.h"
#include "../../authentication/authentication_service.h"
//SD
//SD files list and file commands
void HTTP_Server::handleSDFileList ()
{
level_authenticate_type auth_level = AuthenticationService::authenticated_level();
if (auth_level == LEVEL_GUEST) {
_upload_status = UPLOAD_STATUS_NONE;
_webserver->send (401, "text/plain", "Wrong authentication!");
return;
}
String path ;
String status = "ok";
if ( (_upload_status == UPLOAD_STATUS_FAILED) || (_upload_status == UPLOAD_STATUS_CANCELLED) ) {
status = "Upload failed";
_upload_status = UPLOAD_STATUS_NONE;
}
if (ESP_SD::getState(true) != ESP_SDCARD_IDLE) {
_webserver->send (200, "text/plain", "{\"status\":\"no SD card\"}");
return;
}
if (_webserver->hasArg ("quiet")) {
if(_webserver->arg ("quiet") == "yes") {
Serial.println("quiet");
_webserver->send (200, "text/plain", "{\"status\":\"ok\"}");
return;
}
}
//get current path
if (_webserver->hasArg ("path") ) {
path += _webserver->arg ("path") ;
}
//to have a clean path
path.trim();
path.replace ("//", "/");
if (path[path.length() - 1] != '/') {
path += "/";
}
//check if query need some action
if (_webserver->hasArg ("action") ) {
//delete a file
if (_webserver->arg ("action") == "delete" && _webserver->hasArg ("filename") ) {
String filename;
String shortname = _webserver->arg ("filename");
shortname.replace ("/", "");
filename = path + _webserver->arg ("filename");
filename.replace ("//", "/");
if (!ESP_SD::exists (filename.c_str()) ) {
status = shortname + " does not exists!";
} else {
if (ESP_SD::remove (filename.c_str()) ) {
status = shortname + " deleted";
//what happen if no "/." and no other subfiles for SPIFFS like?
String ptmp = path;
if ( (path != "/") && (path[path.length() - 1] = '/') ) {
ptmp = path.substring (0, path.length() - 1);
}
if (!ESP_SD::exists (ptmp.c_str())) {
ESP_SD::mkdir(ptmp.c_str());
}
} else {
status = "Cannot deleted " ;
status += shortname ;
}
}
}
//delete a directory
if (_webserver->arg ("action") == "deletedir" && _webserver->hasArg ("filename") ) {
String filename;
String shortname = _webserver->arg ("filename");
shortname.replace ("/", "");
filename = path + _webserver->arg ("filename");
filename += "/";
filename.replace ("//", "/");
if (filename != "/") {
if (ESP_SD::rmdir(filename.c_str())) {
log_esp3d("Deleting %s",filename.c_str());
status = shortname ;
status += " deleted";
} else {
status = "Cannot deleted " ;
status += shortname ;
}
}
}
//create a directory
if (_webserver->arg ("action") == "createdir" && _webserver->hasArg ("filename") ) {
String filename;
filename = path + _webserver->arg ("filename") + "/.";
String shortname = _webserver->arg ("filename");
shortname.replace ("/", "");
filename.replace ("//", "/");
if (ESP_SD::exists (filename.c_str()) ) {
status = shortname + " already exists!";
} else {
if (!ESP_SD::mkdir(filename.c_str())) {
status = "Cannot create ";
status += shortname ;
} else {
status = shortname + " created";
}
}
}
}
String buffer2send ;
buffer2send.reserve(1200);
buffer2send = "{\"files\":[";
String ptmp = path;
if ( (path != "/") && (path[path.length() - 1] = '/') ) {
ptmp = path.substring (0, path.length() - 1);
}
_webserver->setContentLength(CONTENT_LENGTH_UNKNOWN);
_webserver->sendHeader("Content-Type","application/json");
_webserver->sendHeader("Cache-Control","no-cache");
_webserver->send(200);
if (ESP_SD::exists(ptmp.c_str())) {
ESP_SDFile f = ESP_SD::open(ptmp.c_str(), ESP_SD_FILE_READ);
//Parse files
ESP_SDFile sub = f.openNextFile();
if (f) {
bool needseparator = false;
while (sub) {
if (needseparator) {
buffer2send+=",";
} else {
//for next entry
needseparator=true;
}
buffer2send+="{\"name\":\"";
buffer2send+=sub.name();
buffer2send+="\",\"size\":\"";
if (sub.isDirectory()) {
buffer2send+="-1";
} else {
buffer2send+=ESP_SD::formatBytes(sub.size());
}
#ifdef FILESYSTEM_TIMESTAMP_FEATURE
buffer2send+="\",\"time\":\"";
time_t t = sub.getLastWrite();
struct tm * tmstruct = localtime(&t);
char str[20]; //buffer should be 20
sprintf(str,"%d-%02d-%02d %02d:%02d:%02d",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
buffer2send+=str;
#endif //FILESYSTEM_TIMESTAMP_FEATURE
buffer2send+="\"}";
if (buffer2send.length() > 1100) {
_webserver->sendContent_P(buffer2send.c_str(),buffer2send.length());
buffer2send = "";
}
sub.close();
sub = f.openNextFile();
}
f.close();
} else {
if (status == "ok") {
status = "cannot open" + ptmp;
} else {
status += ", cannot open" + ptmp;
}
}
} else {
if (status == "ok") {
status = ptmp + " does not exists!";
} else {
status += ", " + ptmp + " does not exists!";
}
}
buffer2send += "],\"path\":\"" + path + "\",";
if (ESP_SD::totalBytes()>0) {
buffer2send += "\"occupation\":\"" + String(100.0*ESP_SD::usedBytes()/ESP_SD::totalBytes()) + "\",";
} else {
status = "SD Error";
buffer2send += "\"occupation\":\"0\",";
}
buffer2send += "\"status\":\"" + status + "\",";
buffer2send += "\"total\":\"" + ESP_SD::formatBytes (ESP_SD::totalBytes()) + "\",";
buffer2send += "\"used\":\"" + ESP_SD::formatBytes (ESP_SD::usedBytes()) + "\"}";
path = "";
_webserver->sendContent_P(buffer2send.c_str(),buffer2send.length());
_webserver->sendContent("");
_upload_status = UPLOAD_STATUS_NONE;
}
#endif //HTTP_FEATURE && SD_DEVICE

View File

@ -47,7 +47,7 @@ void HTTP_Server::handleFSFileList ()
if (_webserver->hasArg ("quiet")) {
if(_webserver->arg ("quiet") == "yes") {
Serial.println("quiet");
_webserver->send (200, "text/plain", status.c_str());
_webserver->send (200, "text/plain", "{\"status\":\"ok\"}");
return;
}
}

View File

@ -0,0 +1,159 @@
/*
upload-SD-files.cpp - ESP3D http handle
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../../include/esp3d_config.h"
#if defined (HTTP_FEATURE) && defined(SD_DEVICE)
#include "../http_server.h"
#if defined (ARDUINO_ARCH_ESP32)
#include <WebServer.h>
#endif //ARDUINO_ARCH_ESP32
#if defined (ARDUINO_ARCH_ESP8266)
#include <ESP8266WebServer.h>
#endif //ARDUINO_ARCH_ESP8266
#include "../../filesystem/esp_sd.h"
#include "../../authentication/authentication_service.h"
//SD files uploader handle
void HTTP_Server::SDFileupload ()
{
//get authentication status
level_authenticate_type auth_level= AuthenticationService::authenticated_level();
static String filename;
static ESP_SDFile fsUploadFile;
static uint32_t timecheck;
//Guest cannot upload - only admin
if (auth_level == LEVEL_GUEST) {
pushError(ESP_ERROR_AUTHENTICATION, "Upload rejected", 401);
_upload_status=UPLOAD_STATUS_FAILED;
} else {
HTTPUpload& upload = _webserver->upload();
String upload_filename = upload.filename;
if ((_upload_status != UPLOAD_STATUS_FAILED) || (upload.status == UPLOAD_FILE_START)) {
//Upload start
if (upload.status == UPLOAD_FILE_START) {
timecheck= millis();
_upload_status = UPLOAD_STATUS_ONGOING;
if (upload_filename[0] != '/') {
filename = "/" + upload_filename;
} else {
filename = upload.filename;
}
//Sanity check
if (ESP_SD::exists (filename.c_str()) ) {
ESP_SD::remove (filename.c_str());
}
if (fsUploadFile.isOpen() ) {
fsUploadFile.close();
}
String sizeargname = upload.filename + "S";
//TODO add busy state and handle it for upload
if (ESP_SD::getState(true) != ESP_SDCARD_IDLE) {
_upload_status=UPLOAD_STATUS_FAILED;
}
if (_upload_status!=UPLOAD_STATUS_FAILED) {
if (_webserver->hasArg (sizeargname.c_str()) ) {
size_t freespace = ESP_SD::totalBytes() - ESP_SD::usedBytes();
size_t filesize = _webserver->arg (sizeargname.c_str()).toInt();
if (freespace < filesize ) {
_upload_status=UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_NOT_ENOUGH_SPACE, "Upload rejected");
}
}
}
if (_upload_status!=UPLOAD_STATUS_FAILED) {
//create file
fsUploadFile = ESP_SD::open(filename.c_str(), ESP_SD_FILE_WRITE);
//check If creation succeed
if (fsUploadFile) {
//if yes upload is started
_upload_status= UPLOAD_STATUS_ONGOING;
} else {
//if no set cancel flag
_upload_status=UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_FILE_CREATION, "File creation failed");
}
}
//Upload write
} else if(upload.status == UPLOAD_FILE_WRITE) {
//check if file is available and no error
if(fsUploadFile && _upload_status == UPLOAD_STATUS_ONGOING) {
//no error so write post date
if(upload.currentSize != fsUploadFile.write(upload.buf, upload.currentSize)) {
//we have a problem set flag UPLOAD_STATUS_FAILED
_upload_status=UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_FILE_WRITE, "File write failed");
}
} else {
//we have a problem set flag UPLOAD_STATUS_FAILED
_upload_status=UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_FILE_WRITE, "File write failed");
}
//Upload end
} else if(upload.status == UPLOAD_FILE_END) {
uint32_t filesize = 0;
//check if file is still open
if(fsUploadFile) {
//close it
fsUploadFile.close();
//check size
String sizeargname = upload.filename + "S";
//fsUploadFile = ESP_SD::open (filename, ESP_FILE_READ);
filesize = fsUploadFile.size();
_upload_status = UPLOAD_STATUS_SUCCESSFUL;
if (_webserver->hasArg (sizeargname.c_str()) ) {
if (_webserver->arg (sizeargname.c_str()) != String(filesize)) {
_upload_status = UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_SIZE, "File upload failed");
}
}
if (_upload_status == UPLOAD_STATUS_ONGOING) {
_upload_status = UPLOAD_STATUS_SUCCESSFUL;
}
} else {
//we have a problem set flag UPLOAD_STATUS_FAILED
_upload_status=UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_FILE_CLOSE, "File close failed");
}
Serial.print(filesize);
Serial.print(" B in ");
Serial.print((millis()-timecheck) / 1000);
Serial.println(" sec");
//Upload cancelled
} else {
if (_upload_status == UPLOAD_STATUS_ONGOING) {
_upload_status = UPLOAD_STATUS_FAILED;
}
}
}
}
if(_upload_status == UPLOAD_STATUS_FAILED) {
cancelUpload();
if(fsUploadFile) {
fsUploadFile.close();
}
if (auth_level != LEVEL_GUEST) {
if (ESP_SD::exists (filename.c_str())) {
ESP_SD::remove (filename.c_str());
}
}
}
}
#endif //HTTP_FEATURE && SD_DEVICE

View File

@ -62,6 +62,10 @@ void HTTP_Server::init_handlers()
//FileSystem
_webserver->on ("/files", HTTP_ANY, handleFSFileList, FSFileupload);
#endif //FILESYSTEM_FEATURE
#ifdef SD_DEVICE
//SD
_webserver->on ("/sdfiles", HTTP_ANY, handleSDFileList, SDFileupload);
#endif //SD_DEVICE
#ifdef WEB_UPDATE_FEATURE
//web update
_webserver->on ("/updatefw", HTTP_ANY, handleUpdate, WebUpdateUpload);

View File

@ -87,11 +87,10 @@ private:
static void WebUpdateUpload ();
#endif //WEB_UPDATE_FEATURE
//static bool is_realtime_cmd(char c);
#ifdef ENABLE_SD_CARD
//static void handle_direct_SDFileList();
//static void SDFile_direct_upload();
//static bool deleteRecursive(String path);
#endif
#ifdef SD_DEVICE
static void SDFileupload ();
static void handleSDFileList ();
#endif //SD_DEVICE
};
#endif //_HTTP_SERVER_H