This commit is contained in:
2024-05-07 00:15:22 +02:00
parent 19c9b52057
commit eae46c8225
2 changed files with 16 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ use base64::{engine::general_purpose, Engine};
use reqwest::StatusCode; use reqwest::StatusCode;
use smartstring::alias::String as SmartString; use smartstring::alias::String as SmartString;
use tempfile::SpooledTempFile; use tempfile::SpooledTempFile;
use tracing::log;
use crate::config; use crate::config;
@@ -54,7 +55,13 @@ pub async fn download(
.unwrap() .unwrap()
.to_string(); .to_string();
let output_file = response_to_tempfile(&mut response).await.unwrap(); let output_file = match response_to_tempfile(&mut response).await {
Ok(v) => v,
Err(err) => {
log::error!("Error: {}", err);
return Err(err);
}
};
Ok((output_file.0, filename)) Ok((output_file.0, filename))
} }

View File

@@ -23,7 +23,9 @@ pub fn get_key(input_data: CreateTask) -> String {
format!("{:x}", md5::compute(data_string)) format!("{:x}", md5::compute(data_string))
} }
pub async fn response_to_tempfile(res: &mut Response) -> Option<(SpooledTempFile, usize)> { pub async fn response_to_tempfile(
res: &mut Response,
) -> Result<(SpooledTempFile, usize), Box<dyn std::error::Error + Send + Sync>> {
let mut tmp_file = tempfile::spooled_tempfile(5 * 1024 * 1024); let mut tmp_file = tempfile::spooled_tempfile(5 * 1024 * 1024);
let mut data_size: usize = 0; let mut data_size: usize = 0;
@@ -34,7 +36,7 @@ pub async fn response_to_tempfile(res: &mut Response) -> Option<(SpooledTempFile
let result = match chunk { let result = match chunk {
Ok(v) => v, Ok(v) => v,
Err(_) => return None, Err(err) => return Err(Box::new(err)),
}; };
let data = match result { let data = match result {
@@ -46,14 +48,16 @@ pub async fn response_to_tempfile(res: &mut Response) -> Option<(SpooledTempFile
match tmp_file.write(data.chunk()) { match tmp_file.write(data.chunk()) {
Ok(_) => (), Ok(_) => (),
Err(_) => return None, Err(err) => {
return Err(Box::new(err));
}
} }
} }
tmp_file.seek(SeekFrom::Start(0)).unwrap(); tmp_file.seek(SeekFrom::Start(0)).unwrap();
} }
Some((tmp_file, data_size)) Ok((tmp_file, data_size))
} }
pub fn get_stream( pub fn get_stream(