mirror of
https://github.com/flibusta-apps/telegram_files_server.git
synced 2026-03-03 07:00:54 +01:00
Cleanup and remove unused downloader
This commit is contained in:
@@ -28,7 +28,7 @@ pub struct MessageInfo {
|
|||||||
|
|
||||||
pub static TEMP_FILES_CACHE: Lazy<Cache<i32, MessageId>> = Lazy::new(|| {
|
pub static TEMP_FILES_CACHE: Lazy<Cache<i32, MessageId>> = Lazy::new(|| {
|
||||||
Cache::builder()
|
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)
|
.max_capacity(4098)
|
||||||
.async_eviction_listener(|_data_id, message_id, _cause| {
|
.async_eviction_listener(|_data_id, message_id, _cause| {
|
||||||
Box::pin(async move {
|
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 bot = ROUND_ROBIN_BOT.get_bot();
|
||||||
|
|
||||||
let forwarded_message = match 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,
|
Ok(v) => v,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!("Error: {}", err);
|
log::error!("Error: {}", err);
|
||||||
return None;
|
return Err(Box::new(err));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let file_id = match forwarded_message.document() {
|
let file_id = forwarded_message.document().unwrap().file.id.clone();
|
||||||
Some(v) => v.file.id.clone(),
|
|
||||||
None => {
|
|
||||||
log::error!("Document not found!");
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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 {
|
let path = match bot.get_file(file_id.clone()).await {
|
||||||
Ok(v) => v.path,
|
Ok(v) => v.path,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!("Error: {}", err);
|
log::error!("Error: {}", err);
|
||||||
return None;
|
return Err(Box::new(err));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
log::info!("File path: {}", path);
|
Ok(File::open(path).await?)
|
||||||
|
|
||||||
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?)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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 {
|
async fn download(Path((chat_id, message_id)): Path<(i64, i32)>) -> impl IntoResponse {
|
||||||
let downloader = match download_file(chat_id, message_id).await {
|
let file = 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 {
|
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!("{}", err);
|
log::error!("{}", err);
|
||||||
|
|||||||
Reference in New Issue
Block a user