Skip to content

Commit 1d9d121

Browse files
feat: Spawn threads using Tokio
1 parent 1311b61 commit 1d9d121

File tree

1 file changed

+73
-60
lines changed

1 file changed

+73
-60
lines changed

plugins/upload/src/lib.rs

Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -72,44 +72,50 @@ async fn download(
7272
body: Option<String>,
7373
on_progress: Channel<ProgressPayload>,
7474
) -> Result<()> {
75-
let client = reqwest::Client::new();
76-
let mut request = if let Some(body) = body {
77-
client.post(url).body(body)
78-
} else {
79-
client.get(url)
80-
};
81-
// Loop trought the headers keys and values
82-
// and add them to the request object.
83-
for (key, value) in headers {
84-
request = request.header(&key, value);
85-
}
86-
87-
let response = request.send().await?;
88-
if !response.status().is_success() {
89-
return Err(Error::HttpErrorCode(
90-
response.status().as_u16(),
91-
response.text().await.unwrap_or_default(),
92-
));
93-
}
94-
let total = response.content_length().unwrap_or(0);
75+
let url = url.to_string();
76+
let file_path = file_path.to_string();
77+
78+
tokio::spawn(async move {
79+
let client = reqwest::Client::new();
80+
let mut request = if let Some(body) = body {
81+
client.post(&url).body(body)
82+
} else {
83+
client.get(&url)
84+
};
85+
// Loop trought the headers keys and values
86+
// and add them to the request object.
87+
for (key, value) in headers {
88+
request = request.header(&key, value);
89+
}
9590

96-
let mut file = BufWriter::new(File::create(file_path).await?);
97-
let mut stream = response.bytes_stream();
91+
let response = request.send().await?;
92+
if !response.status().is_success() {
93+
return Err(Error::HttpErrorCode(
94+
response.status().as_u16(),
95+
response.text().await.unwrap_or_default(),
96+
));
97+
}
98+
let total = response.content_length().unwrap_or(0);
9899

99-
let mut stats = TransferStats::default();
100-
while let Some(chunk) = stream.try_next().await? {
101-
file.write_all(&chunk).await?;
102-
stats.record_chunk_transfer(chunk.len());
103-
let _ = on_progress.send(ProgressPayload {
104-
progress: chunk.len() as u64,
105-
progress_total: stats.total_transferred,
106-
total,
107-
transfer_speed: stats.transfer_speed,
108-
});
109-
}
110-
file.flush().await?;
100+
let mut file = BufWriter::new(File::create(&file_path).await?);
101+
let mut stream = response.bytes_stream();
111102

112-
Ok(())
103+
let mut stats = TransferStats::default();
104+
while let Some(chunk) = stream.try_next().await? {
105+
file.write_all(&chunk).await?;
106+
stats.record_chunk_transfer(chunk.len());
107+
let _ = on_progress.send(ProgressPayload {
108+
progress: chunk.len() as u64,
109+
progress_total: stats.total_transferred,
110+
total,
111+
transfer_speed: stats.transfer_speed,
112+
});
113+
}
114+
file.flush().await?;
115+
Ok(())
116+
})
117+
.await
118+
.map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())))?
113119
}
114120

115121
#[command]
@@ -119,32 +125,39 @@ async fn upload(
119125
headers: HashMap<String, String>,
120126
on_progress: Channel<ProgressPayload>,
121127
) -> Result<String> {
122-
// Read the file
123-
let file = File::open(file_path).await?;
124-
let file_len = file.metadata().await.unwrap().len();
125-
126-
// Create the request and attach the file to the body
127-
let client = reqwest::Client::new();
128-
let mut request = client
129-
.post(url)
130-
.header(reqwest::header::CONTENT_LENGTH, file_len)
131-
.body(file_to_body(on_progress, file));
132-
133-
// Loop through the headers keys and values
134-
// and add them to the request object.
135-
for (key, value) in headers {
136-
request = request.header(&key, value);
137-
}
128+
let url = url.to_string();
129+
let file_path = file_path.to_string();
130+
131+
tokio::spawn(async move {
132+
// Read the file
133+
let file = File::open(&file_path).await?;
134+
let file_len = file.metadata().await.unwrap().len();
135+
136+
// Create the request and attach the file to the body
137+
let client = reqwest::Client::new();
138+
let mut request = client
139+
.post(&url)
140+
.header(reqwest::header::CONTENT_LENGTH, file_len)
141+
.body(file_to_body(on_progress, file));
142+
143+
// Loop through the headers keys and values
144+
// and add them to the request object.
145+
for (key, value) in headers {
146+
request = request.header(&key, value);
147+
}
138148

139-
let response = request.send().await?;
140-
if response.status().is_success() {
141-
response.text().await.map_err(Into::into)
142-
} else {
143-
Err(Error::HttpErrorCode(
144-
response.status().as_u16(),
145-
response.text().await.unwrap_or_default(),
146-
))
147-
}
149+
let response = request.send().await?;
150+
if response.status().is_success() {
151+
response.text().await.map_err(Into::into)
152+
} else {
153+
Err(Error::HttpErrorCode(
154+
response.status().as_u16(),
155+
response.text().await.unwrap_or_default(),
156+
))
157+
}
158+
})
159+
.await
160+
.map_err(|e| Error::Io(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())))?
148161
}
149162

150163
fn file_to_body(channel: Channel<ProgressPayload>, file: File) -> reqwest::Body {

0 commit comments

Comments
 (0)