ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
handle-files.cpp
Go to the documentation of this file.
1 /*
2  handle-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(FILESYSTEM_FEATURE)
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_filesystem.h"
30 #include "../../authentication/authentication_service.h"
31 //Filesystem
32 //Filesystem files list and file commands
33 void HTTP_Server::handleFSFileList ()
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 (_webserver->hasArg ("quiet")) {
48  if(_webserver->arg ("quiet") == "yes") {
49  Serial.println("quiet");
50  _webserver->send (200, "text/plain", "{\"status\":\"ok\"}");
51  return;
52  }
53  }
54  //get current path
55  if (_webserver->hasArg ("path") ) {
56  path += _webserver->arg ("path") ;
57  }
58  //to have a clean path
59  path.trim();
60  path.replace ("//", "/");
61  if (path[path.length() - 1] != '/') {
62  path += "/";
63  }
64  //check if query need some action
65  if (_webserver->hasArg ("action") ) {
66  //delete a file
67  if (_webserver->arg ("action") == "delete" && _webserver->hasArg ("filename") ) {
68  String filename;
69  String shortname = _webserver->arg ("filename");
70  shortname.replace ("/", "");
71  filename = path + _webserver->arg ("filename");
72  filename.replace ("//", "/");
73  if (!ESP_FileSystem::exists (filename.c_str()) ) {
74  status = shortname + " does not exists!";
75  } else {
76  if (ESP_FileSystem::remove (filename.c_str()) ) {
77  status = shortname + " deleted";
78  //what happen if no "/." and no other subfiles for SPIFFS like?
79  String ptmp = path;
80  if ( (path != "/") && (path[path.length() - 1] = '/') ) {
81  ptmp = path.substring (0, path.length() - 1);
82  }
83  if (!ESP_FileSystem::exists (ptmp.c_str())) {
84  ESP_FileSystem::mkdir(ptmp.c_str());
85  }
86  } else {
87  status = "Cannot deleted " ;
88  status += shortname ;
89  }
90  }
91  }
92  //delete a directory
93  if (_webserver->arg ("action") == "deletedir" && _webserver->hasArg ("filename") ) {
94  String filename;
95  String shortname = _webserver->arg ("filename");
96  shortname.replace ("/", "");
97  filename = path + _webserver->arg ("filename");
98  filename += "/";
99  filename.replace ("//", "/");
100  if (filename != "/") {
101  if (ESP_FileSystem::rmdir(filename.c_str())) {
102  log_esp3d("Deleting %s",filename.c_str());
103  status = shortname ;
104  status += " deleted";
105  } else {
106  status = "Cannot deleted " ;
107  status += shortname ;
108  }
109  }
110  }
111  //create a directory
112  if (_webserver->arg ("action") == "createdir" && _webserver->hasArg ("filename") ) {
113  String filename;
114  filename = path + _webserver->arg ("filename") + "/.";
115  String shortname = _webserver->arg ("filename");
116  shortname.replace ("/", "");
117  filename.replace ("//", "/");
118  if (ESP_FileSystem::exists (filename.c_str()) ) {
119  status = shortname + " already exists!";
120  } else {
121  if (!ESP_FileSystem::mkdir(filename.c_str())) {
122  status = "Cannot create ";
123  status += shortname ;
124  } else {
125  status = shortname + " created";
126  }
127  }
128  }
129  }
130  String buffer2send ;
131  buffer2send.reserve(1200);
132  buffer2send = "{\"files\":[";
133  String ptmp = path;
134  if ( (path != "/") && (path[path.length() - 1] = '/') ) {
135  ptmp = path.substring (0, path.length() - 1);
136  }
137  _webserver->setContentLength(CONTENT_LENGTH_UNKNOWN);
138  _webserver->sendHeader("Content-Type","application/json");
139  _webserver->sendHeader("Cache-Control","no-cache");
140  _webserver->send(200);
141  if (ESP_FileSystem::exists(ptmp.c_str())) {
142  ESP_File f = ESP_FileSystem::open(ptmp.c_str(), ESP_FILE_READ);
143  //Parse files
144  ESP_File sub = f.openNextFile();
145  if (f) {
146  bool needseparator = false;
147  while (sub) {
148  if (needseparator) {
149  buffer2send+=",";
150  } else {
151  //for next entry
152  needseparator=true;
153  }
154  buffer2send+="{\"name\":\"";
155  buffer2send+=sub.name();
156  buffer2send+="\",\"size\":\"";
157  if (sub.isDirectory()) {
158  buffer2send+="-1";
159  } else {
160  buffer2send+=ESP_FileSystem::formatBytes(sub.size());
161  }
162 #ifdef FILESYSTEM_TIMESTAMP_FEATURE
163  buffer2send+="\",\"time\":\"";
164  time_t t = sub.getLastWrite();
165  struct tm * tmstruct = localtime(&t);
166  char str[20]; //buffer should be 20
167  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);
168  buffer2send+=str;
169 #endif //FILESYSTEM_TIMESTAMP_FEATURE
170  buffer2send+="\"}";
171  if (buffer2send.length() > 1100) {
172  _webserver->sendContent_P(buffer2send.c_str(),buffer2send.length());
173  buffer2send = "";
174  }
175  sub.close();
176  sub = f.openNextFile();
177  }
178  f.close();
179  } else {
180  if (status == "ok") {
181  status = "cannot open" + ptmp;
182  } else {
183  status += ", cannot open" + ptmp;
184  }
185  }
186  } else {
187  if (status == "ok") {
188  status = ptmp + " does not exists!";
189  } else {
190  status += ", " + ptmp + " does not exists!";
191  }
192  }
193  buffer2send += "],\"path\":\"" + path + "\",";
194 
195  if (ESP_FileSystem::totalBytes()>0) {
196  buffer2send += "\"occupation\":\"" + String(100*ESP_FileSystem::usedBytes()/ESP_FileSystem::totalBytes()) + "\",";
197  } else {
198  status = "FileSystem Error";
199  buffer2send += "\"occupation\":\"0\",";
200  }
201  buffer2send += "\"status\":\"" + status + "\",";
202  buffer2send += "\"total\":\"" + ESP_FileSystem::formatBytes (ESP_FileSystem::totalBytes()) + "\",";
203  buffer2send += "\"used\":\"" + ESP_FileSystem::formatBytes (ESP_FileSystem::usedBytes()) + "\"}";
204  path = "";
205  _webserver->sendContent_P(buffer2send.c_str(),buffer2send.length());
206  _webserver->sendContent("");
207  _upload_status = UPLOAD_STATUS_NONE;
208 }
209 
210 #endif //HTTP_FEATURE && FILESYSTEM_FEATURE
ESP_File::close
void close()
ESP_File::name
const char * name() const
Definition: esp_filesystem.cpp:123
ESP_FILE_READ
#define ESP_FILE_READ
Definition: defines.h:120
ESP_FileSystem::usedBytes
static size_t usedBytes()
ESP_FileSystem::remove
static bool remove(const char *path)
ESP_FileSystem::totalBytes
static size_t totalBytes()
ESP_File::getLastWrite
time_t getLastWrite()
Definition: esp_filesystem.cpp:143
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_FileSystem::mkdir
static bool mkdir(const char *path)
ESP_File::openNextFile
ESP_File openNextFile()
UPLOAD_STATUS_NONE
@ UPLOAD_STATUS_NONE
Definition: http_server.h:38
ESP_File
Definition: esp_filesystem.h:30
level_authenticate_type
level_authenticate_type
Definition: authentication_service.h:25
ESP_FileSystem::rmdir
static bool rmdir(const char *path)
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
ESP_File::size
size_t size()
Definition: esp_filesystem.cpp:138
ESP_FileSystem::formatBytes
static String & formatBytes(uint64_t bytes)
Definition: esp_filesystem.cpp:45
ESP_FileSystem::exists
static bool exists(const char *path)
ESP_FileSystem::open
static ESP_File open(const char *path, uint8_t mode=ESP_FILE_READ)
ESP_File::isDirectory
bool isDirectory()
Definition: esp_filesystem.cpp:133