Merge pull request #31 from flibusta-apps/rewrite-to-rust

Fix download
This commit is contained in:
2024-05-06 23:10:21 +02:00
committed by GitHub
2 changed files with 30 additions and 31 deletions

View File

@@ -9,13 +9,12 @@ use teloxide::{
types::{ChatId, InputFile, MessageId}, types::{ChatId, InputFile, MessageId},
Bot, Bot,
}; };
use tracing::log;
use tokio::io::AsyncRead; use tokio::io::AsyncRead;
use tokio_util::compat::FuturesAsyncReadCompatExt; use tokio_util::compat::FuturesAsyncReadCompatExt;
use tracing::log;
use crate::config::CONFIG;
use super::bot::ROUND_ROBIN_BOT; use super::bot::ROUND_ROBIN_BOT;
use crate::config::CONFIG;
#[derive(Serialize)] #[derive(Serialize)]
pub struct UploadedFile { pub struct UploadedFile {
@@ -29,7 +28,6 @@ pub struct MessageInfo {
pub message_id: i32, pub message_id: i32,
} }
pub async fn upload_file( pub async fn upload_file(
file: Bytes, file: Bytes,
filename: String, filename: String,
@@ -58,37 +56,39 @@ pub async fn upload_file(
pub async fn download_file(chat_id: i64, message_id: i32) -> Option<BotDownloader> { pub async fn download_file(chat_id: i64, message_id: i32) -> Option<BotDownloader> {
let bot = ROUND_ROBIN_BOT.get_bot(); let bot = ROUND_ROBIN_BOT.get_bot();
let result = bot let forwarded_message = match bot
.forward_message( .forward_message(
ChatId(CONFIG.telegram_temp_chat_id), ChatId(CONFIG.telegram_temp_chat_id),
ChatId(chat_id), ChatId(chat_id),
MessageId(message_id), MessageId(message_id),
) )
.await; .await
{
Ok(v) => v,
Err(err) => {
log::error!("Error: {}", err);
return None;
}
};
match result { let file_id = match forwarded_message.document() {
Ok(message) => {
let file_id = match message.document() {
Some(v) => v.file.id.clone(), Some(v) => v.file.id.clone(),
None => { None => {
log::error!("Document not found!"); log::error!("Document not found!");
return None; return None;
} }
}; };
let path = match bot.get_file(file_id.clone()).await { let path = match bot.get_file(file_id.clone()).await {
Ok(v) => v.path, Ok(v) => v.path,
Err(err) => { Err(err) => {
log::error!("Error: {}", err); log::error!("Error: {}", err);
return None; return None;
}, }
}; };
return Some(BotDownloader::new(bot, path)); return Some(BotDownloader::new(bot, path));
} }
Err(_) => None,
}
}
pub struct BotDownloader { pub struct BotDownloader {
bot: Bot, bot: Bot,
@@ -107,7 +107,7 @@ impl BotDownloader {
stream stream
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
.into_async_read() .into_async_read()
.compat() .compat(),
) )
} }
} }

View File

@@ -88,8 +88,7 @@ async fn upload(data: TypedMultipart<UploadFileRequest>) -> impl IntoResponse {
result.unwrap() result.unwrap()
} }
async fn download(Path((chat_id, message_id)): Path<(i64, i32)>) -> impl IntoResponse {
async fn download(Path(chat_id): Path<i64>, Path(message_id): Path<i32>) -> impl IntoResponse {
let downloader = download_file(chat_id, message_id).await; let downloader = download_file(chat_id, message_id).await;
let data = match downloader { let data = match downloader {