Skip to content

Commit

Permalink
Put code using fs APIs under their own feature
Browse files Browse the repository at this point in the history
  • Loading branch information
er-azh authored and Lonami committed Dec 21, 2024
1 parent 173a544 commit f9be5f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/grammers-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }
Expand Down
26 changes: 20 additions & 6 deletions lib/grammers-client/src/client/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -210,6 +220,7 @@ impl Client {
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "fs")]
pub async fn download_media<P: AsRef<Path>>(
&self,
downloadable: &Downloadable,
Expand Down Expand Up @@ -253,6 +264,7 @@ impl Client {
Client::load(path, &mut download).await
}

#[cfg(feature = "fs")]
async fn load<P: AsRef<Path>>(path: P, download: &mut DownloadStream) -> Result<(), io::Error> {
let mut file = fs::File::create(path).await?;
while let Some(chunk) = download
Expand All @@ -267,6 +279,7 @@ impl Client {
}

/// Downloads a `Document` to specified path using multiple connections
#[cfg(feature = "fs")]
async fn download_media_concurrent<P: AsRef<Path>>(
&self,
media: &Media,
Expand Down Expand Up @@ -520,6 +533,7 @@ impl Client {
/// ```
///
/// [`InputMessage`]: crate::InputMessage
#[cfg(feature = "fs")]
pub async fn upload_file<P: AsRef<Path>>(&self, path: P) -> Result<Uploaded, io::Error> {
let path = path.as_ref();

Expand Down
11 changes: 8 additions & 3 deletions lib/grammers-client/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -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<P: AsRef<Path>>(&self, path: P) -> Result<bool, io::Error> {
// TODO probably encode failed download in error
if let Some(media) = self.media() {
Expand Down

0 comments on commit f9be5f7

Please sign in to comment.