From 9e32196ca271c77bfdd99587b7f5d5f62e886b4b Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 3 Jun 2025 12:59:47 -0400 Subject: [PATCH] decode image --- .../endpoints/webhooks/fal_webhook_handler.rs | 31 +++++++++++++++++-- .../webhooks/handle_image_payload.rs | 28 +++++++++++++++++ .../src/http_server/endpoints/webhooks/mod.rs | 3 +- 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/handle_image_payload.rs diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/fal_webhook_handler.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/fal_webhook_handler.rs index 63738725e7..255b8b9fdc 100644 --- a/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/fal_webhook_handler.rs +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/fal_webhook_handler.rs @@ -1,17 +1,32 @@ use std::fmt; use std::sync::Arc; +use crate::http_server::endpoints::webhooks::handle_image_payload::handle_image_payload; use crate::state::server_state::ServerState; use actix_web::error::ResponseError; use actix_web::http::StatusCode; use actix_web::web::Json; use actix_web::{web, HttpRequest, HttpResponse}; -use log::info; +use anyhow::Error; +use errors::AnyhowResult; use http_server_common::response::response_success_helpers::SimpleGenericJsonSuccess; use http_server_common::response::serialize_as_json_error::serialize_as_json_error; +use log::info; use serde_json::Value; use utoipa::ToSchema; +// 1. tauri --> hit endpoint to enqueue +// +// 2. webhook +// - upload media file +// - update job record with media token + status +// +// 3. tauri --> storyteller jobs endpoint polls +// - alert frontend of completion +// +// 4. javascript polls tauri (backend removal) + +// TODO(bt, 2025-06-03): Handle webhook crypto authentication #[derive(Debug, Deserialize, ToSchema)] pub struct FalWebhookRequest { pub status: FalWebhookStatus, @@ -65,6 +80,13 @@ impl fmt::Display for FalWebhookError { } } +impl From for FalWebhookError { + fn from(value: Error) -> Self { + info!("Converting anyhow::Error to FalWebhookError: {:?}", value); + FalWebhookError::ServerError + } +} + // =============== Handler =============== /// Fal webhook @@ -85,13 +107,16 @@ impl fmt::Display for FalWebhookError { pub async fn fal_webhook_handler( http_request: HttpRequest, server_state: web::Data>, - //request_body: web::Bytes request: Json, ) -> Result, FalWebhookError> { - info!("Received FAL webhook body: {:?}", request); + if let Some(payload) = request.payload.as_ref() { + if let Some(payload_obj) = payload.as_object() { + handle_image_payload(payload_obj).await?; + } + } Ok(SimpleGenericJsonSuccess::wrapped(true)) } diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/handle_image_payload.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/handle_image_payload.rs new file mode 100644 index 0000000000..129c56f66b --- /dev/null +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/handle_image_payload.rs @@ -0,0 +1,28 @@ +use anyhow::anyhow; +use log::info; +use serde_json::{Map, Value}; +use errors::AnyhowResult; + +#[derive(Deserialize, Debug)] +pub struct FalWebhookImage { + pub content_type: Option, + pub file_name: Option, + pub file_size: Option, + pub height: Option, + pub width: Option, + pub url: Option, +} + +pub async fn handle_image_payload( + payload: &Map, +) -> AnyhowResult<()> { + + let image_value = payload.get("image") + .ok_or_else(|| anyhow!("no `image` key in payload"))?; + + let image: FalWebhookImage = serde_json::from_value(image_value.clone())?; + + info!("Image payload received: {:?}", image); + + Ok(()) +} diff --git a/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/mod.rs b/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/mod.rs index 179ec0cae4..99c6e1b9d1 100644 --- a/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/mod.rs +++ b/crates/service/web/storyteller_web/src/http_server/endpoints/webhooks/mod.rs @@ -1 +1,2 @@ -pub mod fal_webhook_handler; \ No newline at end of file +pub mod fal_webhook_handler; +pub mod handle_image_payload; \ No newline at end of file