diff options
author | fxqnlr <[email protected]> | 2022-11-28 22:55:14 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2022-11-28 22:55:14 +0100 |
commit | ddde9204c72dd867f920f07f6483be03dda7cf68 (patch) | |
tree | 51ee1140311be4a82a7832bcef2772db7fd4e639 /src/files.rs | |
parent | d8cb7bc5f9c2e01c82f954427a60da6eaf0610ca (diff) | |
download | modlist-ddde9204c72dd867f920f07f6483be03dda7cf68.tar modlist-ddde9204c72dd867f920f07f6483be03dda7cf68.tar.gz modlist-ddde9204c72dd867f920f07f6483be03dda7cf68.zip |
basically update impl; added "good" download;
auto dl on new mod; db to 0.4; etc
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/files.rs b/src/files.rs new file mode 100644 index 0000000..1c0b13c --- /dev/null +++ b/src/files.rs | |||
@@ -0,0 +1,44 @@ | |||
1 | use std::{fs::{File, read_dir}, io::Write, collections::HashMap}; | ||
2 | use futures_util::StreamExt; | ||
3 | use reqwest::Client; | ||
4 | |||
5 | use crate::List; | ||
6 | |||
7 | pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> { | ||
8 | println!("Downloading {}", url); | ||
9 | let dl_path_file = format!("{}/{}", path, name); | ||
10 | let res = Client::new() | ||
11 | .get(String::from(&url)) | ||
12 | .send() | ||
13 | .await?; | ||
14 | |||
15 | // download chunks | ||
16 | let mut file = File::create(String::from(&dl_path_file))?; | ||
17 | let mut stream = res.bytes_stream(); | ||
18 | |||
19 | while let Some(item) = stream.next().await { | ||
20 | let chunk = item?; | ||
21 | file.write_all(&chunk)?; | ||
22 | } | ||
23 | |||
24 | Ok(()) | ||
25 | } | ||
26 | |||
27 | pub fn get_file_path(list: List, versionid: String) -> Result<String, Box<dyn std::error::Error>> { | ||
28 | let mut names: HashMap<String, String> = HashMap::new(); | ||
29 | for file in read_dir(list.download_folder)? { | ||
30 | let path = file?.path(); | ||
31 | if path.is_file() { | ||
32 | let pathstr = path.to_str().ok_or("BAH")?; | ||
33 | let namesplit: Vec<&str> = pathstr.split('.').collect(); | ||
34 | let ver_id = namesplit[namesplit.len() - 2]; | ||
35 | names.insert(String::from(ver_id), String::from(pathstr)); | ||
36 | } | ||
37 | }; | ||
38 | |||
39 | let api_versionid = format!("mr{}", versionid); | ||
40 | |||
41 | let filename = names.get(&api_versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?; | ||
42 | |||
43 | Ok(filename.to_owned()) | ||
44 | } | ||