From 0f5223d3d3f6aeb6bb1a0b09ad3d4ef5731774dd Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sat, 5 Nov 2022 21:53:24 +0100 Subject: added setup & download; direct input --- src/commands/download.rs | 46 +++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 4 ++++ src/commands/modification.rs | 11 +++++---- src/commands/setup.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/commands/update.rs | 2 +- 5 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 src/commands/download.rs create mode 100644 src/commands/setup.rs (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs new file mode 100644 index 0000000..05c54cb --- /dev/null +++ b/src/commands/download.rs @@ -0,0 +1,46 @@ +use std::{io::Write, fs::File}; + +use reqwest::Client; + +use futures_util::StreamExt; + +use crate::{get_current_list, config::Cfg, db::get_dl_links}; + +pub async fn download(config: Cfg) -> Result<(), Box> { + let list = get_current_list(config.clone())?; + + let links = get_dl_links(config.clone(), list)?; + + download_links(config, links).await?; + + Ok(()) +} + +async fn download_links(config: Cfg, links: Vec) -> Result> { + + let dl_path = String::from(&config.downloads); + + 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) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 5d008fd..20badcb 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,7 +1,11 @@ pub mod modification; pub mod list; pub mod update; +pub mod setup; +pub mod download; pub use modification::*; pub use list::*; pub use update::*; +pub use setup::*; +pub use download::*; diff --git a/src/commands/modification.rs b/src/commands/modification.rs index b90c82c..e877a63 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -34,9 +34,12 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box { @@ -44,10 +47,10 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box insert_mod_in_list(config.clone(), current_list, String::from(&project.id), current_version, available_versions)?, + Err(..) => insert_mod_in_list(config.clone(), current_list, String::from(&project.id), current_version.id, available_versions, file)?, }; match get_mods(config.clone()) { diff --git a/src/commands/setup.rs b/src/commands/setup.rs new file mode 100644 index 0000000..0223a21 --- /dev/null +++ b/src/commands/setup.rs @@ -0,0 +1,54 @@ +use std::{fs::File, path::Path, io::{Error, ErrorKind}}; + +use crate::{config::Cfg, db::{db_setup, get_dbversion, create_dbversion, insert_column, get_lists, get_list, get_current_versions, insert_dl_link}, modrinth::get_raw_versions}; + +pub async fn setup(config: Cfg) -> Result<(), Box> { + + let db_file = format!("{}/data.db", String::from(&config.data)); + + if !Path::new(&db_file).exists() { + return create(config, db_file); + } + + match get_dbversion(config.clone()) { + Ok(ver) => { + match ver.as_str() { + _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION"))) + } + }, + Err(..) => to_02(config).await? + }; + + Ok(()) +} + +fn create(config: Cfg, db_file: String) -> Result<(), Box> { + File::create(db_file)?; + db_setup(config)?; + Ok(()) +} + +async fn to_02(config: Cfg) -> Result<(), Box> { + let lists = get_lists(config.clone())?; + + for list in lists { + println!("Updating {}", list); + insert_column(config.clone(), String::from(&list), String::from("current_download"), sqlite::Type::String)?; + + let full_list = get_list(config.clone(), String::from(&list))?; + + let versions = get_current_versions(config.clone(), full_list.clone())?; + + let raw_versions = get_raw_versions(String::from(&config.apis.modrinth), versions).await; + + for ver in raw_versions { + println!("Adding link for {}", ver.project_id); + let file = ver.files.into_iter().find(|f| f.primary).unwrap(); + insert_dl_link(config.clone(), full_list.clone(), ver.project_id, file.url)?; + } + }; + create_dbversion(config)?; + + + Ok(()) +} diff --git a/src/commands/update.rs b/src/commands/update.rs index 6275bce..284d289 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -37,7 +37,7 @@ pub async fn update(config: Cfg) -> Result<(), Box> { }; //println!("{:?}", updatestack); - //download_updates(config, updatestack).await?; + download_updates(config, updatestack).await?; Ok(()) } -- cgit v1.2.3