use std::path::Path;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::time::Instant;
use super::types::UriResult;
use crate::download::request::Request;
#[derive(Debug, Default)]
pub(super) struct UriState {
pub(super) remaining: std::collections::VecDeque<String>,
pub(super) spent: std::collections::VecDeque<String>,
pub(super) results: std::collections::VecDeque<UriResult>,
}
#[derive(Debug)]
pub struct FileEntry {
pub(super) length: u64,
pub(super) offset: u64,
pub(super) uri_state: RwLock<UriState>,
pub(super) request_pool: Vec<Arc<Request>>,
pub(super) in_flight_requests: Vec<Arc<Request>>,
pub(super) path: String,
pub(super) content_type: String,
pub(super) original_name: String,
pub(super) suffix_path: String,
pub(super) last_faster_replace: Instant,
pub(super) max_connection_per_server: usize,
pub(super) requested: bool,
pub(super) unique_protocol: bool,
}
impl Default for FileEntry {
fn default() -> Self {
Self {
length: 0,
offset: 0,
uri_state: RwLock::new(UriState::default()),
request_pool: Vec::new(),
in_flight_requests: Vec::new(),
path: String::new(),
content_type: String::new(),
original_name: String::new(),
suffix_path: String::new(),
last_faster_replace: Instant::now(),
max_connection_per_server: crate::constants::DEFAULT_MAX_CONNECTION_PER_SERVER,
requested: false,
unique_protocol: false,
}
}
}
impl FileEntry {
pub(super) fn read_uri_state(&self) -> RwLockReadGuard<'_, UriState> {
self.uri_state
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub(super) fn write_uri_state(&self) -> RwLockWriteGuard<'_, UriState> {
self.uri_state
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub fn new(path: String, length: u64, offset: u64, uris: Vec<String>) -> Self {
let mut entry = Self {
length,
offset,
path,
requested: true,
..Self::default()
};
for uri in uris {
entry.add_uri(&uri);
}
entry
}
pub fn path(&self) -> &str {
&self.path
}
pub fn set_path(&mut self, path: String) {
self.path = path;
}
pub fn basename(&self) -> String {
if self.path.is_empty() {
return String::new();
}
Path::new(&self.path)
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default()
}
pub fn dirname(&self) -> String {
if self.path.is_empty() {
return String::new();
}
Path::new(&self.path)
.parent()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default()
}
pub fn original_name(&self) -> &str {
&self.original_name
}
pub fn set_original_name(&mut self, name: String) {
self.original_name = name;
}
pub fn suffix_path(&self) -> &str {
&self.suffix_path
}
pub fn set_suffix_path(&mut self, suffix_path: String) {
self.suffix_path = suffix_path;
}
pub fn content_type(&self) -> &str {
&self.content_type
}
pub fn set_content_type(&mut self, content_type: String) {
self.content_type = content_type;
}
pub fn length(&self) -> u64 {
self.length
}
pub fn set_length(&mut self, length: u64) {
self.length = length;
}
pub fn offset(&self) -> u64 {
self.offset
}
pub fn set_offset(&mut self, offset: u64) {
self.offset = offset;
}
pub fn last_offset(&self) -> u64 {
self.offset.saturating_add(self.length)
}
pub fn gtoloff(&self, goff: u64) -> u64 {
debug_assert!(
self.offset <= goff,
"gtoloff: global offset {} < file offset {}",
goff,
self.offset
);
goff.saturating_sub(self.offset)
}
pub fn is_requested(&self) -> bool {
self.requested
}
pub fn set_requested(&mut self, flag: bool) {
self.requested = flag;
}
pub fn is_unique_protocol(&self) -> bool {
self.unique_protocol
}
pub fn set_unique_protocol(&mut self, flag: bool) {
self.unique_protocol = flag;
}
pub fn max_connection_per_server(&self) -> usize {
self.max_connection_per_server
}
pub fn set_max_connection_per_server(&mut self, n: usize) {
self.max_connection_per_server = n.max(1);
}
pub fn release_runtime_resource(&mut self) {
self.request_pool.clear();
self.in_flight_requests.clear();
}
pub fn exists(&self) -> bool {
!self.path.is_empty() && Path::new(&self.path).exists()
}
pub fn cmp_by_offset(&self, other: &FileEntry) -> std::cmp::Ordering {
self.offset.cmp(&other.offset)
}
}
impl PartialEq for FileEntry {
fn eq(&self, other: &Self) -> bool {
self.offset == other.offset
}
}
impl Eq for FileEntry {}
impl Ord for FileEntry {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.offset.cmp(&other.offset)
}
}
impl PartialOrd for FileEntry {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}