diff options
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/src/files.rs b/src/files.rs index 14e5636..0d7dc0a 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 | |||
2 | use futures_util::StreamExt; | 2 | use futures_util::StreamExt; |
3 | use reqwest::Client; | 3 | use reqwest::Client; |
4 | 4 | ||
5 | use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg, error::{MLE, MLError, ErrorType}}; | 5 | use crate::{List, modrinth::Version, db::{userlist_add_disabled_versions, mods_get_name}, config::Cfg, error::{MLE, MLError, ErrorType}}; |
6 | 6 | ||
7 | pub async fn download_versions(current_list: List, versions: Vec<Version>) -> MLE<String> { | 7 | pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> { |
8 | 8 | ||
9 | let dl_path = String::from(¤t_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 | //TODO get project name instead of projectid from db | ||
15 | let project_name = mods_get_name(config.clone(), &ver.project_id)?; | ||
16 | print!("\t({})Download version {}", project_name, ver.id); | ||
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 | ||
25 | async fn download_file(url: String, path: String, name: String) -> MLE<()> { | 33 | async 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 | ||
45 | pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> MLE<()> { | 52 | pub 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 | ||
@@ -97,3 +104,13 @@ pub fn get_downloaded_versions(list: List) -> MLE<HashMap<String, String>> { | |||
97 | } | 104 | } |
98 | Ok(versions) | 105 | Ok(versions) |
99 | } | 106 | } |
107 | |||
108 | pub 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 | } | ||