Skip to content

Commit 0f453a8

Browse files
committed
more multipart
1 parent b1708c8 commit 0f453a8

File tree

5 files changed

+83
-12
lines changed

5 files changed

+83
-12
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ serde = { version = "1.0.106", features = ["derive"] }
4141
serde_urlencoded = "0.7.0"
4242
rand = "0.7.3"
4343
serde_qs = "0.7.0"
44-
multipart = { version = "0.16.1", default-features = false, features = ["server"], optional = true }
44+
multipart = { version = "0.16.1", default-features = false, features = ["server"] }
4545

4646
[dev-dependencies]
4747
http = "0.2.0"

src/body.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use async_std::io::{self, Cursor};
33
use serde::{de::DeserializeOwned, Serialize};
44

55
use std::fmt::{self, Debug};
6-
use std::path::PathBuf;
6+
use std::path::{Path, PathBuf};
77
use std::pin::Pin;
88
use std::task::{Context, Poll};
99

@@ -423,6 +423,19 @@ impl Body {
423423
pub fn set_mime(&mut self, mime: impl Into<Mime>) {
424424
self.mime = mime.into();
425425
}
426+
427+
/// Get the file name of the `Body`, if it's set.
428+
pub fn file_name(&self) -> Option<&PathBuf> {
429+
self.file_name.as_ref()
430+
}
431+
432+
/// Set the file name of the `Body`.
433+
pub fn set_file_name<P>(&mut self, file_name: Option<P>)
434+
where
435+
P: AsRef<Path>,
436+
{
437+
self.file_name = file_name.map(|v| v.as_ref().to_owned());
438+
}
426439
}
427440

428441
impl Debug for Body {

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ mod status;
136136
mod status_code;
137137
mod version;
138138

139+
pub mod multipart;
139140
pub mod trace;
140141
cfg_unstable! {
141142
pub mod upgrade;
142-
pub mod multipart;
143143
}
144144

145145
pub use body::Body;

src/multipart/entry.rs

+54-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::Body;
1+
use crate::{bail, Body, Mime};
22

33
use std::fmt::{self, Debug};
4-
use std::path::PathBuf;
4+
use std::path::Path;
55

66
/// A single multipart entry.
77
///
@@ -20,14 +20,64 @@ impl Entry {
2020
}
2121
}
2222

23+
/// Create an empty `Entry`.
24+
pub fn empty(name: impl AsRef<str>) -> Self {
25+
Self {
26+
name: name.as_ref().to_owned(),
27+
body: Body::empty(),
28+
}
29+
}
30+
31+
/// Create an `Entry` from a file.
32+
///
33+
#[cfg(all(feature = "async_std", not(target_os = "unknown")))]
34+
pub async fn from_file<P>(path: P) -> crate::Result<Self>
35+
where
36+
P: AsRef<std::path::Path>,
37+
{
38+
let path = path.as_ref();
39+
let name = match path.to_str() {
40+
Some(p) => p.to_owned(),
41+
None => bail!("Could not convert file name to unicode"),
42+
};
43+
let body = Body::from_file(path).await?;
44+
Ok(Self::new(name, body))
45+
}
46+
2347
/// Get the entry name.
2448
pub fn name(&self) -> &String {
2549
&self.name
2650
}
2751

52+
/// Set the entry name.
53+
pub fn set_name<S>(&mut self, name: S)
54+
where
55+
S: AsRef<str>,
56+
{
57+
self.name = name.as_ref().to_owned();
58+
}
59+
60+
/// Get the content type.
61+
pub fn content_type(&self) -> Option<Mime> {
62+
todo!();
63+
}
64+
65+
/// Set the content type.
66+
pub fn set_content_type(&mut self, _mime: Option<Mime>) {
67+
todo!();
68+
}
69+
2870
/// Get the file name of the entry, if it's set.
29-
pub fn file_name(&self) -> Option<&PathBuf> {
30-
self.body.file_name.as_ref()
71+
pub fn file_name(&self) -> Option<&Path> {
72+
self.body.file_name().map(|p| p.as_path())
73+
}
74+
75+
/// Set the file name of the `Body`.
76+
pub fn set_file_name<P>(&mut self, file_name: Option<P>)
77+
where
78+
P: AsRef<Path>,
79+
{
80+
self.body.set_file_name(file_name);
3181
}
3282
}
3383

src/multipart/mod.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44
//!
55
//! Request:
66
//! ```
7+
//! use http_types::multipart::{Multipart, Entry};
8+
//!
79
//! let mut req = Request::new(Method::Get, "http://example.website");
810
//!
911
//! let mut multi = Multipart::new();
10-
//! multi.push("hello world");
11-
//! multi.push(Body::from_file("./cats.jpeg").await?);
12+
//! multi.push(Entry::new("description", "hello world"));
13+
//!
14+
//! let mut entry = Entry::from_file("my_file", Body::from_file("./cats.jpeg").await?);
15+
//! entry.set_file_name("cats.jpeg");
16+
//! multi.push("myFile", Body::from_file("./cats.jpeg").await?);
1217
//!
1318
//! req.set_body(multi);
1419
//! ```
1520
//!
1621
//! Response:
1722
//!
1823
//! ```
24+
//! use http_types::multipart::{Multipart, Entry};
1925
//! let mut res = Response::new(200); // get this from somewhere
2026
//!
2127
//! let mut entries = res.body_multipart();
@@ -55,9 +61,11 @@ impl Multipart {
5561
}
5662

5763
/// Add a new entry to the `Multipart` instance.
58-
pub fn push(&mut self, name: impl AsRef<str>, body: impl Into<Body>) {
59-
let entry = Entry::new(name, body);
60-
self.entries.push(entry);
64+
pub fn push<E>(&mut self, entry: E)
65+
where
66+
E: Into<Entry>,
67+
{
68+
self.entries.push(entry.into());
6169
}
6270
}
6371

0 commit comments

Comments
 (0)