mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Update download error processing
This commit is contained in:
@@ -24,7 +24,7 @@ use crate::{
|
||||
services::{
|
||||
batch_downloader::{create_task, get_task, CreateTaskData, Task, TaskObjectType, TaskStatus},
|
||||
book_cache::{
|
||||
download_file, download_file_by_link, get_cached_message, get_download_link,
|
||||
download_file, download_file_by_link, get_cached_message,
|
||||
types::{CachedMessage, DownloadFile},
|
||||
},
|
||||
book_library::{
|
||||
@@ -85,20 +85,27 @@ async fn send_cached_message(
|
||||
need_delete_message: bool,
|
||||
cache: BotCache,
|
||||
) -> BotHandlerInternal {
|
||||
if let Ok(v) = get_cached_message(&download_data, cache).await {
|
||||
if _send_cached(&message, &bot, v).await.is_ok() {
|
||||
if need_delete_message {
|
||||
bot.delete_message(message.chat.id, message.id).await?;
|
||||
}
|
||||
'cached: {
|
||||
if let Ok(v) = get_cached_message(&download_data, cache).await {
|
||||
let cached = match v {
|
||||
Some(v) => v,
|
||||
None => break 'cached,
|
||||
};
|
||||
|
||||
match send_donation_notification(bot.clone(), message).await {
|
||||
Ok(_) => (),
|
||||
Err(err) => log::error!("{:?}", err),
|
||||
}
|
||||
if _send_cached(&message, &bot, cached).await.is_ok() {
|
||||
if need_delete_message {
|
||||
bot.delete_message(message.chat.id, message.id).await?;
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
match send_donation_notification(bot.clone(), message).await {
|
||||
Ok(_) => (),
|
||||
Err(err) => log::error!("{:?}", err),
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
send_with_download_from_channel(message, bot, download_data, need_delete_message).await?;
|
||||
|
||||
@@ -140,6 +147,7 @@ async fn _send_downloaded_file(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
async fn send_with_download_from_channel(
|
||||
message: Message,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
@@ -148,14 +156,16 @@ async fn send_with_download_from_channel(
|
||||
) -> BotHandlerInternal {
|
||||
match download_file(&download_data).await {
|
||||
Ok(v) => {
|
||||
if _send_downloaded_file(&message, bot.clone(), v)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
send_download_link(message.clone(), bot.clone(), download_data).await?;
|
||||
return Ok(());
|
||||
let download_file = match v {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
log::error!("File not found");
|
||||
return Ok(());
|
||||
},
|
||||
};
|
||||
|
||||
_send_downloaded_file(&message, bot.clone(), download_file).await?;
|
||||
|
||||
if need_delete_message {
|
||||
bot.delete_message(message.chat.id, message.id).await?;
|
||||
}
|
||||
@@ -166,36 +176,6 @@ 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 = match get_download_link(&download_data).await {
|
||||
Ok(v) => v,
|
||||
Err(err) => {
|
||||
log::error!("{:?}", err);
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
|
||||
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![],
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn download_handler(
|
||||
message: Message,
|
||||
@@ -424,7 +404,13 @@ async fn wait_archive(
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(v) => v,
|
||||
Ok(v) => match v {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
send_error_message(bot, message.chat.id, message.id).await;
|
||||
return Ok(());
|
||||
},
|
||||
},
|
||||
Err(err) => {
|
||||
send_error_message(bot, message.chat.id, message.id).await;
|
||||
log::error!("{:?}", err);
|
||||
|
||||
@@ -1,30 +1,17 @@
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
use reqwest::StatusCode;
|
||||
use std::fmt;
|
||||
|
||||
use crate::{bots::approved_bot::modules::download::callback_data::DownloadQueryData, bots_manager::BotCache, config};
|
||||
|
||||
use self::types::{CachedMessage, DownloadFile, DownloadLink};
|
||||
use self::types::{CachedMessage, DownloadFile};
|
||||
|
||||
pub mod types;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct DownloadError {
|
||||
status_code: StatusCode,
|
||||
}
|
||||
|
||||
impl fmt::Display for DownloadError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Status code is {0}", self.status_code)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for DownloadError {}
|
||||
|
||||
pub async fn get_cached_message(
|
||||
download_data: &DownloadQueryData,
|
||||
bot_cache: BotCache,
|
||||
) -> Result<CachedMessage, Box<dyn std::error::Error + Send + Sync>> {
|
||||
) -> Result<Option<CachedMessage>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let DownloadQueryData::DownloadData {
|
||||
book_id: id,
|
||||
file_type: format,
|
||||
@@ -43,46 +30,17 @@ pub async fn get_cached_message(
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
if response.status() != StatusCode::OK {
|
||||
return Err(Box::new(DownloadError {
|
||||
status_code: response.status(),
|
||||
}));
|
||||
if response.status() == StatusCode::NO_CONTENT {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
Ok(response.json::<CachedMessage>().await?)
|
||||
Ok(Some(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(
|
||||
download_data: &DownloadQueryData,
|
||||
) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> {
|
||||
) -> Result<Option<DownloadFile>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let DownloadQueryData::DownloadData {
|
||||
book_id: id,
|
||||
file_type: format,
|
||||
@@ -98,10 +56,8 @@ pub async fn download_file(
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
if response.status() != StatusCode::OK {
|
||||
return Err(Box::new(DownloadError {
|
||||
status_code: response.status(),
|
||||
}));
|
||||
if response.status() == StatusCode::NO_CONTENT {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let headers = response.headers();
|
||||
@@ -124,17 +80,17 @@ pub async fn download_file(
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
Ok(DownloadFile {
|
||||
Ok(Some(DownloadFile {
|
||||
response,
|
||||
filename,
|
||||
caption,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn download_file_by_link(
|
||||
filename: String,
|
||||
link: String,
|
||||
) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> {
|
||||
) -> Result<Option<DownloadFile>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let response = reqwest::Client::new()
|
||||
.get(link)
|
||||
.send()
|
||||
@@ -142,14 +98,12 @@ pub async fn download_file_by_link(
|
||||
.error_for_status()?;
|
||||
|
||||
if response.status() != StatusCode::OK {
|
||||
return Err(Box::new(DownloadError {
|
||||
status_code: response.status(),
|
||||
}));
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
Ok(DownloadFile {
|
||||
Ok(Some(DownloadFile {
|
||||
response,
|
||||
filename,
|
||||
caption: "".to_string(),
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user