FSBrowser.ino 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. FSWebServer - Example WebServer with SPIFFS backend for esp8266
  3. Copyright (c) 2015 Hristo Gochkov. All rights reserved.
  4. This file is part of the ESP8266WebServer 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 ("ESP8266 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" esp8266fs.local/edit; done
  19. access the sample web page at http://esp8266fs.local
  20. edit the page by going to http://esp8266fs.local/edit
  21. */
  22. #include <ESP8266WiFi.h>
  23. #include <WiFiClient.h>
  24. #include <ESP8266WebServer.h>
  25. #include <ESP8266mDNS.h>
  26. #include <FS.h>
  27. #define DBG_OUTPUT_PORT Serial
  28. const char* ssid = "wifi-ssid";
  29. const char* password = "wifi-password";
  30. const char* host = "esp8266fs";
  31. ESP8266WebServer server(80);
  32. //holds the current upload
  33. File fsUploadFile;
  34. //format bytes
  35. String formatBytes(size_t bytes){
  36. if (bytes < 1024){
  37. return String(bytes)+"B";
  38. } else if(bytes < (1024 * 1024)){
  39. return String(bytes/1024.0)+"KB";
  40. } else if(bytes < (1024 * 1024 * 1024)){
  41. return String(bytes/1024.0/1024.0)+"MB";
  42. } else {
  43. return String(bytes/1024.0/1024.0/1024.0)+"GB";
  44. }
  45. }
  46. String getContentType(String filename){
  47. if(server.hasArg("download")) return "application/octet-stream";
  48. else if(filename.endsWith(".htm")) return "text/html";
  49. else if(filename.endsWith(".html")) return "text/html";
  50. else if(filename.endsWith(".css")) return "text/css";
  51. else if(filename.endsWith(".js")) return "application/javascript";
  52. else if(filename.endsWith(".png")) return "image/png";
  53. else if(filename.endsWith(".gif")) return "image/gif";
  54. else if(filename.endsWith(".jpg")) return "image/jpeg";
  55. else if(filename.endsWith(".ico")) return "image/x-icon";
  56. else if(filename.endsWith(".xml")) return "text/xml";
  57. else if(filename.endsWith(".pdf")) return "application/x-pdf";
  58. else if(filename.endsWith(".zip")) return "application/x-zip";
  59. else if(filename.endsWith(".gz")) return "application/x-gzip";
  60. return "text/plain";
  61. }
  62. bool handleFileRead(String path){
  63. DBG_OUTPUT_PORT.println("handleFileRead: " + path);
  64. if(path.endsWith("/")) path += "index.htm";
  65. String contentType = getContentType(path);
  66. String pathWithGz = path + ".gz";
  67. if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
  68. if(SPIFFS.exists(pathWithGz))
  69. path += ".gz";
  70. File file = SPIFFS.open(path, "r");
  71. size_t sent = server.streamFile(file, contentType);
  72. file.close();
  73. return true;
  74. }
  75. return false;
  76. }
  77. void handleFileUpload(){
  78. if(server.uri() != "/edit") return;
  79. HTTPUpload& upload = server.upload();
  80. if(upload.status == UPLOAD_FILE_START){
  81. String filename = upload.filename;
  82. if(!filename.startsWith("/")) filename = "/"+filename;
  83. DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
  84. fsUploadFile = SPIFFS.open(filename, "w");
  85. filename = String();
  86. } else if(upload.status == UPLOAD_FILE_WRITE){
  87. //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
  88. if(fsUploadFile)
  89. fsUploadFile.write(upload.buf, upload.currentSize);
  90. } else if(upload.status == UPLOAD_FILE_END){
  91. if(fsUploadFile)
  92. fsUploadFile.close();
  93. DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
  94. }
  95. }
  96. void handleFileDelete(){
  97. if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS");
  98. String path = server.arg(0);
  99. DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
  100. if(path == "/")
  101. return server.send(500, "text/plain", "BAD PATH");
  102. if(!SPIFFS.exists(path))
  103. return server.send(404, "text/plain", "FileNotFound");
  104. SPIFFS.remove(path);
  105. server.send(200, "text/plain", "");
  106. path = String();
  107. }
  108. void handleFileCreate(){
  109. if(server.args() == 0)
  110. return server.send(500, "text/plain", "BAD ARGS");
  111. String path = server.arg(0);
  112. DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
  113. if(path == "/")
  114. return server.send(500, "text/plain", "BAD PATH");
  115. if(SPIFFS.exists(path))
  116. return server.send(500, "text/plain", "FILE EXISTS");
  117. File file = SPIFFS.open(path, "w");
  118. if(file)
  119. file.close();
  120. else
  121. return server.send(500, "text/plain", "CREATE FAILED");
  122. server.send(200, "text/plain", "");
  123. path = String();
  124. }
  125. void handleFileList() {
  126. if(!server.hasArg("dir")) {server.send(500, "text/plain", "BAD ARGS"); return;}
  127. String path = server.arg("dir");
  128. DBG_OUTPUT_PORT.println("handleFileList: " + path);
  129. Dir dir = SPIFFS.openDir(path);
  130. path = String();
  131. String output = "[";
  132. while(dir.next()){
  133. File entry = dir.openFile("r");
  134. if (output != "[") output += ',';
  135. bool isDir = false;
  136. output += "{\"type\":\"";
  137. output += (isDir)?"dir":"file";
  138. output += "\",\"name\":\"";
  139. output += String(entry.name()).substring(1);
  140. output += "\"}";
  141. entry.close();
  142. }
  143. output += "]";
  144. server.send(200, "text/json", output);
  145. }
  146. void setup(void){
  147. DBG_OUTPUT_PORT.begin(115200);
  148. DBG_OUTPUT_PORT.print("\n");
  149. DBG_OUTPUT_PORT.setDebugOutput(true);
  150. SPIFFS.begin();
  151. {
  152. Dir dir = SPIFFS.openDir("/");
  153. while (dir.next()) {
  154. String fileName = dir.fileName();
  155. size_t fileSize = dir.fileSize();
  156. DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
  157. }
  158. DBG_OUTPUT_PORT.printf("\n");
  159. }
  160. //WIFI INIT
  161. DBG_OUTPUT_PORT.printf("Connecting to %s\n", ssid);
  162. if (String(WiFi.SSID()) != String(ssid)) {
  163. WiFi.begin(ssid, password);
  164. }
  165. while (WiFi.status() != WL_CONNECTED) {
  166. delay(500);
  167. DBG_OUTPUT_PORT.print(".");
  168. }
  169. DBG_OUTPUT_PORT.println("");
  170. DBG_OUTPUT_PORT.print("Connected! IP address: ");
  171. DBG_OUTPUT_PORT.println(WiFi.localIP());
  172. MDNS.begin(host);
  173. DBG_OUTPUT_PORT.print("Open http://");
  174. DBG_OUTPUT_PORT.print(host);
  175. DBG_OUTPUT_PORT.println(".local/edit to see the file browser");
  176. //SERVER INIT
  177. //list directory
  178. server.on("/list", HTTP_GET, handleFileList);
  179. //load editor
  180. server.on("/edit", HTTP_GET, [](){
  181. if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
  182. });
  183. //create file
  184. server.on("/edit", HTTP_PUT, handleFileCreate);
  185. //delete file
  186. server.on("/edit", HTTP_DELETE, handleFileDelete);
  187. //first callback is called after the request has ended with all parsed arguments
  188. //second callback handles file uploads at that location
  189. server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload);
  190. //called when the url is not defined here
  191. //use it to load content from SPIFFS
  192. server.onNotFound([](){
  193. if(!handleFileRead(server.uri()))
  194. server.send(404, "text/plain", "FileNotFound");
  195. });
  196. //get heap status, analog input value and all GPIO statuses in one json call
  197. server.on("/all", HTTP_GET, [](){
  198. String json = "{";
  199. json += "\"heap\":"+String(ESP.getFreeHeap());
  200. json += ", \"analog\":"+String(analogRead(A0));
  201. json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
  202. json += "}";
  203. server.send(200, "text/json", json);
  204. json = String();
  205. });
  206. server.begin();
  207. DBG_OUTPUT_PORT.println("HTTP server started");
  208. }
  209. void loop(void){
  210. server.handleClient();
  211. }