Skip to content

Commit

Permalink
Fix the field names in the example
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Apr 8, 2024
1 parent 23f3880 commit 9478a70
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 4 additions & 1 deletion examples/image-uploader/index.html
Expand Up @@ -19,7 +19,10 @@ <h1>image-uploader</h1>
const response = await fetch("/images", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ size, type }),
body: JSON.stringify({
content_length: size,
...type.length > 0 ? { content_type: type } : {}
}),
});
if (response.status !== 200) throw new Error("createImage failed");
const { form_data: formData, method, url } = await response.json();
Expand Down
15 changes: 8 additions & 7 deletions examples/image-uploader/src/main.rs
Expand Up @@ -2,8 +2,8 @@ use cloud_storage_signature::BuildHtmlFormDataOptions;

#[derive(serde::Deserialize)]
struct CreateImageRequestBody {
size: u32,
r#type: String,
content_length: u64,
content_type: String,
}

#[derive(serde::Serialize)]
Expand All @@ -22,14 +22,13 @@ async fn create_image(
service_account_private_key,
},
}): axum::extract::State<AppState>,
axum::extract::Json(CreateImageRequestBody { size, r#type }): axum::extract::Json<
CreateImageRequestBody,
>,
axum::extract::Json(CreateImageRequestBody {
content_length,
content_type,
}): axum::extract::Json<CreateImageRequestBody>,
) -> Result<axum::Json<CreateImageResponseBody>, axum::http::StatusCode> {
let method = "POST".to_string();
let url = format!("https://storage.googleapis.com/{}", bucket_name);
// TODO: Use the size and type to generate the form data.
println!("size = {}, type = {}", size, r#type);
let form_data = cloud_storage_signature::build_html_form_data(BuildHtmlFormDataOptions {
service_account_client_email,
service_account_private_key,
Expand All @@ -38,6 +37,8 @@ async fn create_image(
region: None,
expires: std::time::SystemTime::now() + std::time::Duration::from_secs(60),
accessible_at: None,
content_length: Some(content_length),
content_type: Some(content_type),
})
.map_err(|_| axum::http::StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(axum::Json(CreateImageResponseBody {
Expand Down

0 comments on commit 9478a70

Please sign in to comment.