feat: commit file download
This commit is contained in:
1 parent
d13955e39b
commit
2ad5c7e282
3 files changed
+27
-5
No files matched your search
@@ -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)
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user