Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upload): return type on POST is now string #976

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/upload-returnval.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions plugins/upload/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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,
Expand Down
20 changes: 16 additions & 4 deletions plugins/upload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
Loading