summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2022-12-09 20:30:10 +0100
committerfxqnlr <[email protected]>2022-12-09 20:30:10 +0100
commit1c8964441432047eaa2e866666c4055b35b55253 (patch)
treea14da6da0410a73defa724bac832c7b7f147dcab /src
parenta9b5f7be4e98ecaa016382d3024270192ee586b0 (diff)
downloadmodlist-1c8964441432047eaa2e866666c4055b35b55253.tar
modlist-1c8964441432047eaa2e866666c4055b35b55253.tar.gz
modlist-1c8964441432047eaa2e866666c4055b35b55253.zip
added download with delete; new file format
Diffstat (limited to 'src')
-rw-r--r--src/commands/download.rs69
-rw-r--r--src/commands/update.rs41
-rw-r--r--src/files.rs31
3 files changed, 71 insertions, 70 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs
index 421f058..9f70499 100644
--- a/src/commands/download.rs
+++ b/src/commands/download.rs
@@ -1,50 +1,51 @@
1use crate::{files::get_downloaded_versions, db::{userlist_get_all_applicable_versions_with_mods, userlist_get_all_current_versions_with_mods}}; 1use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::userlist_get_all_current_versions_with_mods, modrinth::get_raw_versions};
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 6
7 let current_list = get_current_list(config.clone())?; 7 let current_list = get_current_list(config.clone())?;
8 8
9 println!("NO IMPLEMENTATION FOR DOWNLOAD YET");
10
11 let downloaded_versions = get_downloaded_versions(current_list.clone())?; 9 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))?; 10 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 11
17 let applicable_version_ids = userlist_get_all_applicable_versions_with_mods(config, current_list.id)?; 12 let mut to_download: Vec<String> = vec![];
18 println!("AP: {:?}", applicable_version_ids); 13 //(mod_id, version_id)
19 14 let mut to_disable: Vec<(String, String)> = vec![];
20 /*
21 let list = get_current_list(config.clone())?;
22 15
23 let links = userlist_get_all_downloads(config.clone(), list.clone().id)?; 16 for version in current_version_ids {
17 let mod_id = version.0;
18 let current_version = version.1;
24 19
25 download_links(config, input, list, links).await?; 20 let current_download = downloaded_versions.get(&mod_id);
26 */
27 Ok(())
28}
29 21
30#[allow(dead_code)] 22 if current_download.is_none() {
31async fn download_links(_config: Cfg, _input: Input, _current_list: List, _links: Vec<String>) -> Result<String, Box<dyn std::error::Error>> { 23 to_download.push(current_version);
32 println!("NO DL IMPLEMENTATION FOR DOWNLOAD YET"); 24 } else {
33 //TODO copy dl from update if possible 25 let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG")?;
34 /* 26 if &current_version != downloaded_version {
35 let dl_path = String::from(&current_list.download_folder); 27 to_disable.push((mod_id.clone(), String::from(downloaded_version)));
36 28 to_download.push(current_version);
37 if input.clean { 29 }
38 let dl_path = &current_list.download_folder; 30 }
39 println!("Cleaning {}", dl_path); 31 }
40 for entry in std::fs::read_dir(dl_path)? { 32
41 let entry = entry?; 33 if !to_download.is_empty() {
42 std::fs::remove_file(entry.path())?; 34 download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?;
35 } else {
36 println!("There are no new versions to download");
37 }
38
39 if !to_disable.is_empty() {
40 for ver in to_disable {
41 if input.delete_old {
42 println!("Deleting version {} for mod {}", ver.1, ver.0);
43 delete_version(current_list.clone(), ver.1)?;
44 } else {
45 disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?;
46 };
43 } 47 }
44 } 48 }
45 */
46 49
47 Ok(String::new()) 50 Ok(())
48} 51}
49
50
diff --git a/src/commands/update.rs b/src/commands/update.rs
index eeb5a4d..498b6a9 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};
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, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version}};
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
@@ -69,18 +69,21 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error:
69 } 69 }
70 } 70 }
71 71
72 if input.direct_download { download_versions(current_list.clone(), updatestack).await?; }; 72 if input.direct_download {
73 download_versions(current_list.clone(), updatestack).await?;
74
75 //Disable old versions
76 for ver in current_versions {
77 if input.delete_old {
78 println!("Deleting version {} for mod {}", ver.0, ver.1);
79 delete_version(current_list.clone(), ver.0)?;
80 } else if ver.0 != "NONE" {
81 println!("Disabling version {} for mod {}", ver.0, ver.1);
82 disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?;
83 };
84 }
85 };
73 86
74 //Disable old versions
75 for ver in current_versions {
76 if input.delete_old {
77 println!("Deleting version {} for mod {}", ver.0, ver.1);
78 delete_version(current_list.clone(), ver.0)?;
79 } else if ver.0 != "NONE" {
80 println!("Disabling version {} for mod {}", ver.0, ver.1);
81 disable_old(config.clone(), current_list.clone(), ver.0, ver.1)?;
82 };
83 }
84 } 87 }
85 88
86 Ok(()) 89 Ok(())
@@ -120,18 +123,6 @@ async fn specific_update(config: Cfg, input: Input, list: List, project: Project
120 Ok(current[0].clone()) 123 Ok(current[0].clone())
121} 124}
122 125
123fn disable_old(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> {
124 println!("Disabling version {} for mod {}", versionid, mod_id);
125 let file = get_file_path(current_list.clone(), String::from(&versionid))?;
126 let disabled = format!("{}.disabled", file);
127
128 rename(file, disabled)?;
129
130 userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?;
131
132 Ok(())
133}
134
135#[tokio::test] 126#[tokio::test]
136async fn download_updates_test() { 127async fn download_updates_test() {
137 128
diff --git a/src/files.rs b/src/files.rs
index 959da75..b1e537c 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -1,8 +1,8 @@
1use std::{fs::{File, read_dir, remove_file}, io::Write, collections::HashMap}; 1use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::HashMap};
2use futures_util::StreamExt; 2use futures_util::StreamExt;
3use reqwest::Client; 3use reqwest::Client;
4 4
5use crate::{List, modrinth::Version}; 5use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg};
6 6
7pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { 7pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> {
8 8
@@ -12,7 +12,7 @@ pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Re
12 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); 12 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap();
13 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); 13 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect();
14 let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; 14 let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?;
15 let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); 15 let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension);
16 download_file(primary_file.url, current_list.clone().download_folder, filename).await?; 16 download_file(primary_file.url, current_list.clone().download_folder, filename).await?;
17 } 17 }
18 18
@@ -39,6 +39,18 @@ async fn download_file(url: String, path: String, name: String) -> Result<(), Bo
39 Ok(()) 39 Ok(())
40} 40}
41 41
42pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> {
43 println!("Disabling version {} for mod {}", versionid, mod_id);
44 let file = get_file_path(current_list.clone(), String::from(&versionid))?;
45 let disabled = format!("{}.disabled", file);
46
47 rename(file, disabled)?;
48
49 userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?;
50
51 Ok(())
52}
53
42pub fn delete_version(list: List, version: String) -> Result<(), Box<dyn std::error::Error>> { 54pub fn delete_version(list: List, version: String) -> Result<(), Box<dyn std::error::Error>> {
43 let file = get_file_path(list, version)?; 55 let file = get_file_path(list, version)?;
44 56
@@ -58,24 +70,21 @@ pub fn get_file_path(list: List, versionid: String) -> Result<String, Box<dyn st
58 names.insert(String::from(ver_id), String::from(pathstr)); 70 names.insert(String::from(ver_id), String::from(pathstr));
59 } 71 }
60 }; 72 };
61 73
62 let api_versionid = format!("mr{}", versionid); 74 let filename = names.get(&versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?;
63
64 let filename = names.get(&api_versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?;
65 75
66 Ok(filename.to_owned()) 76 Ok(filename.to_owned())
67} 77}
68 78
69pub fn get_downloaded_versions(list: List) -> Result<Vec<String>, Box<dyn std::error::Error>> { 79pub fn get_downloaded_versions(list: List) -> Result<HashMap<String, String>, Box<dyn std::error::Error>> {
70 let mut versions: Vec<String> = vec![]; 80 let mut versions: HashMap<String, String> = HashMap::new();
71 for file in read_dir(list.download_folder)? { 81 for file in read_dir(list.download_folder)? {
72 let path = file?.path(); 82 let path = file?.path();
73 if path.is_file() && path.extension().ok_or("BAH")? == "jar" { 83 if path.is_file() && path.extension().ok_or("BAH")? == "jar" {
74 let pathstr = path.to_str().ok_or("BAH")?; 84 let pathstr = path.to_str().ok_or("BAH")?;
75 let namesplit: Vec<&str> = pathstr.split('.').collect(); 85 let namesplit: Vec<&str> = pathstr.split('.').collect();
76 versions.push(String::from(namesplit[namesplit.len() - 2].split_at(2).1)); 86 versions.insert(String::from(namesplit[namesplit.len() - 3]), String::from(namesplit[namesplit.len() - 2]));
77 } 87 }
78 } 88 }
79
80 Ok(versions) 89 Ok(versions)
81} 90}