Added optional force parameter to WebServerRequest::move_file(). Also various fixes and improvements to the new upload temp file system.

This commit is contained in:
Relintai 2024-03-10 09:07:13 +01:00
parent 6addb02bbb
commit 27d97cf766
7 changed files with 40 additions and 24 deletions

View File

@ -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;

View File

@ -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)) {
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);
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);

View File

@ -64,7 +64,7 @@ public:
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 @@ protected:
struct FileEntry {
enum FileEntryType {
FILE_ENTRY_TYPE_MEMORY = 0,
FILE_ENTRY_TYPE_TEMP_FILE = 0,
FILE_ENTRY_TYPE_TEMP_FILE,
};
mutable bool moved;

View File

@ -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);

View File

@ -116,7 +116,7 @@ public:
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;

View File

@ -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);
@ -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);

View File

@ -63,7 +63,7 @@ public:
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 @@ public:
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;