From 7500e356cd6c4c5825975d997e7d272e4df06b62 Mon Sep 17 00:00:00 2001 From: amandel Date: Thu, 5 Aug 2021 20:48:33 +0200 Subject: [PATCH 1/2] Poor man implementation of delete all obs related files --- src/configServer.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/src/configServer.cpp b/src/configServer.cpp index ab2b6071..972e94e5 100644 --- a/src/configServer.cpp +++ b/src/configServer.cpp @@ -392,9 +392,11 @@ static const char* const deleteIndex = "" "" "

SD Card

" - "" - "" - ""; + "" + "" + ""; static const char* const settingsSecurityIndex = "

Http

" @@ -2065,6 +2067,62 @@ static void handleDelete(HTTPRequest *, HTTPResponse * res) { sendHtml(res, html); } +static void deleteFilesFromDirectory(File dir) { + File entry = dir.openNextFile(); + while (entry) { + if (!entry.isDirectory()) { + String fileName = entry.name(); + entry.close(); + log_i("Will delete %s", fileName.c_str()); + SD.remove(fileName); + } + entry = dir.openNextFile(); + } +} + +static void deleteFilesFromDirectory(String dirName) { + File dir = SD.open(dirName); + if (dir.isDirectory()) { + deleteFilesFromDirectory(dir); + } + dir.close(); +} + +static void deleteObsdataFiles() { + File dir = SD.open("/"); + if (dir.isDirectory()) { + File entry = dir.openNextFile(); + while (entry) { + if (!entry.isDirectory()) { + String fileName = entry.name(); + entry.close(); + if (fileName.endsWith("obsdata.csv")) { + log_d("Will delete %s", fileName.c_str()); + SD.remove(fileName); + } + } + entry = dir.openNextFile(); + } + } + dir.close(); +} + +static void deleteAllFromSd() { + // ald_ini.ubx, tracknumber.txt, current_14d.*, *.obsdata.csv, sdflash/*, trash/*, uploaded/* + deleteFilesFromDirectory("/trash"); + SD.rmdir("/trash"); + deleteFilesFromDirectory("/uploaded"); + SD.rmdir("/uploaded"); + deleteFilesFromDirectory("/sdflash"); + SD.rmdir("/sdflash"); + SD.remove("/tracknumber.txt"); + SD.remove("/aid_ini.ubx"); + SD.remove(LAST_MODIFIED_HEADER_FILE_NAME); + SD.remove(ALP_DATA_FILE_NAME); + SD.remove(ALP_NEW_DATA_FILE_NAME); + deleteObsdataFiles(); +} + static void handleDeleteAction(HTTPRequest *req, HTTPResponse * res) { // TODO: Result page with status! const auto params = extractParameters(req); @@ -2080,9 +2138,16 @@ static void handleDeleteAction(HTTPRequest *req, HTTPResponse * res) { } } if (getParameter(params, "config") == "on") { +#ifdef CUSTOM_OBS_DEFAULT_CONFIG + theObsConfig->parseJson(CUSTOM_OBS_DEFAULT_CONFIG); +#else theObsConfig->parseJson("{}"); +#endif theObsConfig->fill(config); } + if (getParameter(params, "sdcard") == "on") { + deleteAllFromSd(); + } sendRedirect(res, "/"); } From 0700aab3d3ac2a5a57fc31ffb0419d402ab5874c Mon Sep 17 00:00:00 2001 From: amandel Date: Thu, 5 Aug 2021 21:04:35 +0200 Subject: [PATCH 2/2] Really delete files if marked for deletion in /trash --- src/configServer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/configServer.cpp b/src/configServer.cpp index 972e94e5..3c2812de 100644 --- a/src/configServer.cpp +++ b/src/configServer.cpp @@ -1602,7 +1602,7 @@ static void handleDeleteFiles(HTTPRequest *req, HTTPResponse * res) { log_w("Failed to moved '%s' to /", fullName.c_str()); html += HTML_ENTITY_FAILED_CROSS; } - } else if (path != "trash") { + } else if (path != "/trash") { if (SD.rename(fullName, "/trash/" + file)) { log_i("Moved '%s'.", fullName.c_str()); html += HTML_ENTITY_WASTEBASKET;