From 27d97cf766f19d58774cc0616cc8cb7deee28db1 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 10 Mar 2024 09:07:13 +0100 Subject: [PATCH] Added optional force parameter to WebServerRequest::move_file(). Also various fixes and improvements to the new upload temp file system. --- modules/http_server_simple/http_parser.cpp | 4 +++ .../simple_web_server_request.cpp | 36 ++++++++++++------- .../simple_web_server_request.h | 4 +-- modules/web/http/web_server_request.cpp | 4 +-- modules/web/http/web_server_request.h | 2 +- .../http/web_server_request_scriptable.cpp | 10 +++--- .../web/http/web_server_request_scriptable.h | 4 +-- 7 files changed, 40 insertions(+), 24 deletions(-) diff --git a/modules/http_server_simple/http_parser.cpp b/modules/http_server_simple/http_parser.cpp index e12a872821..ae13e9fdcf 100644 --- a/modules/http_server_simple/http_parser.cpp +++ b/modules/http_server_simple/http_parser.cpp @@ -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(); } @@ -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; } } @@ -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; diff --git a/modules/http_server_simple/simple_web_server_request.cpp b/modules/http_server_simple/simple_web_server_request.cpp index 570f63d56f..dd991f24cb 100644 --- a/modules/http_server_simple/simple_web_server_request.cpp +++ b/modules/http_server_simple/simple_web_server_request.cpp @@ -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(); @@ -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); diff --git a/modules/http_server_simple/simple_web_server_request.h b/modules/http_server_simple/simple_web_server_request.h index 57198ab216..84ae3147eb 100644 --- a/modules/http_server_simple/simple_web_server_request.h +++ b/modules/http_server_simple/simple_web_server_request.h @@ -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; @@ -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; diff --git a/modules/web/http/web_server_request.cpp b/modules/web/http/web_server_request.cpp index abab76d363..d456248682 100644 --- a/modules/web/http/web_server_request.cpp +++ b/modules/web/http/web_server_request.cpp @@ -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 { @@ -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); diff --git a/modules/web/http/web_server_request.h b/modules/web/http/web_server_request.h index 368d30359f..1748b581d8 100644 --- a/modules/web/http/web_server_request.h +++ b/modules/web/http/web_server_request.h @@ -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; diff --git a/modules/web/http/web_server_request_scriptable.cpp b/modules/web/http/web_server_request_scriptable.cpp index 734190152b..95d5fbe0d7 100644 --- a/modules/web/http/web_server_request_scriptable.cpp +++ b/modules/web/http/web_server_request_scriptable.cpp @@ -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(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(this)->call("_is_file_moved", index); @@ -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 { @@ -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"))); @@ -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); diff --git a/modules/web/http/web_server_request_scriptable.h b/modules/web/http/web_server_request_scriptable.h index d5667cbf58..7dd05f676d 100644 --- a/modules/web/http/web_server_request_scriptable.h +++ b/modules/web/http/web_server_request_scriptable.h @@ -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; @@ -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;