diff options
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 77 |
1 files changed, 51 insertions, 26 deletions
diff --git a/src/files.rs b/src/files.rs index 814f06d..e874d9d 100644 --- a/src/files.rs +++ b/src/files.rs | |||
@@ -1,12 +1,13 @@ | |||
1 | use futures_util::StreamExt; | 1 | use futures_util::StreamExt; |
2 | use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; | 2 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
3 | use reqwest::Client; | 3 | use reqwest::Client; |
4 | use tokio::task::JoinSet; | ||
5 | use std::{ | 4 | use std::{ |
5 | cmp::min, | ||
6 | collections::HashMap, | 6 | collections::HashMap, |
7 | fs::{copy, read_dir, remove_file, rename, File}, | 7 | fs::{copy, read_dir, remove_file, rename, File}, |
8 | io::Write, cmp::min, | 8 | io::Write, |
9 | }; | 9 | }; |
10 | use tokio::task::JoinSet; | ||
10 | 11 | ||
11 | use crate::{ | 12 | use crate::{ |
12 | cache::{copy_cached_version, get_cached_versions}, | 13 | cache::{copy_cached_version, get_cached_versions}, |
@@ -14,34 +15,61 @@ use crate::{ | |||
14 | db::{mods_get_info, userlist_add_disabled_versions}, | 15 | db::{mods_get_info, userlist_add_disabled_versions}, |
15 | error::{ErrorType, MLError, MLE}, | 16 | error::{ErrorType, MLError, MLE}, |
16 | modrinth::Version, | 17 | modrinth::Version, |
17 | List, PROGRESS_CHARS, STYLE_SPINNER, STYLE_BAR_BYTE, STYLE_BAR_POS, | 18 | List, PROGRESS_CHARS, STYLE_BAR_BYTE, STYLE_BAR_POS, STYLE_SPINNER, |
18 | }; | 19 | }; |
19 | 20 | ||
20 | pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>, progress: &MultiProgress, progress_before: &ProgressBar) -> MLE<()> { | 21 | pub async fn download_versions( |
22 | list: List, | ||
23 | config: Cfg, | ||
24 | versions: Vec<Version>, | ||
25 | progress: &MultiProgress, | ||
26 | progress_before: &ProgressBar, | ||
27 | ) -> MLE<()> { | ||
21 | let cached = get_cached_versions(&config.cache); | 28 | let cached = get_cached_versions(&config.cache); |
22 | 29 | ||
23 | let mut js = JoinSet::new(); | 30 | let mut js = JoinSet::new(); |
24 | 31 | ||
25 | let style_spinner = ProgressStyle::with_template(STYLE_SPINNER).unwrap(); | 32 | let style_spinner = ProgressStyle::with_template(STYLE_SPINNER).unwrap(); |
26 | 33 | ||
27 | let all = progress.insert_before(progress_before, ProgressBar::new(versions.len().try_into().unwrap())); | 34 | let all = progress.insert_before( |
28 | all.set_style(ProgressStyle::with_template(STYLE_BAR_POS).unwrap().progress_chars(PROGRESS_CHARS)); | 35 | progress_before, |
36 | ProgressBar::new(versions.len().try_into().unwrap()), | ||
37 | ); | ||
38 | all.set_style( | ||
39 | ProgressStyle::with_template(STYLE_BAR_POS) | ||
40 | .unwrap() | ||
41 | .progress_chars(PROGRESS_CHARS), | ||
42 | ); | ||
29 | all.set_message(format!("✓Downloading {}", list.id)); | 43 | all.set_message(format!("✓Downloading {}", list.id)); |
30 | 44 | ||
31 | for ver in versions { | 45 | for ver in versions { |
32 | let p = progress.insert_before(&all, ProgressBar::new(1)); | 46 | let p = progress.insert_before(&all, ProgressBar::new(1)); |
33 | p.set_style(style_spinner.clone()); | 47 | p.set_style(style_spinner.clone()); |
34 | js.spawn(download_version(config.clone(), list.clone(), ver, cached.clone(), p)); | 48 | js.spawn(download_version( |
49 | config.clone(), | ||
50 | list.clone(), | ||
51 | ver, | ||
52 | cached.clone(), | ||
53 | p, | ||
54 | )); | ||
55 | } | ||
56 | |||
57 | while js.join_next().await.is_some() { | ||
58 | all.inc(1) | ||
35 | } | 59 | } |
36 | 60 | ||
37 | while js.join_next().await.is_some() { all.inc(1) } | ||
38 | |||
39 | all.finish_with_message(format!("✓Downloading {}", list.id)); | 61 | all.finish_with_message(format!("✓Downloading {}", list.id)); |
40 | 62 | ||
41 | Ok(()) | 63 | Ok(()) |
42 | } | 64 | } |
43 | 65 | ||
44 | async fn download_version(config: Cfg, list: List, version: Version, mut cached: HashMap<String, String>, progress: ProgressBar) -> MLE<()> { | 66 | async fn download_version( |
67 | config: Cfg, | ||
68 | list: List, | ||
69 | version: Version, | ||
70 | mut cached: HashMap<String, String>, | ||
71 | progress: ProgressBar, | ||
72 | ) -> MLE<()> { | ||
45 | let project_info = mods_get_info(&config, &version.project_id)?; | 73 | let project_info = mods_get_info(&config, &version.project_id)?; |
46 | 74 | ||
47 | let dl_path = String::from(&list.download_folder); | 75 | let dl_path = String::from(&list.download_folder); |
@@ -59,7 +87,7 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached: | |||
59 | let files = version.files; | 87 | let files = version.files; |
60 | let file = match files.clone().into_iter().find(|f| f.primary) { | 88 | let file = match files.clone().into_iter().find(|f| f.primary) { |
61 | Some(f) => f, | 89 | Some(f) => f, |
62 | None => files[0].clone() | 90 | None => files[0].clone(), |
63 | }; | 91 | }; |
64 | let mut splitname: Vec<&str> = file.filename.split('.').collect(); | 92 | let mut splitname: Vec<&str> = file.filename.split('.').collect(); |
65 | let extension = match splitname.pop().ok_or("") { | 93 | let extension = match splitname.pop().ok_or("") { |
@@ -74,13 +102,7 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached: | |||
74 | extension | 102 | extension |
75 | ); | 103 | ); |
76 | 104 | ||
77 | download_file( | 105 | download_file(&file.url, &list.download_folder, &filename, &progress).await?; |
78 | &file.url, | ||
79 | &list.download_folder, | ||
80 | &filename, | ||
81 | &progress | ||
82 | ) | ||
83 | .await?; | ||
84 | 106 | ||
85 | progress.set_message(format!("Copy {} to cache", version.id)); | 107 | progress.set_message(format!("Copy {} to cache", version.id)); |
86 | let dl_path_file = format!("{}/{}", list.download_folder, filename); | 108 | let dl_path_file = format!("{}/{}", list.download_folder, filename); |
@@ -89,7 +111,10 @@ async fn download_version(config: Cfg, list: List, version: Version, mut cached: | |||
89 | copy(dl_path_file, cache_path)?; | 111 | copy(dl_path_file, cache_path)?; |
90 | } | 112 | } |
91 | 113 | ||
92 | progress.finish_with_message(format!("✓{} - {}{}", project_info.title, version.id, cache_msg)); | 114 | progress.finish_with_message(format!( |
115 | "✓{} - {}{}", | ||
116 | project_info.title, version.id, cache_msg | ||
117 | )); | ||
93 | 118 | ||
94 | Ok(()) | 119 | Ok(()) |
95 | } | 120 | } |
@@ -117,7 +142,7 @@ async fn download_file(url: &str, path: &str, name: &str, progress: &ProgressBar | |||
117 | // progress.inc(1); | 142 | // progress.inc(1); |
118 | let chunk = item?; | 143 | let chunk = item?; |
119 | file.write_all(&chunk)?; | 144 | file.write_all(&chunk)?; |
120 | 145 | ||
121 | // Progress bar | 146 | // Progress bar |
122 | let new = min(downloaded + (chunk.len() as u64), size); | 147 | let new = min(downloaded + (chunk.len() as u64), size); |
123 | downloaded = new; | 148 | downloaded = new; |
@@ -136,7 +161,7 @@ pub fn disable_version( | |||
136 | mod_id: String, | 161 | mod_id: String, |
137 | ) -> MLE<()> { | 162 | ) -> MLE<()> { |
138 | //println!("Disabling version {} for mod {}", versionid, mod_id); | 163 | //println!("Disabling version {} for mod {}", versionid, mod_id); |
139 | let file = get_file_path(current_list.clone(), String::from(&versionid))?; | 164 | let file = get_file_path(¤t_list, String::from(&versionid))?; |
140 | let disabled = format!("{}.disabled", file); | 165 | let disabled = format!("{}.disabled", file); |
141 | 166 | ||
142 | rename(file, disabled)?; | 167 | rename(file, disabled)?; |
@@ -146,7 +171,7 @@ pub fn disable_version( | |||
146 | Ok(()) | 171 | Ok(()) |
147 | } | 172 | } |
148 | 173 | ||
149 | pub fn delete_version(list: List, version: String) -> MLE<()> { | 174 | pub fn delete_version(list: &List, version: String) -> MLE<()> { |
150 | let file = get_file_path(list, version)?; | 175 | let file = get_file_path(list, version)?; |
151 | 176 | ||
152 | remove_file(file)?; | 177 | remove_file(file)?; |
@@ -154,9 +179,9 @@ pub fn delete_version(list: List, version: String) -> MLE<()> { | |||
154 | Ok(()) | 179 | Ok(()) |
155 | } | 180 | } |
156 | 181 | ||
157 | pub fn get_file_path(list: List, versionid: String) -> MLE<String> { | 182 | pub fn get_file_path(list: &List, versionid: String) -> MLE<String> { |
158 | let mut names: HashMap<String, String> = HashMap::new(); | 183 | let mut names: HashMap<String, String> = HashMap::new(); |
159 | for file in read_dir(list.download_folder)? { | 184 | for file in read_dir(&list.download_folder)? { |
160 | let path = file?.path(); | 185 | let path = file?.path(); |
161 | if path.is_file() { | 186 | if path.is_file() { |
162 | let pathstr = match path.to_str().ok_or("") { | 187 | let pathstr = match path.to_str().ok_or("") { |