Skip to main content

teloxide_core_ng/bot/
download.rs

1use bytes::Bytes;
2use futures::{FutureExt, StreamExt, future::BoxFuture, stream::BoxStream};
3use tokio::io::AsyncWrite;
4
5use crate::{
6    DownloadError,
7    bot::Bot,
8    net::{self, Download},
9};
10
11impl Download for Bot {
12    type Err<'dst> = DownloadError;
13
14    // I would like to unbox this, but my coworkers will kill me if they'll see yet
15    // another hand written `Future`. (waffle)
16    type Fut<'dst> = BoxFuture<'dst, Result<(), Self::Err<'dst>>>;
17
18    fn download_file<'dst>(
19        &self,
20        path: &str,
21        destination: &'dst mut (dyn AsyncWrite + Unpin + Send),
22    ) -> Self::Fut<'dst> {
23        net::download_file(
24            &self.client,
25            reqwest::Url::clone(&*self.api_url),
26            &self.token,
27            path,
28            destination,
29        )
30        .boxed()
31    }
32
33    type StreamErr = reqwest::Error;
34
35    type Stream = BoxStream<'static, Result<Bytes, Self::StreamErr>>;
36
37    fn download_file_stream(&self, path: &str) -> Self::Stream {
38        net::download_file_stream(
39            &self.client,
40            reqwest::Url::clone(&*self.api_url),
41            &self.token,
42            path,
43        )
44        .map(|res| res.map_err(crate::errors::hide_token))
45        .boxed()
46    }
47}