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},
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,38 +56,40 @@ pub async fn upload_file(
pub async fn download_file(chat_id: i64, message_id: i32) -> Option<BotDownloader> {
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;
.await
{
Ok(v) => v,
Err(err) => {
log::error!("Error: {}", err);
return None;
}
};
match result {
Ok(message) => {
let file_id = match message.document() {
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));
}
Err(_) => None,
}
}
pub struct BotDownloader {
bot: Bot,
file_path: String,
@@ -107,7 +107,7 @@ impl BotDownloader {
stream
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
.into_async_read()
.compat()
.compat(),
)
}
}

View File

@@ -88,8 +88,7 @@ async fn upload(data: TypedMultipart<UploadFileRequest>) -> impl IntoResponse {
result.unwrap()
}
async fn download(Path(chat_id): Path<i64>, Path(message_id): Path<i32>) -> 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 {