diff options
author | fxqnlr <[email protected]> | 2023-04-20 15:13:58 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-04-20 15:13:58 +0200 |
commit | 8050cfcd70a16273cc2814fe29c8ee08320d85d3 (patch) | |
tree | 13924a7f1a9a0c7515beb47a14fe62d5c387d09a /src/files.rs | |
parent | fde646a876cc43857a278ef09250263a21d547ee (diff) | |
download | modlist-8050cfcd70a16273cc2814fe29c8ee08320d85d3.tar modlist-8050cfcd70a16273cc2814fe29c8ee08320d85d3.tar.gz modlist-8050cfcd70a16273cc2814fe29c8ee08320d85d3.zip |
cargo fmt
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 59 |
1 files changed, 42 insertions, 17 deletions
diff --git a/src/files.rs b/src/files.rs index 6519c6a..6160cb4 100644 --- a/src/files.rs +++ b/src/files.rs | |||
@@ -1,11 +1,20 @@ | |||
1 | use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::HashMap}; | ||
2 | use futures_util::StreamExt; | 1 | use futures_util::StreamExt; |
3 | use reqwest::Client; | 2 | use reqwest::Client; |
4 | 3 | use std::{ | |
5 | use crate::{List, modrinth::Version, db::{userlist_add_disabled_versions, mods_get_info}, config::Cfg, error::{MLE, MLError, ErrorType}}; | 4 | collections::HashMap, |
5 | fs::{read_dir, remove_file, rename, File}, | ||
6 | io::Write, | ||
7 | }; | ||
8 | |||
9 | use crate::{ | ||
10 | config::Cfg, | ||
11 | db::{mods_get_info, userlist_add_disabled_versions}, | ||
12 | error::{ErrorType, MLError, MLE}, | ||
13 | modrinth::Version, | ||
14 | List, | ||
15 | }; | ||
6 | 16 | ||
7 | pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> { | 17 | pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> { |
8 | |||
9 | let dl_path = String::from(&list.download_folder); | 18 | let dl_path = String::from(&list.download_folder); |
10 | 19 | ||
11 | println!(" └Download mods to {}", dl_path); | 20 | println!(" └Download mods to {}", dl_path); |
@@ -21,7 +30,13 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) | |||
21 | Ok(e) => e, | 30 | Ok(e) => e, |
22 | Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")), | 31 | Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")), |
23 | }; | 32 | }; |
24 | let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension); | 33 | let filename = format!( |
34 | "{}.mr.{}.{}.{}", | ||
35 | splitname.join("."), | ||
36 | ver.project_id, | ||
37 | ver.id, | ||
38 | extension | ||
39 | ); | ||
25 | download_file(primary_file.url, list.clone().download_folder, filename).await?; | 40 | download_file(primary_file.url, list.clone().download_folder, filename).await?; |
26 | //tokio::time::sleep(std::time::Duration::new(3, 0)).await; | 41 | //tokio::time::sleep(std::time::Duration::new(3, 0)).await; |
27 | println!(" ✓"); | 42 | println!(" ✓"); |
@@ -32,10 +47,7 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) | |||
32 | 47 | ||
33 | async fn download_file(url: String, path: String, name: String) -> MLE<()> { | 48 | async fn download_file(url: String, path: String, name: String) -> MLE<()> { |
34 | let dl_path_file = format!("{}/{}", path, name); | 49 | let dl_path_file = format!("{}/{}", path, name); |
35 | let res = Client::new() | 50 | let res = Client::new().get(String::from(&url)).send().await?; |
36 | .get(String::from(&url)) | ||
37 | .send() | ||
38 | .await?; | ||
39 | 51 | ||
40 | // download chunks | 52 | // download chunks |
41 | let mut file = File::create(&dl_path_file)?; | 53 | let mut file = File::create(&dl_path_file)?; |
@@ -49,7 +61,12 @@ async fn download_file(url: String, path: String, name: String) -> MLE<()> { | |||
49 | Ok(()) | 61 | Ok(()) |
50 | } | 62 | } |
51 | 63 | ||
52 | pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> MLE<()> { | 64 | pub fn disable_version( |
65 | config: Cfg, | ||
66 | current_list: List, | ||
67 | versionid: String, | ||
68 | mod_id: String, | ||
69 | ) -> MLE<()> { | ||
53 | //println!("Disabling version {} for mod {}", versionid, mod_id); | 70 | //println!("Disabling version {} for mod {}", versionid, mod_id); |
54 | let file = get_file_path(current_list.clone(), String::from(&versionid))?; | 71 | let file = get_file_path(current_list.clone(), String::from(&versionid))?; |
55 | let disabled = format!("{}.disabled", file); | 72 | let disabled = format!("{}.disabled", file); |
@@ -63,7 +80,7 @@ pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_i | |||
63 | 80 | ||
64 | pub fn delete_version(list: List, version: String) -> MLE<()> { | 81 | pub fn delete_version(list: List, version: String) -> MLE<()> { |
65 | let file = get_file_path(list, version)?; | 82 | let file = get_file_path(list, version)?; |
66 | 83 | ||
67 | remove_file(file)?; | 84 | remove_file(file)?; |
68 | 85 | ||
69 | Ok(()) | 86 | Ok(()) |
@@ -76,19 +93,24 @@ pub fn get_file_path(list: List, versionid: String) -> MLE<String> { | |||
76 | if path.is_file() { | 93 | if path.is_file() { |
77 | let pathstr = match path.to_str().ok_or("") { | 94 | let pathstr = match path.to_str().ok_or("") { |
78 | Ok(s) => s, | 95 | Ok(s) => s, |
79 | Err(..) => return Err(MLError::new(ErrorType::Other, "INVALID_PATH")) | 96 | Err(..) => return Err(MLError::new(ErrorType::Other, "INVALID_PATH")), |
80 | }; | 97 | }; |
81 | let namesplit: Vec<&str> = pathstr.split('.').collect(); | 98 | let namesplit: Vec<&str> = pathstr.split('.').collect(); |
82 | let ver_id = namesplit[namesplit.len() - 2]; | 99 | let ver_id = namesplit[namesplit.len() - 2]; |
83 | names.insert(String::from(ver_id), String::from(pathstr)); | 100 | names.insert(String::from(ver_id), String::from(pathstr)); |
84 | } | 101 | } |
85 | }; | 102 | } |
86 | 103 | ||
87 | let filename = match names.get(&versionid).ok_or("") { | 104 | let filename = match names.get(&versionid).ok_or("") { |
88 | Ok(n) => n, | 105 | Ok(n) => n, |
89 | Err(..) => return Err(MLError::new(ErrorType::ArgumentError, "VERSION_NOT_FOUND_IN_FILES")) | 106 | Err(..) => { |
107 | return Err(MLError::new( | ||
108 | ErrorType::ArgumentError, | ||
109 | "VERSION_NOT_FOUND_IN_FILES", | ||
110 | )) | ||
111 | } | ||
90 | }; | 112 | }; |
91 | 113 | ||
92 | Ok(filename.to_owned()) | 114 | Ok(filename.to_owned()) |
93 | } | 115 | } |
94 | 116 | ||
@@ -99,7 +121,10 @@ pub fn get_downloaded_versions(list: List) -> MLE<HashMap<String, String>> { | |||
99 | if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" { | 121 | if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" { |
100 | let pathstr = path.to_str().ok_or("BAH").unwrap(); | 122 | let pathstr = path.to_str().ok_or("BAH").unwrap(); |
101 | let namesplit: Vec<&str> = pathstr.split('.').collect(); | 123 | let namesplit: Vec<&str> = pathstr.split('.').collect(); |
102 | versions.insert(String::from(namesplit[namesplit.len() - 3]), String::from(namesplit[namesplit.len() - 2])); | 124 | versions.insert( |
125 | String::from(namesplit[namesplit.len() - 3]), | ||
126 | String::from(namesplit[namesplit.len() - 2]), | ||
127 | ); | ||
103 | } | 128 | } |
104 | } | 129 | } |
105 | Ok(versions) | 130 | Ok(versions) |
@@ -111,6 +136,6 @@ pub fn clean_list_dir(list: &List) -> MLE<()> { | |||
111 | for entry in std::fs::read_dir(dl_path)? { | 136 | for entry in std::fs::read_dir(dl_path)? { |
112 | let entry = entry?; | 137 | let entry = entry?; |
113 | std::fs::remove_file(entry.path())?; | 138 | std::fs::remove_file(entry.path())?; |
114 | }; | 139 | } |
115 | Ok(()) | 140 | Ok(()) |
116 | } | 141 | } |