diff --git a/src/core/requests/form_builder.rs b/src/core/requests/form_builder.rs index 8311a754..aa31dade 100644 --- a/src/core/requests/form_builder.rs +++ b/src/core/requests/form_builder.rs @@ -3,6 +3,7 @@ use serde::Serialize; use crate::core::types::ParseMode; use crate::core::requests::ChatId; use crate::core::requests::utils; +use std::path::PathBuf; /// This is a convenient struct that builds `reqwest::r#async::multipart::Form` /// 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 { form: self.form.part(name.to_owned(), utils::file_to_part(path_to_file)) } diff --git a/src/core/requests/send_photo.rs b/src/core/requests/send_photo.rs index c0bc5490..12c8a52a 100644 --- a/src/core/requests/send_photo.rs +++ b/src/core/requests/send_photo.rs @@ -1,7 +1,7 @@ use std::path::Path; 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::network; @@ -10,8 +10,7 @@ pub struct SendPhoto<'a> { ctx: RequestContext<'a>, pub chat_id: ChatId, - // TODO: add enum Photo - pub photo: String, + pub photo: InputFile, pub caption: Option, pub parse_mode: Option, pub disable_notification: Option, @@ -25,9 +24,8 @@ impl<'a> Request<'a> for SendPhoto<'a> { fn send(self) -> RequestFuture<'a, ResponseResult> { Box::pin(async move { - let params = FormBuilder::new() + let mut params = FormBuilder::new() .add("chat_id", &self.chat_id) - .add_file("photo", &self.photo) .add_if_some("caption", self.caption.as_ref()) .add_if_some("parse_mode", self.parse_mode.as_ref()) .add_if_some( @@ -37,8 +35,14 @@ impl<'a> Request<'a> for SendPhoto<'a> { .add_if_some( "reply_to_message_id", 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( &self.ctx.client, @@ -54,7 +58,7 @@ impl<'a> SendPhoto<'a> { pub(crate) fn new( ctx: RequestContext<'a>, chat_id: ChatId, - photo: String + photo: InputFile ) -> Self { Self { ctx, @@ -72,7 +76,7 @@ impl<'a> SendPhoto<'a> { self } - pub fn photo>(mut self, photo: T) -> Self { + pub fn photo>(mut self, photo: T) -> Self { self.photo = photo.into(); self } @@ -121,7 +125,7 @@ mod tests { token: TOKEN, }, ChatId::Id(USER_ID), - "D:\\Снимок.png".to_string(), + InputFile::File("D:\\Снимок.png".to_string().parse().unwrap()), ); println!("{:?}", req.send().await); diff --git a/src/core/requests/utils.rs b/src/core/requests/utils.rs index e0419a70..5cd773d5 100644 --- a/src/core/requests/utils.rs +++ b/src/core/requests/utils.rs @@ -3,6 +3,7 @@ use std::fs::File; use bytes::{Bytes, BytesMut}; use tokio::prelude::*; use reqwest::r#async::multipart::Part; +use std::path::PathBuf; 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()) .map(|file| FramedRead::new(file, FileDecoder)) .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 } \ No newline at end of file