Cleanup and remove unused downloader

This commit is contained in:
Шатунов Антон
2024-05-07 02:25:16 +03:00
parent e18d9555a6
commit b705b0cb30
2 changed files with 9 additions and 30 deletions

View File

@@ -28,7 +28,7 @@ pub struct MessageInfo {
pub static TEMP_FILES_CACHE: Lazy<Cache<i32, MessageId>> = Lazy::new(|| {
Cache::builder()
.time_to_idle(std::time::Duration::from_secs(5 * 60 * 60))
.time_to_idle(std::time::Duration::from_secs(16))
.max_capacity(4098)
.async_eviction_listener(|_data_id, message_id, _cause| {
Box::pin(async move {
@@ -69,7 +69,7 @@ pub async fn upload_file(
}
}
pub async fn download_file(chat_id: i64, message_id: i32) -> Option<BotDownloader> {
pub async fn download_file(chat_id: i64, message_id: i32) -> Result<File, Box<dyn Error>> {
let bot = ROUND_ROBIN_BOT.get_bot();
let forwarded_message = match bot
@@ -83,37 +83,21 @@ pub async fn download_file(chat_id: i64, message_id: i32) -> Option<BotDownloade
Ok(v) => v,
Err(err) => {
log::error!("Error: {}", err);
return None;
return Err(Box::new(err));
}
};
let file_id = match forwarded_message.document() {
Some(v) => v.file.id.clone(),
None => {
log::error!("Document not found!");
return None;
}
};
let file_id = forwarded_message.document().unwrap().file.id.clone();
TEMP_FILES_CACHE.insert(message_id, forwarded_message.id.clone()).await;
TEMP_FILES_CACHE.insert(message_id, forwarded_message.id).await;
let path = match bot.get_file(file_id.clone()).await {
Ok(v) => v.path,
Err(err) => {
log::error!("Error: {}", err);
return None;
return Err(Box::new(err));
}
};
log::info!("File path: {}", path);
return Some(BotDownloader(path));
}
pub struct BotDownloader(String);
impl BotDownloader {
pub async fn get_file(self) -> Result<File, Box<dyn Error>> {
Ok(File::open(self.0).await?)
}
}
Ok(File::open(path).await?)
}

View File

@@ -90,12 +90,7 @@ async fn upload(data: TypedMultipart<UploadFileRequest>) -> impl IntoResponse {
}
async fn download(Path((chat_id, message_id)): Path<(i64, i32)>) -> impl IntoResponse {
let downloader = match download_file(chat_id, message_id).await {
Some(v) => v,
None => return StatusCode::BAD_REQUEST.into_response()
};
let file = match downloader.get_file().await {
let file = match download_file(chat_id, message_id).await {
Ok(v) => v,
Err(err) => {
log::error!("{}", err);