From e18d9555a623559c0541de6505026d944c2a019f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A8=D0=B0=D1=82=D1=83=D0=BD=D0=BE=D0=B2=20=D0=90=D0=BD?= =?UTF-8?q?=D1=82=D0=BE=D0=BD?= Date: Tue, 7 May 2024 02:13:17 +0300 Subject: [PATCH] Change download logic to just opening file Co-authored-by: Kurbanov Bulat --- Cargo.lock | 1 + Cargo.toml | 2 +- src/core/file_utils.rs | 30 ++++++------------------------ src/core/views.rs | 17 ++++++++++++----- 4 files changed, 20 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dfc6c99..0d4911f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2217,6 +2217,7 @@ dependencies = [ "libc", "mio", "num_cpus", + "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", diff --git a/Cargo.toml b/Cargo.toml index 54b494e..9866fa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"]} tower-http = { version = "0.5.2", features = ["trace"] } sentry-tracing = "0.32.3" -tokio = "1.37.0" +tokio = { version = "1.37.0", features = [ "full" ] } tokio-util = { version = "0.7.11", features = [ "full" ] } axum-prometheus = "0.6.1" diff --git a/src/core/file_utils.rs b/src/core/file_utils.rs index 8c8f3e6..a48fd9b 100644 --- a/src/core/file_utils.rs +++ b/src/core/file_utils.rs @@ -1,17 +1,13 @@ -use std::pin::Pin; +use std::error::Error; use axum::body::Bytes; -use futures::TryStreamExt; use once_cell::sync::Lazy; use serde::Serialize; use teloxide::{ - net::Download, requests::Requester, types::{ChatId, InputFile, MessageId, Recipient}, - Bot, }; -use tokio::io::AsyncRead; -use tokio_util::compat::FuturesAsyncReadCompatExt; +use tokio::fs::File; use tracing::log; use moka::future::Cache; @@ -111,27 +107,13 @@ pub async fn download_file(chat_id: i64, message_id: i32) -> Option Self { - Self { bot, file_path } - } - - pub fn get_async_read(self) -> Pin> { - let stream = self.bot.download_file_stream(&self.file_path); - - Box::pin( - stream - .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)) - .into_async_read() - .compat(), - ) + pub async fn get_file(self) -> Result> { + Ok(File::open(self.0).await?) } } diff --git a/src/core/views.rs b/src/core/views.rs index 005e688..2c2670f 100644 --- a/src/core/views.rs +++ b/src/core/views.rs @@ -12,6 +12,7 @@ use axum_typed_multipart::{TryFromMultipart, TypedMultipart}; use tokio_util::io::ReaderStream; use tower_http::trace::{self, TraceLayer}; use tracing::Level; +use tracing::log; use crate::config::CONFIG; @@ -89,14 +90,20 @@ async fn upload(data: TypedMultipart) -> impl IntoResponse { } async fn download(Path((chat_id, message_id)): Path<(i64, i32)>) -> impl IntoResponse { - let downloader = download_file(chat_id, message_id).await; - - let data = match downloader { - Some(v) => v.get_async_read(), + let downloader = match download_file(chat_id, message_id).await { + Some(v) => v, None => return StatusCode::BAD_REQUEST.into_response() }; - let reader = ReaderStream::new(data); + let file = match downloader.get_file().await { + Ok(v) => v, + Err(err) => { + log::error!("{}", err); + return StatusCode::BAD_REQUEST.into_response() + } + } ; + + let reader = ReaderStream::new(file); axum::body::Body::from_stream(reader).into_response() }