From fb24f0bc60d9ff910d49646aaeadad99f22826e5 Mon Sep 17 00:00:00 2001 From: Roland Stigge Date: Sun, 7 Jan 2018 16:07:49 +0100 Subject: Implemented file move --- src/webbox.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) (limited to 'src/webbox.cpp') diff --git a/src/webbox.cpp b/src/webbox.cpp index 4575551..8f85332 100644 --- a/src/webbox.cpp +++ b/src/webbox.cpp @@ -290,6 +290,116 @@ int main(int argc, char* argv[]) { } } } + } else if (command == "delete") { // POST! + QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp)); + bool ok; + int contentLength = contentLengthString.toInt(&ok); + + if (!ok) { + FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); + FCGX_PutS(QString("Bad content length").toUtf8().data(), request.out); + } else { + QByteArray content(contentLength, 0); + + int result = FCGX_GetStr(content.data(), content.size(), request.in); + if (result != content.size()) { + FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); + FCGX_PutS(QString("Read error (%1/%2)").arg(result).arg(content.size()).toUtf8().data(), request.out); + } else { + QXmlStreamReader xml(content); + + QString response = ""; + + while (!xml.atEnd()) { + while (xml.readNextStartElement()) { + if (xml.name() == "files") { + while (xml.readNextStartElement()) { + if (xml.name() == "file") { + QString filename = xml.readElementText(); + + QFileInfo fileInfo(path + "/" + filename); + if (fileInfo.isDir()) { + QDir dir(path); + if (!dir.rmdir(filename)) { + response += QString("Error on removing directory %1
").arg(filename); + } + } else if (fileInfo.isFile()) { + QFile file(path + "/" + filename); + if (!file.remove()) { + response += QString("Error on removing file %1
").arg(filename); + } + } else { + response += QString("Error: %1 is neither file nor directory.
").arg(filename); + } + } + } + } + } + } + + if (response == "") { + response = "OK"; + } + + FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); + FCGX_PutS(response.toUtf8().data(), request.out); + } + } + } else if (command == "move") { // POST! + QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp)); + bool ok; + int contentLength = contentLengthString.toInt(&ok); + + if (!ok) { + FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); + FCGX_PutS(QString("Bad content length").toUtf8().data(), request.out); + } else { + QByteArray content(contentLength, 0); + + int result = FCGX_GetStr(content.data(), content.size(), request.in); + if (result != content.size()) { + FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); + FCGX_PutS(QString("Read error (%1/%2)").arg(result).arg(content.size()).toUtf8().data(), request.out); + } else { + QXmlStreamReader xml(content); + + QString response = ""; + QString targetDir; + + while (!xml.atEnd()) { + while (xml.readNextStartElement()) { + if (xml.name() == "request") { + while (xml.readNextStartElement()) { + if (xml.name() == "target") { + targetDir = xml.readElementText(); + } else if (xml.name() == "file") { + QString filename = xml.readElementText(); + + QFileInfo fileInfo(path + "/" + filename); + if (fileInfo.isDir()) { + response += QString("Note: Moving directory %1 not supported.
").arg(filename); + } else if (fileInfo.isFile()) { + QFile file(path + "/" + filename); + if (!file.rename(path + "/" + targetDir + "/" + filename)) { + response += QString("Error on moving file %1
").arg(filename); + } + } else { + response += QString("Error: %1 is neither file nor directory.
").arg(filename); + } + } + } + } + } + } + + if (response == "") { + response = "OK"; + } + + FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); + FCGX_PutS(response.toUtf8().data(), request.out); + } + } } else if (command == "upload") { // POST! QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp)); bool ok; -- cgit v1.2.3