diff --git a/lib/webdav/AsyncWebDav.h b/lib/webdav/AsyncWebDav.h index 20805ad..7d973ac 100644 --- a/lib/webdav/AsyncWebDav.h +++ b/lib/webdav/AsyncWebDav.h @@ -67,10 +67,12 @@ public: if (fileFound) { - toBuffer(buffer, path_.c_str()); + //toBuffer(buffer, path_.c_str()); toBuffer(buffer, RESPONSE_START); toBuffer(buffer, HREF_START); - String fileBaseName = dir_.fileName().substring(0, dir_.fileName().indexOf('_')); + const auto fileName = dir_.fileName(); + const auto fileNameWithoutDir = fileName.substring(fileName.lastIndexOf("/") + 1); + String fileBaseName = fileNameWithoutDir.substring(0, fileNameWithoutDir.indexOf('_')); fileBaseName += ".st"; toBuffer(buffer, fileBaseName.c_str()); toBuffer(buffer, HREF_END); @@ -85,7 +87,7 @@ public: else { toBuffer(buffer, CONTENTLEN_START); - String fileSizeStr(getFileSize(dir_.fileName())); + String fileSizeStr(getFileSize(fileName)); toBuffer(buffer, fileSizeStr.c_str()); toBuffer(buffer, CONTENTLEN_END); } @@ -140,47 +142,26 @@ private: bool finished_; }; -void listFiles(AsyncResponseStream *response, const char *folderPath, Dir *dir) +bool deleteMeasurementFiles(const String &stName, const String &folder) { - using namespace webdav_constants; - response->println(MULTISTATUS_START); - - Serial.println("Before rewind"); - dir->rewind(); - Serial.println("After rewind"); - while (dir->next()) + String baseName = folder + "/" + stName.substring(0, stName.indexOf(".")); + int counter = 0; { - Serial.println("Inside dir loop"); - Serial.println(folderPath); - Serial.println(dir->fileName()); - - response->print(RESPONSE_START); - response->print(HREF_START); - //response->print(folderPath); - response->print(dir->fileName()); - response->print(HREF_END); - response->print(PROPSTAT_START); - response->print(PROP_START); - if (dir->isDirectory()) - { - response->print(RESOURCETYPE_START); - response->print(RESOURCE_COLLECTION); - response->print(RESOURCETYPE_END); - } - else - { - response->print(CONTENTLEN_START); - response->print(dir->fileSize(), DEC); - response->print(CONTENTLEN_END); - } - - response->print(PROP_END); - response->print(STATUS_OK); - response->print(PROPSTAT_END); - response->println(webdav_constants::RESPONSE_END); - break; + auto d = SPIFFS.openDir(folder); + while (d.next()) + if (d.isFile() && d.fileName().startsWith(baseName)) + ++counter; } - response->println(MULTISTATUS_END); + if (counter == 0) + return false; + + for (int i = 0; i < counter; ++i) + { + const String pathToDelete = baseName + "_" + String(i); + if (!SPIFFS.remove(pathToDelete)) + return false; + } + return true; } class SpiffsWebDavHandler : public AsyncWebHandler @@ -202,8 +183,7 @@ public: { if (request->url() == prefix_ + "/" && (request->method() == HTTP_GET || request->method() == HTTP_PROPFIND)) { - Serial.println("Propfind start"); - //AsyncResponseStream *response = request->beginResponseStream("application/xml"); + // send chunked response - it is too large to send in one go auto response = request->beginChunkedResponse("application/xml", WebdavFileListCallback(folder_)); request->send(response); @@ -224,28 +204,20 @@ public: { auto path = folder_ + request->url().substring(prefix_.length()); if (SPIFFS.exists(path)) - { request->send(SPIFFS, path, "application/x-msgpack"); - } else - { request->send(404, "text/plain", "Webdav: File not found"); - } } else if (request->method() == HTTP_DELETE) { - auto path = folder_ + request->url().substring(prefix_.length()); - if (SPIFFS.exists(path)) - { - if (SPIFFS.remove(path)) - request->send(204, "text/plain", "Success"); - else - request->send(404, "text/plain", "Webdav: Invalid delete request"); - } + auto stFileName = request->url().substring(prefix_.length() + 1); + Serial.print("HTTP_DELETE for "); + Serial.println(stFileName); + bool deleteSuccessful = deleteMeasurementFiles(stFileName, folder_); + if (deleteSuccessful) + request->send(204, "text/plain", "Success"); else - { request->send(404, "text/plain", "Webdav: File to delete not found"); - } } else {