Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local TBA file downloading #1173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `filter_boost_added` and `filter_reply_to_story` filters to `MessageFilterExt` trait
- Add `filter_mention_command` filter to `HandlerExt` trait ([issue #494](https://github.com/teloxide/teloxide/issues/494))
- Add `filter_business_connection`, `filter_business_message`, `filter_edited_business_message`, and `filter_deleted_business_messages` filters to update filters ([PR 1146](https://github.com/teloxide/teloxide/pull/1146))
- Add local TBA file downloading support in `crate::net::download`

### Changed

Expand Down
40 changes: 38 additions & 2 deletions crates/teloxide-core/src/bot/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use std::path::Path;

use bytes::Bytes;
use futures::{future::BoxFuture, stream::BoxStream, FutureExt, StreamExt};
use tokio::io::AsyncWrite;

use tokio::{
fs::File,
io::{AsyncReadExt, AsyncWrite, AsyncWriteExt},
};

use crate::{
bot::Bot,
Expand All @@ -17,9 +23,19 @@ impl Download for Bot {

fn download_file<'dst>(
&self,
path: &str,
path: &'dst str,
destination: &'dst mut (dyn AsyncWrite + Unpin + Send),
) -> Self::Fut<'dst> {
let is_localhost = match &self.api_url.host_str() {
Some(host) => ["localhost", "127.0.0.1"].contains(host),
None => false,
};
// If path is absolute and api_url contains localhost, it is pretty clear there
// is a local TBA server with --local option, we can just copy the file
if is_localhost && Path::new(&path).is_absolute() {
return copy_file(path, destination).boxed();
}

net::download_file(
&self.client,
reqwest::Url::clone(&*self.api_url),
Expand All @@ -45,3 +61,23 @@ impl Download for Bot {
.boxed()
}
}

async fn copy_file<'o, D>(path: &'o str, dst: &'o mut D) -> Result<(), DownloadError>
where
D: ?Sized + AsyncWrite + Unpin,
{
let mut src_file = File::open(path).await?;

let mut buffer = [0; 1024];
loop {
let n = src_file.read(&mut buffer).await?;
if n == 0 {
break;
}

dst.write_all(&buffer[..n]).await?;
}

dst.flush().await?;
Ok(())
}
2 changes: 1 addition & 1 deletion crates/teloxide-core/src/local_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ macro_rules! download_forward {

fn download_file<'dst>(
&self,
path: &str,
path: &'dst str,
destination: &'dst mut (dyn tokio::io::AsyncWrite
+ core::marker::Unpin
+ core::marker::Send),
Expand Down
5 changes: 4 additions & 1 deletion crates/teloxide-core/src/net/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub trait Download {
///
/// `path` can be obtained from [`GetFile`].
///
/// If the bot uses a [local bot api](https://github.com/tdlib/telegram-bot-api), this function
/// just copies the file into `destination`.
///
/// To download as a stream of chunks, see [`download_file_stream`].
///
/// ## Examples
Expand All @@ -54,7 +57,7 @@ pub trait Download {
/// [`download_file_stream`]: Self::download_file_stream
fn download_file<'dst>(
&self,
path: &str,
path: &'dst str,
destination: &'dst mut (dyn AsyncWrite + Unpin + Send),
) -> Self::Fut<'dst>;

Expand Down
Loading