diff --git a/Cargo.lock b/Cargo.lock index 0457bed9a..97e4d0a6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1625,6 +1625,9 @@ checksum = "8afb4815a6d6a3bd1c23e6c647f7df18c9800c12e80526a98e18d7decd7cedcd" dependencies = [ "bstr", "cc", + "futures-core", + "futures-task", + "futures-util", "lua-src", "luajit-src", "num-traits", diff --git a/Cargo.toml b/Cargo.toml index 31ac60386..8b2a3904e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,7 @@ dioxus-rsx-interpreter = { git = "https://github.com/dioxuslabs/dioxus/" } proc-macro2 = { version = "1.0", features = ["span-locations"] } # plugin packages -mlua = { version = "0.8.1", features = ["lua54", "vendored"] } +mlua = { version = "0.8.1", features = ["lua54", "vendored", "async"] } [[bin]] path = "src/main.rs" diff --git a/src/plugin/interface/download.rs b/src/plugin/interface/download.rs index 8718e6547..2edc68c9d 100644 --- a/src/plugin/interface/download.rs +++ b/src/plugin/interface/download.rs @@ -1,8 +1,27 @@ +use std::{io::Cursor, path::PathBuf}; + use mlua::UserData; pub struct PluginDownloader; impl UserData for PluginDownloader { - fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(_methods: &mut M) { - // methods.add_function("name", function) - } -} \ No newline at end of file + fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_async_function("download_file", |_, args: (String, String)| async move { + let url = args.0; + let path = args.1; + + let resp = reqwest::get(url).await; + if let Ok(resp) = resp { + let mut content = Cursor::new(resp.bytes().await.unwrap()); + let file = std::fs::File::create(PathBuf::from(path)); + if file.is_err() { + return Ok(false); + } + let mut file = file.unwrap(); + let res = std::io::copy(&mut content, &mut file); + return Ok(res.is_ok()); + } + + Ok(false) + }); + } +}