mirror of
https://github.com/meshtastic/firmware.git
synced 2025-04-27 18:31:37 +00:00
LittleFS recursive display and erase. Cause we got directories now, baby!
This commit is contained in:
parent
b9058ce7c5
commit
697c749a8d
@ -1,22 +1,43 @@
|
|||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
#include "FSCommon.h"
|
#include "FSCommon.h"
|
||||||
|
|
||||||
|
void listDir(fs::FS &fs, const char * dirname, uint8_t levels)
|
||||||
|
#ifdef FSCom
|
||||||
|
{
|
||||||
|
File root = fs.open(dirname);
|
||||||
|
if(!root){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!root.isDirectory()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = root.openNextFile();
|
||||||
|
while(file){
|
||||||
|
if(file.isDirectory()){
|
||||||
|
if(levels){
|
||||||
|
listDir(fs, file.name(), levels -1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DEBUG_MSG(" %s (%i Bytes)\n", file.name(), file.size());
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
file = root.openNextFile();
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void fsInit()
|
void fsInit()
|
||||||
{
|
{
|
||||||
#ifdef FSCom
|
#ifdef FSCom
|
||||||
if (!FSBegin())
|
if (!FSBegin())
|
||||||
{
|
{
|
||||||
DEBUG_MSG("ERROR filesystem mount Failed\n");
|
DEBUG_MSG("ERROR filesystem mount Failed. Formatting...\n");
|
||||||
assert(0); // FIXME - report failure to phone
|
assert(0); // FIXME - report failure to phone
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_MSG("Filesystem files:\n");
|
DEBUG_MSG("Filesystem files:\n");
|
||||||
File dir = FSCom.open("/");
|
listDir(FSCom, "/", 10);
|
||||||
File f = dir.openNextFile();
|
|
||||||
while (f) {
|
|
||||||
DEBUG_MSG(" %s\n", f.name());
|
|
||||||
f.close();
|
|
||||||
f = dir.openNextFile();
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -272,48 +272,85 @@ void handleAPIv1ToRadio(HTTPRequest *req, HTTPResponse *res)
|
|||||||
DEBUG_MSG("webAPI handleAPIv1ToRadio\n");
|
DEBUG_MSG("webAPI handleAPIv1ToRadio\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleFsBrowseStatic(HTTPRequest *req, HTTPResponse *res)
|
bool firstFile = 1;
|
||||||
|
|
||||||
|
void htmlDeleteDir(fs::FS &fs, const char * dirname)
|
||||||
{
|
{
|
||||||
|
File root = fs.open(dirname);
|
||||||
res->setHeader("Content-Type", "application/json");
|
if(!root){
|
||||||
res->setHeader("Access-Control-Allow-Origin", "*");
|
return;
|
||||||
res->setHeader("Access-Control-Allow-Methods", "GET");
|
}
|
||||||
|
if(!root.isDirectory()){
|
||||||
File root = FSCom.open("/");
|
return;
|
||||||
|
}
|
||||||
if (root.isDirectory()) {
|
|
||||||
res->println("{");
|
|
||||||
res->println("\"data\": {");
|
|
||||||
|
|
||||||
File file = root.openNextFile();
|
File file = root.openNextFile();
|
||||||
res->print("\"files\": [");
|
while(file){
|
||||||
bool firstFile = 1;
|
if(file.isDirectory()){
|
||||||
while (file) {
|
htmlDeleteDir(fs, file.name());
|
||||||
String filePath = String(file.name());
|
file.close();
|
||||||
if (filePath.indexOf("/static") == 0) {
|
} else {
|
||||||
|
String fileName = String(file.name());
|
||||||
|
file.close();
|
||||||
|
DEBUG_MSG(" %s\n", fileName.c_str());
|
||||||
|
fs.remove(fileName);
|
||||||
|
|
||||||
|
}
|
||||||
|
file = root.openNextFile();
|
||||||
|
}
|
||||||
|
root.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void htmlListDir(fs::FS &fs, HTTPResponse *res, const char * dirname, uint8_t levels)
|
||||||
|
{
|
||||||
|
File root = fs.open(dirname);
|
||||||
|
if(!root){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!root.isDirectory()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = root.openNextFile();
|
||||||
|
while(file){
|
||||||
|
if(file.isDirectory()){
|
||||||
|
if(levels){
|
||||||
|
htmlListDir(fs, res, file.name(), levels -1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (firstFile) {
|
if (firstFile) {
|
||||||
firstFile = 0;
|
firstFile = 0;
|
||||||
} else {
|
} else {
|
||||||
res->println(",");
|
res->println(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
res->println("{");
|
res->println("{");
|
||||||
|
|
||||||
if (String(file.name()).substring(1).endsWith(".gz")) {
|
if (String(file.name()).substring(1).endsWith(".gz")) {
|
||||||
String modifiedFile = String(file.name()).substring(1);
|
String modifiedFile = String(file.name()).substring(1);
|
||||||
modifiedFile.remove((modifiedFile.length() - 3), 3);
|
modifiedFile.remove((modifiedFile.length() - 3), 3);
|
||||||
res->print("\"nameModified\": \"" + modifiedFile + "\",");
|
res->print("\"nameModified\": \"" + modifiedFile + "\",");
|
||||||
res->print("\"name\": \"" + String(file.name()).substring(1) + "\",");
|
res->print("\"name\": \"" + String(file.name()).substring(1) + "\",");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
res->print("\"name\": \"" + String(file.name()).substring(1) + "\",");
|
res->print("\"name\": \"" + String(file.name()).substring(1) + "\",");
|
||||||
}
|
}
|
||||||
res->print("\"size\": " + String(file.size()));
|
res->print("\"size\": " + String(file.size()));
|
||||||
res->print("}");
|
res->print("}");
|
||||||
}
|
}
|
||||||
|
file.close();
|
||||||
file = root.openNextFile();
|
file = root.openNextFile();
|
||||||
}
|
}
|
||||||
|
root.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleFsBrowseStatic(HTTPRequest *req, HTTPResponse *res)
|
||||||
|
{
|
||||||
|
|
||||||
|
res->setHeader("Content-Type", "application/json");
|
||||||
|
res->setHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
res->setHeader("Access-Control-Allow-Methods", "GET");
|
||||||
|
res->println("{");
|
||||||
|
res->println("\"data\": {");
|
||||||
|
res->print("\"files\": [");
|
||||||
|
htmlListDir(FSCom, res, "/", 10);
|
||||||
res->print("],");
|
res->print("],");
|
||||||
res->print("\"filesystem\" : {");
|
res->print("\"filesystem\" : {");
|
||||||
res->print("\"total\" : " + String(FSCom.totalBytes()) + ",");
|
res->print("\"total\" : " + String(FSCom.totalBytes()) + ",");
|
||||||
@ -323,9 +360,9 @@ void handleFsBrowseStatic(HTTPRequest *req, HTTPResponse *res)
|
|||||||
res->println("},");
|
res->println("},");
|
||||||
res->println("\"status\": \"ok\"");
|
res->println("\"status\": \"ok\"");
|
||||||
res->println("}");
|
res->println("}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void handleFsDeleteStatic(HTTPRequest *req, HTTPResponse *res)
|
void handleFsDeleteStatic(HTTPRequest *req, HTTPResponse *res)
|
||||||
{
|
{
|
||||||
ResourceParameters *params = req->getParams();
|
ResourceParameters *params = req->getParams();
|
||||||
@ -369,6 +406,11 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res)
|
|||||||
|
|
||||||
bool has_set_content_type = false;
|
bool has_set_content_type = false;
|
||||||
|
|
||||||
|
if (filename == "/static/") {
|
||||||
|
filename = "/static/index.html";
|
||||||
|
filenameGzip = "/static/index.html.gz";
|
||||||
|
}
|
||||||
|
|
||||||
if (FSCom.exists(filename.c_str())) {
|
if (FSCom.exists(filename.c_str())) {
|
||||||
file = FSCom.open(filename.c_str());
|
file = FSCom.open(filename.c_str());
|
||||||
if (!file.available()) {
|
if (!file.available()) {
|
||||||
@ -715,19 +757,9 @@ void handleUpdateFs(HTTPRequest *req, HTTPResponse *res)
|
|||||||
if (streamptr != nullptr) {
|
if (streamptr != nullptr) {
|
||||||
DEBUG_MSG("Connection to content server ... success!\n");
|
DEBUG_MSG("Connection to content server ... success!\n");
|
||||||
|
|
||||||
File root = FSCom.open("/");
|
|
||||||
File file = root.openNextFile();
|
|
||||||
|
|
||||||
DEBUG_MSG("Deleting files from /static : \n");
|
DEBUG_MSG("Deleting files from /static : \n");
|
||||||
|
|
||||||
while (file) {
|
htmlDeleteDir(FSCom, "/static");
|
||||||
String filePath = String(file.name());
|
|
||||||
if (filePath.indexOf("/static") == 0) {
|
|
||||||
DEBUG_MSG(" %s\n", file.name());
|
|
||||||
FSCom.remove(file.name());
|
|
||||||
}
|
|
||||||
file = root.openNextFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(5); // Let other network operations run
|
delay(5); // Let other network operations run
|
||||||
|
|
||||||
@ -754,6 +786,8 @@ void handleUpdateFs(HTTPRequest *req, HTTPResponse *res)
|
|||||||
if (!TARUnpacker->tarStreamExpander(streamptr, streamSize, FSCom, "/static")) {
|
if (!TARUnpacker->tarStreamExpander(streamptr, streamSize, FSCom, "/static")) {
|
||||||
res->printf("tarStreamExpander failed with return code #%d\n", TARUnpacker->tarGzGetError());
|
res->printf("tarStreamExpander failed with return code #%d\n", TARUnpacker->tarGzGetError());
|
||||||
Serial.printf("tarStreamExpander failed with return code #%d\n", TARUnpacker->tarGzGetError());
|
Serial.printf("tarStreamExpander failed with return code #%d\n", TARUnpacker->tarGzGetError());
|
||||||
|
// Close the connection on error and free up memory
|
||||||
|
client->stop();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
@ -773,6 +807,8 @@ void handleUpdateFs(HTTPRequest *req, HTTPResponse *res)
|
|||||||
res->printf("Failed to establish http connection\n");
|
res->printf("Failed to establish http connection\n");
|
||||||
Serial.println("Failed to establish http connection");
|
Serial.println("Failed to establish http connection");
|
||||||
return;
|
return;
|
||||||
|
// Close the connection on error and free up memory
|
||||||
|
client->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
res->println("Done! Restarting the device. <a href=/>Click this in 10 seconds</a>");
|
res->println("Done! Restarting the device. <a href=/>Click this in 10 seconds</a>");
|
||||||
@ -794,19 +830,10 @@ void handleDeleteFsContent(HTTPRequest *req, HTTPResponse *res)
|
|||||||
res->println("<h1>Meshtastic</h1>\n");
|
res->println("<h1>Meshtastic</h1>\n");
|
||||||
res->println("Deleting Content in /static/*");
|
res->println("Deleting Content in /static/*");
|
||||||
|
|
||||||
File root = FSCom.open("/");
|
DEBUG_MSG("Deleting files from /static/* : \n");
|
||||||
File file = root.openNextFile();
|
|
||||||
|
|
||||||
DEBUG_MSG("Deleting files from /static : \n");
|
htmlDeleteDir(FSCom, "/static");
|
||||||
|
|
||||||
while (file) {
|
|
||||||
String filePath = String(file.name());
|
|
||||||
if (filePath.indexOf("/static") == 0) {
|
|
||||||
DEBUG_MSG(" %s\n", file.name());
|
|
||||||
FSCom.remove(file.name());
|
|
||||||
}
|
|
||||||
file = root.openNextFile();
|
|
||||||
}
|
|
||||||
res->println("<p><hr><p><a href=/admin>Back to admin</a>\n");
|
res->println("<p><hr><p><a href=/admin>Back to admin</a>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user