From 3c8fdb35eb3982645a1ca794f826fa4d6c4af70d Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Tue, 7 May 2024 19:29:40 +0200 Subject: [PATCH] Update download error processing --- src/bots/approved_bot/modules/download/mod.rs | 86 ++++++++----------- .../approved_bot/services/book_cache/mod.rs | 74 +++------------- 2 files changed, 50 insertions(+), 110 deletions(-) diff --git a/src/bots/approved_bot/modules/download/mod.rs b/src/bots/approved_bot/modules/download/mod.rs index 1298adc..a6e248a 100644 --- a/src/bots/approved_bot/modules/download/mod.rs +++ b/src/bots/approved_bot/modules/download/mod.rs @@ -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>, @@ -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>, - 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 \ - Вы можете скачать его по ссылке (работает 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); diff --git a/src/bots/approved_bot/services/book_cache/mod.rs b/src/bots/approved_bot/services/book_cache/mod.rs index 225225f..1807bf0 100644 --- a/src/bots/approved_bot/services/book_cache/mod.rs +++ b/src/bots/approved_bot/services/book_cache/mod.rs @@ -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> { +) -> Result, Box> { 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::().await?) + Ok(Some(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> { +) -> Result, Box> { 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> { +) -> Result, Box> { 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(), - }) + })) }