diff --git a/lib/grammers-client/Cargo.toml b/lib/grammers-client/Cargo.toml index b1459306..2ae6ec41 100644 --- a/lib/grammers-client/Cargo.toml +++ b/lib/grammers-client/Cargo.toml @@ -19,6 +19,8 @@ html = ["html5ever"] proxy = ["grammers-mtsender/proxy"] parse_invite_link = ["url"] serde = ["grammers-tl-types/impl-serde"] +fs = ["tokio/fs"] +default = ["fs"] [dependencies] chrono = "0.4.38" @@ -39,7 +41,6 @@ os_info = { version = "3.8.2", default-features = false } pin-project-lite = "0.2" pulldown-cmark = { version = "0.12.1", default-features = false, optional = true } tokio = { version = "1.40.0", default-features = false, features = [ - "fs", "rt", ] } url = { version = "2.5.2", optional = true } diff --git a/lib/grammers-client/src/client/files.rs b/lib/grammers-client/src/client/files.rs index e423c48f..6226aa8f 100644 --- a/lib/grammers-client/src/client/files.rs +++ b/lib/grammers-client/src/client/files.rs @@ -6,27 +6,37 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. use std::future::Future; +use std::sync::Arc; use std::task::Poll; -use std::{io::SeekFrom, path::Path, sync::Arc}; use futures::{ stream::{FuturesUnordered, StreamExt}, - Stream, TryStreamExt, + Stream, }; -use tokio::sync::mpsc::unbounded_channel; use tokio::{ - fs, - io::{self, AsyncRead, AsyncReadExt, AsyncSeekExt, AsyncWriteExt}, + io::{self, AsyncRead, AsyncReadExt}, sync::Mutex as AsyncMutex, }; use grammers_mtsender::InvocationError; use grammers_tl_types as tl; -use crate::types::{photo_sizes::PhotoSize, Downloadable, Media, Uploaded}; +use crate::types::{photo_sizes::PhotoSize, Downloadable, Uploaded}; use crate::utils::{generate_random_id, poll_future_ready}; use crate::Client; +#[cfg(feature = "fs")] +use { + crate::types::Media, + futures::TryStreamExt, + std::{io::SeekFrom, path::Path}, + tokio::{ + fs, + io::{AsyncSeekExt, AsyncWriteExt}, + sync::mpsc::unbounded_channel, + }, +}; + pub const MIN_CHUNK_SIZE: i32 = 4 * 1024; pub const MAX_CHUNK_SIZE: i32 = 512 * 1024; const FILE_MIGRATE_ERROR: i32 = 303; @@ -210,6 +220,7 @@ impl Client { /// # Ok(()) /// # } /// ``` + #[cfg(feature = "fs")] pub async fn download_media>( &self, downloadable: &Downloadable, @@ -253,6 +264,7 @@ impl Client { Client::load(path, &mut download).await } + #[cfg(feature = "fs")] async fn load>(path: P, download: &mut DownloadStream) -> Result<(), io::Error> { let mut file = fs::File::create(path).await?; while let Some(chunk) = download @@ -267,6 +279,7 @@ impl Client { } /// Downloads a `Document` to specified path using multiple connections + #[cfg(feature = "fs")] async fn download_media_concurrent>( &self, media: &Media, @@ -520,6 +533,7 @@ impl Client { /// ``` /// /// [`InputMessage`]: crate::InputMessage + #[cfg(feature = "fs")] pub async fn upload_file>(&self, path: P) -> Result { let path = path.as_ref(); diff --git a/lib/grammers-client/src/types/message.rs b/lib/grammers-client/src/types/message.rs index 55aec32e..a93d9cce 100644 --- a/lib/grammers-client/src/types/message.rs +++ b/lib/grammers-client/src/types/message.rs @@ -8,7 +8,7 @@ #[cfg(any(feature = "markdown", feature = "html"))] use crate::parsers; use crate::types::reactions::InputReactions; -use crate::types::{Downloadable, InputMessage, Media, Photo}; +use crate::types::{InputMessage, Media, Photo}; use crate::ChatMap; use crate::{types, Client}; use crate::{utils, InputMedia}; @@ -17,11 +17,15 @@ use grammers_mtsender::InvocationError; use grammers_session::PackedChat; use grammers_tl_types as tl; use std::fmt; -use std::io; -use std::path::Path; use std::sync::Arc; use types::Chat; +#[cfg(feature = "fs")] +use { + crate::types::Downloadable, + std::{io, path::Path}, +}; + pub(crate) const EMPTY_MESSAGE: tl::types::Message = tl::types::Message { out: false, mentioned: false, @@ -674,6 +678,7 @@ impl Message { /// Returns `true` if there was media to download, or `false` otherwise. /// /// Shorthand for `Client::download_media`. + #[cfg(feature = "fs")] pub async fn download_media>(&self, path: P) -> Result { // TODO probably encode failed download in error if let Some(media) = self.media() {