mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-04 13:48:01 +02:00
fix(upload): return type on is now a string (#976)
* fix(upload): return type on POST is now string A POST to a webserver can not always be expected to be a JSON response. Success is now determined by the HTTP return code. Upon success the body content is returned as a string. * feat: add content-length on POST Not all embedded devices are acceptable to receiving unspecified amounts of data. Appending the content-length up front helps this devices succeed. * fix: return values unified The return values was not used. On POST the HTTP error code is returned as an enum. * fix: upload, return value as string * Update plugins/upload/src/lib.rs Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app> * Update plugins/upload/src/lib.rs Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app> * fix: added covector changelog file --------- Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app>
This commit is contained in:
committed by
GitHub
parent
9144521b90
commit
4a5ab18a22
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"upload": patch
|
||||
"upload-js": patch
|
||||
---
|
||||
|
||||
Return the upload response as a string and error out if the status code is not within 200-299.
|
||||
@@ -33,7 +33,7 @@ async function upload(
|
||||
filePath: string,
|
||||
progressHandler?: ProgressHandler,
|
||||
headers?: Map<string, string>,
|
||||
): Promise<void> {
|
||||
): Promise<string> {
|
||||
const ids = new Uint32Array(1);
|
||||
window.crypto.getRandomValues(ids);
|
||||
const id = ids[0];
|
||||
@@ -44,7 +44,7 @@ async function upload(
|
||||
|
||||
await listenToEventIfNeeded("upload://progress");
|
||||
|
||||
await invoke("plugin:upload|upload", {
|
||||
return await invoke("plugin:upload|upload", {
|
||||
id,
|
||||
url,
|
||||
filePath,
|
||||
|
||||
@@ -29,6 +29,8 @@ pub enum Error {
|
||||
Request(#[from] reqwest::Error),
|
||||
#[error("{0}")]
|
||||
ContentLength(String),
|
||||
#[error("request failed with status code {0}: {1}")]
|
||||
HttpErrorCode(u16, String),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
@@ -93,13 +95,17 @@ async fn upload<R: Runtime>(
|
||||
url: &str,
|
||||
file_path: &str,
|
||||
headers: HashMap<String, String>,
|
||||
) -> Result<serde_json::Value> {
|
||||
) -> Result<String> {
|
||||
// Read the file
|
||||
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).body(file_to_body(id, window, file));
|
||||
let mut request = client
|
||||
.post(url)
|
||||
.header(reqwest::header::CONTENT_LENGTH, file_len)
|
||||
.body(file_to_body(id, window, file));
|
||||
|
||||
// Loop trought the headers keys and values
|
||||
// and add them to the request object.
|
||||
@@ -108,8 +114,14 @@ async fn upload<R: Runtime>(
|
||||
}
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
response.json().await.map_err(Into::into)
|
||||
if response.status().is_success() {
|
||||
response.text().await.map_err(Into::into)
|
||||
} else {
|
||||
Err(Error::HttpErrorCode(
|
||||
response.status().as_u16(),
|
||||
response.text().await.unwrap_or_default(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn file_to_body<R: Runtime>(id: u32, window: Window<R>, file: File) -> reqwest::Body {
|
||||
|
||||
Reference in New Issue
Block a user