multer_derive/
file_collection.rs

1use crate::{
2    error::Error, form_file::FormFile, from_multipart_field::FromMultipartField, FromMultipart,
3    MultipartForm,
4};
5
6/// Provides a way to collect all the files in a `form`.
7pub struct FileCollection(Vec<FormFile>);
8
9impl FileCollection {
10    /// Returns all the collected files.
11    pub fn into_inner(self) -> Vec<FormFile> {
12        self.0
13    }
14}
15
16impl FromMultipart for FileCollection {
17    fn from_multipart(
18        multipart: &MultipartForm,
19        ctx: crate::from_multipart::FormContext<'_>,
20    ) -> Result<Self, Error> {
21        let mut files = vec![];
22
23        for field in multipart.fields() {
24            if field.file_name().is_none() {
25                continue;
26            }
27
28            if ctx.field_name.is_some() && ctx.field_name != field.name() {
29                continue;
30            }
31
32            files.push(FormFile::from_field(field)?);
33        }
34
35        Ok(FileCollection(files))
36    }
37}