feat: commit code

This commit is contained in:
mrxiaozhuox committed 2022-03-12 23:48:05 +08:00
1 parent 5fb1b3e20b
commit 25ff6c5937
2 files changed
+34 -9

No files matched your search

+1 -1
View File
@@ -42,7 +42,7 @@ headers = "0.3.7"
# tools download # tools download
dirs = "4.0.0" dirs = "4.0.0"
reqwest = { version = "0.11", features = ["json"] } reqwest = { version = "0.11", features = ["rustls-tls", "stream", "trust-dns"] }
[[bin]] [[bin]]
path = "src/main.rs" path = "src/main.rs"
+33 -8
View File
@@ -1,12 +1,29 @@
use std::path::PathBuf; use std::{fs::create_dir_all, path::PathBuf};
use anyhow::Context;
use futures::StreamExt;
use tokio::io::AsyncWriteExt;
pub enum Tool { pub enum Tool {
WasmOpt, WasmOpt,
} }
pub fn app_path() { pub fn app_path() -> PathBuf {
let data_local = dirs::data_local_dir().unwrap(); let data_local = dirs::data_local_dir().unwrap();
let dioxus_dir = data_local.join("dioxus");
if !dioxus_dir.is_dir() {
create_dir_all(&dioxus_dir).unwrap();
}
dioxus_dir
}
pub fn temp_path() -> PathBuf {
let app_path = app_path();
let temp_path = app_path.join("temp");
if !temp_path.is_dir() {
create_dir_all(&temp_path).unwrap();
}
temp_path
} }
impl Tool { impl Tool {
@@ -55,15 +72,23 @@ impl Tool {
} }
} }
pub async fn download_package(&self) { pub async fn download_package(&self) -> anyhow::Result<PathBuf> {
let temp_dir = temp_path();
let download_dir = dirs::download_dir().unwrap();
let download_url = self.download_url(); let download_url = self.download_url();
let temp_out = temp_dir.join(format!("{}-tool.tmp", self.name()));
let mut file = tokio::fs::File::create(&temp_out)
.await
.context("failed creating temporary output file")?;
let resp = reqwest::get(download_url).await.unwrap(); let resp = reqwest::get(download_url).await.unwrap();
resp. let mut res_bytes = resp.bytes_stream();
while let Some(chunk_res) = res_bytes.next().await {
let chunk = chunk_res.context("error reading chunk from download")?;
let _ = file.write(chunk.as_ref()).await;
}
Ok(temp_out)
} }
} }