actix_multipart/form/
bytes.rs

1//! Reads a field into memory.
2
3use actix_web::{web::BytesMut, HttpRequest};
4use futures_core::future::LocalBoxFuture;
5use futures_util::TryStreamExt as _;
6use mime::Mime;
7
8use crate::{
9    form::{FieldReader, Limits},
10    Field, MultipartError,
11};
12
13/// Read the field into memory.
14#[derive(Debug)]
15pub struct Bytes {
16    /// The data.
17    pub data: actix_web::web::Bytes,
18
19    /// The value of the `Content-Type` header.
20    pub content_type: Option<Mime>,
21
22    /// The `filename` value in the `Content-Disposition` header.
23    pub file_name: Option<String>,
24}
25
26impl<'t> FieldReader<'t> for Bytes {
27    type Future = LocalBoxFuture<'t, Result<Self, MultipartError>>;
28
29    fn read_field(_: &'t HttpRequest, mut field: Field, limits: &'t mut Limits) -> Self::Future {
30        Box::pin(async move {
31            let mut buf = BytesMut::with_capacity(131_072);
32
33            while let Some(chunk) = field.try_next().await? {
34                limits.try_consume_limits(chunk.len(), true)?;
35                buf.extend(chunk);
36            }
37
38            Ok(Bytes {
39                data: buf.freeze(),
40                content_type: field.content_type().map(ToOwned::to_owned),
41                file_name: field
42                    .content_disposition()
43                    .expect("multipart form fields should have a content-disposition header")
44                    .get_filename()
45                    .map(ToOwned::to_owned),
46            })
47        })
48    }
49}