5 changed files with 472 additions and 23 deletions
-
25src/client.rs
-
45src/m3u8_download.rs
-
40src/main.rs
-
348src/new_client.rs
-
37src/new_download_error.rs
@ -0,0 +1,45 @@ |
|||
use std::{ffi::OsString, path::Path};
|
|||
|
|||
use anyhow::anyhow;
|
|||
use http::{uri::{Authority, Scheme}, Uri};
|
|||
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(super) enum DownloadState {
|
|||
Open, // Nothing is done.
|
|||
Prepared, // The Uris to all .ts downloads are prepared.
|
|||
Ready, // All .ts downloads are done.
|
|||
}
|
|||
|
|||
struct TsPart {
|
|||
filename: OsString,
|
|||
url: Uri,
|
|||
state: DownloadState,
|
|||
}
|
|||
|
|||
struct M3u8Download {
|
|||
scheme: Scheme,
|
|||
auth: Authority,
|
|||
base_path: String,
|
|||
ts_parts: Vec<TsPart>,
|
|||
}
|
|||
|
|||
|
|||
impl M3u8Download {
|
|||
pub(super) fn new(uri: Uri) -> anyhow::Result<Self> {
|
|||
let scheme = uri.scheme()
|
|||
. ok_or(anyhow!("Problem scheme in m3u8 uri"))?
|
|||
. clone();
|
|||
let auth= uri.authority()
|
|||
. ok_or(anyhow!("Problem authority in m3u8 uri"))?
|
|||
. clone();
|
|||
let base_path = Path::new(uri.path()).parent()
|
|||
. ok_or(anyhow!("Path problem"))?
|
|||
. to_str()
|
|||
. ok_or(anyhow!("Path problem"))?
|
|||
. to_string();
|
|||
let ts_parts = vec![];
|
|||
|
|||
Ok(Self {scheme, auth, base_path, ts_parts})
|
|||
}
|
|||
}
|
|||
@ -0,0 +1,348 @@ |
|||
use std::{collections::HashMap, io::ErrorKind, path::{Path, PathBuf}, time::Duration};
|
|||
|
|||
use futures_util::StreamExt as _;
|
|||
use http::{
|
|||
header::{CONTENT_LENGTH, RANGE},
|
|||
request::Builder as RequestBuilder,
|
|||
HeaderMap,
|
|||
HeaderValue,
|
|||
Request,
|
|||
Response,
|
|||
StatusCode,
|
|||
Uri
|
|||
};
|
|||
use http_body_util::BodyDataStream;
|
|||
use reqwest::{redirect::Policy, Body};
|
|||
use tokio::{
|
|||
fs::{symlink_metadata, File},
|
|||
io::AsyncWriteExt as _,
|
|||
select,
|
|||
sync::{mpsc, oneshot},
|
|||
task::JoinSet,
|
|||
time::timeout
|
|||
};
|
|||
use tower::{util::BoxCloneService, ServiceBuilder, ServiceExt as _};
|
|||
use tower_http_client::ServiceExt as _;
|
|||
use tower_reqwest::HttpClientLayer;
|
|||
|
|||
use crate::{
|
|||
new_download_error::DownloadError,
|
|||
m3u8_download::DownloadState
|
|||
};
|
|||
|
|||
use log::{debug, error, info};
|
|||
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(super) enum ClientActorMessage {
|
|||
Download {
|
|||
filename: PathBuf,
|
|||
uri: Uri,
|
|||
respond_to: oneshot::Sender<DownloadState>,
|
|||
},
|
|||
}
|
|||
|
|||
#[derive(Clone, Debug)]
|
|||
pub(super) enum ClientActorMessageHandle {
|
|||
Download {
|
|||
filename: PathBuf,
|
|||
uri: Uri,
|
|||
message: ActionIndex,
|
|||
},
|
|||
}
|
|||
|
|||
pub(super) type ActionIndex = u64;
|
|||
type JoinSetResult = Result<Option<ClientActorMessageHandle>, DownloadError>;
|
|||
type HttpClient = BoxCloneService<Request<Body>, Response<Body>, anyhow::Error>;
|
|||
|
|||
|
|||
#[derive(Debug)]
|
|||
struct ClientActor {
|
|||
timeout: Duration,
|
|||
client: HttpClient,
|
|||
tasks: JoinSet<JoinSetResult>,
|
|||
actions: HashMap<ActionIndex, ClientActorMessage>,
|
|||
actions_idx: ActionIndex,
|
|||
|
|||
receiver: mpsc::Receiver<ClientActorMessage>,
|
|||
}
|
|||
|
|||
|
|||
pub(super) struct ClientActorHandle {
|
|||
sender: mpsc::Sender<ClientActorMessage>,
|
|||
abort: oneshot::Sender<JoinSetResult>,
|
|||
}
|
|||
|
|||
async fn run_client(mut actor: ClientActor) {
|
|||
loop {
|
|||
select! {
|
|||
Some(join) = actor.tasks.join_next() => {
|
|||
match join {
|
|||
Err(e) => {
|
|||
error!("FATAL Join failed: {}", e);
|
|||
break
|
|||
},
|
|||
|
|||
Ok(Err(e)) => {
|
|||
info!("Retry failed download: {:?}", e);
|
|||
// retry ... instead of responing here we could also respond
|
|||
// with something that in turn would be used to retry...
|
|||
let client = actor.client.clone();
|
|||
actor.tasks.spawn(async move {
|
|||
download(client, e.action, actor.timeout).await
|
|||
});
|
|||
},
|
|||
|
|||
// when the task finishes
|
|||
Ok(Ok(Some(action))) => {
|
|||
info!("Done download: {:?}", action);
|
|||
use ClientActorMessageHandle::Download;
|
|||
|
|||
match action {
|
|||
Download { filename: _, uri: _, ref message } => {
|
|||
if let Some((_, message)) = actor.actions.remove_entry(message) {
|
|||
use ClientActorMessage::Download;
|
|||
let Download { filename: _, uri: _, respond_to } = message;
|
|||
let _ = respond_to.send(DownloadState::Ready);
|
|||
} else {
|
|||
panic!("Lost a message");
|
|||
}
|
|||
}
|
|||
}
|
|||
},
|
|||
|
|||
// Got a stop message...here we still continue procession until the
|
|||
// JoinSet is empty.
|
|||
Ok(Ok(None)) => (),
|
|||
};
|
|||
}
|
|||
|
|||
Some(message) = actor.receiver.recv() => {
|
|||
actor.handle_message(message).await;
|
|||
}
|
|||
|
|||
else => {}
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
async fn file_size(filename: &Path) -> u64 {
|
|||
// - get all informations to eventually existing file
|
|||
let metadata = match symlink_metadata(filename).await {
|
|||
Ok(metadata) => Some(metadata),
|
|||
Err(error) => match error.kind() {
|
|||
// If we can't write to a file we need to ... well theres nothing we can do
|
|||
ErrorKind::PermissionDenied => panic!("Permission denied on: {:?}", filename),
|
|||
_ => None,
|
|||
}
|
|||
};
|
|||
|
|||
metadata.map_or(0, |m| m.len())
|
|||
}
|
|||
|
|||
async fn request( client: &mut HttpClient
|
|||
, method: &str |
|||
, uri: &Uri
|
|||
, headers: HeaderMap ) -> anyhow::Result<Response<Body>> {
|
|||
let mut request = RequestBuilder::new()
|
|||
. method(method)
|
|||
. uri(uri)
|
|||
. body(Body::default())?;
|
|||
|
|||
request.headers_mut().extend(headers);
|
|||
|
|||
debug!("New Request: {:?}", request);
|
|||
|
|||
let response = client.execute(request).await?;
|
|||
|
|||
debug!("New Response: {:?}", response);
|
|||
|
|||
anyhow::ensure!( response.status().is_success()
|
|||
, "resonse status failed: {}"
|
|||
, response.status() );
|
|||
|
|||
Ok(response)
|
|||
}
|
|||
|
|||
async fn content_length( client: &mut HttpClient
|
|||
, uri: &Uri ) -> anyhow::Result<Option<u64>> {
|
|||
let head = request(client, "HEAD", uri, HeaderMap::new()).await?;
|
|||
|
|||
Ok(head . headers().get(CONTENT_LENGTH)
|
|||
. map(|v| v . to_str()
|
|||
. expect("unable to get CONTENT-LENGTH value")
|
|||
. parse::<u64>()
|
|||
. expect("unable to parse CONTENT-LENGTH value")))
|
|||
}
|
|||
|
|||
async fn open_outfile(status: &StatusCode, filename: &Path) -> File {
|
|||
match status {
|
|||
&StatusCode::PARTIAL_CONTENT =>
|
|||
// Here we assume that this response only comes if the requested
|
|||
// range was fullfillable and thus is the data range in the
|
|||
// response. Thats why I do not check the content-range header.
|
|||
// If that assumption does not hold this needs to be fixec.
|
|||
File::options() . create(true)
|
|||
. append(true)
|
|||
. open(filename)
|
|||
. await
|
|||
. expect("can not create file for writing"),
|
|||
|
|||
_ =>
|
|||
File::create(filename) . await
|
|||
. expect("can not create file for writing"),
|
|||
}
|
|||
}
|
|||
|
|||
async fn store_body( file: &mut File
|
|||
, body: &mut Body
|
|||
, io_timeout: Duration ) -> anyhow::Result<()> {
|
|||
let mut body = BodyDataStream::new(body);
|
|||
|
|||
loop {
|
|||
// give timeout somehow... probably from client.
|
|||
let data = timeout(io_timeout, body.next()).await?;
|
|||
match data {
|
|||
None => break,
|
|||
Some(Err(e)) => return Err(e.into()),
|
|||
Some(Ok(data)) => {
|
|||
file . write_all(data.as_ref()).await?;
|
|||
file . flush().await?;
|
|||
},
|
|||
}
|
|||
};
|
|||
|
|||
Ok(())
|
|||
}
|
|||
|
|||
async fn download( mut client: HttpClient
|
|||
, message: ClientActorMessageHandle
|
|||
, io_timeout: Duration ) -> JoinSetResult {
|
|||
let ClientActorMessageHandle::Download { ref filename, ref uri, message: _ } = message;
|
|||
|
|||
// - get all informations to eventually existing file
|
|||
let mut from = file_size(filename).await;
|
|||
|
|||
// - get infos to uri
|
|||
let content_length = content_length(&mut client, uri).await
|
|||
. map_err(|e| DownloadError::new(message.clone(), Some(e)))?;
|
|||
|
|||
if let Some(content_length) = content_length {
|
|||
if from != 0 && content_length - 1 <= from {
|
|||
return Ok(None);
|
|||
}
|
|||
} else {
|
|||
from = 0;
|
|||
}
|
|||
|
|||
// - do the neccessry request.
|
|||
let range_value: HeaderValue = format!("bytes={}-", from)
|
|||
. parse()
|
|||
. expect("Error creating range header value");
|
|||
let mut headers = HeaderMap::new();
|
|||
headers.insert(RANGE, range_value);
|
|||
|
|||
let mut response = request(&mut client, "GET", uri, headers).await
|
|||
. map_err(|e| DownloadError::new(message.clone(), Some(e)))?;
|
|||
|
|||
// - open or create file
|
|||
// - download Data
|
|||
store_body( &mut open_outfile(&response.status(), filename).await
|
|||
, response.body_mut()
|
|||
, io_timeout )
|
|||
. await
|
|||
. map_err(|e| DownloadError::new(message.clone(), Some(e)))?;
|
|||
|
|||
Ok(Some(message))
|
|||
}
|
|||
|
|||
|
|||
impl ClientActor {
|
|||
pub(super) fn new( concurrency_limit: usize |
|||
, timeout: Duration
|
|||
, receiver: mpsc::Receiver<ClientActorMessage>
|
|||
, abort_rx: oneshot::Receiver<JoinSetResult> ) -> anyhow::Result<Self> {
|
|||
let client = ServiceBuilder::new()
|
|||
// Add some layers.
|
|||
. concurrency_limit(concurrency_limit)
|
|||
. timeout(timeout)
|
|||
// Make client compatible with the `tower-http` layers.
|
|||
. layer(HttpClientLayer)
|
|||
. service( reqwest::Client::builder()
|
|||
. redirect(Policy::limited(5))
|
|||
. build()? )
|
|||
. map_err(anyhow::Error::msg)
|
|||
. boxed_clone();
|
|||
|
|||
debug!("-> client: {:?}", client);
|
|||
|
|||
let mut tasks = JoinSet::new();
|
|||
|
|||
tasks.spawn(async move {
|
|||
let _ = abort_rx.await;
|
|||
Ok(None)
|
|||
});
|
|||
|
|||
let actions = HashMap::new();
|
|||
let actions_idx = 0;
|
|||
|
|||
Ok(Self {timeout, client, tasks, receiver, actions, actions_idx})
|
|||
}
|
|||
|
|||
async fn handle_message(&mut self, message: ClientActorMessage) {
|
|||
self.actions.insert(self.actions_idx, message);
|
|||
|
|||
use ClientActorMessage::Download;
|
|||
|
|||
match self.actions.get(&self.actions_idx) {
|
|||
Some(Download { ref filename, ref uri, respond_to: _ }) => {
|
|||
// spawn a task that does the work
|
|||
let client = self.client.clone();
|
|||
let timeout = self.timeout;
|
|||
|
|||
let handle = ClientActorMessageHandle::Download {
|
|||
filename: filename.to_path_buf(),
|
|||
uri: uri.clone(),
|
|||
message: self.actions_idx,
|
|||
};
|
|||
|
|||
self.tasks.spawn(async move {
|
|||
download(client, handle, timeout).await
|
|||
});
|
|||
|
|||
self.actions_idx += 1;
|
|||
},
|
|||
|
|||
None => (),
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
impl ClientActorHandle {
|
|||
pub(super) fn new(timeout: Duration) -> Self {
|
|||
let (sender, receiver) = mpsc::channel(1);
|
|||
let (abort, abort_rx) = oneshot::channel::<JoinSetResult>();
|
|||
let actor = ClientActor::new( 20
|
|||
, timeout
|
|||
, receiver
|
|||
, abort_rx )
|
|||
. expect("Client create error");
|
|||
tokio::spawn(run_client(actor));
|
|||
|
|||
Self { sender, abort }
|
|||
}
|
|||
|
|||
pub(super) fn stop(self) {
|
|||
let _ = self.abort.send(Ok(None));
|
|||
}
|
|||
|
|||
pub(super) async fn download(&self, filename: impl AsRef<Path>, uri: &Uri) {
|
|||
let filename = filename.as_ref().to_path_buf();
|
|||
let uri = uri.to_owned();
|
|||
let (send, receive) = oneshot::channel();
|
|||
let msg = ClientActorMessage::Download { filename, uri, respond_to: send };
|
|||
|
|||
let _ = self.sender.send(msg).await;
|
|||
receive.await.expect("Actor cancelled unexpected");
|
|||
}
|
|||
}
|
|||
@ -0,0 +1,37 @@ |
|||
use std::{error, fmt};
|
|||
|
|||
use crate::new_client::ClientActorMessageHandle;
|
|||
|
|||
|
|||
#[derive(Debug)]
|
|||
pub(super) struct DownloadError {
|
|||
pub(super) action: ClientActorMessageHandle,
|
|||
pub(super) source: Option<anyhow::Error>,
|
|||
}
|
|||
|
|||
|
|||
impl DownloadError {
|
|||
pub(super) fn new( action: ClientActorMessageHandle
|
|||
, source: Option<anyhow::Error> ) -> Self {
|
|||
let action = action.to_owned();
|
|||
Self { action, source }
|
|||
}
|
|||
}
|
|||
|
|||
impl error::Error for DownloadError {
|
|||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|||
match &self.source {
|
|||
None => None,
|
|||
Some(e) => Some(e.as_ref()),
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
impl fmt::Display for DownloadError {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|||
match &self.source {
|
|||
None => write!(f, "download error: {:?}", self.action),
|
|||
Some(err) => write!(f, "download error ({:?}): {}", self.action, err),
|
|||
}
|
|||
}
|
|||
}
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue