Webdav delete working

This commit is contained in:
Martin Bauer 2020-05-19 22:03:10 +02:00
parent be4a4a13bd
commit 123c2a534b
1 changed files with 29 additions and 57 deletions

View File

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