Browse Source

Fix state setting after restart download

main v0.2.3
Georg Hopp 11 months ago
parent
commit
04b351a5fe
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 3
      src/client.rs
  2. 28
      src/client_actor/util.rs
  3. 10
      src/m3u8_download.rs

3
src/client.rs

@ -164,6 +164,7 @@ impl Client {
let file = util::open_or_create(&response.status(), &filename).await;
// - download Data
Ok( self.clone().store_body( file
, from as usize
, content_type
, response.body_mut() ).await? )
}
@ -207,10 +208,10 @@ impl Client {
async fn store_body( self
, mut file: File
, mut size: usize
, content_type: Option<String>
, body: &mut ClientBody ) -> Result<DownloadState, DownloadError> {
let mut body = BodyDataStream::new(body);
let mut size = 0;
let mut state = DownloadState::Partial { content_type: content_type.clone(), size };
loop {

28
src/client_actor/util.rs

@ -14,7 +14,7 @@ async fn process_next_result(mut actor: ClientActor, result: ClientTaskResult) -
use ClientActorMessageHandle::{Download, GetData};
match result {
Err(e) => {
Err(mut 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...
@ -22,16 +22,26 @@ async fn process_next_result(mut actor: ClientActor, result: ClientTaskResult) -
actor.tasks.spawn(async move {
match e.action {
Download { .. } => {
client.download( e.action.filename()
, &e.action.uri()
, &HeaderMap::new() ).await
. map_err(|source| ClientActorError::new(&e.action, source))?;
Ok(Some(e.action))
let result = client.download( e.action.filename()
, &e.action.uri()
, &HeaderMap::new()).await;
match result {
Err(source) => Err(ClientActorError::new(&e.action, source)),
Ok(state) => {
e.action.set_state(state);
Ok(Some(e.action))
},
}
},
GetData { .. } => {
client.data(&e.action.uri(), &HeaderMap::new()).await
. map_err(|source| ClientActorError::new(&e.action, source))?;
Ok(Some(e.action))
let result = client.data(&e.action.uri(), &HeaderMap::new()).await;
match result {
Err(source) => Err(ClientActorError::new(&e.action, source)),
Ok(data) => {
*e.action.buffer_mut() = Some(data);
Ok(Some(e.action))
},
}
},
}
});

10
src/m3u8_download.rs

@ -45,10 +45,12 @@ impl TsPart {
async fn download(&mut self, client: &ClientActorHandle) -> &Self {
let state = client.download(self.filename.clone(), &self.uri).await;
debug!("TsPart::download got: {:?}", state);
match state {
Ok(DownloadState::Done { content_type, .. }) => {
self.state = TsState::Ready;
self.content_type = content_type;
debug!("DONE TsPart: {:?}", self);
},
_ => self.state = TsState::Failed,
};
@ -98,14 +100,14 @@ impl M3u8Download {
pub(super) async fn download(&mut self, client: &ClientActorHandle) {
loop {
let unfinished: Vec<_> = self.ts_parts.iter_mut()
. filter_map(|p| match p.state {
TsState::Ready => if p.content_type != Some("video/MP2T".to_string()) {
Some(p.download(client))
. filter_map(|ts_part| match ts_part.state {
TsState::Ready => if ts_part.content_type != Some("video/MP2T".to_string()) {
Some(ts_part.download(client))
} else {
None
}
_ => Some(p.download(client))
_ => Some(ts_part.download(client))
}).collect();
debug!("UNFINISHED NOW: {}", unfinished.len());

Loading…
Cancel
Save