From 389c669a4744629e19e48e8243dac1a8875e60c2 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Tue, 12 Jul 2016 10:48:10 +0600 Subject: [PATCH] CLOUD: Add "directory" form for webserver "/upload" The attribute is Chrome-only. --- .../sdl_net/handlers/filespagehandler.cpp | 5 ++- .../sdl_net/uploadfileclienthandler.cpp | 29 ++++++++++-------- .../sdl_net/uploadfileclienthandler.h | 1 + backends/networking/wwwroot.zip | Bin 231000 -> 231402 bytes backends/networking/wwwroot/.files.html | 8 ++++- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/backends/networking/sdl_net/handlers/filespagehandler.cpp b/backends/networking/sdl_net/handlers/filespagehandler.cpp index 9dba0c54adc..e519c5ac8ba 100644 --- a/backends/networking/sdl_net/handlers/filespagehandler.cpp +++ b/backends/networking/sdl_net/handlers/filespagehandler.cpp @@ -59,7 +59,9 @@ void FilesPageHandler::handle(Client &client) { "
" \ "

{upload_file_desc}

" \ "
" \ - "" \ + "" \ + "{or_upload_directory_desc}" \ + "" \ "" \ "
" "
" \ @@ -90,6 +92,7 @@ void FilesPageHandler::handle(Client &client) { replace(response, "{upload_file_button}", _("Upload files")); //button in the tab replace(response, "{create_directory_desc}", _("Type new directory name:")); replace(response, "{upload_file_desc}", _("Select a file to upload:")); + replace(response, "{or_upload_directory_desc}", _("Or select a directory (works in Chrome only):")); replace(response, "{content}", content); LocalWebserver::setClientGetHandler(client, response); } diff --git a/backends/networking/sdl_net/uploadfileclienthandler.cpp b/backends/networking/sdl_net/uploadfileclienthandler.cpp index 0ca5e3f6845..7cbb11874c5 100644 --- a/backends/networking/sdl_net/uploadfileclienthandler.cpp +++ b/backends/networking/sdl_net/uploadfileclienthandler.cpp @@ -24,6 +24,7 @@ #include "backends/fs/fs-factory.h" #include "backends/networking/sdl_net/handlerutils.h" #include "backends/networking/sdl_net/localwebserver.h" +#include "common/file.h" #include "common/memstream.h" #include "common/translation.h" @@ -31,7 +32,7 @@ namespace Networking { UploadFileClientHandler::UploadFileClientHandler(Common::String parentDirectoryPath): _state(UFH_READING_CONTENT), _headersStream(nullptr), _contentStream(nullptr), - _parentDirectoryPath(parentDirectoryPath) {} + _parentDirectoryPath(parentDirectoryPath), _uploadedFiles(0) {} UploadFileClientHandler::~UploadFileClientHandler() { delete _headersStream; @@ -114,16 +115,13 @@ void UploadFileClientHandler::handleBlockHeaders(Client *client) { Common::String headers = readEverythingFromMemoryStream(_headersStream); Common::String fieldName = ""; readFromThatUntilDoubleQuote(headers.c_str(), "name=\"", fieldName); - if (!fieldName.hasPrefix("upload_file[")) return; + if (!fieldName.hasPrefix("upload_file")) return; Common::String filename = ""; readFromThatUntilDoubleQuote(headers.c_str(), "filename=\"", filename); - // check that is not empty - if (filename.empty()) { - setErrorMessageHandler(*client, _("Invalid filename!")); - return; - } + // skip block if is empty + if (filename.empty()) return; // check that / doesn't exist Common::String path = _parentDirectoryPath; @@ -134,12 +132,15 @@ void UploadFileClientHandler::handleBlockHeaders(Client *client) { return; } - // create file stream - _contentStream = originalNode->createWriteStream(); - if (_contentStream == nullptr) { + // create file stream (and necessary subdirectories) + Common::DumpFile *f = new Common::DumpFile(); + if (!f->open(originalNode->getPath(), true)) { + delete f; setErrorMessageHandler(*client, _("Failed to upload the file!")); return; } + + _contentStream = f; } void UploadFileClientHandler::handleBlockContent(Client *client) { @@ -148,6 +149,7 @@ void UploadFileClientHandler::handleBlockContent(Client *client) { // if previous block headers were file-related and created a stream if (_contentStream) { _contentStream->flush(); + ++_uploadedFiles; if (client->noMoreContent()) { // success - redirect back to directory listing @@ -165,9 +167,12 @@ void UploadFileClientHandler::handleBlockContent(Client *client) { } } - // if no file field was found, but no more content avaiable - failure + // no more content avaiable if (client->noMoreContent()) { - setErrorMessageHandler(*client, _("No file was passed!")); + // if no file field was found - failure + if (_uploadedFiles == 0) { + setErrorMessageHandler(*client, _("No file was passed!")); + } else _state = UFH_STOP; return; } } diff --git a/backends/networking/sdl_net/uploadfileclienthandler.h b/backends/networking/sdl_net/uploadfileclienthandler.h index de6941bf4ff..f61a2fab5e5 100644 --- a/backends/networking/sdl_net/uploadfileclienthandler.h +++ b/backends/networking/sdl_net/uploadfileclienthandler.h @@ -41,6 +41,7 @@ class UploadFileClientHandler: public ClientHandler { Common::MemoryReadWriteStream *_headersStream; Common::WriteStream *_contentStream; Common::String _parentDirectoryPath; + uint32 _uploadedFiles; void handleBlockHeaders(Client *client); void handleBlockContent(Client *client); diff --git a/backends/networking/wwwroot.zip b/backends/networking/wwwroot.zip index ae19ef7d29d021f7055e11d5a658531170f28216..3aa9bdda6524a4b311496ddc9d85eead99382360 100644 GIT binary patch delta 516 zcmY*W!Ac`R5bexNBrcfKqB)kmxC@frTm%os999nzL2}uXNSLHcGp1*H=*Z+sDk3vtM{s&ZmTbM)mrD!^*$0CyWd~i-;n*@ zs1UMIxvwpGqjkn0Mc^{}Vjx1~K#xx-1QD14Lga^72Va9s6r|YGs4h@Jpdpe55*B^_b`PAR%Ixmi-FBXrXMb(I$!4XoyY*@Hr%SZS1b*#n V-8n2T*(aAtnbb;z{29B1kpHBZn<@YR delta 123 zcmaFW&v&DTPa?pZnMH&F1h|7=c_eL2pEQ|`fnhS+MhP~S$*-7rChukuXO50lYQDzW zevOr}{TeIN_G_%npBng3)la|H#_Y+ohi$rIJF_{HFURyK5dFP;`s8+I6Q-8>=_lKn MrP;JvnHd-u0Brpx;Q#;t diff --git a/backends/networking/wwwroot/.files.html b/backends/networking/wwwroot/.files.html index c83a59f718b..2c0bceb3331 100644 --- a/backends/networking/wwwroot/.files.html +++ b/backends/networking/wwwroot/.files.html @@ -26,7 +26,13 @@