Merged updated FSBrowser

pull/41/head
Hieromon Ikasamo 6 years ago
parent 20c1d9da14
commit 524ad252fb
  1. 114
      examples/FSBrowser/FSBrowser.ino
  2. 3
      examples/FSBrowser/data/edit.htm
  3. 4
      examples/FSBrowser/data/index.htm
  4. 3
      examples/FSBrowser/data/sub1/edit.htm/edit.htm
  5. 97
      examples/FSBrowser/data/sub2/index.htm

@ -53,6 +53,8 @@ WebServer server(80);
#endif #endif
//Add a below line for AutoConnect. //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 //holds the current upload
File fsUploadFile; File fsUploadFile;
@ -70,32 +72,49 @@ String formatBytes(size_t bytes){
} }
String getContentType(String filename) { String getContentType(String filename) {
if(server.hasArg("download")) return "application/octet-stream"; if (server.hasArg("download")) {
else if(filename.endsWith(".htm")) return "text/html"; return "application/octet-stream";
else if(filename.endsWith(".html")) return "text/html"; } else if (filename.endsWith(".htm")) {
else if(filename.endsWith(".css")) return "text/css"; return "text/html";
else if(filename.endsWith(".js")) return "application/javascript"; } else if (filename.endsWith(".html")) {
else if(filename.endsWith(".png")) return "image/png"; return "text/html";
else if(filename.endsWith(".gif")) return "image/gif"; } else if (filename.endsWith(".css")) {
else if(filename.endsWith(".jpg")) return "image/jpeg"; return "text/css";
else if(filename.endsWith(".ico")) return "image/x-icon"; } else if (filename.endsWith(".js")) {
else if(filename.endsWith(".xml")) return "text/xml"; return "application/javascript";
else if(filename.endsWith(".pdf")) return "application/x-pdf"; } else if (filename.endsWith(".png")) {
else if(filename.endsWith(".zip")) return "application/x-zip"; return "image/png";
else if(filename.endsWith(".gz")) return "application/x-gzip"; } 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"; return "text/plain";
} }
bool handleFileRead(String path) { bool handleFileRead(String path) {
DBG_OUTPUT_PORT.println("handleFileRead: " + path); DBG_OUTPUT_PORT.println("handleFileRead: " + path);
if(path.endsWith("/")) path += "index.htm"; if (path.endsWith("/")) {
path += "index.htm";
}
String contentType = getContentType(path); String contentType = getContentType(path);
String pathWithGz = path + ".gz"; String pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
if(SPIFFS.exists(pathWithGz)) if (SPIFFS.exists(pathWithGz)) {
path += ".gz"; path += ".gz";
}
File file = SPIFFS.open(path, "r"); File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType); server.streamFile(file, contentType);
file.close(); file.close();
return true; return true;
} }
@ -103,58 +122,75 @@ bool handleFileRead(String path){
} }
void handleFileUpload() { void handleFileUpload() {
if(server.uri() != "/edit") return; if (server.uri() != "/edit") {
return;
}
HTTPUpload& upload = server.upload(); HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) { if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename; 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); DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
fsUploadFile = SPIFFS.open(filename, "w"); fsUploadFile = SPIFFS.open(filename, "w");
filename = String(); 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); //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
if(fsUploadFile) if (fsUploadFile) {
fsUploadFile.write(upload.buf, upload.currentSize); fsUploadFile.write(upload.buf, upload.currentSize);
}
} else if (upload.status == UPLOAD_FILE_END) { } else if (upload.status == UPLOAD_FILE_END) {
if(fsUploadFile) if (fsUploadFile) {
fsUploadFile.close(); fsUploadFile.close();
}
DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize); DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
} }
} }
void handleFileDelete() { void handleFileDelete() {
if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS"); if (server.args() == 0) {
return server.send(500, "text/plain", "BAD ARGS");
}
String path = server.arg(0); String path = server.arg(0);
DBG_OUTPUT_PORT.println("handleFileDelete: " + path); DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
if(path == "/") if (path == "/") {
return server.send(500, "text/plain", "BAD PATH"); return server.send(500, "text/plain", "BAD PATH");
if(!SPIFFS.exists(path)) }
if (!SPIFFS.exists(path)) {
return server.send(404, "text/plain", "FileNotFound"); return server.send(404, "text/plain", "FileNotFound");
}
SPIFFS.remove(path); SPIFFS.remove(path);
server.send(200, "text/plain", ""); server.send(200, "text/plain", "");
path = String(); path = String();
} }
void handleFileCreate() { void handleFileCreate() {
if(server.args() == 0) if (server.args() == 0) {
return server.send(500, "text/plain", "BAD ARGS"); return server.send(500, "text/plain", "BAD ARGS");
}
String path = server.arg(0); String path = server.arg(0);
DBG_OUTPUT_PORT.println("handleFileCreate: " + path); DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
if(path == "/") if (path == "/") {
return server.send(500, "text/plain", "BAD 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"); return server.send(500, "text/plain", "FILE EXISTS");
}
File file = SPIFFS.open(path, "w"); File file = SPIFFS.open(path, "w");
if(file) if (file) {
file.close(); file.close();
else } else {
return server.send(500, "text/plain", "CREATE FAILED"); return server.send(500, "text/plain", "CREATE FAILED");
}
server.send(200, "text/plain", ""); server.send(200, "text/plain", "");
path = String(); path = String();
} }
void handleFileList() { 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"); String path = server.arg("dir");
DBG_OUTPUT_PORT.println("handleFileList: " + path); DBG_OUTPUT_PORT.println("handleFileList: " + path);
@ -169,7 +205,9 @@ void handleFileList() {
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
while (dir.next()) { while (dir.next()) {
File entry = dir.openFile("r"); File entry = dir.openFile("r");
if (output != "[") output += ','; if (output != "[") {
output += ',';
}
bool isDir = false; bool isDir = false;
output += "{\"type\":\""; output += "{\"type\":\"";
output += (isDir) ? "dir" : "file"; output += (isDir) ? "dir" : "file";
@ -244,7 +282,9 @@ void setup(void){
server.on("/list", HTTP_GET, handleFileList); server.on("/list", HTTP_GET, handleFileList);
//load editor //load editor
server.on("/edit", HTTP_GET, []() { server.on("/edit", HTTP_GET, []() {
if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound"); if (!handleFileRead("/edit.htm")) {
server.send(404, "text/plain", "FileNotFound");
}
}); });
//create file //create file
server.on("/edit", HTTP_PUT, handleFileCreate); server.on("/edit", HTTP_PUT, handleFileCreate);
@ -252,7 +292,9 @@ void setup(void){
server.on("/edit", HTTP_DELETE, handleFileDelete); server.on("/edit", HTTP_DELETE, handleFileDelete);
//first callback is called after the request has ended with all parsed arguments //first callback is called after the request has ended with all parsed arguments
//second callback handles file uploads at that location //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 //called when the url is not defined here
//use it to load content from SPIFFS //use it to load content from SPIFFS
@ -277,6 +319,11 @@ void setup(void){
server.send(200, "text/json", json); server.send(200, "text/json", json);
json = String(); json = String();
}); });
//Add HOME path
portal.home(String("/"));
//Register AutoConnect menu
portal.join({ FSBedit, FSBlist });
//Replacement as follows to make AutoConnect recognition. //Replacement as follows to make AutoConnect recognition.
//server.begin(); //server.begin();
portal.begin(); portal.begin();
@ -298,4 +345,7 @@ void loop(void){
//Replacement as follows to make AutoConnect recognition. //Replacement as follows to make AutoConnect recognition.
//server.handleClient(); //server.handleClient();
portal.handleClient(); portal.handleClient();
#ifdef ARDUINO_ARCH_ESP8266
MDNS.update();
#endif
} }

File diff suppressed because one or more lines are too long

@ -84,7 +84,6 @@
</script> </script>
</head> </head>
<body id="index" style="margin:0; padding:0;" onload="onBodyLoad()"> <body id="index" style="margin:0; padding:0;" onload="onBodyLoad()">
<div><p><a href="/_ac">MENU</a></p></div>
<div id="controls" style="display: block; border: 1px solid rgb(68, 68, 68); padding: 5px; margin: 5px; width: 362px; background-color: rgb(238, 238, 238);"> <div id="controls" style="display: block; border: 1px solid rgb(68, 68, 68); padding: 5px; margin: 5px; width: 362px; background-color: rgb(238, 238, 238);">
<label>Period (ms):</label> <label>Period (ms):</label>
<input type="number" id="refresh-rate"/> <input type="number" id="refresh-rate"/>
@ -94,5 +93,8 @@
<div id="heap"></div> <div id="heap"></div>
<div id="analog"></div> <div id="analog"></div>
<div id="digital"></div> <div id="digital"></div>
<p style="padding-top:15px;text-align:center">
<a href="/_ac"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAC2klEQVRIS61VvWsUQRSfmU2pon9BUIkQUaKFaCBKgooSb2d3NSSFKbQR/KrEIiIKBiGF2CgRxEpjQNHs7mwOUcghwUQ7g58IsbGxEBWsb2f8zR177s3t3S2cA8ftzPu993vzvoaSnMu2vRKlaqgKp74Q/tE8qjQPyHGcrUrRjwlWShmDbFMURd/a6TcQwNiYUmpFCPElUebcuQ2vz6aNATMVReHEPwzfSSntDcNwNo2rI+DcvQzhpAbA40VKyV0p1Q9snzBG1qYVcYufXV1sREraDcxpyHdXgkfpRBj6Uwm2RsC5dxxmZ9pdOY9cKTISRcHTCmGiUCh4fYyplTwG2mAUbtMTBMHXOgK9QfyXEZr+TkgQ1oUwDA40hEgfIAfj+HuQRaBzAs9eKyUZ5Htx+T3ZODKG8DzOJMANhmGomJVMXPll+hx9UUAlzZrJJ4QNCDG3VEfguu7mcpmcB/gkBOtShhQhchAlu5jlLUgc9ENgyP5gf9+y6LTv+58p5zySkgwzLNOIGc8sEoT1Lc53NMlbCQQuvMxeCME1NNPVVkmH/i3IzzXDtCSA0qQQwZWOCJDY50jsQRjJmkslEOxvTcDRO6zPxOh5xZglKkYLhWM9jMVnkIsTyMT6NBj7IbOCEjm6HxNVVTo2WXqEWJZ1T8rytB6GxizyDkPhWVpBqfiXUtbo/HywYJSpA9kMamNNPZ71R9Hcm+TMHHZNGw3EuraXEUldbfvw25UdOjqOt+JhMwJd7+jSTpZaEiIcaCDwPK83jtWnTkwnunFMtxeL/ge9r4XItt1RNNaj/0GAcV2bR3U5sG3nEh6M61US+Qrfd9Bs31GGulI2GOS/8dgcQZV1w+ApjIxB7TDwF9GcNzJzoA+rD0/8HvPnXQJCt2qFCwbBTfRI7UyXumWVt+HJ9NO4XI++bdsb0YyrqXmlh+AWOLHaLqS5CLQR5EggR3YlcVS9gKeH2hnX8r8Kmi1CAsl36QAAAABJRU5ErkJggg==" border="0" title="AutoConnect menu" alt="AutoConnect menu"/></a>
</p>
</body> </body>
</html> </html>

File diff suppressed because one or more lines are too long

@ -1,97 +0,0 @@
<!--
FSWebServer - Example Index Page
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the ESP8266WebServer library for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>ESP Monitor</title>
<script type="text/javascript" src="graphs.js"></script>
<script type="text/javascript">
var heap,temp,digi;
var reloadPeriod = 1000;
var running = false;
function loadValues(){
if(!running) return;
var xh = new XMLHttpRequest();
xh.onreadystatechange = function(){
if (xh.readyState == 4){
if(xh.status == 200) {
var res = JSON.parse(xh.responseText);
heap.add(res.heap);
temp.add(res.analog);
digi.add(res.gpio);
if(running) setTimeout(loadValues, reloadPeriod);
} else running = false;
}
};
xh.open("GET", "/all", true);
xh.send(null);
};
function run(){
if(!running){
running = true;
loadValues();
}
}
function onBodyLoad(){
var refreshInput = document.getElementById("refresh-rate");
refreshInput.value = reloadPeriod;
refreshInput.onchange = function(e){
var value = parseInt(e.target.value);
reloadPeriod = (value > 0)?value:0;
e.target.value = reloadPeriod;
}
var stopButton = document.getElementById("stop-button");
stopButton.onclick = function(e){
running = false;
}
var startButton = document.getElementById("start-button");
startButton.onclick = function(e){
run();
}
// Example with 10K thermistor
//function calcThermistor(v) {
// var t = Math.log(((10230000 / v) - 10000));
// t = (1/(0.001129148+(0.000234125*t)+(0.0000000876741*t*t*t)))-273.15;
// return (t>120)?0:Math.round(t*10)/10;
//}
//temp = createGraph(document.getElementById("analog"), "Temperature", 100, 128, 10, 40, false, "cyan", calcThermistor);
temp = createGraph(document.getElementById("analog"), "Analog Input", 100, 128, 0, 1023, false, "cyan");
heap = createGraph(document.getElementById("heap"), "Current Heap", 100, 125, 0, 30000, true, "orange");
digi = createDigiGraph(document.getElementById("digital"), "GPIO", 100, 146, [0, 4, 5, 16], "gold");
run();
}
</script>
</head>
<body id="index" style="margin:0; padding:0;" onload="onBodyLoad()">
<div id="controls" style="display: block; border: 1px solid rgb(68, 68, 68); padding: 5px; margin: 5px; width: 362px; background-color: rgb(238, 238, 238);">
<label>Period (ms):</label>
<input type="number" id="refresh-rate"/>
<input type="button" id="start-button" value="Start"/>
<input type="button" id="stop-button" value="Stop"/>
</div>
<div id="heap"></div>
<div id="analog"></div>
<div id="digital"></div>
</body>
</html>
Loading…
Cancel
Save