From 1890d59428dfcca861ea1b7820411d80cc60d713 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 22 Jan 2023 22:34:17 +0100 Subject: Added list version cmd, fixed some todos --- src/files.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index 14e5636..0d7dc0a 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,13 +2,19 @@ use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::Has use futures_util::StreamExt; use reqwest::Client; -use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg, error::{MLE, MLError, ErrorType}}; +use crate::{List, modrinth::Version, db::{userlist_add_disabled_versions, mods_get_name}, config::Cfg, error::{MLE, MLError, ErrorType}}; -pub async fn download_versions(current_list: List, versions: Vec) -> MLE { +pub async fn download_versions(list: List, config: Cfg, versions: Vec) -> MLE { - let dl_path = String::from(¤t_list.download_folder); + let dl_path = String::from(&list.download_folder); + + println!("Download to directory from: {} ({})", list.id, dl_path); for ver in versions { + //TODO get project name instead of projectid from db + let project_name = mods_get_name(config.clone(), &ver.project_id)?; + print!("\t({})Download version {}", project_name, ver.id); + 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("") { @@ -16,14 +22,15 @@ pub async fn download_versions(current_list: List, versions: Vec) -> ML 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, current_list.clone().download_folder, filename).await?; + download_file(primary_file.url, list.clone().download_folder, filename).await?; + tokio::time::sleep(std::time::Duration::new(3, 0)).await; + println!(" ✓"); } Ok(dl_path) } async fn download_file(url: String, path: String, name: String) -> MLE<()> { - println!("Downloading {}", url); let dl_path_file = format!("{}/{}", path, name); let res = Client::new() .get(String::from(&url)) @@ -43,7 +50,7 @@ async fn download_file(url: String, path: String, name: String) -> MLE<()> { } pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> MLE<()> { - println!("Disabling version {} for mod {}", versionid, mod_id); + //println!("Disabling version {} for mod {}", versionid, mod_id); let file = get_file_path(current_list.clone(), String::from(&versionid))?; let disabled = format!("{}.disabled", file); @@ -97,3 +104,13 @@ pub fn get_downloaded_versions(list: List) -> MLE> { } 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(()) +} -- cgit v1.2.3