use futures_util::StreamExt; use reqwest::Client; use std::{ collections::HashMap, fs::{read_dir, remove_file, rename, File, copy}, io::Write, }; use crate::{ config::Cfg, db::{mods_get_info, userlist_add_disabled_versions}, error::{ErrorType, MLError, MLE}, modrinth::Version, List, cache::{get_cached_versions, copy_cached_version}, devdir, }; pub async fn download_versions(list: List, config: Cfg, versions: Vec) -> MLE { let mut cached = get_cached_versions(&devdir(&config.cache)); println!("{:#?}", cached); let dl_path = String::from(&list.download_folder); println!(" └Download mods to {}", dl_path); for ver in versions { let project_info = mods_get_info(config.clone(), &ver.project_id)?; //Check cache if already downloaded let c = cached.remove(&ver.id); if c.is_some() { print!("\t└({})Get version {} from cache", project_info.title, ver.id); //Force flush of stdout, else print! doesn't print instantly std::io::stdout().flush().unwrap(); copy_cached_version(&c.unwrap(), &dl_path); println!(" ✓"); } else { print!("\t└({})Download version {}", project_info.title, ver.id); //Force flush of stdout, else print! doesn't print instantly std::io::stdout().flush().unwrap(); let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); let extension = match splitname.pop().ok_or("") { Ok(e) => e, Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")), }; let filename = format!( "{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension ); download_file(primary_file.url, list.clone().download_folder, filename.clone()).await?; println!(" ✓"); //Copy file to cache print!("\t └Copy to cache"); //Force flush of stdout, else print! doesn't print instantly std::io::stdout().flush().unwrap(); let dl_path_file = format!("{}/{}", list.download_folder, filename); let cache_path = format!("{}/{}", devdir(&config.clone().cache), filename); // println!("{}:{}", dl_path_file, cache_path); copy(dl_path_file, cache_path)?; println!(" ✓"); } } Ok(dl_path) } async fn download_file(url: String, path: String, name: String) -> MLE<()> { let dl_path_file = format!("{}/{}", path, name); let res = Client::new().get(String::from(&url)).send().await?; // download chunks let mut file = File::create(&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 disable_version( config: Cfg, current_list: List, versionid: String, mod_id: String, ) -> MLE<()> { //println!("Disabling version {} for mod {}", versionid, mod_id); let file = get_file_path(current_list.clone(), String::from(&versionid))?; let disabled = format!("{}.disabled", file); rename(file, disabled)?; userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?; Ok(()) } pub fn delete_version(list: List, version: String) -> MLE<()> { let file = get_file_path(list, version)?; remove_file(file)?; Ok(()) } pub fn get_file_path(list: List, versionid: String) -> MLE { let mut names: HashMap = HashMap::new(); for file in read_dir(list.download_folder)? { let path = file?.path(); if path.is_file() { let pathstr = match path.to_str().ok_or("") { Ok(s) => s, Err(..) => return Err(MLError::new(ErrorType::Other, "INVALID_PATH")), }; let namesplit: Vec<&str> = pathstr.split('.').collect(); let ver_id = namesplit[namesplit.len() - 2]; names.insert(String::from(ver_id), String::from(pathstr)); } } let filename = match names.get(&versionid).ok_or("") { Ok(n) => n, Err(..) => { return Err(MLError::new( ErrorType::ArgumentError, "VERSION_NOT_FOUND_IN_FILES", )) } }; Ok(filename.to_owned()) } pub fn get_downloaded_versions(list: List) -> MLE> { let mut versions: HashMap = HashMap::new(); for file in read_dir(&list.download_folder)? { let path = file?.path(); if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" { let pathstr = path.to_str().ok_or("BAH").unwrap(); let namesplit: Vec<&str> = pathstr.split('.').collect(); versions.insert( String::from(namesplit[namesplit.len() - 3]), String::from(namesplit[namesplit.len() - 2]), ); } } Ok(versions) } pub fn clean_list_dir(list: &List) -> MLE<()> { let dl_path = &list.download_folder; println!(" └Clean directory for: {}", list.id); for entry in std::fs::read_dir(dl_path)? { let entry = entry?; std::fs::remove_file(entry.path())?; } Ok(()) }