You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, can I write custom extractor for Multipart forms? I tried something like this, but I'm afraid it might not be possible due to the lifetime of multipart.
structStreamedFile{filename:String,fieldname:String,content_type:String,stream:Pin<Box<dynStream<Item = Result<Bytes,MultipartError>> + Send>>,}#[async_trait]impl<S>FromRequest<S>forStreamedFilewhereS:Send + Sync,{typeRejection = (StatusCode,String);asyncfnfrom_request(request:Request,state:&S) -> Result<Self,Self::Rejection>{// Create the multipart object from the requestletOk(mut multipart) = Multipart::from_request(request, state).awaitelse{returnErr((StatusCode::BAD_REQUEST,"Expect Multipart form with ...".into()));};let field = match multipart.next_field().await{Ok(Some(field)) => field,Ok(None) => returnErr((StatusCode::BAD_REQUEST,"Missing multipart field".into())),Err(error) => returnErr((error.status(), error.body_text())),};let fieldname = field
.name().map(|v| v.to_string()).ok_or((StatusCode::BAD_REQUEST,"I don't care".into()))?;let filename = field.file_name().map(|v| v.to_string()).ok_or((StatusCode::BAD_REQUEST,"Missing filename annotation for file field".into(),))?;let content_type = field.content_type().unwrap_or("application/octet-stream").to_string();let stream = field.into_stream().boxed();Ok(StreamedFile{
fieldname,
filename,
content_type,
stream,})}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Summary
Hello, can I write custom extractor for Multipart forms? I tried something like this, but I'm afraid it might not be possible due to the lifetime of multipart.
axum version
0.7.7
Beta Was this translation helpful? Give feedback.
All reactions