diff --git a/src/main.rs b/src/main.rs index 479766e..4560d9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use axum::body::Bytes; use axum::extract::{Multipart, Query}; use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; @@ -66,6 +67,36 @@ async fn test_handler() -> Result<(), AppError> { Ok(()) } +async fn send_image_to_dd( + file_contents: Vec, + file_name: String, + file_mime_type: &str, + options: &Options, +) -> anyhow::Result { + let part = reqwest::multipart::Part::bytes(file_contents) + .file_name(file_name) + .mime_str(file_mime_type) + .unwrap(); + let form = reqwest::multipart::Form::new().part("file", part); + + log::debug!("calling dd"); + + let resp = reqwest::Client::new() + .post("http://localhost:4443") + .multipart(form) + .header("authorization", "Bearer 123") + .query(&[("threshold", options.threshold.clone())]) + .send() + .await?; + + let body = resp.text().await?; + log::info!("body: {}", &body); + let json_response: WrappedResponse = serde_json::from_str(&body)?; + + log::debug!("called!"); + Ok(json_response) +} + #[debug_handler] async fn upload_file( options: Query, @@ -90,27 +121,13 @@ async fn upload_file( } if let Some(file_contents) = maybe_file_contents { - let part = reqwest::multipart::Part::bytes(file_contents.to_vec()) - .file_name(maybe_file_name.unwrap()) - .mime_str(maybe_file_type.unwrap().as_ref()) - .unwrap(); - let form = reqwest::multipart::Form::new().part("file", part); - - log::debug!("calling dd"); - - let resp = reqwest::Client::new() - .post("http://localhost:4443") - .multipart(form) - .header("authorization", "Bearer 123") - .query(&[("threshold", options.threshold.clone())]) - .send() - .await?; - - let body = resp.text().await?; - log::info!("body: {}", &body); - let json_response: WrappedResponse = serde_json::from_str(&body)?; - - log::debug!("called!"); + let json_response = send_image_to_dd( + file_contents.to_vec(), + maybe_file_name.unwrap(), + &maybe_file_type.unwrap(), + &options, + ) + .await?; Ok((StatusCode::OK, Json(json_response))) } else { Ok((