summaryrefslogtreecommitdiff
path: root/src/commands/download.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2022-11-20 23:54:20 +0100
committerfxqnlr <[email protected]>2022-11-20 23:54:20 +0100
commitc00673fd0e01d1438798dbb1635a761a76a2b559 (patch)
tree90c1f564fd7225e7312003c82ca269677cc5778d /src/commands/download.rs
parent477e0ecbb7bb34b581c518bfc2bc7ebc210b4673 (diff)
downloadmodlist-c00673fd0e01d1438798dbb1635a761a76a2b559.tar
modlist-c00673fd0e01d1438798dbb1635a761a76a2b559.tar.gz
modlist-c00673fd0e01d1438798dbb1635a761a76a2b559.zip
extracted filedownload to fn;
fixed some tests; added direct-dl to update
Diffstat (limited to 'src/commands/download.rs')
-rw-r--r--src/commands/download.rs39
1 files changed, 7 insertions, 32 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs
index db0fc93..82d6b02 100644
--- a/src/commands/download.rs
+++ b/src/commands/download.rs
@@ -1,9 +1,3 @@
1use std::{io::Write, fs::File};
2
3use reqwest::Client;
4
5use futures_util::StreamExt;
6
7use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; 1use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input};
8 2
9pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 3pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
@@ -16,9 +10,11 @@ pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::erro
16 Ok(()) 10 Ok(())
17} 11}
18 12
19async fn download_links(config: Cfg, input: Input, current_list: List, links: Vec<String>) -> Result<String, Box<dyn std::error::Error>> { 13async fn download_links(_config: Cfg, _input: Input, _current_list: List, _links: Vec<String>) -> Result<String, Box<dyn std::error::Error>> {
20 14 println!("NO DL IMPLEMENTATION FOR DOWNLOAD YET");
21 let dl_path = String::from(&config.downloads); 15 //TODO copy dl from update if possible
16 /*
17 let dl_path = String::from(&current_list.download_folder);
22 18
23 if input.clean { 19 if input.clean {
24 let dl_path = &current_list.download_folder; 20 let dl_path = &current_list.download_folder;
@@ -28,28 +24,7 @@ async fn download_links(config: Cfg, input: Input, current_list: List, links: Ve
28 std::fs::remove_file(entry.path())?; 24 std::fs::remove_file(entry.path())?;
29 } 25 }
30 } 26 }
27 */
31 28
32 for link in links { 29 Ok(String::new())
33 let filename = link.split('/').last().unwrap();
34 let dl_path_file = format!("{}/{}", config.downloads, filename);
35 println!("Downloading {}", link);
36
37 let res = Client::new()
38 .get(String::from(&link))
39 .send()
40 .await
41 .or(Err(format!("Failed to GET from '{}'", &link)))?;
42
43 // download chunks
44 let mut file = File::create(String::from(&dl_path_file)).or(Err(format!("Failed to create file '{}'", dl_path_file)))?;
45 let mut stream = res.bytes_stream();
46
47 while let Some(item) = stream.next().await {
48 let chunk = item.or(Err("Error while downloading file"))?;
49 file.write_all(&chunk)
50 .or(Err("Error while writing to file"))?;
51 }
52 }
53
54 Ok(dl_path)
55} 30}