feat: commit file download

This commit is contained in:
mrxiaozhuox 2022-08-01 16:03:17 +08:00
parent d13955e39b
commit 2ad5c7e282
3 changed files with 27 additions and 5 deletions

3
Cargo.lock generated
View file

@ -1625,6 +1625,9 @@ checksum = "8afb4815a6d6a3bd1c23e6c647f7df18c9800c12e80526a98e18d7decd7cedcd"
dependencies = [
"bstr",
"cc",
"futures-core",
"futures-task",
"futures-util",
"lua-src",
"luajit-src",
"num-traits",

View file

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

View file

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