Skip to content

Commit

Permalink
more multipart
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Sep 27, 2020
1 parent b1708c8 commit 0f453a8
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ serde = { version = "1.0.106", features = ["derive"] }
serde_urlencoded = "0.7.0"
rand = "0.7.3"
serde_qs = "0.7.0"
multipart = { version = "0.16.1", default-features = false, features = ["server"], optional = true }
multipart = { version = "0.16.1", default-features = false, features = ["server"] }

[dev-dependencies]
http = "0.2.0"
Expand Down
15 changes: 14 additions & 1 deletion src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use async_std::io::{self, Cursor};
use serde::{de::DeserializeOwned, Serialize};

use std::fmt::{self, Debug};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::task::{Context, Poll};

Expand Down Expand Up @@ -423,6 +423,19 @@ impl Body {
pub fn set_mime(&mut self, mime: impl Into<Mime>) {
self.mime = mime.into();
}

/// Get the file name of the `Body`, if it's set.
pub fn file_name(&self) -> Option<&PathBuf> {
self.file_name.as_ref()
}

/// Set the file name of the `Body`.
pub fn set_file_name<P>(&mut self, file_name: Option<P>)
where
P: AsRef<Path>,
{
self.file_name = file_name.map(|v| v.as_ref().to_owned());
}
}

impl Debug for Body {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ mod status;
mod status_code;
mod version;

pub mod multipart;
pub mod trace;
cfg_unstable! {
pub mod upgrade;
pub mod multipart;
}

pub use body::Body;
Expand Down
58 changes: 54 additions & 4 deletions src/multipart/entry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Body;
use crate::{bail, Body, Mime};

use std::fmt::{self, Debug};
use std::path::PathBuf;
use std::path::Path;

/// A single multipart entry.
///
Expand All @@ -20,14 +20,64 @@ impl Entry {
}
}

/// Create an empty `Entry`.
pub fn empty(name: impl AsRef<str>) -> Self {
Self {
name: name.as_ref().to_owned(),
body: Body::empty(),
}
}

/// Create an `Entry` from a file.
///
#[cfg(all(feature = "async_std", not(target_os = "unknown")))]
pub async fn from_file<P>(path: P) -> crate::Result<Self>
where
P: AsRef<std::path::Path>,
{
let path = path.as_ref();
let name = match path.to_str() {
Some(p) => p.to_owned(),
None => bail!("Could not convert file name to unicode"),
};
let body = Body::from_file(path).await?;
Ok(Self::new(name, body))
}

/// Get the entry name.
pub fn name(&self) -> &String {
&self.name
}

/// Set the entry name.
pub fn set_name<S>(&mut self, name: S)
where
S: AsRef<str>,
{
self.name = name.as_ref().to_owned();
}

/// Get the content type.
pub fn content_type(&self) -> Option<Mime> {
todo!();
}

/// Set the content type.
pub fn set_content_type(&mut self, _mime: Option<Mime>) {
todo!();
}

/// Get the file name of the entry, if it's set.
pub fn file_name(&self) -> Option<&PathBuf> {
self.body.file_name.as_ref()
pub fn file_name(&self) -> Option<&Path> {
self.body.file_name().map(|p| p.as_path())
}

/// Set the file name of the `Body`.
pub fn set_file_name<P>(&mut self, file_name: Option<P>)
where
P: AsRef<Path>,
{
self.body.set_file_name(file_name);
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/multipart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
//!
//! Request:
//! ```
//! use http_types::multipart::{Multipart, Entry};
//!
//! let mut req = Request::new(Method::Get, "http://example.website");
//!
//! let mut multi = Multipart::new();
//! multi.push("hello world");
//! multi.push(Body::from_file("./cats.jpeg").await?);
//! multi.push(Entry::new("description", "hello world"));
//!
//! let mut entry = Entry::from_file("my_file", Body::from_file("./cats.jpeg").await?);
//! entry.set_file_name("cats.jpeg");
//! multi.push("myFile", Body::from_file("./cats.jpeg").await?);
//!
//! req.set_body(multi);
//! ```
//!
//! Response:
//!
//! ```
//! use http_types::multipart::{Multipart, Entry};
//! let mut res = Response::new(200); // get this from somewhere
//!
//! let mut entries = res.body_multipart();
Expand Down Expand Up @@ -55,9 +61,11 @@ impl Multipart {
}

/// Add a new entry to the `Multipart` instance.
pub fn push(&mut self, name: impl AsRef<str>, body: impl Into<Body>) {
let entry = Entry::new(name, body);
self.entries.push(entry);
pub fn push<E>(&mut self, entry: E)
where
E: Into<Entry>,
{
self.entries.push(entry.into());
}
}

Expand Down

0 comments on commit 0f453a8

Please sign in to comment.