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 --- planmodlist.xopp | Bin 197651 -> 193806 bytes src/apis/modrinth.rs | 12 ++++++------ src/commands/download.rs | 39 +++++++----------------------------- src/commands/update.rs | 50 ++++++++++++++--------------------------------- src/input.rs | 2 +- src/lib.rs | 24 ++++++++++++++++++++++- tests/db.rs | 28 +++++++++++++------------- 7 files changed, 66 insertions(+), 89 deletions(-) diff --git a/planmodlist.xopp b/planmodlist.xopp index a1fd2fa..5851878 100644 Binary files a/planmodlist.xopp and b/planmodlist.xopp differ diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index ec8d203..c99cfbf 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -4,7 +4,7 @@ use serde::Deserialize; use crate::{Modloader, List}; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct Project { pub slug: String, pub title: String, @@ -29,21 +29,21 @@ pub struct Project { pub versions: Vec, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct License { pub id: String, pub name: String, pub url: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct ModeratorMessage { pub message: String, pub body: Option, } #[allow(non_camel_case_types)] -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub enum Side { required, optional, @@ -51,7 +51,7 @@ pub enum Side { } #[allow(non_camel_case_types)] -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub enum Type { r#mod, modpack, @@ -59,7 +59,7 @@ pub enum Type { } #[allow(non_camel_case_types)] -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub enum Status { approved, rejected, 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()) } diff --git a/src/input.rs b/src/input.rs index 19aa2c2..0b616d4 100644 --- a/src/input.rs +++ b/src/input.rs @@ -129,7 +129,7 @@ pub async fn get_input(config: Cfg) -> Result<(), Box> { #[test] fn input_from() { - let string = "lis add test 1.19.2 fabric"; + let string = "list add test 1.19.2 fabric"; let input = Input{ command: Cmd::List, subcommand: Some(Subcmd::Add), args: Some(vec![String::from("test"), String::from("1.19.2"), String::from("fabric")]), force_download: false, direct_download: false, all_lists: false, clean: false, delete_old: false }; assert_eq!(Input::from(string).unwrap(), input); } diff --git a/src/lib.rs b/src/lib.rs index 51b4487..e4ebf76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,10 +5,12 @@ pub mod input; pub mod db; pub mod error; -use std::io::{Error, ErrorKind}; +use std::{io::{Error, ErrorKind, Write}, fs::File}; pub use apis::*; pub use commands::*; +use futures_util::StreamExt; +use reqwest::Client; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Modloader { @@ -32,3 +34,23 @@ impl Modloader { } } } + +pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box> { + 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(()) +} diff --git a/tests/db.rs b/tests/db.rs index 7127fc6..e0bdb66 100644 --- a/tests/db.rs +++ b/tests/db.rs @@ -78,7 +78,7 @@ fn test_mods_get_versions() { fn test_userlist_insert() { let config = setup(); - lists_insert(config.clone(), String::from("UL_I_LIST"), String::from("UL_I_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("UL_I_LIST"), String::from("UL_I_MC"), Modloader::Fabric, String::from("UL_I_FOLDER")).unwrap(); userlist_insert(config, String::from("UL_I_LIST"), String::from("UL_I"), String::from("UL_I_VER1"), vec![String::from("UL_I_VER1"), String::from("UL_I_VER2")], String::from("localhost:8080/dl/UL_I_VER1.test")).unwrap(); } @@ -86,7 +86,7 @@ fn test_userlist_insert() { fn test_userlist_get_all_ids() { let config = setup(); - lists_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI_MC"), Modloader::Fabric, String::from("UL_GAI_FOLDER")).unwrap(); userlist_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI1"), String::from("UL_GAI1_VER1"), vec![String::from("UL_GAI2_VER1"), String::from("UL_GAI1_VER2")], String::from("localhost:8080/dl/UL_GAI1_VER1.test")).unwrap(); userlist_insert(config.clone(), String::from("UL_GAI_LIST"), String::from("UL_GAI2"), String::from("UL_GAI2_VER1"), vec![String::from("UL_GAI2_VER1"), String::from("UL_GAI2_VER2")], String::from("localhost:8080/dl/UL_GAI2_VER1.test")).unwrap(); let ids = userlist_get_all_ids(config, String::from("UL_GAI_LIST")).unwrap(); @@ -99,7 +99,7 @@ fn test_userlist_get_all_ids() { fn test_userlist_remove() { let config = setup(); - lists_insert(config.clone(), String::from("UL_R_LIST"), String::from("UL_R_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("UL_R_LIST"), String::from("UL_R_MC"), Modloader::Fabric, String::from("UL_R_FOLDER")).unwrap(); userlist_insert(config.clone(), String::from("UL_R_LIST"), String::from("UL_R"), String::from("UL_R_VER1"), vec![String::from("UL_R_VER1"), String::from("UL_R_VER2")], String::from("localhost:8080/dl/UL_R_VER1.test")).unwrap(); let ids = userlist_get_all_ids(config.clone(), String::from("UL_R_LIST")).unwrap(); assert!(ids.contains(&String::from("UL_R"))); @@ -111,7 +111,7 @@ fn test_userlist_remove() { fn test_userlist_get_applicable_versions() { let config = setup(); - lists_insert(config.clone(), String::from("UL_GAV_LIST"), String::from("UL_GAV_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("UL_GAV_LIST"), String::from("UL_GAV_MC"), Modloader::Fabric, String::from("UL_GAV_FOLDER")).unwrap(); userlist_insert(config.clone(), String::from("UL_GAV_LIST"), String::from("UL_GAV"), String::from("UL_GAV_VER1"), vec![String::from("UL_GAV_VER1"), String::from("UL_GAV_VER2")], String::from("localhost:8080/dl/UL_GAV_VER1.test")).unwrap(); assert_eq!(userlist_get_applicable_versions(config, String::from("UL_GAV_LIST"), String::from("UL_GAV")).unwrap(), String::from("UL_GAV_VER1|UL_GAV_VER2")); } @@ -120,7 +120,7 @@ fn test_userlist_get_applicable_versions() { fn test_userlist_get_all_current_version_ids() { let config = setup(); - lists_insert(config.clone(), String::from("UL_GACVI_LIST"), String::from("UL_GACVI_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("UL_GACVI_LIST"), String::from("UL_GACVI_MC"), Modloader::Fabric, String::from("UL_GACVI_FOLDER")).unwrap(); userlist_insert(config.clone(), String::from("UL_GACVI_LIST"), String::from("UL_GACVI1"), String::from("UL_GACVI1_VER1"), vec![String::from("UL_GACVI2_VER1"), String::from("UL_GACVI1_VER2")], String::from("localhost:8080/dl/UL_GACVI1_VER1.test")).unwrap(); userlist_insert(config.clone(), String::from("UL_GACVI_LIST"), String::from("UL_GACVI2"), String::from("UL_GACVI2_VER1"), vec![String::from("UL_GACVI2_VER1"), String::from("UL_GACVI2_VER2")], String::from("localhost:8080/dl/UL_GACVI2_VER1.test")).unwrap(); @@ -134,7 +134,7 @@ fn test_userlist_get_all_current_version_ids() { fn test_userlist_change_versions() { let config = setup(); - lists_insert(config.clone(), String::from("UL_CV_LIST"), String::from("UL_CV_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("UL_CV_LIST"), String::from("UL_CV_MC"), Modloader::Fabric, String::from("UL_CV_FOLDER")).unwrap(); userlist_insert(config.clone(), String::from("UL_CV_LIST"), String::from("UL_CV"), String::from("UL_CV_VER1"), vec![String::from("UL_CV_VER1"), String::from("UL_CV_VER2")], String::from("localhost:8080/dl/UL_CV_VER1.test")).unwrap(); let versions = userlist_get_all_current_version_ids(config.clone(), String::from("UL_CV_LIST")).unwrap(); assert!(versions.contains(&String::from("UL_CV_VER1"))); @@ -149,7 +149,7 @@ fn test_userlist_change_versions() { fn test_userlist_get_all_downloads() { let config = setup(); - lists_insert(config.clone(), String::from("UL_GAD_LIST"), String::from("UL_GAD_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("UL_GAD_LIST"), String::from("UL_GAD_MC"), Modloader::Fabric, String::from("UL_GAD_FOLDER")).unwrap(); userlist_insert(config.clone(), String::from("UL_GAD_LIST"), String::from("UL_GAD1"), String::from("UL_GAD1_VER1"), vec![String::from("UL_GAD1_VER1"), String::from("UL_GAD1_VER1")], String::from("localhost:8080/dl/UL_GAD1_VER1.test")).unwrap(); userlist_insert(config.clone(), String::from("UL_GAD_LIST"), String::from("UL_GAD2"), String::from("UL_GAD2_VER1"), vec![String::from("UL_GAD2_VER1"), String::from("UL_GAD2_VER1")], String::from("localhost:8080/dl/UL_GAD2_VER1.test")).unwrap(); let links = userlist_get_all_downloads(config, String::from("UL_GAD_LIST")).unwrap(); @@ -164,14 +164,14 @@ fn test_userlist_get_all_downloads() { fn test_lists_insert() { let config = setup(); - lists_insert(config, String::from("L_I_LIST"), String::from("L_I_MC"), Modloader::Fabric).unwrap(); + lists_insert(config, String::from("L_I_LIST"), String::from("L_I_MC"), Modloader::Fabric, String::from("L_I_FOLDER")).unwrap(); } #[test] fn test_lists_remove() { let config = setup(); - lists_insert(config.clone(), String::from("L_R_LIST"), String::from("L_R_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("L_R_LIST"), String::from("L_R_MC"), Modloader::Fabric, String::from("L_R_FOLDER")).unwrap(); lists_remove(config, String::from("L_R_LIST")).unwrap(); } @@ -179,17 +179,17 @@ fn test_lists_remove() { fn test_lists_get() { let config = setup(); - lists_insert(config.clone(), String::from("L_G_LIST"), String::from("L_G_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("L_G_LIST"), String::from("L_G_MC"), Modloader::Fabric, String::from("L_G_FOLDER")).unwrap(); - assert_eq!(lists_get(config, String::from("L_G_LIST")).unwrap(), List { id: String::from("L_G_LIST"), mc_version: String::from("L_G_MC"), modloader: Modloader::Fabric }); + assert_eq!(lists_get(config, String::from("L_G_LIST")).unwrap(), List { id: String::from("L_G_LIST"), mc_version: String::from("L_G_MC"), modloader: Modloader::Fabric, download_folder: String::from("L_G_FOLDER") }); } #[test] fn test_lists_get_all_ids() { let config = setup(); - lists_insert(config.clone(), String::from("L_GAI1_LIST"), String::from("L_GAI1_MC"), Modloader::Fabric).unwrap(); - lists_insert(config.clone(), String::from("L_GAI2_LIST"), String::from("L_GAI2_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("L_GAI1_LIST"), String::from("L_GAI1_MC"), Modloader::Fabric, String::from("L_GAI1_FOLDER")).unwrap(); + lists_insert(config.clone(), String::from("L_GAI2_LIST"), String::from("L_GAI2_MC"), Modloader::Fabric, String::from("L_GAI2_FOLDER")).unwrap(); let ids = lists_get_all_ids(config).unwrap(); assert!(ids.contains(&String::from("L_GAI1_LIST"))); @@ -217,7 +217,7 @@ fn test_config_get_current_list() { fn test_s_userlist_update_download() { let config = setup(); - lists_insert(config.clone(), String::from("UL_UD_LIST"), String::from("UL_UD_MC"), Modloader::Fabric).unwrap(); + lists_insert(config.clone(), String::from("UL_UD_LIST"), String::from("UL_UD_MC"), Modloader::Fabric, String::from("UL_UD_FOLDER")).unwrap(); userlist_insert(config.clone(), String::from("UL_UD_LIST"), String::from("UL_UD"), String::from("UL_UD_VER1"), vec![String::from("UL_UD_VER1"), String::from("UL_UD_VER1")], String::from("localhost:8080/dl/UL_UD_VER1.test")).unwrap(); s_userlist_update_download(config.clone(), String::from("UL_UD_LIST"), String::from("UL_UD"), String::from("localhost:8080/dl/UL_UD_VER1X.test")).unwrap(); let links = userlist_get_all_downloads(config, String::from("UL_UD_LIST")).unwrap(); -- cgit v1.2.3