|
|
@ -72,44 +72,50 @@ async fn download(
|
|
|
|
body: Option<String>,
|
|
|
|
body: Option<String>,
|
|
|
|
on_progress: Channel<ProgressPayload>,
|
|
|
|
on_progress: Channel<ProgressPayload>,
|
|
|
|
) -> Result<()> {
|
|
|
|
) -> Result<()> {
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let url = url.to_string();
|
|
|
|
let mut request = if let Some(body) = body {
|
|
|
|
let file_path = file_path.to_string();
|
|
|
|
client.post(url).body(body)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
tokio::spawn(async move {
|
|
|
|
client.get(url)
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
};
|
|
|
|
let mut request = if let Some(body) = body {
|
|
|
|
// Loop trought the headers keys and values
|
|
|
|
client.post(&url).body(body)
|
|
|
|
// and add them to the request object.
|
|
|
|
} else {
|
|
|
|
for (key, value) in headers {
|
|
|
|
client.get(&url)
|
|
|
|
request = request.header(&key, value);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Loop trought the headers keys and values
|
|
|
|
|
|
|
|
// and add them to the request object.
|
|
|
|
let response = request.send().await?;
|
|
|
|
for (key, value) in headers {
|
|
|
|
if !response.status().is_success() {
|
|
|
|
request = request.header(&key, value);
|
|
|
|
return Err(Error::HttpErrorCode(
|
|
|
|
}
|
|
|
|
response.status().as_u16(),
|
|
|
|
|
|
|
|
response.text().await.unwrap_or_default(),
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let total = response.content_length().unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut file = BufWriter::new(File::create(file_path).await?);
|
|
|
|
let response = request.send().await?;
|
|
|
|
let mut stream = response.bytes_stream();
|
|
|
|
if !response.status().is_success() {
|
|
|
|
|
|
|
|
return Err(Error::HttpErrorCode(
|
|
|
|
|
|
|
|
response.status().as_u16(),
|
|
|
|
|
|
|
|
response.text().await.unwrap_or_default(),
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let total = response.content_length().unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
|
|
let mut stats = TransferStats::default();
|
|
|
|
let mut file = BufWriter::new(File::create(&file_path).await?);
|
|
|
|
while let Some(chunk) = stream.try_next().await? {
|
|
|
|
let mut stream = response.bytes_stream();
|
|
|
|
file.write_all(&chunk).await?;
|
|
|
|
|
|
|
|
stats.record_chunk_transfer(chunk.len());
|
|
|
|
|
|
|
|
let _ = on_progress.send(ProgressPayload {
|
|
|
|
|
|
|
|
progress: chunk.len() as u64,
|
|
|
|
|
|
|
|
progress_total: stats.total_transferred,
|
|
|
|
|
|
|
|
total,
|
|
|
|
|
|
|
|
transfer_speed: stats.transfer_speed,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
file.flush().await?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
let mut stats = TransferStats::default();
|
|
|
|
|
|
|
|
while let Some(chunk) = stream.try_next().await? {
|
|
|
|
|
|
|
|
file.write_all(&chunk).await?;
|
|
|
|
|
|
|
|
stats.record_chunk_transfer(chunk.len());
|
|
|
|
|
|
|
|
let _ = on_progress.send(ProgressPayload {
|
|
|
|
|
|
|
|
progress: chunk.len() as u64,
|
|
|
|
|
|
|
|
progress_total: stats.total_transferred,
|
|
|
|
|
|
|
|
total,
|
|
|
|
|
|
|
|
transfer_speed: stats.transfer_speed,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
file.flush().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.await
|
|
|
|
|
|
|
|
.map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())))?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[command]
|
|
|
|
#[command]
|
|
|
@ -119,32 +125,39 @@ async fn upload(
|
|
|
|
headers: HashMap<String, String>,
|
|
|
|
headers: HashMap<String, String>,
|
|
|
|
on_progress: Channel<ProgressPayload>,
|
|
|
|
on_progress: Channel<ProgressPayload>,
|
|
|
|
) -> Result<String> {
|
|
|
|
) -> Result<String> {
|
|
|
|
// Read the file
|
|
|
|
let url = url.to_string();
|
|
|
|
let file = File::open(file_path).await?;
|
|
|
|
let file_path = file_path.to_string();
|
|
|
|
let file_len = file.metadata().await.unwrap().len();
|
|
|
|
|
|
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
// Create the request and attach the file to the body
|
|
|
|
// Read the file
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let file = File::open(&file_path).await?;
|
|
|
|
let mut request = client
|
|
|
|
let file_len = file.metadata().await.unwrap().len();
|
|
|
|
.post(url)
|
|
|
|
|
|
|
|
.header(reqwest::header::CONTENT_LENGTH, file_len)
|
|
|
|
// Create the request and attach the file to the body
|
|
|
|
.body(file_to_body(on_progress, file));
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
|
|
|
let mut request = client
|
|
|
|
// Loop through the headers keys and values
|
|
|
|
.post(&url)
|
|
|
|
// and add them to the request object.
|
|
|
|
.header(reqwest::header::CONTENT_LENGTH, file_len)
|
|
|
|
for (key, value) in headers {
|
|
|
|
.body(file_to_body(on_progress, file));
|
|
|
|
request = request.header(&key, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
// Loop through the headers keys and values
|
|
|
|
|
|
|
|
// and add them to the request object.
|
|
|
|
|
|
|
|
for (key, value) in headers {
|
|
|
|
|
|
|
|
request = request.header(&key, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let response = request.send().await?;
|
|
|
|
let response = request.send().await?;
|
|
|
|
if response.status().is_success() {
|
|
|
|
if response.status().is_success() {
|
|
|
|
response.text().await.map_err(Into::into)
|
|
|
|
response.text().await.map_err(Into::into)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Err(Error::HttpErrorCode(
|
|
|
|
Err(Error::HttpErrorCode(
|
|
|
|
response.status().as_u16(),
|
|
|
|
response.status().as_u16(),
|
|
|
|
response.text().await.unwrap_or_default(),
|
|
|
|
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 {
|
|
|
|
fn file_to_body(channel: Channel<ProgressPayload>, file: File) -> reqwest::Body {
|
|
|
|