Skip to content

Commit

Permalink
Added optional force parameter to WebServerRequest::move_file(). Also…
Browse files Browse the repository at this point in the history
… various fixes and improvements to the new upload temp file system.
  • Loading branch information
Relintai committed Mar 10, 2024
1 parent 6addb02 commit 27d97cf
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
4 changes: 4 additions & 0 deletions modules/http_server_simple/http_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ void HTTPParser::reset() {
_is_ready = false;
_content_type = REQUEST_CONTENT_URLENCODED;
_error = false;
_current_upload_files_size = 0;
_current_request_size = 0;
_request.unref();
_requests.clear();
}
Expand Down Expand Up @@ -246,6 +248,7 @@ void HTTPParser::_process_multipart_header_value(const String &val) {

if (_upload_file_access) {
memdelete(_upload_file_access);
_upload_file_access = NULL;
}
}

Expand Down Expand Up @@ -309,6 +312,7 @@ int HTTPParser::on_message_begin() {
}

_current_request_size = 0;
_current_upload_files_size = 0;

_in_header = true;
_content_type = REQUEST_CONTENT_URLENCODED;
Expand Down
36 changes: 24 additions & 12 deletions modules/http_server_simple/simple_web_server_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,34 @@ String SimpleWebServerRequest::get_file_data_str(const int index) const {

return data;
}
Error SimpleWebServerRequest::move_file(const int index, const String &p_dest_file) {
Error SimpleWebServerRequest::move_file(const int index, const String &p_dest_file, const bool p_force) {
ERR_FAIL_INDEX_V(index, _files.size(), ERR_INVALID_PARAMETER);

DirAccess *dir = DirAccess::create_for_path(p_dest_file.get_base_dir());
String base_dir = p_dest_file.get_base_dir();
String file_name = p_dest_file.get_file();

DirAccess *dir = DirAccess::create_for_path(base_dir);

if (!dir) {
return ERR_FILE_BAD_PATH;
}

if (dir->file_exists(p_dest_file)) {
return ERR_ALREADY_IN_USE;
if (!p_force) {
if (dir->file_exists(file_name)) {
return ERR_ALREADY_IN_USE;
}
}

memdelete(dir);
dir = NULL;

const FileEntry &e = _files[index];

if (e.type == FileEntry::FILE_ENTRY_TYPE_MEMORY) {
memdelete(dir);

Error err;
FileAccess *f = FileAccess::open(e.path, FileAccess::WRITE, &err);
FileAccess *f = FileAccess::open(p_dest_file, FileAccess::WRITE, &err);
if (!f) {
return ERR_FILE_BAD_PATH;
return err;
}

PoolByteArray::Read r = e.data.read();
Expand All @@ -177,13 +183,19 @@ Error SimpleWebServerRequest::move_file(const int index, const String &p_dest_fi
return OK;
}

dir->rename(e.path, p_dest_file);
dir = DirAccess::create_for_path(e.path);
ERR_FAIL_COND_V_MSG(!dir->file_exists(e.path), ERR_DOES_NOT_EXIST, "Original temp file does not exist. BUG!");

Error err = dir->rename(e.path, p_dest_file);

memdelete(dir);

e.moved = true;
e.path = p_dest_file;
if (err == OK) {
e.moved = true;
e.path = p_dest_file;
}

return OK;
return err;
}
bool SimpleWebServerRequest::is_file_moved(const int index) const {
ERR_FAIL_INDEX_V(index, _files.size(), true);
Expand Down
4 changes: 2 additions & 2 deletions modules/http_server_simple/simple_web_server_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SimpleWebServerRequest : public WebServerRequest {
virtual uint64_t get_file_length(const int index) const;
virtual PoolByteArray get_file_data(const int index) const;
virtual String get_file_data_str(const int index) const;
virtual Error move_file(const int index, const String &p_dest_file);
virtual Error move_file(const int index, const String &p_dest_file, const bool p_force = false);
virtual bool is_file_moved(const int index) const;

virtual String get_parameter(const String &key) const;
Expand Down Expand Up @@ -125,7 +125,7 @@ class SimpleWebServerRequest : public WebServerRequest {
struct FileEntry {
enum FileEntryType {
FILE_ENTRY_TYPE_MEMORY = 0,
FILE_ENTRY_TYPE_TEMP_FILE = 0,
FILE_ENTRY_TYPE_TEMP_FILE,
};

mutable bool moved;
Expand Down
4 changes: 2 additions & 2 deletions modules/web/http/web_server_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ PoolByteArray WebServerRequest::get_file_data(const int index) const {
String WebServerRequest::get_file_data_str(const int index) const {
return String();
}
Error WebServerRequest::move_file(const int index, const String &p_dest_file) {
Error WebServerRequest::move_file(const int index, const String &p_dest_file, const bool p_force) {
return ERR_PRINTER_ON_FIRE;
}
bool WebServerRequest::is_file_moved(const int index) const {
Expand Down Expand Up @@ -656,7 +656,7 @@ void WebServerRequest::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_file_length", "index"), &WebServerRequest::get_file_length);
ClassDB::bind_method(D_METHOD("get_file_data", "index"), &WebServerRequest::get_file_data);
ClassDB::bind_method(D_METHOD("get_file_data_str", "index"), &WebServerRequest::get_file_data_str);
ClassDB::bind_method(D_METHOD("move_file", "index", "dest_file"), &WebServerRequest::move_file);
ClassDB::bind_method(D_METHOD("move_file", "index", "dest_file", "force"), &WebServerRequest::move_file, DEFVAL(false));
ClassDB::bind_method(D_METHOD("is_file_moved", "index"), &WebServerRequest::is_file_moved);

ClassDB::bind_method(D_METHOD("get_parameter", "key"), &WebServerRequest::get_parameter);
Expand Down
2 changes: 1 addition & 1 deletion modules/web/http/web_server_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class WebServerRequest : public Reference {
virtual uint64_t get_file_length(const int index) const;
virtual PoolByteArray get_file_data(const int index) const;
virtual String get_file_data_str(const int index) const;
virtual Error move_file(const int index, const String &p_dest_file);
virtual Error move_file(const int index, const String &p_dest_file, const bool p_force = false);
virtual bool is_file_moved(const int index) const;

virtual String get_parameter(const String &key) const;
Expand Down
10 changes: 5 additions & 5 deletions modules/web/http/web_server_request_scriptable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ PoolByteArray WebServerRequestScriptable::get_file_data(const int index) const {
String WebServerRequestScriptable::get_file_data_str(const int index) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_file_data_str", index);
}
Error WebServerRequestScriptable::move_file(const int index, const String &p_dest_file) {
return (Error)(int)call("_move_file", index, p_dest_file);
Error WebServerRequestScriptable::move_file(const int index, const String &p_dest_file, const bool p_force) {
return (Error)(int)call("_move_file", index, p_dest_file, p_force);
}
bool WebServerRequestScriptable::is_file_moved(const int index) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_is_file_moved", index);
Expand Down Expand Up @@ -176,7 +176,7 @@ PoolByteArray WebServerRequestScriptable::_get_file_data(const int index) const
String WebServerRequestScriptable::_get_file_data_str(const int index) const {
return String();
}
Error WebServerRequestScriptable::_move_file(const int index, const String &p_dest_file) {
Error WebServerRequestScriptable::_move_file(const int index, const String &p_dest_file, const bool p_force) {
return ERR_PRINTER_ON_FIRE;
}
bool WebServerRequestScriptable::_is_file_moved(const int index) const {
Expand Down Expand Up @@ -308,7 +308,7 @@ void WebServerRequestScriptable::_bind_methods() {
BIND_VMETHOD(MethodInfo(Variant::INT, "_get_file_length", PropertyInfo(Variant::INT, "index")));
BIND_VMETHOD(MethodInfo(Variant::POOL_BYTE_ARRAY, "_get_file_data", PropertyInfo(Variant::INT, "index")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_file_data_str", PropertyInfo(Variant::INT, "index")));
BIND_VMETHOD(MethodInfo(Variant::INT, "_move_file", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::STRING, "dest_file")));
BIND_VMETHOD(MethodInfo(Variant::INT, "_move_file", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::STRING, "dest_file"), PropertyInfo(Variant::BOOL, "force")));
BIND_VMETHOD(MethodInfo(Variant::BOOL, "_is_file_moved", PropertyInfo(Variant::INT, "index")));

BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_parameter", PropertyInfo(Variant::STRING, "key")));
Expand Down Expand Up @@ -350,7 +350,7 @@ void WebServerRequestScriptable::_bind_methods() {
ClassDB::bind_method(D_METHOD("_get_file_length", "index"), &WebServerRequestScriptable::_get_file_length);
ClassDB::bind_method(D_METHOD("_get_file_data", "index"), &WebServerRequestScriptable::_get_file_data);
ClassDB::bind_method(D_METHOD("_get_file_data_str", "index"), &WebServerRequestScriptable::_get_file_data_str);
ClassDB::bind_method(D_METHOD("_move_file", "index", "dest_file"), &WebServerRequestScriptable::_move_file);
ClassDB::bind_method(D_METHOD("_move_file", "index", "dest_file", "force"), &WebServerRequestScriptable::_move_file);
ClassDB::bind_method(D_METHOD("_is_file_moved", "index"), &WebServerRequestScriptable::_is_file_moved);

ClassDB::bind_method(D_METHOD("_get_parameter", "key"), &WebServerRequestScriptable::_get_parameter);
Expand Down
4 changes: 2 additions & 2 deletions modules/web/http/web_server_request_scriptable.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class WebServerRequestScriptable : public WebServerRequest {
virtual uint64_t get_file_length(const int index) const;
virtual PoolByteArray get_file_data(const int index) const;
virtual String get_file_data_str(const int index) const;
virtual Error move_file(const int index, const String &p_dest_file);
virtual Error move_file(const int index, const String &p_dest_file, const bool p_force = false);
virtual bool is_file_moved(const int index) const;

virtual String get_parameter(const String &key) const;
Expand Down Expand Up @@ -105,7 +105,7 @@ class WebServerRequestScriptable : public WebServerRequest {
virtual int _get_file_length(const int index) const;
virtual PoolByteArray _get_file_data(const int index) const;
virtual String _get_file_data_str(const int index) const;
virtual Error _move_file(const int index, const String &p_dest_file);
virtual Error _move_file(const int index, const String &p_dest_file, const bool p_force);
virtual bool _is_file_moved(const int index) const;

virtual String _get_parameter(const String &key) const;
Expand Down

0 comments on commit 27d97cf

Please sign in to comment.