diff options
author | fxqnlr <[email protected]> | 2022-11-20 23:54:20 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2022-11-20 23:54:20 +0100 |
commit | c00673fd0e01d1438798dbb1635a761a76a2b559 (patch) | |
tree | 90c1f564fd7225e7312003c82ca269677cc5778d /src/commands | |
parent | 477e0ecbb7bb34b581c518bfc2bc7ebc210b4673 (diff) | |
download | modlist-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')
-rw-r--r-- | src/commands/download.rs | 39 | ||||
-rw-r--r-- | src/commands/update.rs | 50 |
2 files changed, 22 insertions, 67 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 @@ | |||
1 | use std::{io::Write, fs::File}; | ||
2 | |||
3 | use reqwest::Client; | ||
4 | |||
5 | use futures_util::StreamExt; | ||
6 | |||
7 | use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; | 1 | use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; |
8 | 2 | ||
9 | pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 3 | pub 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 | ||
19 | async fn download_links(config: Cfg, input: Input, current_list: List, links: Vec<String>) -> Result<String, Box<dyn std::error::Error>> { | 13 | async 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(¤t_list.download_folder); | ||
22 | 18 | ||
23 | if input.clean { | 19 | if input.clean { |
24 | let dl_path = ¤t_list.download_folder; | 20 | let dl_path = ¤t_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 | } |
diff --git a/src/commands/update.rs b/src/commands/update.rs index 85630f5..eba5e91 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs | |||
@@ -1,10 +1,6 @@ | |||
1 | use std::{io::{Error, ErrorKind, Write}, fs::File}; | 1 | use std::io::{Error, ErrorKind}; |
2 | 2 | ||
3 | use reqwest::Client; | 3 | use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List, input::Input, download_file}; |
4 | |||
5 | use futures_util::StreamExt; | ||
6 | |||
7 | use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List, input::Input}; | ||
8 | 4 | ||
9 | pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 5 | pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
10 | 6 | ||
@@ -29,16 +25,16 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error: | |||
29 | 25 | ||
30 | //Adding to stack if not the same versions in the list OR if clean == true | 26 | //Adding to stack if not the same versions in the list OR if clean == true |
31 | if input.clone().clean || (project.versions.join("|") != current_version.versions) { | 27 | if input.clone().clean || (project.versions.join("|") != current_version.versions) { |
32 | updatestack.push(match specific_update(config.clone(), input.clone(), current_list.clone(), project).await { | 28 | updatestack.push(match specific_update(config.clone(), input.clone(), current_list.clone(), project.clone()).await { |
33 | Ok(ver) => ver, | 29 | Ok(ver) => ver, |
34 | //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") | 30 | //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") |
35 | Err(_) => { continue; }, | 31 | Err(_) => { println!("({}) No new version found for the specified minecraft version", project.title); continue; }, |
36 | }); | 32 | }); |
33 | } else { | ||
34 | println!("({}) No new version found", project.title); | ||
37 | }; | 35 | }; |
38 | }; | 36 | }; |
39 | //println!("{:?}", updatestack); | 37 | |
40 | |||
41 | |||
42 | if input.clean { | 38 | if input.clean { |
43 | let dl_path = ¤t_list.download_folder; | 39 | let dl_path = ¤t_list.download_folder; |
44 | println!("Cleaning {}", dl_path); | 40 | println!("Cleaning {}", dl_path); |
@@ -47,8 +43,8 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error: | |||
47 | std::fs::remove_file(entry.path())?; | 43 | std::fs::remove_file(entry.path())?; |
48 | } | 44 | } |
49 | } | 45 | } |
50 | 46 | ||
51 | download_updates(config, current_list, updatestack).await?; | 47 | if input.direct_download { download_updates(current_list, updatestack).await?; }; |
52 | 48 | ||
53 | Ok(()) | 49 | Ok(()) |
54 | } | 50 | } |
@@ -82,41 +78,25 @@ async fn specific_update(config: Cfg, input: Input, list: List, project: Project | |||
82 | Ok(current[0].clone()) | 78 | Ok(current[0].clone()) |
83 | } | 79 | } |
84 | 80 | ||
85 | async fn download_updates(config: Cfg, current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { | 81 | async fn download_updates(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { |
86 | 82 | ||
87 | let dl_path = String::from(¤t_list.download_folder); | 83 | let dl_path = String::from(¤t_list.download_folder); |
88 | 84 | ||
89 | for ver in versions { | 85 | for ver in versions { |
90 | let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); | 86 | let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); |
91 | let dl_path_file = format!("{}/{}", config.downloads, primary_file.filename); | 87 | download_file(primary_file.url, current_list.clone().download_folder, primary_file.filename).await?; |
92 | println!("Downloading {}", primary_file.url); | ||
93 | |||
94 | let res = Client::new() | ||
95 | .get(String::from(&primary_file.url)) | ||
96 | .send() | ||
97 | .await | ||
98 | .or(Err(format!("Failed to GET from '{}'", &primary_file.url)))?; | ||
99 | |||
100 | // download chunks | ||
101 | let mut file = File::create(String::from(&dl_path_file)).or(Err(format!("Failed to create file '{}'", dl_path_file)))?; | ||
102 | let mut stream = res.bytes_stream(); | ||
103 | |||
104 | while let Some(item) = stream.next().await { | ||
105 | let chunk = item.or(Err("Error while downloading file"))?; | ||
106 | file.write_all(&chunk) | ||
107 | .or(Err("Error while writing to file"))?; | ||
108 | } | ||
109 | } | 88 | } |
110 | 89 | ||
90 | |||
111 | Ok(dl_path) | 91 | Ok(dl_path) |
112 | } | 92 | } |
113 | 93 | ||
114 | #[tokio::test] | 94 | #[tokio::test] |
115 | async fn download_updates_test() { | 95 | async fn download_updates_test() { |
116 | 96 | ||
117 | use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, config::{Cfg, Apis}}; | 97 | use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; |
118 | 98 | ||
119 | let config = Cfg { data: "...".to_string(), clean_remove: false, downloads: "./dl".to_string(), apis: Apis { modrinth: "...".to_string() } }; | 99 | let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") }; |
120 | 100 | ||
121 | let versions = vec![Version { | 101 | let versions = vec![Version { |
122 | id: "dEqtGnT9".to_string(), | 102 | id: "dEqtGnT9".to_string(), |
@@ -148,5 +128,5 @@ async fn download_updates_test() { | |||
148 | "fabric".to_string() | 128 | "fabric".to_string() |
149 | ] | 129 | ] |
150 | }]; | 130 | }]; |
151 | assert_eq!(download_updates(config, versions).await.unwrap(), "./dl") | 131 | assert!(download_updates(current_list, versions).await.is_ok()) |
152 | } | 132 | } |