From 3bccf120e0a6653804a1678f383e973ceffb9955 Mon Sep 17 00:00:00 2001 From: Roland Stigge Date: Sun, 7 Jan 2018 13:15:39 +0100 Subject: Fix ZIP download --- TODO | 1 + debian/control | 1 + html/webbox.js | 1 + src/webbox.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 debian/control diff --git a/TODO b/TODO index e9877fb..e9d6667 100644 --- a/TODO +++ b/TODO @@ -10,6 +10,7 @@ download zip Prio 2 (for future versions) ====== +handle errors from http gallery chromecast player diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..7725dc4 --- /dev/null +++ b/debian/control @@ -0,0 +1 @@ +Depends: zip diff --git a/html/webbox.js b/html/webbox.js index 2b70292..faa2bb2 100644 --- a/html/webbox.js +++ b/html/webbox.js @@ -299,6 +299,7 @@ function download(filename) { xhr.open("POST", "/bin/query" + currentDir + "?command=download-zip", true); xhr.setRequestHeader("Content-type", "text/xml"); + xhr.responseType = 'blob'; xhr.send(xmlDocument); } } else { diff --git a/src/webbox.cpp b/src/webbox.cpp index b5d35c2..190d7e7 100644 --- a/src/webbox.cpp +++ b/src/webbox.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #define PROGRAMVERSION "1.0" #define BUFSIZE 1000000 @@ -218,6 +220,16 @@ int main(int argc, char* argv[]) { QXmlStreamReader xml(content); QByteArray zipData; + QStringList argumentList; + QTemporaryFile tempfile(QDir::tempPath() + "/webboxXXXXXX.zip"); + tempfile.open(); + QFileInfo fileInfo(tempfile); + QString tempfilePath = fileInfo.absolutePath(); + QString tempfileName = fileInfo.fileName(); + tempfile.close(); + tempfile.remove(); + + argumentList << tempfileName; // zip filename while (!xml.atEnd()) { while (xml.readNextStartElement()) { @@ -226,17 +238,55 @@ int main(int argc, char* argv[]) { if (xml.name() == "file") { QString filename = xml.readElementText(); - zipData.append(filename.toUtf8()); // TBD + argumentList.append(path + filename); // add parts } } } } } - FCGX_PutS(QString("Content-Disposition: attachment; filename=\"%1\"\r\n").arg("webbox-download.zip").toUtf8().data(), request.out); - FCGX_PutS("Content-Type: application/octet-stream\r\n\r\n", request.out); - - FCGX_PutStr(zipData.data(), zipData.size(), request.out); + QProcess process; + process.setWorkingDirectory(tempfilePath); + process.setProgram("/usr/bin/zip"); + process.setArguments(argumentList); + process.start(); + process.waitForFinished(); + + QString debugText = process.readAll(); + process.setReadChannel(QProcess::StandardError); + debugText += process.readAll(); + + if (process.state() != QProcess::NotRunning || + process.exitCode() != 0 || + process.exitStatus() != QProcess::NormalExit) + { + FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); + FCGX_PutS(QString("Error running process: %1 %2 %3 %4 %5 %6 %7"). + arg(process.state()). + arg(process.exitCode()). + arg(process.exitStatus()). + arg(tempfilePath). + arg(tempfileName). + arg(argumentList[0]). + arg(debugText).toUtf8().data(), request.out); + } else { + + QFile tempfile(tempfilePath + "/" + tempfileName); + if (!tempfile.open(QIODevice::ReadOnly)) { + FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); + FCGX_PutS(QString("Error reading file").toUtf8().data(), request.out); + } else { + zipData = tempfile.readAll(); + + FCGX_PutS(QString("Content-Disposition: attachment; filename=\"%1\"\r\n").arg("webbox-download.zip").toUtf8().data(), request.out); + FCGX_PutS("Content-Type: application/octet-stream\r\n\r\n", request.out); + + FCGX_PutStr(zipData.data(), zipData.size(), request.out); + } + + tempfile.close(); + tempfile.remove(); + } } } } else if (command == "upload") { // POST! -- cgit v1.2.3