20 #include "../../../include/esp3d_config.h"
21 #if defined (HTTP_FEATURE) && defined(SD_DEVICE)
22 #include "../http_server.h"
23 #if defined (ARDUINO_ARCH_ESP32)
24 #include <WebServer.h>
25 #endif //ARDUINO_ARCH_ESP32
26 #if defined (ARDUINO_ARCH_ESP8266)
27 #include <ESP8266WebServer.h>
28 #endif //ARDUINO_ARCH_ESP8266
29 #include "../../filesystem/esp_sd.h"
30 #include "../../authentication/authentication_service.h"
33 void HTTP_Server::handleSDFileList ()
38 _webserver->send (401,
"text/plain",
"Wrong authentication!");
44 status =
"Upload failed";
48 _webserver->send (200,
"text/plain",
"{\"status\":\"no SD card\"}");
51 if (_webserver->hasArg (
"quiet")) {
52 if(_webserver->arg (
"quiet") ==
"yes") {
53 Serial.println(
"quiet");
54 _webserver->send (200,
"text/plain",
"{\"status\":\"ok\"}");
60 if (_webserver->hasArg (
"path") ) {
61 path += _webserver->arg (
"path") ;
65 path.replace (
"//",
"/");
66 if (path[path.length() - 1] !=
'/') {
70 if (_webserver->hasArg (
"action") ) {
72 if (_webserver->arg (
"action") ==
"delete" && _webserver->hasArg (
"filename") ) {
74 String shortname = _webserver->arg (
"filename");
75 shortname.replace (
"/",
"");
76 filename = path + _webserver->arg (
"filename");
77 filename.replace (
"//",
"/");
79 status = shortname +
" does not exists!";
82 status = shortname +
" deleted";
85 if ( (path !=
"/") && (path[path.length() - 1] =
'/') ) {
86 ptmp = path.substring (0, path.length() - 1);
92 status =
"Cannot deleted " ;
98 if (_webserver->arg (
"action") ==
"deletedir" && _webserver->hasArg (
"filename") ) {
100 String shortname = _webserver->arg (
"filename");
101 shortname.replace (
"/",
"");
102 filename = path + _webserver->arg (
"filename");
104 filename.replace (
"//",
"/");
105 if (filename !=
"/") {
107 log_esp3d(
"Deleting %s",filename.c_str());
109 status +=
" deleted";
111 status =
"Cannot deleted " ;
112 status += shortname ;
117 if (_webserver->arg (
"action") ==
"createdir" && _webserver->hasArg (
"filename") ) {
119 filename = path + _webserver->arg (
"filename") +
"/.";
120 String shortname = _webserver->arg (
"filename");
121 shortname.replace (
"/",
"");
122 filename.replace (
"//",
"/");
124 status = shortname +
" already exists!";
127 status =
"Cannot create ";
128 status += shortname ;
130 status = shortname +
" created";
136 buffer2send.reserve(1200);
137 buffer2send =
"{\"files\":[";
139 if ( (path !=
"/") && (path[path.length() - 1] =
'/') ) {
140 ptmp = path.substring (0, path.length() - 1);
142 _webserver->setContentLength(CONTENT_LENGTH_UNKNOWN);
143 _webserver->sendHeader(
"Content-Type",
"application/json");
144 _webserver->sendHeader(
"Cache-Control",
"no-cache");
145 _webserver->send(200);
151 bool needseparator =
false;
159 buffer2send+=
"{\"name\":\"";
160 buffer2send+=sub.
name();
161 buffer2send+=
"\",\"size\":\"";
167 #ifdef FILESYSTEM_TIMESTAMP_FEATURE
168 buffer2send+=
"\",\"time\":\"";
170 struct tm * tmstruct = localtime(&t);
172 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);
174 #endif //FILESYSTEM_TIMESTAMP_FEATURE
176 if (buffer2send.length() > 1100) {
177 _webserver->sendContent_P(buffer2send.c_str(),buffer2send.length());
185 if (status ==
"ok") {
186 status =
"cannot open" + ptmp;
188 status +=
", cannot open" + ptmp;
192 if (status ==
"ok") {
193 status = ptmp +
" does not exists!";
195 status +=
", " + ptmp +
" does not exists!";
198 buffer2send +=
"],\"path\":\"" + path +
"\",";
204 buffer2send +=
"\"occupation\":\"0\",";
206 buffer2send +=
"\"status\":\"" + status +
"\",";
210 _webserver->sendContent_P(buffer2send.c_str(),buffer2send.length());
211 _webserver->sendContent(
"");
215 #endif //HTTP_FEATURE && SD_DEVICE