Skip to content

Commit 5f1de11

Browse files
yoshuawuytsFishrock123
authored andcommitted
more multipart
1 parent f397468 commit 5f1de11

File tree

5 files changed

+83
-12
lines changed

5 files changed

+83
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ serde_urlencoded = "0.7.0"
4444
rand = "0.7.3"
4545
serde_qs = "0.7.0"
4646
base64 = "0.13.0"
47-
multipart = { version = "0.16.1", default-features = false, features = ["server"], optional = true }
47+
multipart = { version = "0.16.1", default-features = false, features = ["server"] }
4848

4949
[dev-dependencies]
5050
http = "0.2.0"

src/body.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use futures_lite::{io, prelude::*};
22
use serde::{de::DeserializeOwned, Serialize};
33

44
use std::fmt::{self, Debug};
5-
use std::path::PathBuf;
5+
use std::path::{Path, PathBuf};
66
use std::pin::Pin;
77
use std::task::{Context, Poll};
88

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

425438
impl Debug for Body {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ mod status;
139139
mod status_code;
140140
mod version;
141141

142+
pub mod multipart;
142143
pub mod trace;
143144
cfg_unstable! {
144145
pub mod upgrade;
145-
pub mod multipart;
146146
}
147147

148148
pub use body::Body;

src/multipart/entry.rs

Lines changed: 54 additions & 4 deletions
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

Lines changed: 13 additions & 5 deletions
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)