diff options
author | FxQnLr <[email protected]> | 2022-12-07 17:45:16 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2022-12-07 17:45:16 +0100 |
commit | 758e608e6c331df2a5900de7f932f9b498573ed1 (patch) | |
tree | 4ec306b7b69613286f1d070a52c7b26d6280b111 /src/files.rs | |
parent | 2ec20c50e7c02d82b248835988df040bd266b659 (diff) | |
download | modlist-758e608e6c331df2a5900de7f932f9b498573ed1.tar modlist-758e608e6c331df2a5900de7f932f9b498573ed1.tar.gz modlist-758e608e6c331df2a5900de7f932f9b498573ed1.zip |
groundwork for downloads; cleanup
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/files.rs b/src/files.rs index 2c5994d..959da75 100644 --- a/src/files.rs +++ b/src/files.rs | |||
@@ -2,9 +2,24 @@ use std::{fs::{File, read_dir, remove_file}, io::Write, collections::HashMap}; | |||
2 | use futures_util::StreamExt; | 2 | use futures_util::StreamExt; |
3 | use reqwest::Client; | 3 | use reqwest::Client; |
4 | 4 | ||
5 | use crate::List; | 5 | use crate::{List, modrinth::Version}; |
6 | 6 | ||
7 | pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> { | 7 | pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { |
8 | |||
9 | let dl_path = String::from(¤t_list.download_folder); | ||
10 | |||
11 | for ver in versions { | ||
12 | let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); | ||
13 | let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); | ||
14 | let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; | ||
15 | let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); | ||
16 | download_file(primary_file.url, current_list.clone().download_folder, filename).await?; | ||
17 | } | ||
18 | |||
19 | Ok(dl_path) | ||
20 | } | ||
21 | |||
22 | async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> { | ||
8 | println!("Downloading {}", url); | 23 | println!("Downloading {}", url); |
9 | let dl_path_file = format!("{}/{}", path, name); | 24 | let dl_path_file = format!("{}/{}", path, name); |
10 | let res = Client::new() | 25 | let res = Client::new() |
@@ -50,3 +65,17 @@ pub fn get_file_path(list: List, versionid: String) -> Result<String, Box<dyn st | |||
50 | 65 | ||
51 | Ok(filename.to_owned()) | 66 | Ok(filename.to_owned()) |
52 | } | 67 | } |
68 | |||
69 | pub fn get_downloaded_versions(list: List) -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
70 | let mut versions: Vec<String> = vec![]; | ||
71 | for file in read_dir(list.download_folder)? { | ||
72 | let path = file?.path(); | ||
73 | if path.is_file() && path.extension().ok_or("BAH")? == "jar" { | ||
74 | let pathstr = path.to_str().ok_or("BAH")?; | ||
75 | let namesplit: Vec<&str> = pathstr.split('.').collect(); | ||
76 | versions.push(String::from(namesplit[namesplit.len() - 2].split_at(2).1)); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | Ok(versions) | ||
81 | } | ||