diff --git a/src/bots/approved_bot/modules/download.rs b/src/bots/approved_bot/modules/download.rs index 3d48899..ff487f1 100644 --- a/src/bots/approved_bot/modules/download.rs +++ b/src/bots/approved_bot/modules/download.rs @@ -21,7 +21,7 @@ use crate::{ services::{ book_cache::{ download_file, get_cached_message, - types::{CachedMessage, DownloadFile}, download_file_by_link, + types::{CachedMessage, DownloadFile}, download_file_by_link, get_download_link, }, book_library::{get_book, get_author_books_available_types, get_translator_books_available_types, get_sequence_books_available_types}, donation_notificatioins::send_donation_notification, user_settings::get_user_or_default_lang_codes, batch_downloader::{TaskObjectType, CreateTaskData}, @@ -311,7 +311,14 @@ async fn send_with_download_from_channel( ) -> BotHandlerInternal { match download_file(&download_data).await { Ok(v) => { - _send_downloaded_file(&message, bot.clone(), v).await?; + match _send_downloaded_file(&message, bot.clone(), v).await { + Ok(_) => (), + Err(err) => { + log::error!("{:?}", err); + + send_download_link(message.clone(), bot.clone(), download_data).await?; + }, + }; if need_delete_message { bot.delete_message(message.chat.id, message.id).await?; @@ -323,6 +330,33 @@ async fn send_with_download_from_channel( } } +async fn send_download_link( + message: Message, + bot: CacheMe>, + download_data: DownloadQueryData, +) -> BotHandlerInternal { + let link_data = get_download_link(&download_data).await?; + + bot + .edit_message_text( + message.chat.id, + message.id, + format!( + "Файл не может быть загружен в чат! \n \ + Вы можете скачать его по ссылке (работает 3 часа)", + link_data.link + ) + ) + .parse_mode(ParseMode::Html) + .reply_markup(InlineKeyboardMarkup { + inline_keyboard: vec![], + }) + .send() + .await?; + + Ok(()) +} + async fn download_handler( message: Message, bot: CacheMe>, diff --git a/src/bots/approved_bot/services/book_cache/mod.rs b/src/bots/approved_bot/services/book_cache/mod.rs index 03f8385..6034fac 100644 --- a/src/bots/approved_bot/services/book_cache/mod.rs +++ b/src/bots/approved_bot/services/book_cache/mod.rs @@ -4,7 +4,7 @@ use std::fmt; use crate::{config, bots::approved_bot::modules::download::DownloadQueryData}; -use self::types::{CachedMessage, DownloadFile}; +use self::types::{CachedMessage, DownloadFile, DownloadLink}; pub mod types; @@ -46,6 +46,31 @@ pub async fn get_cached_message( Ok(response.json::().await?) } +pub async fn get_download_link( + download_data: &DownloadQueryData +) -> Result> { + let DownloadQueryData::DownloadData { book_id: id, file_type: format } = download_data; + + let client = reqwest::Client::new(); + let response = client + .get(format!( + "{}/api/v1/link/{id}/{format}/", + &config::CONFIG.cache_server_url + )) + .header("Authorization", &config::CONFIG.cache_server_api_key) + .send() + .await? + .error_for_status()?; + + if response.status() != StatusCode::OK { + return Err(Box::new(DownloadError { + status_code: response.status(), + })); + }; + + Ok(response.json::().await?) +} + pub async fn download_file( download_data: &DownloadQueryData, ) -> Result> { diff --git a/src/bots/approved_bot/services/book_cache/types.rs b/src/bots/approved_bot/services/book_cache/types.rs index 66ef3e5..1140d7b 100644 --- a/src/bots/approved_bot/services/book_cache/types.rs +++ b/src/bots/approved_bot/services/book_cache/types.rs @@ -12,3 +12,8 @@ pub struct DownloadFile { pub filename: String, pub caption: String, } + +#[derive(Deserialize)] +pub struct DownloadLink { + pub link: String +}