mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Fix download from cache server
This commit is contained in:
@@ -6,7 +6,7 @@ use tokio_util::compat::FuturesAsyncReadCompatExt;
|
|||||||
use crate::{
|
use crate::{
|
||||||
bots::{
|
bots::{
|
||||||
approved_bot::services::book_cache::{
|
approved_bot::services::book_cache::{
|
||||||
clear_book_cache, download_file, get_cached_message,
|
download_file, get_cached_message,
|
||||||
types::{CachedMessage, DownloadFile},
|
types::{CachedMessage, DownloadFile},
|
||||||
},
|
},
|
||||||
BotHandlerInternal,
|
BotHandlerInternal,
|
||||||
@@ -46,8 +46,8 @@ impl CommandParse<Self> for DownloadData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn _send_cached(
|
async fn _send_cached(
|
||||||
message: Message,
|
message: &Message,
|
||||||
bot: AutoSend<Bot>,
|
bot: &AutoSend<Bot>,
|
||||||
cached_message: CachedMessage,
|
cached_message: CachedMessage,
|
||||||
) -> BotHandlerInternal {
|
) -> BotHandlerInternal {
|
||||||
match bot
|
match bot
|
||||||
@@ -69,42 +69,33 @@ async fn send_cached_message(
|
|||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
download_data: DownloadData,
|
download_data: DownloadData,
|
||||||
) -> BotHandlerInternal {
|
) -> BotHandlerInternal {
|
||||||
let cached_message = get_cached_message(&download_data).await;
|
match get_cached_message(&download_data).await {
|
||||||
match cached_message {
|
Ok(v) => match _send_cached(&message, &bot, v).await {
|
||||||
Ok(v) => match _send_cached(message.clone(), bot.clone(), v).await {
|
|
||||||
Ok(_) => return Ok(()),
|
Ok(_) => return Ok(()),
|
||||||
Err(err) => log::info!("{:?}", err),
|
Err(err) => log::warn!("{:?}", err),
|
||||||
},
|
},
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
};
|
};
|
||||||
|
|
||||||
match clear_book_cache(&download_data).await {
|
match get_cached_message(&download_data).await {
|
||||||
Ok(_) => (),
|
Ok(v) => match _send_cached(&message, &bot, v).await {
|
||||||
Err(err) => log::error!("{:?}", err),
|
Ok(v_2) => Ok(v_2),
|
||||||
};
|
Err(err) => Err(err),
|
||||||
|
},
|
||||||
let cached_message = get_cached_message(&download_data).await;
|
Err(err) => Err(err),
|
||||||
match cached_message {
|
|
||||||
Ok(v) => _send_cached(message, bot, v).await,
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_with_download_from_channel(
|
async fn _send_downloaded_file(
|
||||||
message: Message,
|
message: &Message,
|
||||||
bot: AutoSend<Bot>,
|
bot: &AutoSend<Bot>,
|
||||||
download_data: DownloadData,
|
downloaded_data: DownloadFile
|
||||||
) -> BotHandlerInternal {
|
) -> BotHandlerInternal {
|
||||||
let downloaded_file = match download_file(&download_data).await {
|
|
||||||
Ok(v) => v,
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
};
|
|
||||||
|
|
||||||
let DownloadFile {
|
let DownloadFile {
|
||||||
response,
|
response,
|
||||||
filename,
|
filename,
|
||||||
caption,
|
caption,
|
||||||
} = downloaded_file;
|
} = downloaded_data;
|
||||||
|
|
||||||
let data = response
|
let data = response
|
||||||
.bytes_stream()
|
.bytes_stream()
|
||||||
@@ -125,6 +116,28 @@ async fn send_with_download_from_channel(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn send_with_download_from_channel(
|
||||||
|
message: Message,
|
||||||
|
bot: AutoSend<Bot>,
|
||||||
|
download_data: DownloadData,
|
||||||
|
) -> BotHandlerInternal {
|
||||||
|
match download_file(&download_data).await {
|
||||||
|
Ok(v) => match _send_downloaded_file(&message, &bot, v).await {
|
||||||
|
Ok(_) => return Ok(()),
|
||||||
|
Err(err) => log::warn!("{:?}", err),
|
||||||
|
},
|
||||||
|
Err(err) => return Err(err),
|
||||||
|
};
|
||||||
|
|
||||||
|
match download_file(&download_data).await {
|
||||||
|
Ok(v) => match _send_downloaded_file(&message, &bot, v).await {
|
||||||
|
Ok(v_2) => Ok(v_2),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
},
|
||||||
|
Err(err) => Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn download_handler(
|
async fn download_handler(
|
||||||
message: Message,
|
message: Message,
|
||||||
bot: AutoSend<Bot>,
|
bot: AutoSend<Bot>,
|
||||||
|
|||||||
@@ -51,32 +51,6 @@ pub async fn get_cached_message(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn clear_book_cache(
|
|
||||||
download_data: &DownloadData,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
||||||
let DownloadData { format, id } = download_data;
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let response = client
|
|
||||||
.delete(format!(
|
|
||||||
"{}/api/v1/{id}/{format}",
|
|
||||||
&config::CONFIG.cache_server_url
|
|
||||||
))
|
|
||||||
.header("Authorization", &config::CONFIG.cache_server_api_key)
|
|
||||||
.send()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let response = match response {
|
|
||||||
Ok(v) => v,
|
|
||||||
Err(err) => return Err(Box::new(err)),
|
|
||||||
};
|
|
||||||
|
|
||||||
match response.error_for_status() {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(err) => return Err(Box::new(err)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn download_file(
|
pub async fn download_file(
|
||||||
download_data: &DownloadData,
|
download_data: &DownloadData,
|
||||||
) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
@@ -97,38 +71,41 @@ pub async fn download_file(
|
|||||||
Err(err) => return Err(Box::new(err)),
|
Err(err) => return Err(Box::new(err)),
|
||||||
};
|
};
|
||||||
|
|
||||||
match response.error_for_status() {
|
let response = match response.error_for_status() {
|
||||||
Ok(response) => {
|
Ok(response) => response,
|
||||||
if response.status() != StatusCode::OK {
|
|
||||||
return Err(Box::new(DownloadError {
|
|
||||||
status_code: response.status()
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
let headers = response.headers();
|
|
||||||
let filename = headers
|
|
||||||
.get("content-disposition")
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap()
|
|
||||||
.replace('"', "")
|
|
||||||
.split("filename=")
|
|
||||||
.collect::<Vec<&str>>()
|
|
||||||
.get(1)
|
|
||||||
.unwrap()
|
|
||||||
.to_string();
|
|
||||||
let caption = std::str::from_utf8(
|
|
||||||
&base64::decode(headers.get("x-caption-b64").unwrap()).unwrap(),
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
Ok(DownloadFile {
|
|
||||||
response,
|
|
||||||
filename,
|
|
||||||
caption,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Err(err) => return Err(Box::new(err)),
|
Err(err) => return Err(Box::new(err)),
|
||||||
}
|
};
|
||||||
|
|
||||||
|
if response.status() != StatusCode::OK {
|
||||||
|
return Err(Box::new(DownloadError {
|
||||||
|
status_code: response.status()
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
let headers = response.headers();
|
||||||
|
|
||||||
|
log::debug!("Download headers: {:?}", headers);
|
||||||
|
|
||||||
|
let filename = headers
|
||||||
|
.get("content-disposition")
|
||||||
|
.unwrap()
|
||||||
|
.to_str()
|
||||||
|
.unwrap()
|
||||||
|
.replace('"', "")
|
||||||
|
.split("filename=")
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.get(1)
|
||||||
|
.unwrap()
|
||||||
|
.to_string();
|
||||||
|
let caption = std::str::from_utf8(
|
||||||
|
&base64::decode(headers.get("x-caption-b64").unwrap()).unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Ok(DownloadFile {
|
||||||
|
response,
|
||||||
|
filename,
|
||||||
|
caption,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user