ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
handle-SD-files.cpp
Go to the documentation of this file.
1 /*
2  handle-SD-files.cpp - ESP3D http handle
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
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"
31 //SD
32 //SD files list and file commands
33 void HTTP_Server::handleSDFileList ()
34 {
36  if (auth_level == LEVEL_GUEST) {
37  _upload_status = UPLOAD_STATUS_NONE;
38  _webserver->send (401, "text/plain", "Wrong authentication!");
39  return;
40  }
41  String path ;
42  String status = "ok";
43  if ( (_upload_status == UPLOAD_STATUS_FAILED) || (_upload_status == UPLOAD_STATUS_CANCELLED) ) {
44  status = "Upload failed";
45  _upload_status = UPLOAD_STATUS_NONE;
46  }
47  if (ESP_SD::getState(true) != ESP_SDCARD_IDLE) {
48  _webserver->send (200, "text/plain", "{\"status\":\"no SD card\"}");
49  return;
50  }
51  if (_webserver->hasArg ("quiet")) {
52  if(_webserver->arg ("quiet") == "yes") {
53  Serial.println("quiet");
54  _webserver->send (200, "text/plain", "{\"status\":\"ok\"}");
55  return;
56  }
57  }
58 
59  //get current path
60  if (_webserver->hasArg ("path") ) {
61  path += _webserver->arg ("path") ;
62  }
63  //to have a clean path
64  path.trim();
65  path.replace ("//", "/");
66  if (path[path.length() - 1] != '/') {
67  path += "/";
68  }
69  //check if query need some action
70  if (_webserver->hasArg ("action") ) {
71  //delete a file
72  if (_webserver->arg ("action") == "delete" && _webserver->hasArg ("filename") ) {
73  String filename;
74  String shortname = _webserver->arg ("filename");
75  shortname.replace ("/", "");
76  filename = path + _webserver->arg ("filename");
77  filename.replace ("//", "/");
78  if (!ESP_SD::exists (filename.c_str()) ) {
79  status = shortname + " does not exists!";
80  } else {
81  if (ESP_SD::remove (filename.c_str()) ) {
82  status = shortname + " deleted";
83  //what happen if no "/." and no other subfiles for SPIFFS like?
84  String ptmp = path;
85  if ( (path != "/") && (path[path.length() - 1] = '/') ) {
86  ptmp = path.substring (0, path.length() - 1);
87  }
88  if (!ESP_SD::exists (ptmp.c_str())) {
89  ESP_SD::mkdir(ptmp.c_str());
90  }
91  } else {
92  status = "Cannot deleted " ;
93  status += shortname ;
94  }
95  }
96  }
97  //delete a directory
98  if (_webserver->arg ("action") == "deletedir" && _webserver->hasArg ("filename") ) {
99  String filename;
100  String shortname = _webserver->arg ("filename");
101  shortname.replace ("/", "");
102  filename = path + _webserver->arg ("filename");
103  filename += "/";
104  filename.replace ("//", "/");
105  if (filename != "/") {
106  if (ESP_SD::rmdir(filename.c_str())) {
107  log_esp3d("Deleting %s",filename.c_str());
108  status = shortname ;
109  status += " deleted";
110  } else {
111  status = "Cannot deleted " ;
112  status += shortname ;
113  }
114  }
115  }
116  //create a directory
117  if (_webserver->arg ("action") == "createdir" && _webserver->hasArg ("filename") ) {
118  String filename;
119  filename = path + _webserver->arg ("filename") + "/.";
120  String shortname = _webserver->arg ("filename");
121  shortname.replace ("/", "");
122  filename.replace ("//", "/");
123  if (ESP_SD::exists (filename.c_str()) ) {
124  status = shortname + " already exists!";
125  } else {
126  if (!ESP_SD::mkdir(filename.c_str())) {
127  status = "Cannot create ";
128  status += shortname ;
129  } else {
130  status = shortname + " created";
131  }
132  }
133  }
134  }
135  String buffer2send ;
136  buffer2send.reserve(1200);
137  buffer2send = "{\"files\":[";
138  String ptmp = path;
139  if ( (path != "/") && (path[path.length() - 1] = '/') ) {
140  ptmp = path.substring (0, path.length() - 1);
141  }
142  _webserver->setContentLength(CONTENT_LENGTH_UNKNOWN);
143  _webserver->sendHeader("Content-Type","application/json");
144  _webserver->sendHeader("Cache-Control","no-cache");
145  _webserver->send(200);
146  if (ESP_SD::exists(ptmp.c_str())) {
147  ESP_SDFile f = ESP_SD::open(ptmp.c_str(), ESP_FILE_READ);
148  //Parse files
149  ESP_SDFile sub = f.openNextFile();
150  if (f) {
151  bool needseparator = false;
152  while (sub) {
153  if (needseparator) {
154  buffer2send+=",";
155  } else {
156  //for next entry
157  needseparator=true;
158  }
159  buffer2send+="{\"name\":\"";
160  buffer2send+=sub.name();
161  buffer2send+="\",\"size\":\"";
162  if (sub.isDirectory()) {
163  buffer2send+="-1";
164  } else {
165  buffer2send+=ESP_SD::formatBytes(sub.size());
166  }
167 #ifdef FILESYSTEM_TIMESTAMP_FEATURE
168  buffer2send+="\",\"time\":\"";
169  time_t t = sub.getLastWrite();
170  struct tm * tmstruct = localtime(&t);
171  char str[20]; //buffer should be 20
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);
173  buffer2send+=str;
174 #endif //FILESYSTEM_TIMESTAMP_FEATURE
175  buffer2send+="\"}";
176  if (buffer2send.length() > 1100) {
177  _webserver->sendContent_P(buffer2send.c_str(),buffer2send.length());
178  buffer2send = "";
179  }
180  sub.close();
181  sub = f.openNextFile();
182  }
183  f.close();
184  } else {
185  if (status == "ok") {
186  status = "cannot open" + ptmp;
187  } else {
188  status += ", cannot open" + ptmp;
189  }
190  }
191  } else {
192  if (status == "ok") {
193  status = ptmp + " does not exists!";
194  } else {
195  status += ", " + ptmp + " does not exists!";
196  }
197  }
198  buffer2send += "],\"path\":\"" + path + "\",";
199 
200  if (ESP_SD::totalBytes()>0) {
201  buffer2send += "\"occupation\":\"" + String(100.0*ESP_SD::usedBytes()/ESP_SD::totalBytes()) + "\",";
202  } else {
203  status = "SD Error";
204  buffer2send += "\"occupation\":\"0\",";
205  }
206  buffer2send += "\"status\":\"" + status + "\",";
207  buffer2send += "\"total\":\"" + ESP_SD::formatBytes (ESP_SD::totalBytes()) + "\",";
208  buffer2send += "\"used\":\"" + ESP_SD::formatBytes (ESP_SD::usedBytes()) + "\"}";
209  path = "";
210  _webserver->sendContent_P(buffer2send.c_str(),buffer2send.length());
211  _webserver->sendContent("");
212  _upload_status = UPLOAD_STATUS_NONE;
213 }
214 
215 #endif //HTTP_FEATURE && SD_DEVICE
ESP_SDFile::isDirectory
bool isDirectory()
Definition: esp_sd.cpp:125
ESP_SD::open
static ESP_SDFile open(const char *path, uint8_t mode=ESP_FILE_READ)
ESP_SDFile::name
const char * name() const
Definition: esp_sd.cpp:115
ESP_FILE_READ
#define ESP_FILE_READ
Definition: defines.h:120
ESP_SD::totalBytes
static uint64_t totalBytes()
ESP_SDFile
Definition: esp_sd.h:31
ESP_SD::remove
static bool remove(const char *path)
ESP_SDFile::size
size_t size()
Definition: esp_sd.cpp:130
ESP_SDCARD_IDLE
#define ESP_SDCARD_IDLE
Definition: defines.h:80
AuthenticationService::authenticated_level
static level_authenticate_type authenticated_level(const char *pwd=nullptr)
Definition: authentication_service.cpp:61
UPLOAD_STATUS_FAILED
@ UPLOAD_STATUS_FAILED
Definition: http_server.h:39
UPLOAD_STATUS_CANCELLED
@ UPLOAD_STATUS_CANCELLED
Definition: http_server.h:40
LEVEL_GUEST
@ LEVEL_GUEST
Definition: authentication_service.h:26
ESP_SD::rmdir
static bool rmdir(const char *path)
ESP_SD::mkdir
static bool mkdir(const char *path)
ESP_SDFile::getLastWrite
time_t getLastWrite()
Definition: esp_sd.cpp:135
ESP_SD::formatBytes
static String & formatBytes(uint64_t bytes)
Definition: esp_sd.cpp:68
UPLOAD_STATUS_NONE
@ UPLOAD_STATUS_NONE
Definition: http_server.h:38
ESP_SDFile::openNextFile
ESP_SDFile openNextFile()
ESP_SD::getState
static uint8_t getState(bool refresh)
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
ESP_SDFile::close
void close()
ESP_SD::exists
static bool exists(const char *path)
ESP_SD::usedBytes
static uint64_t usedBytes()