From 758e608e6c331df2a5900de7f932f9b498573ed1 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 7 Dec 2022 17:45:16 +0100 Subject: groundwork for downloads; cleanup --- .gitignore | 1 + data.db | Bin 24576 -> 24576 bytes dl2/Capes-1.4.5+1.19.mric9rDGCw.jar | Bin 87580 -> 0 bytes planmodlist.xopp | Bin 193938 -> 221078 bytes src/commands/download.rs | 31 +++++++++++++------------- src/commands/modification.rs | 2 +- src/commands/update.rs | 2 +- src/db.rs | 42 +++++++++++++++++++++++++++++++++++- src/files.rs | 33 ++++++++++++++++++++++++++-- 9 files changed, 90 insertions(+), 21 deletions(-) delete mode 100644 dl2/Capes-1.4.5+1.19.mric9rDGCw.jar diff --git a/.gitignore b/.gitignore index fbc8fea..e7ec2c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /target /api-tests /dl +/dl2 /test_tmp .planmodlist.autosave.xopp diff --git a/data.db b/data.db index cdc265d..6a619f8 100644 Binary files a/data.db and b/data.db differ diff --git a/dl2/Capes-1.4.5+1.19.mric9rDGCw.jar b/dl2/Capes-1.4.5+1.19.mric9rDGCw.jar deleted file mode 100644 index 26193e6..0000000 Binary files a/dl2/Capes-1.4.5+1.19.mric9rDGCw.jar and /dev/null differ diff --git a/planmodlist.xopp b/planmodlist.xopp index 7640c1e..6a928b5 100644 Binary files a/planmodlist.xopp and b/planmodlist.xopp differ diff --git a/src/commands/download.rs b/src/commands/download.rs index b0efdc2..421f058 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,9 +1,22 @@ -use crate::{modrinth::Version, files::download_file}; +use crate::{files::get_downloaded_versions, db::{userlist_get_all_applicable_versions_with_mods, userlist_get_all_current_versions_with_mods}}; #[allow(unused_imports)] 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> { +pub async fn download(config: Cfg, _input: Input) -> Result<(), Box> { + + let current_list = get_current_list(config.clone())?; + println!("NO IMPLEMENTATION FOR DOWNLOAD YET"); + + let downloaded_versions = get_downloaded_versions(current_list.clone())?; + println!("DL: {:?}", downloaded_versions); + + let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?; + println!("CU: {:?}", current_version_ids); + + let applicable_version_ids = userlist_get_all_applicable_versions_with_mods(config, current_list.id)?; + println!("AP: {:?}", applicable_version_ids); + /* let list = get_current_list(config.clone())?; @@ -34,18 +47,4 @@ async fn download_links(_config: Cfg, _input: Input, _current_list: List, _links Ok(String::new()) } -pub async fn download_versions(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 mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); - let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; - let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); - download_file(primary_file.url, current_list.clone().download_folder, filename).await?; - } - - Ok(dl_path) -} diff --git a/src/commands/modification.rs b/src/commands/modification.rs index f36c8c6..91243fc 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,6 +1,6 @@ use std::io::{Error, ErrorKind}; -use crate::{modrinth::{project, versions, extract_current_version, Version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, download_versions, files::delete_version}; +use crate::{modrinth::{project, versions, extract_current_version, Version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}}; pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { diff --git a/src/commands/update.rs b/src/commands/update.rs index bf13319..eeb5a4d 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,6 +1,6 @@ use std::{io::{Error, ErrorKind}, fs::rename}; -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, lists_get_all_ids, lists_get, userlist_get_current_version, userlist_add_disabled_versions, mods_change_versions}, List, input::Input, files::{get_file_path, delete_version}, download_versions}; +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, lists_get_all_ids, lists_get, userlist_get_current_version, userlist_add_disabled_versions, mods_change_versions}, List, input::Input, files::{get_file_path, delete_version, download_versions}}; pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { diff --git a/src/db.rs b/src/db.rs index 9d862d6..cd32d1f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -122,7 +122,7 @@ pub fn mods_get_versions(config: Cfg, mods: Vec) -> Result Result, Box> { + let data = format!("{}/data.db", config.data); + let connection = Connection::open(data)?; + + let mut versions: Vec<(String, String)> = Vec::new(); + let mut stmt = connection.prepare(format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str())?; + let id_iter = stmt.query_map([], |row| { + Ok(vec![row.get::(0)?, row.get::(1)?]) + })?; + + for ver in id_iter { + let out = ver?; + versions.push((out[0].to_owned(), out[1].to_owned())); + }; + + if versions.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; + + Ok(versions) +} + pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); @@ -248,6 +268,26 @@ pub fn userlist_get_all_current_version_ids(config: Cfg, list_id: String) -> Res Ok(versions) } +pub fn userlist_get_all_current_versions_with_mods(config: Cfg, list_id: String) -> Result, Box> { + let data = format!("{}/data.db", config.data); + let connection = Connection::open(data)?; + + let mut versions: Vec<(String, String)> = Vec::new(); + let mut stmt = connection.prepare(format!("SELECT mod_id, current_version FROM {}", list_id).as_str())?; + let id_iter = stmt.query_map([], |row| { + Ok(vec![row.get::(0)?, row.get::(1)?]) + })?; + + for ver in id_iter { + let out = ver?; + versions.push((out[0].to_owned(), out[1].to_owned())); + }; + + if versions.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; + + Ok(versions) +} + pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: String, versions: String, link: String, mod_id: String) -> Result<(), Box> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; diff --git a/src/files.rs b/src/files.rs index 2c5994d..959da75 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,9 +2,24 @@ use std::{fs::{File, read_dir, remove_file}, io::Write, collections::HashMap}; use futures_util::StreamExt; use reqwest::Client; -use crate::List; +use crate::{List, modrinth::Version}; -pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box> { +pub async fn download_versions(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 mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); + let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; + let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); + download_file(primary_file.url, current_list.clone().download_folder, filename).await?; + } + + Ok(dl_path) +} + +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() @@ -50,3 +65,17 @@ pub fn get_file_path(list: List, versionid: String) -> Result Result, Box> { + let mut versions: Vec = vec![]; + for file in read_dir(list.download_folder)? { + let path = file?.path(); + if path.is_file() && path.extension().ok_or("BAH")? == "jar" { + let pathstr = path.to_str().ok_or("BAH")?; + let namesplit: Vec<&str> = pathstr.split('.').collect(); + versions.push(String::from(namesplit[namesplit.len() - 2].split_at(2).1)); + } + } + + Ok(versions) +} -- cgit v1.2.3