Browse Source

some cleanups

main
Georg Hopp 1 year ago
parent
commit
171bba16e8
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 56
      src/client.rs
  2. 7
      src/main.rs

56
src/client.rs

@ -32,6 +32,14 @@ use crate::download_error::DownloadError;
use log::{debug, error, info}; use log::{debug, error, info};
macro_rules! dlerror {
($message:ident, $($err:tt)*) => {{
DownloadError::new( $message.clone()
, Some(anyhow!($($err)*) ))
}};
}
#[derive(Debug)] #[derive(Debug)]
pub(super) enum ClientActorMessage { pub(super) enum ClientActorMessage {
Download { Download {
@ -128,7 +136,6 @@ async fn run_client(mut actor: ClientActor) {
Download { filename: _, uri: _, respond_to } => { Download { filename: _, uri: _, respond_to } => {
let _ = respond_to.send(Ok(state)); let _ = respond_to.send(Ok(state));
}, },
_ => panic!("Wrong variant ... this should never happen"), _ => panic!("Wrong variant ... this should never happen"),
} }
} else { } else {
@ -137,14 +144,13 @@ async fn run_client(mut actor: ClientActor) {
}, },
GetData { ref uri, buffer, ref message } => { GetData { ref uri, buffer, ref message } => {
info!("Done download: {:?}", uri);
info!("Done get_data: {:?}", uri);
if let Some((_, message)) = actor.actions.remove_entry(message) { if let Some((_, message)) = actor.actions.remove_entry(message) {
use ClientActorMessage::GetData; use ClientActorMessage::GetData;
match message { match message {
GetData { uri: _, respond_to } => { GetData { uri: _, respond_to } => {
let _ = respond_to.send(buffer); let _ = respond_to.send(buffer);
}, },
_ => panic!("Wrong variant ... this should never happen"), _ => panic!("Wrong variant ... this should never happen"),
} }
} else { } else {
@ -280,24 +286,22 @@ async fn download( mut client: HttpClient
if let Download { ref filename, ref uri, state: _, message: _ } = message { if let Download { ref filename, ref uri, state: _, message: _ } = message {
(filename, uri) (filename, uri)
} else { } else {
Err(DownloadError::new( message.clone()
, Some(anyhow!("Called with invalid variant")) ))?
Err(dlerror!(message, "Called with invalid variant"))?
}; };
// - get all informations to eventually existing file // - get all informations to eventually existing file
let mut from = file_size(filename).await; let mut from = file_size(filename).await;
// - get infos to uri // - get infos to uri
let (content_length, content_type) =
content_info(&mut client, uri).await
. map_err(|e| DownloadError::new(message.clone(), Some(e)))?;
let ( content_length
, content_type ) = content_info(&mut client, uri).await
. map_err(|e| dlerror!(message, e))?;
if let Download { filename: _, uri: _, ref mut state, message: _ } = message { if let Download { filename: _, uri: _, ref mut state, message: _ } = message {
let content_type = content_type.clone(); let content_type = content_type.clone();
*state = Some(DownloadState::GotHead { content_type }); *state = Some(DownloadState::GotHead { content_type });
} else { } else {
Err(DownloadError::new( message.clone()
, Some(anyhow!("Called with invalid variant")) ))?;
Err(dlerror!(message, "Called with invalid variant"))?;
} }
if let Some(content_length) = content_length { if let Some(content_length) = content_length {
@ -316,14 +320,13 @@ async fn download( mut client: HttpClient
headers.insert(RANGE, range_value); headers.insert(RANGE, range_value);
let mut response = request(&mut client, "GET", uri, headers).await let mut response = request(&mut client, "GET", uri, headers).await
. map_err(|e| DownloadError::new(message.clone(), Some(e)))?;
. map_err(|e| dlerror!(message, e))?;
if let Download { filename: _, uri: _, ref mut state, message: _ } = message { if let Download { filename: _, uri: _, ref mut state, message: _ } = message {
let content_type = content_type.clone(); let content_type = content_type.clone();
*state = Some(DownloadState::Responded { content_type }); *state = Some(DownloadState::Responded { content_type });
} else { } else {
Err(DownloadError::new( message.clone()
, Some(anyhow!("Called with invalid variant")) ))?;
Err(dlerror!(message, "Called with invalid variant"))?;
} }
// - open or create file // - open or create file
@ -332,14 +335,13 @@ async fn download( mut client: HttpClient
, response.body_mut() , response.body_mut()
, io_timeout ) , io_timeout )
. await . await
. map_err(|e| DownloadError::new(message.clone(), Some(e)))?;
. map_err(|e| dlerror!(message, e))?;
if let Download { filename: _, uri: _, ref mut state, message: _ } = message { if let Download { filename: _, uri: _, ref mut state, message: _ } = message {
let content_type = content_type.clone(); let content_type = content_type.clone();
*state = Some(DownloadState::Done { content_type }); *state = Some(DownloadState::Done { content_type });
} else { } else {
Err(DownloadError::new( message.clone()
, Some(anyhow!("Called with invalid variant")) ))?;
Err(dlerror!(message, "Called with invalid variant"))?;
} }
Ok(Some(message)) Ok(Some(message))
@ -349,37 +351,31 @@ pub(super) async fn body_bytes( mut client: HttpClient
, mut message: ClientActorMessageHandle ) -> JoinSetResult { , mut message: ClientActorMessageHandle ) -> JoinSetResult {
use ClientActorMessageHandle::GetData; use ClientActorMessageHandle::GetData;
let uri =
if let GetData { ref uri, buffer: _, message: _ } = message {
uri
} else {
return Err(DownloadError::new(
message.clone(),
Some(anyhow!("Called with invalid variant")) ));
};
let uri = if let GetData { ref uri, buffer: _, message: _ } = message {
uri
} else {
Err(dlerror!(message, "Called with invalid variant"))?
};
let mut response = request( &mut client let mut response = request( &mut client
, "GET" , "GET"
, uri , uri
, HeaderMap::new() ) , HeaderMap::new() )
. await . await
. map_err(|e| DownloadError::new(message.clone(), Some(e)))?;
. map_err(|e| dlerror!(message, e))?;
// read body into Vec<u8> // read body into Vec<u8>
let body: Vec<u8> = BodyReader::new(response.body_mut()) let body: Vec<u8> = BodyReader::new(response.body_mut())
. bytes() . bytes()
. await . await
. map_err(|e| DownloadError::new( message.clone()
, Some(anyhow!(e.to_string())) ))?
. map_err(|e| dlerror!(message, e))?
. to_vec(); . to_vec();
let buffer = let buffer =
if let GetData { uri: _, ref mut buffer, message: _ } = message { if let GetData { uri: _, ref mut buffer, message: _ } = message {
buffer buffer
} else { } else {
return Err(DownloadError::new(
message.clone(),
Some(anyhow!("Called with invalid variant")) ));
Err(dlerror!(message, "Called with invalid variant"))?
}; };
*buffer = Some(body); *buffer = Some(body);

7
src/main.rs

@ -21,13 +21,6 @@ use log::{debug, info};
use process::{enter_download_dir, ffmpeg, remove_download_dir}; use process::{enter_download_dir, ffmpeg, remove_download_dir};
#[allow(dead_code)]
#[derive(Debug)]
struct DownloadMessage {
url: String,
}
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Args { struct Args {
#[arg(short, long)] #[arg(short, long)]

Loading…
Cancel
Save