aboutsummaryrefslogtreecommitdiff
path: root/backends/networking/sdl_net/handlers/filespagehandler.cpp
diff options
context:
space:
mode:
authorAlexander Tkachev2016-07-06 18:03:56 +0600
committerAlexander Tkachev2016-08-24 16:07:55 +0600
commitab4361a76b40b56348ec46311121a0552f0c9d6b (patch)
treed0f73ba85786e94fd9bd6931052ef90d7294bafb /backends/networking/sdl_net/handlers/filespagehandler.cpp
parent48e3fff6bcea94da5bd46ee2def17eb6bdca716c (diff)
downloadscummvm-rg350-ab4361a76b40b56348ec46311121a0552f0c9d6b.tar.gz
scummvm-rg350-ab4361a76b40b56348ec46311121a0552f0c9d6b.tar.bz2
scummvm-rg350-ab4361a76b40b56348ec46311121a0552f0c9d6b.zip
CLOUD: Make "/create" work
One can now create directories through browser.
Diffstat (limited to 'backends/networking/sdl_net/handlers/filespagehandler.cpp')
-rw-r--r--backends/networking/sdl_net/handlers/filespagehandler.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/backends/networking/sdl_net/handlers/filespagehandler.cpp b/backends/networking/sdl_net/handlers/filespagehandler.cpp
index 360fe201b8..327071fc11 100644
--- a/backends/networking/sdl_net/handlers/filespagehandler.cpp
+++ b/backends/networking/sdl_net/handlers/filespagehandler.cpp
@@ -22,6 +22,7 @@
#include "backends/networking/sdl_net/handlers/filespagehandler.h"
#include "backends/networking/sdl_net/localwebserver.h"
+#include "backends/fs/fs-factory.h"
#include "common/file.h"
#include "common/translation.h"
@@ -35,6 +36,11 @@ FilesPageHandler::FilesPageHandler() {}
FilesPageHandler::~FilesPageHandler() {}
void FilesPageHandler::handle(Client &client) {
+ if (client.path() == "/files") handleFiles(client);
+ else handleCreateDirectory(client);
+}
+
+void FilesPageHandler::handleFiles(Client &client) {
Common::String response = "<html><head><title>ScummVM</title></head><body><table>{content}</table></body></html>"; //TODO: add controls
Common::String itemTemplate = "<tr><td><a href=\"{link}\">{name}</a></td><td>{size}</td></tr>\n"; //TODO: load this template too?
@@ -61,6 +67,8 @@ void FilesPageHandler::handle(Client &client) {
//these occur twice:
replace(response, "{create_directory_button}", _("Create directory"));
replace(response, "{create_directory_button}", _("Create directory"));
+ replace(response, "{path}", client.queryParameter("path"));
+ replace(response, "{path}", client.queryParameter("path"));
replace(response, "{upload_files_button}", _("Upload files")); //tab
replace(response, "{upload_file_button}", _("Upload files")); //button in the tab
replace(response, "{create_directory_desc}", _("Type new directory name:"));
@@ -69,6 +77,27 @@ void FilesPageHandler::handle(Client &client) {
LocalWebserver::setClientGetHandler(client, response);
}
+void FilesPageHandler::handleCreateDirectory(Client &client) {
+ Common::String path = client.queryParameter("path");
+ Common::String name = client.queryParameter("directory_name");
+ Common::String errorMessage = "";
+
+ // show an error message if failed to create directory
+ if (!createDirectory(path, name, errorMessage)) {
+ handleErrorMessage(
+ client,
+ Common::String::format(
+ "%s<br/><a href=\"files?path=/\">%s</a>",
+ errorMessage.c_str(),
+ _("Back to the files manager")
+ )
+ );
+ return;
+ }
+
+ handleFiles(client);
+}
+
void FilesPageHandler::handleErrorMessage(Client &client, Common::String message) {
Common::String response = "<html><head><title>ScummVM</title></head><body>{message}</body></html>";
@@ -80,6 +109,50 @@ void FilesPageHandler::handleErrorMessage(Client &client, Common::String message
LocalWebserver::setClientGetHandler(client, response);
}
+bool FilesPageHandler::createDirectory(Common::String path, Common::String name, Common::String &errorMessage) {
+ // check that <path> is not an absolute root
+ if (path == "" || path == "/") {
+ errorMessage = _("Can't create directory here!");
+ return false;
+ }
+
+ // transform virtual path to actual file system one
+ Common::String prefixToRemove = "", prefixToAdd = "";
+ if (!transformPath(path, prefixToRemove, prefixToAdd) || path.empty()) {
+ errorMessage = _("Invalid path!");
+ return false;
+ }
+
+ // check that <path> exists and is directory
+ AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(path);
+ if (!node->exists()) {
+ errorMessage = _("Parent directory doesn't exists!");
+ return false;
+ }
+ if (!node->isDirectory()) {
+ errorMessage = _("Can't create a directory within a file!");
+ return false;
+ }
+
+ // check that <directory_name> doesn't exist or is directory
+ if (path.lastChar() != '/' && path.lastChar() != '\\') path += '/';
+ node = g_system->getFilesystemFactory()->makeFileNodePath(path + name);
+ if (node->exists()) {
+ if (!node->isDirectory()) {
+ errorMessage = _("There is a file with that name in the parent directory!");
+ return false;
+ } else return true;
+ }
+
+ // create the <directory_name> in <path>
+ if (!node->create(true)) {
+ errorMessage = _("Failed to create the directory!");
+ return false;
+ }
+
+ return true;
+}
+
bool FilesPageHandler::listDirectory(Common::String path, Common::String &content, const Common::String &itemTemplate) {
if (path == "" || path == "/") {
addItem(content, itemTemplate, true, "/root/", _("File system root"));