2016-07-09 15:49:18 +06:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "backends/networking/sdl_net/uploadfileclienthandler.h"
|
2016-07-09 16:10:58 +06:00
|
|
|
#include "backends/fs/fs-factory.h"
|
|
|
|
#include "backends/networking/sdl_net/handlerutils.h"
|
|
|
|
#include "backends/networking/sdl_net/localwebserver.h"
|
2016-07-12 10:48:10 +06:00
|
|
|
#include "common/file.h"
|
2016-07-09 16:10:58 +06:00
|
|
|
#include "common/memstream.h"
|
|
|
|
#include "common/translation.h"
|
2016-07-09 15:49:18 +06:00
|
|
|
|
|
|
|
namespace Networking {
|
|
|
|
|
|
|
|
UploadFileClientHandler::UploadFileClientHandler(Common::String parentDirectoryPath):
|
|
|
|
_state(UFH_READING_CONTENT), _headersStream(nullptr), _contentStream(nullptr),
|
2016-07-12 10:48:10 +06:00
|
|
|
_parentDirectoryPath(parentDirectoryPath), _uploadedFiles(0) {}
|
2016-07-09 15:49:18 +06:00
|
|
|
|
|
|
|
UploadFileClientHandler::~UploadFileClientHandler() {
|
|
|
|
delete _headersStream;
|
|
|
|
delete _contentStream;
|
|
|
|
}
|
|
|
|
|
2016-07-09 16:10:58 +06:00
|
|
|
void UploadFileClientHandler::handle(Client *client) {
|
|
|
|
if (client == nullptr) {
|
|
|
|
warning("UploadFileClientHandler::handle(): empty client pointer");
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:49:18 +06:00
|
|
|
|
2016-07-09 16:10:58 +06:00
|
|
|
while (true) {
|
|
|
|
switch (_state) {
|
|
|
|
case UFH_READING_CONTENT:
|
|
|
|
if (client->readContent(nullptr)) {
|
|
|
|
_state = UFH_READING_BLOCK_HEADERS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UFH_READING_BLOCK_HEADERS:
|
|
|
|
if (_headersStream == nullptr)
|
|
|
|
_headersStream = new Common::MemoryReadWriteStream(DisposeAfterUse::YES);
|
|
|
|
|
|
|
|
if (client->readBlockHeaders(_headersStream)) {
|
|
|
|
handleBlockHeaders(client);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UFH_READING_BLOCK_CONTENT:
|
|
|
|
// _contentStream is created by handleBlockHeaders() if needed
|
|
|
|
|
|
|
|
if (client->readBlockContent(_contentStream)) {
|
|
|
|
handleBlockContent(client);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UFH_ERROR:
|
|
|
|
case UFH_STOP:
|
|
|
|
return;
|
2016-07-09 15:49:18 +06:00
|
|
|
}
|
2016-07-09 16:10:58 +06:00
|
|
|
|
|
|
|
break;
|
2016-07-09 15:49:18 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 16:10:58 +06:00
|
|
|
namespace {
|
2016-07-09 15:49:18 +06:00
|
|
|
void readFromThatUntilDoubleQuote(const char *cstr, Common::String needle, Common::String &result) {
|
|
|
|
const char *position = strstr(cstr, needle.c_str());
|
|
|
|
|
|
|
|
if (position) {
|
|
|
|
char c;
|
|
|
|
for (const char *i = position + needle.size(); c = *i, c != 0; ++i) {
|
2016-07-22 15:38:24 +03:00
|
|
|
if (c == '"')
|
|
|
|
break;
|
2016-07-09 15:49:18 +06:00
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::String readEverythingFromMemoryStream(Common::MemoryReadWriteStream *stream) {
|
|
|
|
Common::String result;
|
|
|
|
char buf[1024];
|
|
|
|
uint32 readBytes;
|
|
|
|
while (true) {
|
|
|
|
readBytes = stream->read(buf, 1024);
|
2016-07-22 15:38:24 +03:00
|
|
|
if (readBytes == 0)
|
|
|
|
break;
|
2016-07-09 15:49:18 +06:00
|
|
|
result += Common::String(buf, readBytes);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UploadFileClientHandler::handleBlockHeaders(Client *client) {
|
2016-07-09 16:10:58 +06:00
|
|
|
_state = UFH_READING_BLOCK_CONTENT;
|
|
|
|
|
2016-07-09 15:49:18 +06:00
|
|
|
// search for "upload_file" field
|
|
|
|
Common::String headers = readEverythingFromMemoryStream(_headersStream);
|
|
|
|
Common::String fieldName = "";
|
|
|
|
readFromThatUntilDoubleQuote(headers.c_str(), "name=\"", fieldName);
|
2016-07-22 15:38:24 +03:00
|
|
|
if (!fieldName.hasPrefix("upload_file"))
|
|
|
|
return;
|
2016-07-09 15:49:18 +06:00
|
|
|
|
|
|
|
Common::String filename = "";
|
|
|
|
readFromThatUntilDoubleQuote(headers.c_str(), "filename=\"", filename);
|
|
|
|
|
2016-07-12 10:48:10 +06:00
|
|
|
// skip block if <filename> is empty
|
2016-07-22 15:38:24 +03:00
|
|
|
if (filename.empty())
|
|
|
|
return;
|
2016-07-09 15:49:18 +06:00
|
|
|
|
|
|
|
// check that <path>/<filename> doesn't exist
|
|
|
|
Common::String path = _parentDirectoryPath;
|
2016-07-22 15:38:24 +03:00
|
|
|
if (path.lastChar() != '/' && path.lastChar() != '\\')
|
|
|
|
path += '/';
|
2016-07-09 15:49:18 +06:00
|
|
|
AbstractFSNode *originalNode = g_system->getFilesystemFactory()->makeFileNodePath(path + filename);
|
|
|
|
if (originalNode->exists()) {
|
|
|
|
setErrorMessageHandler(*client, _("There is a file with that name in the parent directory!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-26 13:01:09 +06:00
|
|
|
// remove previous stream (if there is one)
|
|
|
|
if (_contentStream) {
|
|
|
|
delete _contentStream;
|
|
|
|
_contentStream = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:48:10 +06:00
|
|
|
// create file stream (and necessary subdirectories)
|
|
|
|
Common::DumpFile *f = new Common::DumpFile();
|
|
|
|
if (!f->open(originalNode->getPath(), true)) {
|
|
|
|
delete f;
|
2016-07-09 15:49:18 +06:00
|
|
|
setErrorMessageHandler(*client, _("Failed to upload the file!"));
|
|
|
|
return;
|
|
|
|
}
|
2016-07-12 10:48:10 +06:00
|
|
|
|
|
|
|
_contentStream = f;
|
2016-07-09 15:49:18 +06:00
|
|
|
}
|
|
|
|
|
2016-07-09 16:10:58 +06:00
|
|
|
void UploadFileClientHandler::handleBlockContent(Client *client) {
|
|
|
|
_state = UFH_READING_BLOCK_HEADERS;
|
|
|
|
|
|
|
|
// if previous block headers were file-related and created a stream
|
|
|
|
if (_contentStream) {
|
|
|
|
_contentStream->flush();
|
2016-07-12 10:48:10 +06:00
|
|
|
++_uploadedFiles;
|
2016-07-10 10:29:51 +06:00
|
|
|
|
2016-07-26 13:01:09 +06:00
|
|
|
delete _contentStream;
|
|
|
|
_contentStream = nullptr;
|
|
|
|
|
2016-07-10 10:29:51 +06:00
|
|
|
if (client->noMoreContent()) {
|
|
|
|
// success - redirect back to directory listing
|
2016-07-29 12:22:42 +06:00
|
|
|
setSuccessHandler(*client);
|
2016-07-10 10:29:51 +06:00
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:49:18 +06:00
|
|
|
}
|
2016-07-21 11:44:36 +06:00
|
|
|
|
2016-07-12 10:48:10 +06:00
|
|
|
// no more content avaiable
|
2016-07-09 16:10:58 +06:00
|
|
|
if (client->noMoreContent()) {
|
2016-07-12 10:48:10 +06:00
|
|
|
// if no file field was found - failure
|
|
|
|
if (_uploadedFiles == 0) {
|
|
|
|
setErrorMessageHandler(*client, _("No file was passed!"));
|
2016-07-22 15:38:24 +03:00
|
|
|
} else {
|
2016-07-29 12:22:42 +06:00
|
|
|
setSuccessHandler(*client);
|
2016-07-22 15:38:24 +03:00
|
|
|
}
|
2016-07-09 15:49:18 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UploadFileClientHandler::setErrorMessageHandler(Client &client, Common::String message) {
|
|
|
|
HandlerUtils::setFilesManagerErrorMessageHandler(client, message);
|
|
|
|
_state = UFH_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-07-29 12:22:42 +06:00
|
|
|
void UploadFileClientHandler::setSuccessHandler(Client &client) {
|
|
|
|
// success - redirect back to directory listing
|
|
|
|
HandlerUtils::setMessageHandler(
|
|
|
|
client,
|
|
|
|
Common::String::format(
|
|
|
|
"%s<br/><a href=\"files?path=%s\">%s</a>",
|
|
|
|
_("Uploaded successfully!"),
|
|
|
|
client.queryParameter("path").c_str(),
|
|
|
|
_("Back to parent directory")
|
|
|
|
),
|
|
|
|
(client.queryParameter("ajax") == "true" ? "/filesAJAX?path=" : "/files?path=") +
|
|
|
|
LocalWebserver::urlEncodeQueryParameterValue(client.queryParameter("path"))
|
|
|
|
);
|
|
|
|
_state = UFH_STOP;
|
|
|
|
}
|
|
|
|
|
2016-07-09 15:49:18 +06:00
|
|
|
} // End of namespace Networking
|