1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-01 02:21:46 +02:00

Added $LocalFS/Delete

especially useful for $LocalFS/Delete=config.yaml
This commit is contained in:
Mitch Bradley
2021-06-10 12:34:58 -10:00
parent 54aefa54fd
commit 215408024e

View File

@@ -706,34 +706,29 @@ namespace WebUI {
return Error::Ok; return Error::Ok;
} }
static Error deleteSDObject(char* parameter, AuthenticationLevel auth_level) { // ESP215 static Error deleteObject(fs::FS fs, char* name) {
parameter = trim(parameter); name = trim(name);
if (*parameter == '\0') { if (*name == '\0') {
webPrintln("Missing file name!"); webPrintln("Missing file name!");
return Error::InvalidValue; return Error::InvalidValue;
} }
SDCard::State state = config->_sdCard->get_state(true); String path = name;
if (state != SDCard::State::Idle) { if (name[0] != '/') {
webPrintln((state == SDCard::State::NotPresent) ? "No SD card" : "Busy");
return Error::Ok;
}
String path = parameter;
if (parameter[0] != '/') {
path = "/" + path; path = "/" + path;
} }
File file2del = SD.open(path); File file2del = fs.open(path);
if (!file2del) { if (!file2del) {
webPrintln("Cannot stat file!"); webPrintln("Cannot stat file!");
return Error::FsFileNotFound; return Error::FsFileNotFound;
} }
if (file2del.isDirectory()) { if (file2del.isDirectory()) {
if (!SD.rmdir(path)) { if (!fs.rmdir(path)) {
webPrintln("Cannot delete directory! Is directory empty?"); webPrintln("Cannot delete directory! Is directory empty?");
return Error::FsFailedDelDir; return Error::FsFailedDelDir;
} }
webPrintln("Directory deleted."); webPrintln("Directory deleted.");
} else { } else {
if (!SD.remove(path)) { if (!fs.remove(path)) {
webPrintln("Cannot delete file!"); webPrintln("Cannot delete file!");
return Error::FsFailedDelFile; return Error::FsFailedDelFile;
} }
@@ -743,6 +738,15 @@ namespace WebUI {
return Error::Ok; 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 static Error listSDFiles(char* parameter, AuthenticationLevel auth_level) { // ESP210
SDCard::State state = config->_sdCard->get_state(true); SDCard::State state = config->_sdCard->get_state(true);
if (state != SDCard::State::Idle) { if (state != SDCard::State::Idle) {
@@ -765,7 +769,7 @@ namespace WebUI {
return Error::Ok; 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 //char temp_filename[128]; // to help filter by extension TODO: 128 needs a definition based on something
File root = fs.open(dirname); File root = fs.open(dirname);
if (!root) { 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 static Error listLocalFiles(char* parameter, AuthenticationLevel auth_level) { // No ESP command
webPrintln(""); webPrintln("");
listDirLocalFS(SPIFFS, "/", 10, espresponse->client()); listDirLocalFS(SPIFFS, "/", 10, espresponse->client());
@@ -802,7 +808,7 @@ namespace WebUI {
return Error::Ok; 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 root = fs.open(dirname);
File file = root.openNextFile(); File file = root.openNextFile();
while (file) { while (file) {
@@ -1026,6 +1032,7 @@ namespace WebUI {
new WebCommand("path", WEBCMD, WU, "ESP700", "LocalFS/Run", runLocalFile); 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/List", listLocalFiles);
new WebCommand("path", WEBCMD, WU, NULL, "LocalFS/ListJSON", listLocalFilesJSON); new WebCommand("path", WEBCMD, WU, NULL, "LocalFS/ListJSON", listLocalFilesJSON);
new WebCommand("path", WEBCMD, WU, NULL, "LocalFS/Delete", deleteLocalFile);
#endif #endif
#ifdef ENABLE_NOTIFICATIONS #ifdef ENABLE_NOTIFICATIONS
new WebCommand( new WebCommand(