feat: Spawn threads using Tokio

pull/2838/head
Matthew Richardson 6 days ago
parent 426627a591
commit 79f7242cf9
No known key found for this signature in database
GPG Key ID: 94F48D0A011CC828

@ -72,11 +72,15 @@ async fn download(
body: Option<String>,
on_progress: Channel<ProgressPayload>,
) -> Result<()> {
let url = url.to_string();
let file_path = file_path.to_string();
tokio::spawn(async move {
let client = reqwest::Client::new();
let mut request = if let Some(body) = body {
client.post(url).body(body)
client.post(&url).body(body)
} else {
client.get(url)
client.get(&url)
};
// Loop trought the headers keys and values
// and add them to the request object.
@ -93,7 +97,7 @@ async fn download(
}
let total = response.content_length().unwrap_or(0);
let mut file = BufWriter::new(File::create(file_path).await?);
let mut file = BufWriter::new(File::create(&file_path).await?);
let mut stream = response.bytes_stream();
let mut stats = TransferStats::default();
@ -108,8 +112,10 @@ async fn download(
});
}
file.flush().await?;
Ok(())
})
.await
.map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())))?
}
#[command]
@ -119,14 +125,18 @@ async fn upload(
headers: HashMap<String, String>,
on_progress: Channel<ProgressPayload>,
) -> Result<String> {
let url = url.to_string();
let file_path = file_path.to_string();
tokio::spawn(async move {
// Read the file
let file = File::open(file_path).await?;
let file = File::open(&file_path).await?;
let file_len = file.metadata().await.unwrap().len();
// Create the request and attach the file to the body
let client = reqwest::Client::new();
let mut request = client
.post(url)
.post(&url)
.header(reqwest::header::CONTENT_LENGTH, file_len)
.body(file_to_body(on_progress, file));
@ -145,6 +155,9 @@ async fn upload(
response.text().await.unwrap_or_default(),
))
}
})
.await
.map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())))?
}
fn file_to_body(channel: Channel<ProgressPayload>, file: File) -> reqwest::Body {

Loading…
Cancel
Save