diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/download.rs | 31 | ||||
-rw-r--r-- | src/commands/modification.rs | 2 | ||||
-rw-r--r-- | src/commands/update.rs | 2 | ||||
-rw-r--r-- | src/db.rs | 42 | ||||
-rw-r--r-- | src/files.rs | 33 |
5 files changed, 89 insertions, 21 deletions
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 @@ | |||
1 | use crate::{modrinth::Version, files::download_file}; | 1 | use crate::{files::get_downloaded_versions, db::{userlist_get_all_applicable_versions_with_mods, userlist_get_all_current_versions_with_mods}}; |
2 | #[allow(unused_imports)] | 2 | #[allow(unused_imports)] |
3 | use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; | 3 | use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; |
4 | 4 | ||
5 | pub async fn download(_config: Cfg, _input: Input) -> Result<(), Box<dyn std::error::Error>> { | 5 | pub async fn download(config: Cfg, _input: Input) -> Result<(), Box<dyn std::error::Error>> { |
6 | |||
7 | let current_list = get_current_list(config.clone())?; | ||
8 | |||
6 | println!("NO IMPLEMENTATION FOR DOWNLOAD YET"); | 9 | println!("NO IMPLEMENTATION FOR DOWNLOAD YET"); |
10 | |||
11 | let downloaded_versions = get_downloaded_versions(current_list.clone())?; | ||
12 | println!("DL: {:?}", downloaded_versions); | ||
13 | |||
14 | let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?; | ||
15 | println!("CU: {:?}", current_version_ids); | ||
16 | |||
17 | let applicable_version_ids = userlist_get_all_applicable_versions_with_mods(config, current_list.id)?; | ||
18 | println!("AP: {:?}", applicable_version_ids); | ||
19 | |||
7 | /* | 20 | /* |
8 | let list = get_current_list(config.clone())?; | 21 | let list = get_current_list(config.clone())?; |
9 | 22 | ||
@@ -34,18 +47,4 @@ async fn download_links(_config: Cfg, _input: Input, _current_list: List, _links | |||
34 | Ok(String::new()) | 47 | Ok(String::new()) |
35 | } | 48 | } |
36 | 49 | ||
37 | pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { | ||
38 | |||
39 | let dl_path = String::from(¤t_list.download_folder); | ||
40 | |||
41 | for ver in versions { | ||
42 | let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); | ||
43 | let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); | ||
44 | let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; | ||
45 | let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); | ||
46 | download_file(primary_file.url, current_list.clone().download_folder, filename).await?; | ||
47 | } | ||
48 | |||
49 | Ok(dl_path) | ||
50 | } | ||
51 | 50 | ||
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 @@ | |||
1 | use std::io::{Error, ErrorKind}; | 1 | use std::io::{Error, ErrorKind}; |
2 | 2 | ||
3 | 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}; | 3 | 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}}; |
4 | 4 | ||
5 | pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 5 | pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
6 | 6 | ||
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 @@ | |||
1 | use std::{io::{Error, ErrorKind}, fs::rename}; | 1 | use std::{io::{Error, ErrorKind}, fs::rename}; |
2 | 2 | ||
3 | 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}; | 3 | 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}}; |
4 | 4 | ||
5 | pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 5 | pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
6 | 6 | ||
@@ -122,7 +122,7 @@ pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> Result<Vec<DBModlist | |||
122 | let mut wherestr = String::from("WHERE"); | 122 | let mut wherestr = String::from("WHERE"); |
123 | for (i, id) in mods.iter().enumerate() { | 123 | for (i, id) in mods.iter().enumerate() { |
124 | let mut or = " OR"; | 124 | let mut or = " OR"; |
125 | if i == mods.len() - 1 { or = "" }; | 125 | if i == mods.len() - 1 { or = "" }; |
126 | wherestr = format!("{} id = '{}'{}", wherestr, id, or); | 126 | wherestr = format!("{} id = '{}'{}", wherestr, id, or); |
127 | } | 127 | } |
128 | 128 | ||
@@ -209,6 +209,26 @@ pub fn userlist_get_applicable_versions(config: Cfg, list_id: String, mod_id: St | |||
209 | } | 209 | } |
210 | } | 210 | } |
211 | 211 | ||
212 | pub fn userlist_get_all_applicable_versions_with_mods(config: Cfg, list_id: String) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> { | ||
213 | let data = format!("{}/data.db", config.data); | ||
214 | let connection = Connection::open(data)?; | ||
215 | |||
216 | let mut versions: Vec<(String, String)> = Vec::new(); | ||
217 | let mut stmt = connection.prepare(format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str())?; | ||
218 | let id_iter = stmt.query_map([], |row| { | ||
219 | Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?]) | ||
220 | })?; | ||
221 | |||
222 | for ver in id_iter { | ||
223 | let out = ver?; | ||
224 | versions.push((out[0].to_owned(), out[1].to_owned())); | ||
225 | }; | ||
226 | |||
227 | if versions.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; | ||
228 | |||
229 | Ok(versions) | ||
230 | } | ||
231 | |||
212 | pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { | 232 | pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { |
213 | let data = format!("{}/data.db", config.data); | 233 | let data = format!("{}/data.db", config.data); |
214 | let connection = Connection::open(data).unwrap(); | 234 | let connection = Connection::open(data).unwrap(); |
@@ -248,6 +268,26 @@ pub fn userlist_get_all_current_version_ids(config: Cfg, list_id: String) -> Res | |||
248 | Ok(versions) | 268 | Ok(versions) |
249 | } | 269 | } |
250 | 270 | ||
271 | pub fn userlist_get_all_current_versions_with_mods(config: Cfg, list_id: String) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> { | ||
272 | let data = format!("{}/data.db", config.data); | ||
273 | let connection = Connection::open(data)?; | ||
274 | |||
275 | let mut versions: Vec<(String, String)> = Vec::new(); | ||
276 | let mut stmt = connection.prepare(format!("SELECT mod_id, current_version FROM {}", list_id).as_str())?; | ||
277 | let id_iter = stmt.query_map([], |row| { | ||
278 | Ok(vec![row.get::<usize, String>(0)?, row.get::<usize, String>(1)?]) | ||
279 | })?; | ||
280 | |||
281 | for ver in id_iter { | ||
282 | let out = ver?; | ||
283 | versions.push((out[0].to_owned(), out[1].to_owned())); | ||
284 | }; | ||
285 | |||
286 | if versions.is_empty() { return Err(Box::new(std::io::Error::new(ErrorKind::Other, "NO_MODS_ON_LIST"))); }; | ||
287 | |||
288 | Ok(versions) | ||
289 | } | ||
290 | |||
251 | pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: String, versions: String, link: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { | 291 | pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: String, versions: String, link: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { |
252 | let data = format!("{}/data.db", config.data); | 292 | let data = format!("{}/data.db", config.data); |
253 | let connection = Connection::open(data)?; | 293 | 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}; | |||
2 | use futures_util::StreamExt; | 2 | use futures_util::StreamExt; |
3 | use reqwest::Client; | 3 | use reqwest::Client; |
4 | 4 | ||
5 | use crate::List; | 5 | use crate::{List, modrinth::Version}; |
6 | 6 | ||
7 | pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> { | 7 | pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { |
8 | |||
9 | let dl_path = String::from(¤t_list.download_folder); | ||
10 | |||
11 | for ver in versions { | ||
12 | let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); | ||
13 | let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); | ||
14 | let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; | ||
15 | let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); | ||
16 | download_file(primary_file.url, current_list.clone().download_folder, filename).await?; | ||
17 | } | ||
18 | |||
19 | Ok(dl_path) | ||
20 | } | ||
21 | |||
22 | async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> { | ||
8 | println!("Downloading {}", url); | 23 | println!("Downloading {}", url); |
9 | let dl_path_file = format!("{}/{}", path, name); | 24 | let dl_path_file = format!("{}/{}", path, name); |
10 | let res = Client::new() | 25 | let res = Client::new() |
@@ -50,3 +65,17 @@ pub fn get_file_path(list: List, versionid: String) -> Result<String, Box<dyn st | |||
50 | 65 | ||
51 | Ok(filename.to_owned()) | 66 | Ok(filename.to_owned()) |
52 | } | 67 | } |
68 | |||
69 | pub fn get_downloaded_versions(list: List) -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
70 | let mut versions: Vec<String> = vec![]; | ||
71 | for file in read_dir(list.download_folder)? { | ||
72 | let path = file?.path(); | ||
73 | if path.is_file() && path.extension().ok_or("BAH")? == "jar" { | ||
74 | let pathstr = path.to_str().ok_or("BAH")?; | ||
75 | let namesplit: Vec<&str> = pathstr.split('.').collect(); | ||
76 | versions.push(String::from(namesplit[namesplit.len() - 2].split_at(2).1)); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | Ok(versions) | ||
81 | } | ||