From c00673fd0e01d1438798dbb1635a761a76a2b559 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 20 Nov 2022 23:54:20 +0100 Subject: extracted filedownload to fn; fixed some tests; added direct-dl to update --- src/commands/download.rs | 39 +++++++------------------------------ src/commands/update.rs | 50 +++++++++++++++--------------------------------- 2 files changed, 22 insertions(+), 67 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index db0fc93..82d6b02 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,9 +1,3 @@ -use std::{io::Write, fs::File}; - -use reqwest::Client; - -use futures_util::StreamExt; - use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; pub async fn download(config: Cfg, input: Input) -> Result<(), Box> { @@ -16,9 +10,11 @@ pub async fn download(config: Cfg, input: Input) -> Result<(), Box) -> Result> { - - let dl_path = String::from(&config.downloads); +async fn download_links(_config: Cfg, _input: Input, _current_list: List, _links: Vec) -> Result> { + println!("NO DL IMPLEMENTATION FOR DOWNLOAD YET"); + //TODO copy dl from update if possible + /* + let dl_path = String::from(¤t_list.download_folder); if input.clean { let dl_path = ¤t_list.download_folder; @@ -28,28 +24,7 @@ async fn download_links(config: Cfg, input: Input, current_list: List, links: Ve std::fs::remove_file(entry.path())?; } } + */ - for link in links { - let filename = link.split('/').last().unwrap(); - let dl_path_file = format!("{}/{}", config.downloads, filename); - println!("Downloading {}", link); - - let res = Client::new() - .get(String::from(&link)) - .send() - .await - .or(Err(format!("Failed to GET from '{}'", &link)))?; - - // download chunks - let mut file = File::create(String::from(&dl_path_file)).or(Err(format!("Failed to create file '{}'", dl_path_file)))?; - let mut stream = res.bytes_stream(); - - while let Some(item) = stream.next().await { - let chunk = item.or(Err("Error while downloading file"))?; - file.write_all(&chunk) - .or(Err("Error while writing to file"))?; - } - } - - Ok(dl_path) + Ok(String::new()) } diff --git a/src/commands/update.rs b/src/commands/update.rs index 85630f5..eba5e91 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,10 +1,6 @@ -use std::{io::{Error, ErrorKind, Write}, fs::File}; +use std::io::{Error, ErrorKind}; -use reqwest::Client; - -use futures_util::StreamExt; - -use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List, input::Input}; +use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List, input::Input, download_file}; pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { @@ -29,16 +25,16 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box ver, //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") - Err(_) => { continue; }, + Err(_) => { println!("({}) No new version found for the specified minecraft version", project.title); continue; }, }); + } else { + println!("({}) No new version found", project.title); }; }; - //println!("{:?}", updatestack); - - + if input.clean { let dl_path = ¤t_list.download_folder; println!("Cleaning {}", dl_path); @@ -47,8 +43,8 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box) -> Result> { +async fn download_updates(current_list: List, versions: Vec) -> Result> { let dl_path = String::from(¤t_list.download_folder); for ver in versions { let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); - let dl_path_file = format!("{}/{}", config.downloads, primary_file.filename); - println!("Downloading {}", primary_file.url); - - let res = Client::new() - .get(String::from(&primary_file.url)) - .send() - .await - .or(Err(format!("Failed to GET from '{}'", &primary_file.url)))?; - - // download chunks - let mut file = File::create(String::from(&dl_path_file)).or(Err(format!("Failed to create file '{}'", dl_path_file)))?; - let mut stream = res.bytes_stream(); - - while let Some(item) = stream.next().await { - let chunk = item.or(Err("Error while downloading file"))?; - file.write_all(&chunk) - .or(Err("Error while writing to file"))?; - } + download_file(primary_file.url, current_list.clone().download_folder, primary_file.filename).await?; } + Ok(dl_path) } #[tokio::test] async fn download_updates_test() { - use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, config::{Cfg, Apis}}; + use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; - let config = Cfg { data: "...".to_string(), clean_remove: false, downloads: "./dl".to_string(), apis: Apis { modrinth: "...".to_string() } }; + let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") }; let versions = vec![Version { id: "dEqtGnT9".to_string(), @@ -148,5 +128,5 @@ async fn download_updates_test() { "fabric".to_string() ] }]; - assert_eq!(download_updates(config, versions).await.unwrap(), "./dl") + assert!(download_updates(current_list, versions).await.is_ok()) } -- cgit v1.2.3