blob: 2c5994d1cabe5c3e69d9f969fb9b4f77f7467ee7 (
plain) (
tree)
|
|
use std::{fs::{File, read_dir, remove_file}, 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<dyn std::error::Error>> {
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 delete_version(list: List, version: String) -> Result<(), Box<dyn std::error::Error>> {
let file = get_file_path(list, version)?;
remove_file(file)?;
Ok(())
}
pub fn get_file_path(list: List, versionid: String) -> Result<String, Box<dyn std::error::Error>> {
let mut names: HashMap<String, String> = 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())
}
|