From f3b702eb4ed3a252d3e7f0a05b876ca1861d49c7 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 13 Jun 2022 12:21:36 +0000 Subject: [PATCH] Update --- src/downloader/downloader.rs | 28 ++++++++++++++++++++++++---- src/downloader/local_file.rs | 11 +++++++++++ src/downloader/tasks.rs | 10 +++++++--- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/downloader/downloader.rs b/src/downloader/downloader.rs index 614f825..3b19caa 100644 --- a/src/downloader/downloader.rs +++ b/src/downloader/downloader.rs @@ -36,9 +36,20 @@ use std::sync::atomic::AtomicBool; use tokio::task::JoinHandle; use url::Url; +/// Get the target file name +pub trait GetTargetFileName { + /// Get the target file name. + /// The target file name just used in the message in progress bar. + /// + /// If is unknown, return [None] is fine. + fn get_target_file_name(&self) -> Option { + None + } +} + #[derive(Debug)] /// A file downloader -pub struct DownloaderInternal { +pub struct DownloaderInternal { /// The webclient pub client: Arc, /// The download status @@ -131,7 +142,7 @@ impl DownloaderInternal { } } -impl DownloaderInternal { +impl DownloaderInternal { /// Add a new task to tasks /// * `task` - Task pub fn add_task(&self, task: JoinHandle>) { @@ -196,6 +207,15 @@ impl DownloaderInternal { self.status.get_ref().clone() } + #[inline] + /// Get the target file name + pub fn get_target_file_name(&self) -> Option { + match self.file.get_ref().deref() { + Some(f) => { f.get_target_file_name() } + None => { None } + } + } + #[inline] /// Advances the position of the progress bar by `delta` pub fn inc_progress_bar(&self, delta: u64) { @@ -293,7 +313,7 @@ impl DownloaderInternal { } /// A file downloader -pub struct Downloader { +pub struct Downloader { /// internal type downloader: Arc>, /// The task to check status. @@ -327,7 +347,7 @@ macro_rules! define_downloader_fn { } } -impl Downloader { +impl Downloader { /// Start download if download not started. /// /// Returns the status of the Downloader diff --git a/src/downloader/local_file.rs b/src/downloader/local_file.rs index 76731a9..f3a314a 100644 --- a/src/downloader/local_file.rs +++ b/src/downloader/local_file.rs @@ -1,4 +1,5 @@ use crate::ext::io::ClearFile; +use super::downloader::GetTargetFileName; use std::fs::File; use std::fs::remove_file; use std::io::Seek; @@ -55,6 +56,16 @@ impl Deref for LocalFile { } } +impl GetTargetFileName for LocalFile { + #[inline] + fn get_target_file_name(&self) -> Option { + match self.path.to_str() { + Some(s) => { Some(String::from(s)) } + None => { None } + } + } +} + impl Seek for LocalFile { fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { self.file.as_ref().unwrap().seek(pos) diff --git a/src/downloader/tasks.rs b/src/downloader/tasks.rs index 843cb5b..7b4fef5 100644 --- a/src/downloader/tasks.rs +++ b/src/downloader/tasks.rs @@ -2,6 +2,7 @@ use crate::ext::io::ClearFile; use crate::ext::rw_lock::GetRwLock; use crate::ext::try_err::TryErr; use crate::gettext; +use super::downloader::GetTargetFileName; use super::error::DownloaderError; use super::downloader::DownloaderInternal; use futures_util::StreamExt; @@ -16,7 +17,7 @@ use std::sync::Arc; use std::time::Duration; /// Create a download tasks in simple thread mode. -pub async fn create_download_tasks_simple(d: Arc>) -> Result<(), DownloaderError> { +pub async fn create_download_tasks_simple(d: Arc>) -> Result<(), DownloaderError> { let mut start = if d.pd.is_downloading() { d.pd.get_downloaded_file_size() } else { @@ -104,7 +105,7 @@ pub async fn create_download_tasks_simple(d: Arc>, re: Response) -> Result<(), DownloaderError> { +pub async fn handle_download(d: Arc>, re: Response) -> Result<(), DownloaderError> { let mut stream = re.bytes_stream(); let is_multi = d.is_multi_threads(); loop { @@ -139,6 +140,9 @@ pub async fn handle_download(d: Arc { if !is_multi { d.pd.complete()?; + if d.enabled_progress_bar() { + d.set_progress_bar_message(format!("{} {}", gettext("Downloaded file:"), d.get_target_file_name().unwrap_or(String::from("(unknown)")))); + } } break; } @@ -159,7 +163,7 @@ pub async fn handle_download(d: Arc(d: Arc>) -> Result<(), DownloaderError> { +pub async fn check_tasks(d: Arc>) -> Result<(), DownloaderError> { if !d.is_multi_threads() { let task = tokio::spawn(create_download_tasks_simple(Arc::clone(&d))); d.add_task(task);