FSBrowser32.ino 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. FSWebServer - Example WebServer with FS backend for esp8266/esp32
  3. Copyright (c) 2015 Hristo Gochkov. All rights reserved.
  4. This file is part of the WebServer library for Arduino environment.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. upload the contents of the data folder with MkSPIFFS Tool ("ESP32 Sketch Data Upload" in Tools menu in Arduino IDE)
  17. or you can upload the contents of a folder if you CD in that folder and run the following command:
  18. for file in `ls -A1`; do curl -F "file=@$PWD/$file" esp32fs.local/edit; done
  19. access the sample web page at http://esp32fs.local
  20. edit the page by going to http://esp32fs.local/edit
  21. */
  22. #include <WiFi.h>
  23. #include <WiFiClient.h>
  24. #include <WebServer.h>
  25. #include <ESPmDNS.h>
  26. #define USE_SPIFFS
  27. // You only need to format the filesystem once
  28. #define FORMAT_FILESYSTEM false
  29. #define DBG_OUTPUT_PORT Serial
  30. #ifdef USE_SPIFFS
  31. #define FILESYSTEM SPIFFS
  32. #include <SPIFFS.h>
  33. #else
  34. #define FILESYSTEM FFat
  35. #include <FFat.h>
  36. #endif
  37. #include <AutoConnect.h>
  38. const char* ssid = "wifi-ssid";
  39. const char* password = "wifi-password";
  40. const char* host = "esp32fs";
  41. WebServer server(80);
  42. //holds the current upload
  43. File fsUploadFile;
  44. // Additional lines as the below to apply AutoConnect
  45. AutoConnect portal(server);
  46. AutoConnectConfig config;
  47. //format bytes
  48. String formatBytes(size_t bytes) {
  49. if (bytes < 1024) {
  50. return String(bytes) + "B";
  51. } else if (bytes < (1024 * 1024)) {
  52. return String(bytes / 1024.0) + "KB";
  53. } else if (bytes < (1024 * 1024 * 1024)) {
  54. return String(bytes / 1024.0 / 1024.0) + "MB";
  55. } else {
  56. return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
  57. }
  58. }
  59. String getContentType(String filename) {
  60. if (server.hasArg("download")) {
  61. return "application/octet-stream";
  62. } else if (filename.endsWith(".htm")) {
  63. return "text/html";
  64. } else if (filename.endsWith(".html")) {
  65. return "text/html";
  66. } else if (filename.endsWith(".css")) {
  67. return "text/css";
  68. } else if (filename.endsWith(".js")) {
  69. return "application/javascript";
  70. } else if (filename.endsWith(".png")) {
  71. return "image/png";
  72. } else if (filename.endsWith(".gif")) {
  73. return "image/gif";
  74. } else if (filename.endsWith(".jpg")) {
  75. return "image/jpeg";
  76. } else if (filename.endsWith(".ico")) {
  77. return "image/x-icon";
  78. } else if (filename.endsWith(".xml")) {
  79. return "text/xml";
  80. } else if (filename.endsWith(".pdf")) {
  81. return "application/x-pdf";
  82. } else if (filename.endsWith(".zip")) {
  83. return "application/x-zip";
  84. } else if (filename.endsWith(".gz")) {
  85. return "application/x-gzip";
  86. }
  87. return "text/plain";
  88. }
  89. bool exists(String path){
  90. bool yes = false;
  91. File file = FILESYSTEM.open(path, "r");
  92. if(!file.isDirectory()){
  93. yes = true;
  94. }
  95. file.close();
  96. return yes;
  97. }
  98. bool handleFileRead(String path) {
  99. DBG_OUTPUT_PORT.println("handleFileRead: " + path);
  100. if (path.endsWith("/")) {
  101. path += "index.htm";
  102. }
  103. String contentType = getContentType(path);
  104. String pathWithGz = path + ".gz";
  105. if (exists(pathWithGz) || exists(path)) {
  106. if (exists(pathWithGz)) {
  107. path += ".gz";
  108. }
  109. File file = FILESYSTEM.open(path, "r");
  110. server.streamFile(file, contentType);
  111. file.close();
  112. return true;
  113. }
  114. return false;
  115. }
  116. void handleFileUpload() {
  117. if (server.uri() != "/edit") {
  118. return;
  119. }
  120. HTTPUpload& upload = server.upload();
  121. if (upload.status == UPLOAD_FILE_START) {
  122. String filename = upload.filename;
  123. if (!filename.startsWith("/")) {
  124. filename = "/" + filename;
  125. }
  126. DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
  127. fsUploadFile = FILESYSTEM.open(filename, "w");
  128. filename = String();
  129. } else if (upload.status == UPLOAD_FILE_WRITE) {
  130. //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
  131. if (fsUploadFile) {
  132. fsUploadFile.write(upload.buf, upload.currentSize);
  133. }
  134. } else if (upload.status == UPLOAD_FILE_END) {
  135. if (fsUploadFile) {
  136. fsUploadFile.close();
  137. }
  138. DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
  139. }
  140. }
  141. void handleFileDelete() {
  142. if (server.args() == 0) {
  143. return server.send(500, "text/plain", "BAD ARGS");
  144. }
  145. String path = server.arg(0);
  146. DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
  147. if (path == "/") {
  148. return server.send(500, "text/plain", "BAD PATH");
  149. }
  150. if (!exists(path)) {
  151. return server.send(404, "text/plain", "FileNotFound");
  152. }
  153. FILESYSTEM.remove(path);
  154. server.send(200, "text/plain", "");
  155. path = String();
  156. }
  157. void handleFileCreate() {
  158. if (server.args() == 0) {
  159. return server.send(500, "text/plain", "BAD ARGS");
  160. }
  161. String path = server.arg(0);
  162. DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
  163. if (path == "/") {
  164. return server.send(500, "text/plain", "BAD PATH");
  165. }
  166. if (exists(path)) {
  167. return server.send(500, "text/plain", "FILE EXISTS");
  168. }
  169. File file = FILESYSTEM.open(path, "w");
  170. if (file) {
  171. file.close();
  172. } else {
  173. return server.send(500, "text/plain", "CREATE FAILED");
  174. }
  175. server.send(200, "text/plain", "");
  176. path = String();
  177. }
  178. void handleFileList() {
  179. if (!server.hasArg("dir")) {
  180. server.send(500, "text/plain", "BAD ARGS");
  181. return;
  182. }
  183. String path = server.arg("dir");
  184. DBG_OUTPUT_PORT.println("handleFileList: " + path);
  185. File root = FILESYSTEM.open(path);
  186. path = String();
  187. String output = "[";
  188. if(root.isDirectory()){
  189. File file = root.openNextFile();
  190. while(file){
  191. if (output != "[") {
  192. output += ',';
  193. }
  194. output += "{\"type\":\"";
  195. output += (file.isDirectory()) ? "dir" : "file";
  196. output += "\",\"name\":\"";
  197. output += String(file.name()).substring(1);
  198. output += "\"}";
  199. file = root.openNextFile();
  200. }
  201. }
  202. output += "]";
  203. server.send(200, "text/json", output);
  204. }
  205. void setup(void) {
  206. DBG_OUTPUT_PORT.begin(115200);
  207. DBG_OUTPUT_PORT.print("\n");
  208. DBG_OUTPUT_PORT.setDebugOutput(true);
  209. if (FORMAT_FILESYSTEM) FILESYSTEM.format();
  210. FILESYSTEM.begin();
  211. {
  212. File root = FILESYSTEM.open("/");
  213. File file = root.openNextFile();
  214. while(file){
  215. String fileName = file.name();
  216. size_t fileSize = file.size();
  217. DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
  218. file = root.openNextFile();
  219. }
  220. DBG_OUTPUT_PORT.printf("\n");
  221. }
  222. // With applying AutoConnect, making WiFi connection is not necessary.
  223. //WIFI INIT
  224. // DBG_OUTPUT_PORT.printf("Connecting to %s\n", ssid);
  225. // if (String(WiFi.SSID()) != String(ssid)) {
  226. // WiFi.mode(WIFI_STA);
  227. // WiFi.begin(ssid, password);
  228. // }
  229. // while (WiFi.status() != WL_CONNECTED) {
  230. // delay(500);
  231. // DBG_OUTPUT_PORT.print(".");
  232. // }
  233. // DBG_OUTPUT_PORT.println("");
  234. // DBG_OUTPUT_PORT.print("Connected! IP address: ");
  235. // DBG_OUTPUT_PORT.println(WiFi.localIP());
  236. // MDNS.begin(host);
  237. // DBG_OUTPUT_PORT.print("Open http://");
  238. // DBG_OUTPUT_PORT.print(host);
  239. // DBG_OUTPUT_PORT.println(".local/edit to see the file browser");
  240. //SERVER INIT
  241. //list directory
  242. server.on("/list", HTTP_GET, handleFileList);
  243. //load editor
  244. server.on("/edit", HTTP_GET, []() {
  245. if (!handleFileRead("/edit.htm")) {
  246. server.send(404, "text/plain", "FileNotFound");
  247. }
  248. });
  249. //create file
  250. server.on("/edit", HTTP_PUT, handleFileCreate);
  251. //delete file
  252. server.on("/edit", HTTP_DELETE, handleFileDelete);
  253. //first callback is called after the request has ended with all parsed arguments
  254. //second callback handles file uploads at that location
  255. server.on("/edit", HTTP_POST, []() {
  256. server.send(200, "text/plain", "");
  257. }, handleFileUpload);
  258. // Default handler for all URIs not defined above
  259. // Use it to read files from filesystem
  260. // To make AutoConnect recognize the 404 handler, replace it with:
  261. //called when the url is not defined here
  262. //use it to load content from FILESYSTEM
  263. // server.onNotFound([]() {
  264. portal.onNotFound([]() {
  265. if (!handleFileRead(server.uri())) {
  266. server.send(404, "text/plain", "FileNotFound");
  267. }
  268. });
  269. //get heap status, analog input value and all GPIO statuses in one json call
  270. server.on("/all", HTTP_GET, []() {
  271. String json = "{";
  272. json += "\"heap\":" + String(ESP.getFreeHeap());
  273. json += ", \"analog\":" + String(analogRead(A0));
  274. json += ", \"gpio\":" + String((uint32_t)(0));
  275. json += "}";
  276. server.send(200, "text/json", json);
  277. json = String();
  278. });
  279. // server.begin();
  280. // DBG_OUTPUT_PORT.println("HTTP server started");
  281. // Start AutoConnect
  282. config.title = "FSBrowser";
  283. portal.config(config);
  284. portal.append("/edit", "Edit");
  285. portal.append("/list?dir=\"/\"", "List");
  286. if (portal.begin()) {
  287. DBG_OUTPUT_PORT.print(F("Connected! IP address: "));
  288. DBG_OUTPUT_PORT.println(WiFi.localIP());
  289. }
  290. DBG_OUTPUT_PORT.println("HTTP server started");
  291. // With applying AutoConnect, the MDNS service must be started after
  292. // establishing a WiFi connection.
  293. // MDNS INIT
  294. if (MDNS.begin(host)) {
  295. MDNS.addService("http", "tcp", 80);
  296. DBG_OUTPUT_PORT.print(F("Open http://"));
  297. DBG_OUTPUT_PORT.print(host);
  298. DBG_OUTPUT_PORT.println(F(".local/edit to open the FileSystem Browser"));
  299. DBG_OUTPUT_PORT.print(F("Open http://"));
  300. DBG_OUTPUT_PORT.print(host);
  301. DBG_OUTPUT_PORT.println(F(".local/_ac to AutoConnect statistics"));
  302. }
  303. }
  304. void loop(void) {
  305. // To make AutoConnect recognize the client handling, replace it with:
  306. // server.handleClient();
  307. portal.handleClient();
  308. }