From ddde9204c72dd867f920f07f6483be03dda7cf68 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 28 Nov 2022 22:55:14 +0100 Subject: basically update impl; added "good" download; auto dl on new mod; db to 0.4; etc --- src/files.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/files.rs (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs new file mode 100644 index 0000000..1c0b13c --- /dev/null +++ b/src/files.rs @@ -0,0 +1,44 @@ +use std::{fs::{File, read_dir}, io::Write, collections::HashMap}; +use futures_util::StreamExt; +use reqwest::Client; + +use crate::List; + +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(()) +} + +pub fn get_file_path(list: List, versionid: String) -> Result> { + let mut names: HashMap = HashMap::new(); + for file in read_dir(list.download_folder)? { + let path = file?.path(); + if path.is_file() { + let pathstr = path.to_str().ok_or("BAH")?; + let namesplit: Vec<&str> = pathstr.split('.').collect(); + let ver_id = namesplit[namesplit.len() - 2]; + names.insert(String::from(ver_id), String::from(pathstr)); + } + }; + + let api_versionid = format!("mr{}", versionid); + + let filename = names.get(&api_versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?; + + Ok(filename.to_owned()) +} -- cgit v1.2.3