From 215408024e4072c893a357ace6d569926873ea0b Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Thu, 10 Jun 2021 12:34:58 -1000 Subject: [PATCH] Added $LocalFS/Delete especially useful for $LocalFS/Delete=config.yaml --- Grbl_Esp32/src/WebUI/WebSettings.cpp | 37 +++++++++++++++++----------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/Grbl_Esp32/src/WebUI/WebSettings.cpp b/Grbl_Esp32/src/WebUI/WebSettings.cpp index 2f969022..0f3553a7 100644 --- a/Grbl_Esp32/src/WebUI/WebSettings.cpp +++ b/Grbl_Esp32/src/WebUI/WebSettings.cpp @@ -706,34 +706,29 @@ namespace WebUI { return Error::Ok; } - static Error deleteSDObject(char* parameter, AuthenticationLevel auth_level) { // ESP215 - parameter = trim(parameter); - if (*parameter == '\0') { + static Error deleteObject(fs::FS fs, char* name) { + name = trim(name); + if (*name == '\0') { webPrintln("Missing file name!"); return Error::InvalidValue; } - SDCard::State state = config->_sdCard->get_state(true); - if (state != SDCard::State::Idle) { - webPrintln((state == SDCard::State::NotPresent) ? "No SD card" : "Busy"); - return Error::Ok; - } - String path = parameter; - if (parameter[0] != '/') { + String path = name; + if (name[0] != '/') { path = "/" + path; } - File file2del = SD.open(path); + File file2del = fs.open(path); if (!file2del) { webPrintln("Cannot stat file!"); return Error::FsFileNotFound; } if (file2del.isDirectory()) { - if (!SD.rmdir(path)) { + if (!fs.rmdir(path)) { webPrintln("Cannot delete directory! Is directory empty?"); return Error::FsFailedDelDir; } webPrintln("Directory deleted."); } else { - if (!SD.remove(path)) { + if (!fs.remove(path)) { webPrintln("Cannot delete file!"); return Error::FsFailedDelFile; } @@ -743,6 +738,15 @@ namespace WebUI { return Error::Ok; } + static Error deleteSDObject(char* parameter, AuthenticationLevel auth_level) { // ESP215 + SDCard::State state = config->_sdCard->get_state(true); + if (state != SDCard::State::Idle) { + webPrintln((state == SDCard::State::NotPresent) ? "No SD card" : "Busy"); + return Error::Ok; + } + return deleteObject(SD, parameter); + } + static Error listSDFiles(char* parameter, AuthenticationLevel auth_level) { // ESP210 SDCard::State state = config->_sdCard->get_state(true); if (state != SDCard::State::Idle) { @@ -765,7 +769,7 @@ namespace WebUI { return Error::Ok; } - void listDirLocalFS(fs::FS& fs, const char* dirname, uint8_t levels, uint8_t client) { + void listDirLocalFS(fs::FS fs, const char* dirname, uint8_t levels, uint8_t client) { //char temp_filename[128]; // to help filter by extension TODO: 128 needs a definition based on something File root = fs.open(dirname); if (!root) { @@ -791,6 +795,8 @@ namespace WebUI { } } + static Error deleteLocalFile(char* parameter, AuthenticationLevel auth_level) { return deleteObject(SPIFFS, parameter); } + static Error listLocalFiles(char* parameter, AuthenticationLevel auth_level) { // No ESP command webPrintln(""); listDirLocalFS(SPIFFS, "/", 10, espresponse->client()); @@ -802,7 +808,7 @@ namespace WebUI { return Error::Ok; } - static void listDirJSON(fs::FS& fs, const char* dirname, uint8_t levels, JSONencoder* j) { + static void listDirJSON(fs::FS fs, const char* dirname, uint8_t levels, JSONencoder* j) { File root = fs.open(dirname); File file = root.openNextFile(); while (file) { @@ -1026,6 +1032,7 @@ namespace WebUI { new WebCommand("path", WEBCMD, WU, "ESP700", "LocalFS/Run", runLocalFile); new WebCommand("path", WEBCMD, WU, NULL, "LocalFS/List", listLocalFiles); new WebCommand("path", WEBCMD, WU, NULL, "LocalFS/ListJSON", listLocalFilesJSON); + new WebCommand("path", WEBCMD, WU, NULL, "LocalFS/Delete", deleteLocalFile); #endif #ifdef ENABLE_NOTIFICATIONS new WebCommand(