CLOUD: Add "directory" form for webserver "/upload"
The attribute is Chrome-only.
This commit is contained in:
parent
29e6020574
commit
389c669a47
5 changed files with 29 additions and 14 deletions
|
@ -59,7 +59,9 @@ void FilesPageHandler::handle(Client &client) {
|
|||
"<hr/>" \
|
||||
"<p>{upload_file_desc}</p>" \
|
||||
"<form action=\"upload?path={path}\" method=\"post\" enctype=\"multipart/form-data\">" \
|
||||
"<input type=\"file\" name=\"upload_file[]\" multiple/>" \
|
||||
"<input type=\"file\" name=\"upload_file-f\" allowdirs multiple/>" \
|
||||
"<span>{or_upload_directory_desc}</span>" \
|
||||
"<input type=\"file\" name=\"upload_file-d\" directory webkitdirectory multiple/>" \
|
||||
"<input type=\"submit\" value=\"{upload_file_button}\"/>" \
|
||||
"</form>"
|
||||
"<hr/>" \
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 <filename> is not empty
|
||||
if (filename.empty()) {
|
||||
setErrorMessageHandler(*client, _("Invalid filename!"));
|
||||
return;
|
||||
}
|
||||
// skip block if <filename> is empty
|
||||
if (filename.empty()) return;
|
||||
|
||||
// check that <path>/<filename> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Binary file not shown.
|
@ -26,7 +26,13 @@
|
|||
<div id="upload_file" class="modal">
|
||||
<p>{upload_file_desc}</p>
|
||||
<form action="upload?path={path}" method="post" enctype="multipart/form-data">
|
||||
<input type="file" name="upload_file[]" multiple/>
|
||||
<!-- we don't need "[]" in the name, as our webserver is not using PHP -->
|
||||
<!-- "allowdirs" is a proposal, not implemented yet -->
|
||||
<input type="file" name="upload_file-f" allowdirs multiple/>
|
||||
<br/><br/>
|
||||
<p>{or_upload_directory_desc}</p>
|
||||
<!-- "directory"/"webkitdirectory" works in Chrome only yet, "multiple" is just in case here -->
|
||||
<input type="file" name="upload_file-d" directory webkitdirectory multiple/>
|
||||
<input type="submit" value="{upload_file_button}"/>
|
||||
</form>
|
||||
</div>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue