summaryrefslogtreecommitdiff
path: root/platform/javascript/api
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2021-05-19 15:53:04 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2021-05-21 15:31:23 +0200
commitfdf66a21f1a330595f7345f3b0024e0e7cafa28a (patch)
tree4d7e4cc526f209065baba845a0e3ab15d8e99f7c /platform/javascript/api
parent8c2beeea907cb39baac20df198392ae4fdf64029 (diff)
[HTML5] Add easy to use download API.
New `JavaScript.download_buffer` method to create a prompt that let the user download a file.
Diffstat (limited to 'platform/javascript/api')
-rw-r--r--platform/javascript/api/api.cpp5
-rw-r--r--platform/javascript/api/javascript_singleton.h1
-rw-r--r--platform/javascript/api/javascript_tools_editor_plugin.cpp9
3 files changed, 13 insertions, 2 deletions
diff --git a/platform/javascript/api/api.cpp b/platform/javascript/api/api.cpp
index 039ce815e4..5ad2bf56cf 100644
--- a/platform/javascript/api/api.cpp
+++ b/platform/javascript/api/api.cpp
@@ -70,6 +70,7 @@ void JavaScript::_bind_methods() {
mi.arguments.push_back(PropertyInfo(Variant::STRING, "object"));
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "create_object", &JavaScript::_create_object_bind, mi);
}
+ ClassDB::bind_method(D_METHOD("download_buffer", "buffer", "name", "mime"), &JavaScript::download_buffer, DEFVAL("application/octet-stream"));
}
#if !defined(JAVASCRIPT_ENABLED) || !defined(JAVASCRIPT_EVAL_ENABLED)
@@ -100,3 +101,7 @@ Variant JavaScript::_create_object_bind(const Variant **p_args, int p_argcount,
return Ref<JavaScriptObject>();
}
#endif
+#if !defined(JAVASCRIPT_ENABLED)
+void JavaScript::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) {
+}
+#endif
diff --git a/platform/javascript/api/javascript_singleton.h b/platform/javascript/api/javascript_singleton.h
index 45e9950acb..1615efa87e 100644
--- a/platform/javascript/api/javascript_singleton.h
+++ b/platform/javascript/api/javascript_singleton.h
@@ -58,6 +58,7 @@ public:
Ref<JavaScriptObject> get_interface(const String &p_interface);
Ref<JavaScriptObject> create_callback(const Callable &p_callable);
Variant _create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
+ void download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime = "application/octet-stream");
static JavaScript *get_singleton();
JavaScript();
diff --git a/platform/javascript/api/javascript_tools_editor_plugin.cpp b/platform/javascript/api/javascript_tools_editor_plugin.cpp
index 015440f5be..ac4e6a1256 100644
--- a/platform/javascript/api/javascript_tools_editor_plugin.cpp
+++ b/platform/javascript/api/javascript_tools_editor_plugin.cpp
@@ -41,7 +41,7 @@
// JavaScript functions defined in library_godot_editor_tools.js
extern "C" {
-extern void godot_js_editor_download_file(const char *p_path, const char *p_name, const char *p_mime);
+extern int godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);
}
static void _javascript_editor_init_callback() {
@@ -69,7 +69,12 @@ void JavaScriptToolsEditorPlugin::_download_zip(Variant p_v) {
String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
_zip_recursive(resource_path, base_path, zip);
zipClose(zip, nullptr);
- godot_js_editor_download_file("/tmp/project.zip", "project.zip", "application/zip");
+ FileAccess *f = FileAccess::open("/tmp/project.zip", FileAccess::READ);
+ ERR_FAIL_COND_MSG(!f, "Unable to create zip file");
+ Vector<uint8_t> buf;
+ buf.resize(f->get_len());
+ f->get_buffer(buf.ptrw(), buf.size());
+ godot_js_os_download_buffer(buf.ptr(), buf.size(), "project.zip", "application/zip");
}
void JavaScriptToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {