mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
changed field type String to InputFile
This commit is contained in:
parent
1ffd2697f1
commit
58ae51f318
3 changed files with 19 additions and 13 deletions
|
@ -3,6 +3,7 @@ use serde::Serialize;
|
||||||
use crate::core::types::ParseMode;
|
use crate::core::types::ParseMode;
|
||||||
use crate::core::requests::ChatId;
|
use crate::core::requests::ChatId;
|
||||||
use crate::core::requests::utils;
|
use crate::core::requests::utils;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// This is a convenient struct that builds `reqwest::r#async::multipart::Form`
|
/// This is a convenient struct that builds `reqwest::r#async::multipart::Form`
|
||||||
/// from scratch.
|
/// from scratch.
|
||||||
|
@ -38,7 +39,7 @@ impl FormBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_file(self, name: &str, path_to_file: &String) -> Self {
|
pub fn add_file(self, name: &str, path_to_file: &PathBuf) -> Self {
|
||||||
Self {
|
Self {
|
||||||
form: self.form.part(name.to_owned(), utils::file_to_part(path_to_file))
|
form: self.form.part(name.to_owned(), utils::file_to_part(path_to_file))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::core::requests::{RequestContext, ChatId, Request, RequestFuture, ResponseResult};
|
use crate::core::requests::{RequestContext, ChatId, Request, RequestFuture, ResponseResult};
|
||||||
use crate::core::types::{ParseMode, Message};
|
use crate::core::types::{ParseMode, Message, InputFile};
|
||||||
use crate::core::requests::form_builder::FormBuilder;
|
use crate::core::requests::form_builder::FormBuilder;
|
||||||
use crate::core::network;
|
use crate::core::network;
|
||||||
|
|
||||||
|
@ -10,8 +10,7 @@ pub struct SendPhoto<'a> {
|
||||||
ctx: RequestContext<'a>,
|
ctx: RequestContext<'a>,
|
||||||
|
|
||||||
pub chat_id: ChatId,
|
pub chat_id: ChatId,
|
||||||
// TODO: add enum Photo
|
pub photo: InputFile,
|
||||||
pub photo: String,
|
|
||||||
pub caption: Option<String>,
|
pub caption: Option<String>,
|
||||||
pub parse_mode: Option<ParseMode>,
|
pub parse_mode: Option<ParseMode>,
|
||||||
pub disable_notification: Option<bool>,
|
pub disable_notification: Option<bool>,
|
||||||
|
@ -25,9 +24,8 @@ impl<'a> Request<'a> for SendPhoto<'a> {
|
||||||
|
|
||||||
fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> {
|
fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let params = FormBuilder::new()
|
let mut params = FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
.add_file("photo", &self.photo)
|
|
||||||
.add_if_some("caption", self.caption.as_ref())
|
.add_if_some("caption", self.caption.as_ref())
|
||||||
.add_if_some("parse_mode", self.parse_mode.as_ref())
|
.add_if_some("parse_mode", self.parse_mode.as_ref())
|
||||||
.add_if_some(
|
.add_if_some(
|
||||||
|
@ -37,8 +35,14 @@ impl<'a> Request<'a> for SendPhoto<'a> {
|
||||||
.add_if_some(
|
.add_if_some(
|
||||||
"reply_to_message_id",
|
"reply_to_message_id",
|
||||||
self.reply_to_message_id.as_ref()
|
self.reply_to_message_id.as_ref()
|
||||||
)
|
);
|
||||||
.build();
|
|
||||||
|
params = match self.photo {
|
||||||
|
InputFile::File(path) => params.add_file("photo", &path),
|
||||||
|
InputFile::Url(url) => params.add("photo", &url),
|
||||||
|
InputFile::FileId(file_id) => params.add("photo", &file_id),
|
||||||
|
};
|
||||||
|
let params = params.build();
|
||||||
|
|
||||||
network::request(
|
network::request(
|
||||||
&self.ctx.client,
|
&self.ctx.client,
|
||||||
|
@ -54,7 +58,7 @@ impl<'a> SendPhoto<'a> {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
ctx: RequestContext<'a>,
|
ctx: RequestContext<'a>,
|
||||||
chat_id: ChatId,
|
chat_id: ChatId,
|
||||||
photo: String
|
photo: InputFile
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
ctx,
|
ctx,
|
||||||
|
@ -72,7 +76,7 @@ impl<'a> SendPhoto<'a> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn photo<T: Into<String>>(mut self, photo: T) -> Self {
|
pub fn photo<T: Into<InputFile>>(mut self, photo: T) -> Self {
|
||||||
self.photo = photo.into();
|
self.photo = photo.into();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -121,7 +125,7 @@ mod tests {
|
||||||
token: TOKEN,
|
token: TOKEN,
|
||||||
},
|
},
|
||||||
ChatId::Id(USER_ID),
|
ChatId::Id(USER_ID),
|
||||||
"D:\\Снимок.png".to_string(),
|
InputFile::File("D:\\Снимок.png".to_string().parse().unwrap()),
|
||||||
);
|
);
|
||||||
|
|
||||||
println!("{:?}", req.send().await);
|
println!("{:?}", req.send().await);
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::fs::File;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use tokio::prelude::*;
|
use tokio::prelude::*;
|
||||||
use reqwest::r#async::multipart::Part;
|
use reqwest::r#async::multipart::Part;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
struct FileDecoder;
|
struct FileDecoder;
|
||||||
|
|
||||||
|
@ -18,10 +19,10 @@ impl tokio::codec::Decoder for FileDecoder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file_to_part(path_to_file: &String) -> Part {
|
pub fn file_to_part(path_to_file: &PathBuf) -> Part {
|
||||||
let file = tokio::fs::File::open(path_to_file.clone())
|
let file = tokio::fs::File::open(path_to_file.clone())
|
||||||
.map(|file| FramedRead::new(file, FileDecoder))
|
.map(|file| FramedRead::new(file, FileDecoder))
|
||||||
.flatten_stream();
|
.flatten_stream();
|
||||||
let part = Part::stream(file).file_name("file");
|
let part = Part::stream(file).file_name(path_to_file.file_name().unwrap().to_str().unwrap().to_string());
|
||||||
part
|
part
|
||||||
}
|
}
|
Loading…
Reference in a new issue