summaryrefslogtreecommitdiff
path: root/src/files.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs35
1 files changed, 26 insertions, 9 deletions
diff --git a/src/files.rs b/src/files.rs
index 998ed32..8c822b2 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -2,13 +2,19 @@ use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::Has
2use futures_util::StreamExt; 2use futures_util::StreamExt;
3use reqwest::Client; 3use reqwest::Client;
4 4
5use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg, error::{MLE, MLError, ErrorType}}; 5use crate::{List, modrinth::Version, db::{userlist_add_disabled_versions, mods_get_name}, config::Cfg, error::{MLE, MLError, ErrorType}};
6 6
7pub async fn download_versions(current_list: List, versions: Vec<Version>) -> MLE<String> { 7pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> {
8 8
9 let dl_path = String::from(&current_list.download_folder); 9 let dl_path = String::from(&list.download_folder);
10
11 println!("Download to directory from: {} ({})", list.id, dl_path);
10 12
11 for ver in versions { 13 for ver in versions {
14 let project_name = mods_get_name(config.clone(), &ver.project_id)?;
15 print!("\t({})Download version {}", project_name, ver.id);
16 //Force flush of stdout, else print! doesn't print instantly
17 std::io::stdout().flush().unwrap();
12 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); 18 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap();
13 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); 19 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect();
14 let extension = match splitname.pop().ok_or("") { 20 let extension = match splitname.pop().ok_or("") {
@@ -16,14 +22,15 @@ pub async fn download_versions(current_list: List, versions: Vec<Version>) -> ML
16 Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")), 22 Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")),
17 }; 23 };
18 let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension); 24 let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension);
19 download_file(primary_file.url, current_list.clone().download_folder, filename).await?; 25 download_file(primary_file.url, list.clone().download_folder, filename).await?;
26 //tokio::time::sleep(std::time::Duration::new(3, 0)).await;
27 println!(" ✓");
20 } 28 }
21 29
22 Ok(dl_path) 30 Ok(dl_path)
23} 31}
24 32
25async fn download_file(url: String, path: String, name: String) -> MLE<()> { 33async fn download_file(url: String, path: String, name: String) -> MLE<()> {
26 println!("Downloading {}", url);
27 let dl_path_file = format!("{}/{}", path, name); 34 let dl_path_file = format!("{}/{}", path, name);
28 let res = Client::new() 35 let res = Client::new()
29 .get(String::from(&url)) 36 .get(String::from(&url))
@@ -43,7 +50,7 @@ async fn download_file(url: String, path: String, name: String) -> MLE<()> {
43} 50}
44 51
45pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> MLE<()> { 52pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> MLE<()> {
46 println!("Disabling version {} for mod {}", versionid, mod_id); 53 //println!("Disabling version {} for mod {}", versionid, mod_id);
47 let file = get_file_path(current_list.clone(), String::from(&versionid))?; 54 let file = get_file_path(current_list.clone(), String::from(&versionid))?;
48 let disabled = format!("{}.disabled", file); 55 let disabled = format!("{}.disabled", file);
49 56
@@ -85,15 +92,25 @@ pub fn get_file_path(list: List, versionid: String) -> MLE<String> {
85 Ok(filename.to_owned()) 92 Ok(filename.to_owned())
86} 93}
87 94
88pub fn get_downloaded_versions(list: List) -> Result<HashMap<String, String>, Box<dyn std::error::Error>> { 95pub fn get_downloaded_versions(list: List) -> MLE<HashMap<String, String>> {
89 let mut versions: HashMap<String, String> = HashMap::new(); 96 let mut versions: HashMap<String, String> = HashMap::new();
90 for file in read_dir(&list.download_folder)? { 97 for file in read_dir(&list.download_folder)? {
91 let path = file?.path(); 98 let path = file?.path();
92 if path.is_file() && path.extension().ok_or("BAH")? == "jar" { 99 if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" {
93 let pathstr = path.to_str().ok_or("BAH")?; 100 let pathstr = path.to_str().ok_or("BAH").unwrap();
94 let namesplit: Vec<&str> = pathstr.split('.').collect(); 101 let namesplit: Vec<&str> = pathstr.split('.').collect();
95 versions.insert(String::from(namesplit[namesplit.len() - 3]), String::from(namesplit[namesplit.len() - 2])); 102 versions.insert(String::from(namesplit[namesplit.len() - 3]), String::from(namesplit[namesplit.len() - 2]));
96 } 103 }
97 } 104 }
98 Ok(versions) 105 Ok(versions)
99} 106}
107
108pub fn clean_list_dir(list: &List) -> MLE<()> {
109 let dl_path = &list.download_folder;
110 println!("Clean directory for: {}", list.id);
111 for entry in std::fs::read_dir(dl_path)? {
112 let entry = entry?;
113 std::fs::remove_file(entry.path())?;
114 };
115 Ok(())
116}