From c00673fd0e01d1438798dbb1635a761a76a2b559 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 20 Nov 2022 23:54:20 +0100 Subject: extracted filedownload to fn; fixed some tests; added direct-dl to update --- src/lib.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 51b4487..e4ebf76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,10 +5,12 @@ pub mod input; pub mod db; pub mod error; -use std::io::{Error, ErrorKind}; +use std::{io::{Error, ErrorKind, Write}, fs::File}; pub use apis::*; pub use commands::*; +use futures_util::StreamExt; +use reqwest::Client; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Modloader { @@ -32,3 +34,23 @@ impl Modloader { } } } + +pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box> { + println!("Downloading {}", url); + let dl_path_file = format!("{}/{}", path, name); + let res = Client::new() + .get(String::from(&url)) + .send() + .await?; + + // download chunks + let mut file = File::create(String::from(&dl_path_file))?; + let mut stream = res.bytes_stream(); + + while let Some(item) = stream.next().await { + let chunk = item?; + file.write_all(&chunk)?; + } + + Ok(()) +} -- cgit v1.2.3