Webdav delete working
This commit is contained in:
parent
be4a4a13bd
commit
123c2a534b
|
@ -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;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
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
|
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,28 +204,20 @@ 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_);
|
||||||
request->send(204, "text/plain", "Success");
|
if (deleteSuccessful)
|
||||||
else
|
request->send(204, "text/plain", "Success");
|
||||||
request->send(404, "text/plain", "Webdav: Invalid delete request");
|
|
||||||
}
|
|
||||||
else
|
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
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue