Skip to content

Commit

Permalink
feat: add method to turn into header
Browse files Browse the repository at this point in the history
  • Loading branch information
fMeow committed May 29, 2021
1 parent e695847 commit 04ba0bc
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,90 @@ impl FormData {

Ok(nodes)
}
/// Into an object with `std::io::Read`
pub fn into_reader(self) -> Result<impl ConcatRead + Send + Sync, crate::Error> {
let nodes = self.to_multipart()?;
let boundary = generate_boundary();
let mut readers: Vec<Box<dyn Read + Send + Sync>> = vec![];

for node in nodes {
// write a boundary
readers.push(Box::new(Cursor::new(b"--".to_vec())));
readers.push(Box::new(Cursor::new(boundary.clone())));
readers.push(Box::new(Cursor::new(b"\r\n".to_vec())));

match node {
Node::Part(part) => {
// write the part's headers
for header in part.headers.iter() {
readers.push(Box::new(Cursor::new(
header.name().as_bytes().to_vec().into_iter(),
)));
readers.push(Box::new(Cursor::new(b": ".to_vec())));
readers.push(Box::new(Cursor::new(
header.value_string().as_bytes().to_vec(),
)));
readers.push(Box::new(Cursor::new(b"\r\n".to_vec())));
}

// write the blank line
readers.push(Box::new(Cursor::new(b"\r\n".to_vec())));

// write the part's content
readers.push(Box::new(Cursor::new(part.body)));
}
Node::File(filepart) => {
// write the part's headers
for header in filepart.headers.iter() {
readers.push(Box::new(Cursor::new(header.name().as_bytes().to_vec())));
readers.push(Box::new(Cursor::new(b": ".to_vec())));
readers.push(Box::new(Cursor::new(
header.value_string().as_bytes().to_vec(),
)));
readers.push(Box::new(Cursor::new(b"\r\n".to_vec())));
}

// write the blank line
readers.push(Box::new(Cursor::new(b"\r\n".to_vec())));

// write out the files's content
let file = BufReader::new(
File::open(&filepart.path).map_err(|_| crate::Error::InvalidFile)?,
);
readers.push(Box::new(file));
}
Node::Multipart((headers, subnodes)) => {
// get boundary
let boundary = get_multipart_boundary(&headers)?;

// write the multipart headers
for header in headers.iter() {
readers.push(Box::new(Cursor::new(header.name().as_bytes().to_vec())));
readers.push(Box::new(Cursor::new(b": ".to_vec())));
readers.push(Box::new(Cursor::new(
header.value_string().as_bytes().to_vec(),
)));
readers.push(Box::new(Cursor::new(b"\r\n".to_vec())));
}

// write the blank line
readers.push(Box::new(Cursor::new(b"\r\n".to_vec())));

// recurse
multipart_to_read(boundary.clone(), subnodes)?;
}
}

// write a line terminator
readers.push(Box::new(Cursor::new(b"\r\n".to_vec())));
}

// write a final boundary
readers.push(Box::new(Cursor::new(b"--".to_vec())));
readers.push(Box::new(Cursor::new(boundary.clone())));
readers.push(Box::new(Cursor::new(b"--".to_vec())));

let reader = concat_reader::concat(readers);
Ok(reader)
}
}

0 comments on commit 04ba0bc

Please sign in to comment.