summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2022-12-07 17:45:16 +0100
committerFxQnLr <[email protected]>2022-12-07 17:45:16 +0100
commit758e608e6c331df2a5900de7f932f9b498573ed1 (patch)
tree4ec306b7b69613286f1d070a52c7b26d6280b111
parent2ec20c50e7c02d82b248835988df040bd266b659 (diff)
downloadmodlist-758e608e6c331df2a5900de7f932f9b498573ed1.tar
modlist-758e608e6c331df2a5900de7f932f9b498573ed1.tar.gz
modlist-758e608e6c331df2a5900de7f932f9b498573ed1.zip
groundwork for downloads; cleanup
-rw-r--r--.gitignore1
-rw-r--r--data.dbbin24576 -> 24576 bytes
-rw-r--r--dl2/Capes-1.4.5+1.19.mric9rDGCw.jarbin87580 -> 0 bytes
-rw-r--r--planmodlist.xoppbin193938 -> 221078 bytes
-rw-r--r--src/commands/download.rs31
-rw-r--r--src/commands/modification.rs2
-rw-r--r--src/commands/update.rs2
-rw-r--r--src/db.rs42
-rw-r--r--src/files.rs33
9 files changed, 90 insertions, 21 deletions
diff --git a/.gitignore b/.gitignore
index fbc8fea..e7ec2c6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
1/target 1/target
2/api-tests 2/api-tests
3/dl 3/dl
4/dl2
4/test_tmp 5/test_tmp
5.planmodlist.autosave.xopp 6.planmodlist.autosave.xopp
diff --git a/data.db b/data.db
index cdc265d..6a619f8 100644
--- a/data.db
+++ b/data.db
Binary files 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
--- a/dl2/Capes-1.4.5+1.19.mric9rDGCw.jar
+++ /dev/null
Binary files differ
diff --git a/planmodlist.xopp b/planmodlist.xopp
index 7640c1e..6a928b5 100644
--- a/planmodlist.xopp
+++ b/planmodlist.xopp
Binary files 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 @@
1use crate::{modrinth::Version, files::download_file}; 1use 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)]
3use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; 3use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input};
4 4
5pub async fn download(_config: Cfg, _input: Input) -> Result<(), Box<dyn std::error::Error>> { 5pub 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(&current_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
37pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> {
38
39 let dl_path = String::from(&current_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 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use 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}; 3use 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
5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5pub 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 @@
1use std::{io::{Error, ErrorKind}, fs::rename}; 1use std::{io::{Error, ErrorKind}, fs::rename};
2 2
3use 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}; 3use 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
5pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
6 6
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<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
212pub 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
212pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { 232pub 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
271pub 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
251pub 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>> { 291pub 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};
2use futures_util::StreamExt; 2use futures_util::StreamExt;
3use reqwest::Client; 3use reqwest::Client;
4 4
5use crate::List; 5use crate::{List, modrinth::Version};
6 6
7pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> { 7pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> {
8
9 let dl_path = String::from(&current_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
22async 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
69pub 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}