use std::fmt;
use std::path::PathBuf;
use std::sync::Arc;
use reqwest::header::HeaderMap;
use reqwest::Method;
use crate::direction::Direction;
use crate::http_breakpoint::{BreakpointDownload, BreakpointDownloadHttpConfig, BreakpointUpload};
#[derive(Clone)]
pub struct PounceTask {
pub(crate) direction: Direction,
pub(crate) file_name: String,
pub(crate) file_path: PathBuf,
pub(crate) total_size: u64,
pub(crate) chunk_size: u64,
pub(crate) url: String,
pub(crate) method: Method,
pub(crate) headers: HeaderMap,
pub(crate) client_file_sign: Option<String>,
pub(crate) breakpoint_upload: Option<Arc<dyn BreakpointUpload + Send + Sync>>,
pub(crate) breakpoint_download: Option<Arc<dyn BreakpointDownload + Send + Sync>>,
pub(crate) breakpoint_download_http: Option<BreakpointDownloadHttpConfig>,
pub(crate) max_chunk_retries: u32,
}
impl fmt::Debug for PounceTask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PounceTask")
.field("direction", &self.direction)
.field("file_name", &self.file_name)
.field("file_path", &self.file_path)
.field("total_size", &self.total_size)
.field("chunk_size", &self.chunk_size)
.field("url", &self.url)
.field("method", &self.method)
.field("headers", &self.headers)
.field("client_file_sign", &self.client_file_sign)
.field(
"breakpoint_upload",
&self
.breakpoint_upload
.as_ref()
.map(|_| "Arc<dyn BreakpointUpload + Send + Sync>"),
)
.field(
"breakpoint_download",
&self
.breakpoint_download
.as_ref()
.map(|_| "Arc<dyn BreakpointDownload + Send + Sync>"),
)
.field("breakpoint_download_http", &self.breakpoint_download_http)
.field("max_chunk_retries", &self.max_chunk_retries)
.finish()
}
}
impl PounceTask {
pub const DEFAULT_MAX_CHUNK_RETRIES: u32 = 3;
pub(crate) fn normalized_chunk_size(chunk_size: u64) -> u64 {
if chunk_size == 0 {
1024 * 1024
} else {
chunk_size
}
}
pub(crate) fn normalized_max_chunk_retries(max_chunk_retries: u32) -> u32 {
max_chunk_retries
}
pub(crate) fn is_empty(&self) -> bool {
self.file_path.as_os_str().is_empty()
|| self.file_name.is_empty()
|| self.url.is_empty()
|| match self.direction {
Direction::Upload => self.total_size == 0,
Direction::Download => false,
}
}
}