From 416f4dc383ff5a1194da3a5532a8e159a4a1dac0 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 23 Apr 2023 18:24:32 +0200 Subject: added caching, better data location --- src/files.rs | 67 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 21 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index 6160cb4..ecf9b52 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,7 +2,7 @@ use futures_util::StreamExt; use reqwest::Client; use std::{ collections::HashMap, - fs::{read_dir, remove_file, rename, File}, + fs::{read_dir, remove_file, rename, File, copy}, io::Write, }; @@ -11,35 +11,60 @@ use crate::{ db::{mods_get_info, userlist_add_disabled_versions}, error::{ErrorType, MLError, MLE}, modrinth::Version, - List, + 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)?; - 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).await?; - //tokio::time::sleep(std::time::Duration::new(3, 0)).await; - println!(" ✓"); + + //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) -- cgit v1.2.3