From 524ad252fb2e1da0daaa256264b959ffcbe8cd5c Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Sun, 10 Feb 2019 22:23:42 +0900 Subject: [PATCH] Merged updated FSBrowser --- examples/FSBrowser/FSBrowser.ino | 142 ++++++++++++------ examples/FSBrowser/data/edit.htm | 3 - examples/FSBrowser/data/index.htm | 4 +- .../FSBrowser/data/sub1/edit.htm/edit.htm | 3 - examples/FSBrowser/data/sub2/index.htm | 97 ------------ 5 files changed, 99 insertions(+), 150 deletions(-) delete mode 100644 examples/FSBrowser/data/edit.htm delete mode 100644 examples/FSBrowser/data/sub1/edit.htm/edit.htm delete mode 100644 examples/FSBrowser/data/sub2/index.htm diff --git a/examples/FSBrowser/FSBrowser.ino b/examples/FSBrowser/FSBrowser.ino index 9eeadf7..d6f4ba2 100644 --- a/examples/FSBrowser/FSBrowser.ino +++ b/examples/FSBrowser/FSBrowser.ino @@ -52,7 +52,9 @@ const char* host = "esp32fs"; WebServer server(80); #endif //Add a below line for AutoConnect. -AutoConnect portal(server); +AutoConnect portal(server); +AutoConnectAux FSBedit("/edit", "Edit"); +AutoConnectAux FSBlist("/list?dir=\"/\"", "List"); //holds the current upload File fsUploadFile; @@ -69,92 +71,126 @@ String formatBytes(size_t bytes){ } } -String getContentType(String filename){ - if(server.hasArg("download")) return "application/octet-stream"; - else if(filename.endsWith(".htm")) return "text/html"; - else if(filename.endsWith(".html")) return "text/html"; - else if(filename.endsWith(".css")) return "text/css"; - else if(filename.endsWith(".js")) return "application/javascript"; - else if(filename.endsWith(".png")) return "image/png"; - else if(filename.endsWith(".gif")) return "image/gif"; - else if(filename.endsWith(".jpg")) return "image/jpeg"; - else if(filename.endsWith(".ico")) return "image/x-icon"; - else if(filename.endsWith(".xml")) return "text/xml"; - else if(filename.endsWith(".pdf")) return "application/x-pdf"; - else if(filename.endsWith(".zip")) return "application/x-zip"; - else if(filename.endsWith(".gz")) return "application/x-gzip"; +String getContentType(String filename) { + if (server.hasArg("download")) { + return "application/octet-stream"; + } else if (filename.endsWith(".htm")) { + return "text/html"; + } else if (filename.endsWith(".html")) { + return "text/html"; + } else if (filename.endsWith(".css")) { + return "text/css"; + } else if (filename.endsWith(".js")) { + return "application/javascript"; + } else if (filename.endsWith(".png")) { + return "image/png"; + } else if (filename.endsWith(".gif")) { + return "image/gif"; + } else if (filename.endsWith(".jpg")) { + return "image/jpeg"; + } else if (filename.endsWith(".ico")) { + return "image/x-icon"; + } else if (filename.endsWith(".xml")) { + return "text/xml"; + } else if (filename.endsWith(".pdf")) { + return "application/x-pdf"; + } else if (filename.endsWith(".zip")) { + return "application/x-zip"; + } else if (filename.endsWith(".gz")) { + return "application/x-gzip"; + } return "text/plain"; } -bool handleFileRead(String path){ +bool handleFileRead(String path) { DBG_OUTPUT_PORT.println("handleFileRead: " + path); - if(path.endsWith("/")) path += "index.htm"; + if (path.endsWith("/")) { + path += "index.htm"; + } String contentType = getContentType(path); String pathWithGz = path + ".gz"; - if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){ - if(SPIFFS.exists(pathWithGz)) + if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { + if (SPIFFS.exists(pathWithGz)) { path += ".gz"; + } File file = SPIFFS.open(path, "r"); - size_t sent = server.streamFile(file, contentType); + server.streamFile(file, contentType); file.close(); return true; } return false; } -void handleFileUpload(){ - if(server.uri() != "/edit") return; +void handleFileUpload() { + if (server.uri() != "/edit") { + return; + } HTTPUpload& upload = server.upload(); - if(upload.status == UPLOAD_FILE_START){ + if (upload.status == UPLOAD_FILE_START) { String filename = upload.filename; - if(!filename.startsWith("/")) filename = "/"+filename; + if (!filename.startsWith("/")) { + filename = "/" + filename; + } DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename); fsUploadFile = SPIFFS.open(filename, "w"); filename = String(); - } else if(upload.status == UPLOAD_FILE_WRITE){ + } else if (upload.status == UPLOAD_FILE_WRITE) { //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize); - if(fsUploadFile) + if (fsUploadFile) { fsUploadFile.write(upload.buf, upload.currentSize); - } else if(upload.status == UPLOAD_FILE_END){ - if(fsUploadFile) + } + } else if (upload.status == UPLOAD_FILE_END) { + if (fsUploadFile) { fsUploadFile.close(); + } DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize); } } -void handleFileDelete(){ - if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS"); +void handleFileDelete() { + if (server.args() == 0) { + return server.send(500, "text/plain", "BAD ARGS"); + } String path = server.arg(0); DBG_OUTPUT_PORT.println("handleFileDelete: " + path); - if(path == "/") + if (path == "/") { return server.send(500, "text/plain", "BAD PATH"); - if(!SPIFFS.exists(path)) + } + if (!SPIFFS.exists(path)) { return server.send(404, "text/plain", "FileNotFound"); + } SPIFFS.remove(path); server.send(200, "text/plain", ""); path = String(); } -void handleFileCreate(){ - if(server.args() == 0) +void handleFileCreate() { + if (server.args() == 0) { return server.send(500, "text/plain", "BAD ARGS"); + } String path = server.arg(0); DBG_OUTPUT_PORT.println("handleFileCreate: " + path); - if(path == "/") + if (path == "/") { return server.send(500, "text/plain", "BAD PATH"); - if(SPIFFS.exists(path)) + } + if (SPIFFS.exists(path)) { return server.send(500, "text/plain", "FILE EXISTS"); + } File file = SPIFFS.open(path, "w"); - if(file) + if (file) { file.close(); - else + } else { return server.send(500, "text/plain", "CREATE FAILED"); + } server.send(200, "text/plain", ""); path = String(); } void handleFileList() { - if(!server.hasArg("dir")) {server.send(500, "text/plain", "BAD ARGS"); return;} + if (!server.hasArg("dir")) { + server.send(500, "text/plain", "BAD ARGS"); + return; + } String path = server.arg("dir"); DBG_OUTPUT_PORT.println("handleFileList: " + path); @@ -167,12 +203,14 @@ void handleFileList() { String output = "["; #if defined(ARDUINO_ARCH_ESP8266) - while(dir.next()){ + while (dir.next()) { File entry = dir.openFile("r"); - if (output != "[") output += ','; + if (output != "[") { + output += ','; + } bool isDir = false; output += "{\"type\":\""; - output += (isDir)?"dir":"file"; + output += (isDir) ? "dir" : "file"; output += "\",\"name\":\""; output += String(entry.name()).substring(1); output += "\"}"; @@ -207,7 +245,7 @@ void setup(void){ { #if defined(ARDUINO_ARCH_ESP8266) Dir dir = SPIFFS.openDir("/"); - while (dir.next()) { + while (dir.next()) { String fileName = dir.fileName(); size_t fileSize = dir.fileSize(); DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); @@ -243,8 +281,10 @@ void setup(void){ //list directory server.on("/list", HTTP_GET, handleFileList); //load editor - server.on("/edit", HTTP_GET, [](){ - if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound"); + server.on("/edit", HTTP_GET, []() { + if (!handleFileRead("/edit.htm")) { + server.send(404, "text/plain", "FileNotFound"); + } }); //create file server.on("/edit", HTTP_PUT, handleFileCreate); @@ -252,7 +292,9 @@ void setup(void){ server.on("/edit", HTTP_DELETE, handleFileDelete); //first callback is called after the request has ended with all parsed arguments //second callback handles file uploads at that location - server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload); + server.on("/edit", HTTP_POST, []() { + server.send(200, "text/plain", ""); + }, handleFileUpload); //called when the url is not defined here //use it to load content from SPIFFS @@ -277,6 +319,11 @@ void setup(void){ server.send(200, "text/json", json); json = String(); }); + + //Add HOME path + portal.home(String("/")); + //Register AutoConnect menu + portal.join({ FSBedit, FSBlist }); //Replacement as follows to make AutoConnect recognition. //server.begin(); portal.begin(); @@ -298,4 +345,7 @@ void loop(void){ //Replacement as follows to make AutoConnect recognition. //server.handleClient(); portal.handleClient(); +#ifdef ARDUINO_ARCH_ESP8266 + MDNS.update(); +#endif } diff --git a/examples/FSBrowser/data/edit.htm b/examples/FSBrowser/data/edit.htm deleted file mode 100644 index cfd70ba..0000000 --- a/examples/FSBrowser/data/edit.htm +++ /dev/null @@ -1,3 +0,0 @@ -ESP Editor
diff --git a/examples/FSBrowser/data/index.htm b/examples/FSBrowser/data/index.htm index dd767c3..549ef07 100644 --- a/examples/FSBrowser/data/index.htm +++ b/examples/FSBrowser/data/index.htm @@ -84,7 +84,6 @@ -

MENU

@@ -94,5 +93,8 @@
+

+ AutoConnect menu +

\ No newline at end of file diff --git a/examples/FSBrowser/data/sub1/edit.htm/edit.htm b/examples/FSBrowser/data/sub1/edit.htm/edit.htm deleted file mode 100644 index cfd70ba..0000000 --- a/examples/FSBrowser/data/sub1/edit.htm/edit.htm +++ /dev/null @@ -1,3 +0,0 @@ -ESP Editor
diff --git a/examples/FSBrowser/data/sub2/index.htm b/examples/FSBrowser/data/sub2/index.htm deleted file mode 100644 index 4e1dc7d..0000000 --- a/examples/FSBrowser/data/sub2/index.htm +++ /dev/null @@ -1,97 +0,0 @@ - - - - - - ESP Monitor - - - - -
- - - - -
-
-
-
- - \ No newline at end of file