Add sending download link

This commit is contained in:
2023-08-15 22:13:06 +02:00
parent 8d0fc244ad
commit 301d878b31
3 changed files with 67 additions and 3 deletions

View File

@@ -21,7 +21,7 @@ use crate::{
services::{ services::{
book_cache::{ book_cache::{
download_file, get_cached_message, 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}, 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}, 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 { ) -> BotHandlerInternal {
match download_file(&download_data).await { match download_file(&download_data).await {
Ok(v) => { 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 { if need_delete_message {
bot.delete_message(message.chat.id, message.id).await?; 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<Throttle<Bot>>,
download_data: DownloadQueryData,
) -> BotHandlerInternal {
let link_data = get_download_link(&download_data).await?;
bot
.edit_message_text(
message.chat.id,
message.id,
format!(
"Файл не может быть загружен в чат! \n \
Вы можете скачать его <a href=\"{}\">по ссылке</a> (работает 3 часа)",
link_data.link
)
)
.parse_mode(ParseMode::Html)
.reply_markup(InlineKeyboardMarkup {
inline_keyboard: vec![],
})
.send()
.await?;
Ok(())
}
async fn download_handler( async fn download_handler(
message: Message, message: Message,
bot: CacheMe<Throttle<Bot>>, bot: CacheMe<Throttle<Bot>>,

View File

@@ -4,7 +4,7 @@ use std::fmt;
use crate::{config, bots::approved_bot::modules::download::DownloadQueryData}; use crate::{config, bots::approved_bot::modules::download::DownloadQueryData};
use self::types::{CachedMessage, DownloadFile}; use self::types::{CachedMessage, DownloadFile, DownloadLink};
pub mod types; pub mod types;
@@ -46,6 +46,31 @@ pub async fn get_cached_message(
Ok(response.json::<CachedMessage>().await?) Ok(response.json::<CachedMessage>().await?)
} }
pub async fn get_download_link(
download_data: &DownloadQueryData
) -> Result<DownloadLink, Box<dyn std::error::Error + Send + Sync>> {
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::<DownloadLink>().await?)
}
pub async fn download_file( pub async fn download_file(
download_data: &DownloadQueryData, download_data: &DownloadQueryData,
) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> { ) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> {

View File

@@ -12,3 +12,8 @@ pub struct DownloadFile {
pub filename: String, pub filename: String,
pub caption: String, pub caption: String,
} }
#[derive(Deserialize)]
pub struct DownloadLink {
pub link: String
}