mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-23 20:53:06 +00:00
feat: commit file download
This commit is contained in:
parent
d13955e39b
commit
2ad5c7e282
3 changed files with 27 additions and 5 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
@ -1625,6 +1625,9 @@ checksum = "8afb4815a6d6a3bd1c23e6c647f7df18c9800c12e80526a98e18d7decd7cedcd"
|
|||
dependencies = [
|
||||
"bstr",
|
||||
"cc",
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
"lua-src",
|
||||
"luajit-src",
|
||||
"num-traits",
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue