Setting InputFile::Memory file name

This commit is contained in:
jairinhohw 2020-03-24 17:45:19 -03:00
parent 9bfec9546f
commit cb68f1f7aa
2 changed files with 22 additions and 14 deletions

View file

@ -33,8 +33,8 @@ impl FormBuilder {
Self { form: self.form.text(name, string) }
}
Some(FormValue::File(path)) => self.add_file(name, path).await,
Some(FormValue::Memory(data)) => {
self.add_file_from_memory(name, data)
Some(FormValue::Memory { file_name, data }) => {
self.add_file_from_memory(name, file_name, data)
}
None => self,
}
@ -53,16 +53,20 @@ impl FormBuilder {
}
}
fn add_file_from_memory<'a, N>(self, name: N, data: Vec<u8>) -> Self
fn add_file_from_memory<'a, N>(
self,
name: N,
file_name: String,
data: Vec<u8>,
) -> Self
where
N: Into<Cow<'a, str>>,
{
let name = name.into().into_owned();
Self {
form: self
.form
.part(name.clone(), file_from_memory_to_part(data, name)),
form: self.form.part(
name.into().into_owned(),
file_from_memory_to_part(data, file_name),
),
}
}
@ -73,7 +77,7 @@ impl FormBuilder {
pub(crate) enum FormValue {
File(PathBuf),
Memory(Vec<u8>),
Memory { file_name: String, data: Vec<u8> },
Str(String),
}
@ -170,7 +174,10 @@ impl IntoFormValue for InputFile {
fn into_form_value(&self) -> Option<FormValue> {
match self {
InputFile::File(path) => Some(FormValue::File(path.clone())),
InputFile::Memory(data) => Some(FormValue::Memory(data.clone())),
InputFile::Memory { file_name, data } => Some(FormValue::Memory {
file_name: file_name.clone(),
data: data.clone(),
}),
InputFile::Url(url) => Some(FormValue::Str(url.clone())),
InputFile::FileId(file_id) => Some(FormValue::Str(file_id.clone())),
}

View file

@ -8,7 +8,7 @@ use std::path::PathBuf;
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize)]
pub enum InputFile {
File(PathBuf),
Memory(Vec<u8>),
Memory { file_name: String, data: Vec<u8> },
Url(String),
FileId(String),
}
@ -21,11 +21,12 @@ impl InputFile {
Self::File(path.into())
}
pub fn memory<D>(data: D) -> Self
pub fn memory<S, D>(file_name: S, data: D) -> Self
where
S: Into<String>,
D: Into<Vec<u8>>,
{
Self::Memory(data.into())
Self::Memory { file_name: file_name.into(), data: data.into() }
}
pub fn url<T>(url: T) -> Self
@ -90,7 +91,7 @@ impl Serialize for InputFile {
),
)
}
InputFile::Memory(data) => {
InputFile::Memory { data, .. } => {
// NOTE: file should be actually attached with
// multipart/form-data
serializer.serialize_str(&format!(