From 794ab9ec1b7581f88f9f5da0687102757929cab4 Mon Sep 17 00:00:00 2001 From: luc lebosse Date: Mon, 11 Jul 2016 23:48:54 +0200 Subject: [PATCH] Support of subdirectories in SPIFFS Allows navigation as normal Filesystem add /delete directory parse directory tree allows user to upload/delete in /user directory only when admin has full access --- esp3d/config.h | 2 +- esp3d/data/settings.tpl | 118 ++++++++++++++++++++++++------ esp3d/webinterface.cpp | 157 ++++++++++++++++++++++++++++++++++------ 3 files changed, 234 insertions(+), 43 deletions(-) diff --git a/esp3d/config.h b/esp3d/config.h index 5ec4ef6b..78908c51 100644 --- a/esp3d/config.h +++ b/esp3d/config.h @@ -68,7 +68,7 @@ #include #include "wifi.h" //version and sources location -#define FW_VERSION "0.7.73" +#define FW_VERSION "0.7.8" #define REPOSITORY "https://github.com/luc-github/ESP8266" diff --git a/esp3d/data/settings.tpl b/esp3d/data/settings.tpl index 4c28307d..127ef258 100644 --- a/esp3d/data/settings.tpl +++ b/esp3d/data/settings.tpl @@ -1,8 +1,8 @@ $INCLUDE[header.inc]$ $INCLUDE[css2.inc]$ - +
Extra Settings
@@ -33,13 +33,56 @@ $SUCCESS_MSG$   

-
Namesize
+ +
+
+ + +
+
 
+
+
TypeNameSize
$INCLUDE[footer.inc]$ diff --git a/esp3d/webinterface.cpp b/esp3d/webinterface.cpp index b12c0e55..295467c1 100644 --- a/esp3d/webinterface.cpp +++ b/esp3d/webinterface.cpp @@ -2764,42 +2764,157 @@ void handleFileList() return; } String path ; - String status="Ok"; + String status = "Ok"; + //be sure root is correct according authentication if (auth_level == LEVEL_ADMIN) path = "/"; else path = "/user"; - if(web_interface->WebServer.hasArg("path"))path += web_interface->WebServer.hasArg("path"); + //get current path + if(web_interface->WebServer.hasArg("path"))path += web_interface->WebServer.arg("path") ; + //to have a clean path + path.trim(); + path.replace("//","/"); + if (path[path.length()-1] !='/')path +="/"; + //check if query need some action if(web_interface->WebServer.hasArg("action")) { - if(web_interface->WebServer.arg("action")=="delete" && web_interface->WebServer.hasArg("filename")) { + //delete a file + if(web_interface->WebServer.arg("action") == "delete" && web_interface->WebServer.hasArg("filename")) { String filename; - filename = web_interface->WebServer.arg("filename"); + String shortname = web_interface->WebServer.arg("filename"); + shortname.replace("/",""); + filename = path + web_interface->WebServer.arg("filename"); + filename.replace("//","/"); if(!SPIFFS.exists(filename)) { - status="Cannot delete, file not found!"; + status = shortname + F(" does not exists!"); } else { - SPIFFS.remove(filename); + if (SPIFFS.remove(filename)) + { + status = shortname + F(" deleted"); + //what happen if no "/." and no other subfiles ? + Dir dir = SPIFFS.openDir(path); + if (!dir.next()) + { //keep directory alive even empty + File r = SPIFFS.open(path+"/.","w"); + if (r)r.close(); + } + } + else { + status = F("Cannot deleted ") ; + status+=shortname ; + } + } + } + //delete a directory + if(web_interface->WebServer.arg("action") == "deletedir" && web_interface->WebServer.hasArg("filename")) { + String filename; + String shortname = web_interface->WebServer.arg("filename"); + shortname.replace("/",""); + filename = path + web_interface->WebServer.arg("filename"); + filename += "/."; + filename.replace("//","/"); + if (filename != "/") + { + bool delete_error = false; + Dir dir = SPIFFS.openDir(path + shortname); + { + while (dir.next()) { + String fullpath = dir.fileName(); + if (!SPIFFS.remove(dir.fileName())) { + delete_error = true; + status = F("Cannot deleted ") ; + status+=fullpath; + } + } + } + if (!delete_error){ + status = shortname ; + status+=" deleted"; + } + } + } + //create a directory + if(web_interface->WebServer.arg("action")=="createdir" && web_interface->WebServer.hasArg("filename")) { + String filename; + filename = path + web_interface->WebServer.arg("filename") +"/."; + String shortname = web_interface->WebServer.arg("filename"); + shortname.replace("/",""); + filename.replace("//","/"); + if(SPIFFS.exists(filename)) { + status = shortname + F(" already exists!"); + } else { + File r = SPIFFS.open(filename,"w"); + if (!r) { + status = F("Cannot create "); + status += shortname ; + } + else { + r.close(); + status = shortname + F(" created"); + } } } } - String jsonfile = "{\"path\":\"" + path + "\","; + String jsonfile = "{"; Dir dir = SPIFFS.openDir(path); jsonfile+="\"files\":["; bool firstentry=true; + String subdirlist=""; while (dir.next()) { - if (!firstentry) { - jsonfile+=","; - } else { - firstentry=false; - } - jsonfile+="{"; - jsonfile+="\"name\":\""; - jsonfile+=dir.fileName(); - jsonfile+="\",\"size\":\""; - File f = dir.openFile("r"); - jsonfile+=formatBytes(f.size()); - jsonfile+="\""; - jsonfile+="}"; - f.close(); + String filename = dir.fileName(); + String size =""; + bool addtolist=true; + //remove path from name + filename = filename.substring(path.length(),filename.length()); + //check if file or subfile + if (filename.indexOf("/")>-1) { + //Do not rely on "/." to define directory as SPIFFS upload won't create it but directly files + //and no need to overload SPIFFS if not necessary to create "/." if no need + //it will reduce SPIFFS available space so limit it to creation + filename = filename.substring(0,filename.indexOf("/")); + String tag="*"; + tag = filename + "*"; + if (subdirlist.indexOf(tag)>-1) //already in list + { + addtolist = false; //no need to add + } + else + { + size = -1; //it is subfile so display only directory, size will be -1 to describe it is directory + if (subdirlist.length()==0)subdirlist+="*"; + subdirlist += filename + "*"; //add to list + } + } + else + { + //do not add "." file + if (filename!=".") + { + File f = dir.openFile("r"); + size = formatBytes(f.size()); + f.close(); + } + else + { + addtolist = false; + } + } + if(addtolist) + { + if (!firstentry) { + jsonfile+=","; + } else { + firstentry=false; + } + jsonfile+="{"; + jsonfile+="\"name\":\""; + jsonfile+=filename; + jsonfile+="\",\"size\":\""; + jsonfile+=size; + jsonfile+="\""; + jsonfile+="}"; + } } jsonfile+="],"; + jsonfile+="\"path\":\"" + path + "\","; jsonfile+="\"status\":\"" + status + "\","; FSInfo info; SPIFFS.info(info);