mirror of
https://github.com/flibusta-apps/telegram_files_server.git
synced 2025-12-06 04:25:38 +01:00
@@ -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,37 +56,39 @@ 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,
|
||||
@@ -107,7 +107,7 @@ impl BotDownloader {
|
||||
stream
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
|
||||
.into_async_read()
|
||||
.compat()
|
||||
.compat(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user