1
0
Fork 0

added file download feature

This commit is contained in:
Kim, Jimin 2022-01-14 14:48:59 +09:00
parent 592cf0166b
commit c5bef484c2
6 changed files with 99 additions and 18 deletions

2
src-tauri/Cargo.lock generated
View file

@ -2336,6 +2336,7 @@ dependencies = [
"serde_urlencoded 0.7.0",
"tokio",
"tokio-native-tls",
"tokio-util",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
@ -3352,6 +3353,7 @@ checksum = "0237232789cf037d5480773fe568aac745bfe2afbc11a863e97901780a6b47cc"
name = "wbm-installer"
version = "1.0.0"
dependencies = [
"futures-util",
"reqwest",
"serde",
"serde_json",

View file

@ -12,8 +12,9 @@ build = "src/build.rs"
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.0-beta.8", features = ["dialog-open", "shell-open"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
tauri = { version = "1.0.0-beta.8", features = ["dialog-open", "path-all", "shell-open"] }
reqwest = { version = "0.11", features = ["blocking", "json", "stream"] }
futures-util = "0.3"
[build-dependencies]
tauri-build = { version = "1.0.0-beta.4" }

View file

@ -1,4 +1,22 @@
use tauri::api::path::cache_dir;
use crate::util;
#[tauri::command]
pub fn install() {
println!("Installing WBM!");
pub async fn install() {
let game_path = util::get_default_game_path();
if game_path.is_some() {
println!("game path: {}", game_path.unwrap());
}
let cache_dir_raw = util::buf2str(cache_dir());
if cache_dir_raw.is_some() {
let result = util::download_release_zip(
&"https://github.com/War-Brokers-Mods/WBM/releases/download/v1.7.1.0/WBM.zip",
format!("{}/WBM.zip", cache_dir_raw.unwrap()).as_str(),
)
.await;
println!("{:#?}", result);
}
}

View file

@ -22,8 +22,9 @@ pub async fn status(req_type: String) -> StatusData {
} else if req_type == GAME_PATH {
// returns an empty string if the game doesn't exist in the default path
match util::get_default_game_path() {
Ok(path) => status_data.game_path = path,
Err(_) => {}
Some(path) => status_data.game_path = path,
// todo: send feedback to frontend
None => {}
}
}

View file

@ -1,4 +1,11 @@
use futures_util::StreamExt;
use std::cmp::min;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use tauri::api::path::home_dir;
/// build client with header
pub fn build_client() -> reqwest::Client {
@ -9,21 +16,37 @@ pub fn build_client() -> reqwest::Client {
}
/// gets path to WB game files.
pub fn get_default_game_path() -> Result<String, String> {
pub fn get_default_game_path() -> Option<String> {
let home = buf2str(home_dir());
if home.is_none() {
return None;
}
let home = home.unwrap();
let game_path = match std::env::consts::OS {
"linux" => "~/.steam/steam/steamapps/common/WarBrokers",
"macos" => "~/Library/Application Support/Steam/steamapps/common/WarBrokers",
"windows" => "C:\\Program Files (x86)\\Steam\\steamapps\\common\\WarBrokers",
"linux" => format!("{}/.steam/steam/steamapps/common/WarBrokers", home),
"macos" => format!(
"{}/Library/Application Support/Steam/steamapps/common/WarBrokers",
home
),
"windows" => String::from("C:\\Program Files (x86)\\Steam\\steamapps\\common\\WarBrokers"),
_ => panic!("Unsupported platform!"),
_ => return None,
};
// https://stackoverflow.com/a/32384768/12979111
return if fs::metadata(game_path).is_ok() {
Ok(String::from(game_path))
} else {
Err(String::from("Failed to default game path"))
};
if fs::metadata(game_path.as_str()).is_ok() {
return Some(String::from(game_path));
}
return None;
}
pub fn buf2str(input: Option<PathBuf>) -> Option<String> {
if input.is_none() {
return None;
}
return Some(String::from(input.unwrap().to_str().unwrap()));
}
/// get the latest WBM release version.
@ -44,4 +67,37 @@ pub async fn get_latest_release() -> String {
return res;
}
pub fn download_release_zip() {}
/// download a specific release version
pub async fn download_release_zip(url: &str, path: &str) -> Result<(), String> {
let client = build_client();
let res = client
.get(url)
.send()
.await
.or(Err(format!("Failed to GET from '{}'", &url)))?;
let total_size = res
.content_length()
.ok_or(format!("Failed to get content length from '{}'", &url))?;
// download chunks
let mut file = File::create(path).or(Err(format!("Failed to create file '{}'", path)))?;
let mut stream = res.bytes_stream();
let mut downloaded: u64 = 0;
while let Some(item) = stream.next().await {
let chunk = item.or(Err(format!("Error while downloading file")))?;
file.write(&chunk)
.or(Err(format!("Error while writing to file")))?;
let new = min(downloaded + (chunk.len() as u64), total_size);
downloaded = new;
}
return Ok(());
}

View file

@ -38,6 +38,9 @@
},
"dialog": {
"open": true
},
"path": {
"all": true
}
},
"windows": [