1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
use super::*;
use std::convert::Infallible;

use futures::future::FutureExt;

// Returns raw bytes of multipart payload
impl FromField for Vec<u8> {
    // Should never return error as anything fits
    type Error = Infallible;
    type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;

    fn from_field(mut field: actix_multipart::Field) -> Self::Future {
        async move {
            let mut vec: Vec<u8> = Vec::new();
            while let Some(chunk) = field.next().await {
                vec.extend(chunk.iter().flat_map(|m| m.iter()))
            }
            Ok(vec)
        }
        .boxed_local()
    }
}

/// Type for wrapping any other field in order to get its name and mime.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct File<T> {
    pub name: PathBuf,
    pub mime: Mime,
    pub inner: T,
}

impl<T> File<T> {
    pub fn into_inner(self) -> T {
        self.inner
    }
}

impl<T> FromField for File<T>
where
    T: FromField + 'static,
    T::Error: std::fmt::Debug,
{
    type Error = Error;
    type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;

    fn from_field(field: actix_multipart::Field) -> Self::Future {
        let mime = field.content_type().clone();
        let mut disp = get_content_disposition(&field);
        let name = disp
            .remove("filename")
            .ok_or(Error::NoFilenameError)
            .map(|s| PathBuf::from(s.to_string())); //
        let inner = T::from_field(field);

        async move {
            let name = name?;
            let inner: T = inner.await.unwrap();

            Ok(Self { name, mime, inner })
        }
        .boxed_local()
    }
}

/// Type for wrapping json decoding of multipart field
#[derive(Deref, DerefMut)]
pub struct Json<T>(pub T);

impl<T: serde::de::DeserializeOwned + 'static> FromField for Json<T> {
    type Error = serde_json::error::Error;
    type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;

    fn from_field(field: actix_multipart::Field) -> Self::Future {
        let vec = Vec::<u8>::from_field(field);
        async move {
            let vec = vec.await.unwrap();
            let json: T = serde_json::from_reader(&*vec)?;
            Ok(Self(json))
        }
        .boxed_local()
    }
}

impl<T: FromField + 'static> FromField for Box<T> {
    type Error = T::Error;
    type Future = futures::future::Map<T::Future, fn(Result<T, T::Error>) -> Result<Box<T>, T::Error>>;

    fn from_field(field: actix_multipart::Field) -> Self::Future {
        T::from_field(field).map(|res| res.map(Box::new))
    }
}

impl FromField for String {
    type Error = Error;
    type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;

    fn from_field(field: actix_multipart::Field) -> Self::Future {
        async move {
            let vec = Vec::<u8>::from_field(field).await.unwrap();
            Ok(String::from_utf8(vec)?)
        }
        .boxed_local()
    }
}