From e75ecf427d2d824ff1934a7dc8e10aacb31a2f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A8=D0=B0=D1=82=D1=83=D0=BD=D0=BE=D0=B2=20=D0=90=D0=BD?= =?UTF-8?q?=D1=82=D0=BE=D0=BD?= Date: Tue, 7 May 2024 00:04:55 +0300 Subject: [PATCH] Fix download --- src/core/file_utils.rs | 62 ++++++++++++++++++++---------------------- src/core/views.rs | 3 +- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/core/file_utils.rs b/src/core/file_utils.rs index 53658be..515d3ef 100644 --- a/src/core/file_utils.rs +++ b/src/core/file_utils.rs @@ -9,13 +9,12 @@ use teloxide::{ types::{ChatId, InputFile, MessageId}, Bot, }; -use tracing::log; use tokio::io::AsyncRead; use tokio_util::compat::FuturesAsyncReadCompatExt; +use tracing::log; -use crate::config::CONFIG; use super::bot::ROUND_ROBIN_BOT; - +use crate::config::CONFIG; #[derive(Serialize)] pub struct UploadedFile { @@ -29,7 +28,6 @@ pub struct MessageInfo { pub message_id: i32, } - pub async fn upload_file( file: Bytes, filename: String, @@ -58,41 +56,39 @@ pub async fn upload_file( pub async fn download_file(chat_id: i64, message_id: i32) -> Option { let bot = ROUND_ROBIN_BOT.get_bot(); - let result = bot + let forwarded_message = match bot .forward_message( ChatId(CONFIG.telegram_temp_chat_id), ChatId(chat_id), MessageId(message_id), ) - .await; - - match result { - Ok(message) => { - if message.document() == None { - return None; - } - - let file_id = match message.document() { - Some(v) => v.file.id.clone(), - None => { - log::error!("Document not found!"); - return None; - } - }; - let path = match bot.get_file(file_id.clone()).await { - Ok(v) => v.path, - Err(err) => { - log::error!("Error: {}", err); - return None; - }, - }; - - return Some(BotDownloader::new(bot, path)); + .await + { + Ok(v) => v, + Err(err) => { + log::error!("Error: {}", err); + return None; } - Err(_) => None, - } -} + }; + let file_id = match forwarded_message.document() { + Some(v) => v.file.id.clone(), + None => { + log::error!("Document not found!"); + return None; + } + }; + + let path = match bot.get_file(file_id.clone()).await { + Ok(v) => v.path, + Err(err) => { + log::error!("Error: {}", err); + return None; + } + }; + + return Some(BotDownloader::new(bot, path)); +} pub struct BotDownloader { bot: Bot, @@ -111,7 +107,7 @@ impl BotDownloader { stream .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)) .into_async_read() - .compat() + .compat(), ) } } diff --git a/src/core/views.rs b/src/core/views.rs index 5d8e953..9100c88 100644 --- a/src/core/views.rs +++ b/src/core/views.rs @@ -88,8 +88,7 @@ async fn upload(data: TypedMultipart) -> impl IntoResponse { result.unwrap() } - -async fn download(Path(chat_id): Path, Path(message_id): Path) -> impl IntoResponse { +async fn download(Path((chat_id, message_id)): Path<(i64, i32)>) -> impl IntoResponse { let downloader = download_file(chat_id, message_id).await; let data = match downloader {