use std::sync::Arc;
use tracing::info;
use aria2_protocol::metalink::parser::UrlEntry;
use super::{FileDownloadInfo, MetalinkDownloadCommand, MetalinkFileInfo};
use crate::download::{DownloadContext, file_entry::FileEntry};
use crate::error::{Aria2Error, FatalError, Result};
use crate::request::request_group::{DownloadOptions, GroupId, RequestGroup};
use crate::util::rwlock_ext::RwLockRecover;
impl MetalinkDownloadCommand {
pub fn new(
gid: GroupId,
metalink_bytes: &[u8],
options: &DownloadOptions,
output_dir: Option<&str>,
) -> Result<Self> {
super::validate_http_client_options(options)?;
let doc = aria2_protocol::metalink::parser::MetalinkDocument::parse(metalink_bytes, None)
.map_err(|e| {
Aria2Error::Fatal(FatalError::Config(format!("Metalink parse failed: {}", e)))
})?;
if doc.files.is_empty() {
return Err(Aria2Error::Fatal(FatalError::Config(
"Metalink contains no files".into(),
)));
}
let file = &doc.files[0];
if file.urls.is_empty() {
let has_torrent_metaurl = file
.meta_urls
.iter()
.any(|m| m.mediatype == aria2_protocol::metalink::parser::MediaType::Torrent);
if !has_torrent_metaurl {
return Err(Aria2Error::Fatal(FatalError::Config(
"Metalink file has no download URL".into(),
)));
}
}
let dir = output_dir
.map(|d| d.to_string())
.or_else(|| options.dir.clone())
.unwrap_or_else(|| ".".to_string());
let filename = file.name.clone();
let path = std::path::PathBuf::from(&dir).join(&filename);
let urls: Vec<String> = file
.get_sorted_urls()
.iter()
.map(|u| u.url.clone())
.collect();
let file_info = FileDownloadInfo {
expected_size: file.size,
hash_entry: file.strongest_hash().cloned(),
sorted_urls: file.get_sorted_urls().into_iter().cloned().collect(),
pieces: file.pieces.clone(),
torrent_metaurls: file
.meta_urls
.iter()
.filter(|metaurl| {
metaurl.mediatype == aria2_protocol::metalink::parser::MediaType::Torrent
})
.cloned()
.collect(),
};
let group = RequestGroup::new(gid, urls, options.clone());
group.set_output_name(file.name.clone());
if doc.files.len() > 1 {
info!(
"MetalinkDownloadCommand created for first file of {}: {} -> {} ({} mirrors) \
[use create_multi_file() for all files]",
doc.files.len(),
file.name,
path.display(),
file.urls.len()
);
} else {
info!(
"MetalinkDownloadCommand created: {} -> {} ({} mirrors)",
file.name,
path.display(),
file.urls.len()
);
}
Ok(Self {
group: Arc::new(std::sync::RwLock::new(group)),
output_path: path,
started: false,
completed: false,
completed_bytes: 0,
progress_offset: 0,
file_info: Some(file_info),
grouped_file_infos: Vec::new(),
protocol_runtime: None,
global_limiter: None,
outbound_network_policy: Arc::new(crate::network::OutboundNetworkPolicy::direct()),
#[cfg(feature = "bittorrent")]
public_tracker_catalog: None,
#[cfg(feature = "bittorrent")]
bt_registry: None,
#[cfg(feature = "bittorrent")]
bt_listener: None,
#[cfg(feature = "bittorrent")]
lpd_manager: None,
#[cfg(feature = "bittorrent")]
open_file_budget: None,
})
}
pub fn create_multi_file(
metalink_bytes: &[u8],
options: &DownloadOptions,
output_dir: Option<&str>,
base_uri: Option<&str>,
gid_start: u64,
) -> Result<Vec<MetalinkFileInfo>> {
super::validate_http_client_options(options)?;
let doc =
aria2_protocol::metalink::parser::MetalinkDocument::parse(metalink_bytes, base_uri)
.map_err(|e| {
Aria2Error::Fatal(FatalError::Config(format!("Metalink parse failed: {}", e)))
})?;
if doc.files.is_empty() {
return Err(Aria2Error::Fatal(FatalError::Config(
"Metalink contains no files".into(),
)));
}
let dir = output_dir
.map(|d| d.to_string())
.or_else(|| options.dir.clone())
.unwrap_or_else(|| ".".to_string());
let mut commands = Vec::with_capacity(doc.files.len());
for (i, file) in doc.files.iter().enumerate() {
let has_torrent_metaurl = file.meta_urls.iter().any(|metaurl| {
metaurl.mediatype == aria2_protocol::metalink::parser::MediaType::Torrent
});
if file.urls.is_empty() && !has_torrent_metaurl {
tracing::debug!(
index = i,
name = %file.name,
"Skipping Metalink file with no downloadable resources"
);
continue;
}
let gid = GroupId::new(gid_start + i as u64);
let path = std::path::PathBuf::from(&dir).join(&file.name);
let sorted_urls: Vec<UrlEntry> = file
.get_sorted_urls()
.iter()
.map(|u| (*u).clone())
.collect();
let urls: Vec<String> = sorted_urls
.iter()
.filter(|u| u.is_non_p2p())
.map(|u| u.url.clone())
.collect();
let group = RequestGroup::new(gid, urls, options.clone());
group.set_output_name(file.name.clone());
let file_info = FileDownloadInfo {
expected_size: file.size,
hash_entry: file.strongest_hash().cloned(),
sorted_urls,
pieces: file.pieces.clone(),
torrent_metaurls: file
.meta_urls
.iter()
.filter(|m| m.mediatype == aria2_protocol::metalink::parser::MediaType::Torrent)
.cloned()
.collect(),
};
info!(
gid = gid.value(),
index = i,
name = %file.name,
path = %path.display(),
mirrors = file.urls.len(),
"Created MetalinkDownloadCommand for file"
);
commands.push(MetalinkFileInfo {
command: Self {
group: Arc::new(std::sync::RwLock::new(group)),
output_path: path,
started: false,
completed: false,
completed_bytes: 0,
progress_offset: 0,
file_info: Some(file_info),
grouped_file_infos: Vec::new(),
protocol_runtime: None,
global_limiter: None,
outbound_network_policy: Arc::new(
crate::network::OutboundNetworkPolicy::direct(),
),
#[cfg(feature = "bittorrent")]
public_tracker_catalog: None,
#[cfg(feature = "bittorrent")]
bt_registry: None,
#[cfg(feature = "bittorrent")]
bt_listener: None,
#[cfg(feature = "bittorrent")]
lpd_manager: None,
#[cfg(feature = "bittorrent")]
open_file_budget: None,
},
file_index: i,
});
}
Ok(commands)
}
pub fn create_multi_file_for_single(
file: &aria2_protocol::metalink::parser::MetalinkFile,
options: &DownloadOptions,
output_dir: Option<&str>,
gid_start: u64,
) -> Result<Vec<MetalinkFileInfo>> {
super::validate_http_client_options(options)?;
let dir = output_dir
.map(|d| d.to_string())
.or_else(|| options.dir.clone())
.unwrap_or_else(|| ".".to_string());
let torrent_metaurls: Vec<_> = file
.meta_urls
.iter()
.filter(|metaurl| {
metaurl.mediatype == aria2_protocol::metalink::parser::MediaType::Torrent
})
.cloned()
.collect();
if file.urls.is_empty() && torrent_metaurls.is_empty() {
return Ok(Vec::new());
}
let gid = GroupId::new(gid_start);
let path = std::path::PathBuf::from(&dir).join(&file.name);
let sorted_urls: Vec<UrlEntry> = file
.get_sorted_urls()
.iter()
.map(|u| (*u).clone())
.collect();
let urls: Vec<String> = sorted_urls
.iter()
.filter(|u| u.is_non_p2p())
.map(|u| u.url.clone())
.collect();
if urls.is_empty() && torrent_metaurls.is_empty() {
tracing::debug!(
name = %file.name,
"Skipping Metalink file with no downloadable resources"
);
return Ok(Vec::new());
}
let group = RequestGroup::new(gid, urls, options.clone());
group.set_output_name(file.name.clone());
let file_info = FileDownloadInfo {
expected_size: file.size,
hash_entry: file.strongest_hash().cloned(),
sorted_urls,
pieces: file.pieces.clone(),
torrent_metaurls,
};
info!(
gid = gid.value(),
name = %file.name,
path = %path.display(),
mirrors = file.urls.len(),
"Created MetalinkDownloadCommand for single file"
);
Ok(vec![MetalinkFileInfo {
command: Self {
group: Arc::new(std::sync::RwLock::new(group)),
output_path: path,
started: false,
completed: false,
completed_bytes: 0,
progress_offset: 0,
file_info: Some(file_info),
grouped_file_infos: Vec::new(),
protocol_runtime: None,
global_limiter: None,
outbound_network_policy: Arc::new(crate::network::OutboundNetworkPolicy::direct()),
#[cfg(feature = "bittorrent")]
public_tracker_catalog: None,
#[cfg(feature = "bittorrent")]
bt_registry: None,
#[cfg(feature = "bittorrent")]
bt_listener: None,
#[cfg(feature = "bittorrent")]
lpd_manager: None,
#[cfg(feature = "bittorrent")]
open_file_budget: None,
},
file_index: 0,
}])
}
pub(crate) fn create_multi_file_group(
files: &[aria2_protocol::metalink::parser::MetalinkFile],
options: &DownloadOptions,
output_dir: Option<&str>,
gid: u64,
) -> Result<Self> {
super::validate_http_client_options(options)?;
if files.is_empty() {
return Err(Aria2Error::Fatal(FatalError::Config(
"Metalink group contains no files".to_string(),
)));
}
let dir = output_dir
.map(str::to_owned)
.or_else(|| options.dir.clone())
.unwrap_or_else(|| ".".to_string());
let mut entries = Vec::with_capacity(files.len());
let mut grouped_file_infos = Vec::with_capacity(files.len());
let mut offset = 0u64;
for file in files {
let sorted_urls: Vec<UrlEntry> = file.get_sorted_urls().into_iter().cloned().collect();
let urls: Vec<String> = sorted_urls
.iter()
.filter(|url| url.is_non_p2p())
.map(|url| url.url.clone())
.collect();
let torrent_metaurls = file
.meta_urls
.iter()
.filter(|metaurl| {
metaurl.mediatype == aria2_protocol::metalink::parser::MediaType::Torrent
})
.cloned()
.collect::<Vec<_>>();
if urls.is_empty() && torrent_metaurls.is_empty() {
continue;
}
let path = std::path::PathBuf::from(&dir).join(&file.name);
let mut entry = FileEntry::new(
path.to_string_lossy().into_owned(),
file.size.unwrap_or(0),
offset,
urls.clone(),
);
if let Some(original_name) = torrent_metaurls
.first()
.and_then(|metaurl| metaurl.name.clone())
{
entry.set_original_name(original_name);
}
entry.set_suffix_path(file.name.clone());
entry.set_max_connection_per_server(
options
.max_connection_per_server
.unwrap_or(crate::constants::DEFAULT_MAX_CONNECTION_PER_SERVER as u16)
.clamp(1, 16) as usize,
);
entry.set_unique_protocol(options.metalink_enable_unique_protocol);
offset = offset.saturating_add(file.size.unwrap_or(0));
entries.push(entry);
grouped_file_infos.push((
path,
FileDownloadInfo {
expected_size: file.size,
hash_entry: file.strongest_hash().cloned(),
sorted_urls,
pieces: file.pieces.clone(),
torrent_metaurls,
},
));
}
if grouped_file_infos.is_empty() {
return Err(Aria2Error::Fatal(FatalError::Config(
"Metalink group contains no downloadable files".to_string(),
)));
}
let group = RequestGroup::new(GroupId::new(gid), Vec::new(), options.clone());
let mut context = DownloadContext::new_default();
context.set_file_entries(entries);
group.set_download_context(Arc::new(context));
group.set_output_name(files[0].name.clone());
let output_path = grouped_file_infos[0].0.clone();
Ok(Self {
group: Arc::new(std::sync::RwLock::new(group)),
output_path,
started: false,
completed: false,
completed_bytes: 0,
progress_offset: 0,
file_info: None,
grouped_file_infos,
protocol_runtime: None,
global_limiter: None,
outbound_network_policy: Arc::new(crate::network::OutboundNetworkPolicy::direct()),
#[cfg(feature = "bittorrent")]
public_tracker_catalog: None,
#[cfg(feature = "bittorrent")]
bt_registry: None,
#[cfg(feature = "bittorrent")]
bt_listener: None,
#[cfg(feature = "bittorrent")]
lpd_manager: None,
#[cfg(feature = "bittorrent")]
open_file_budget: None,
})
}
#[cfg(feature = "metalink")]
pub(crate) fn new_with_group_source(
group: Arc<std::sync::RwLock<RequestGroup>>,
metalink_bytes: &[u8],
file_index: usize,
options: &DownloadOptions,
base_uri: Option<&str>,
policy: &crate::network::OutboundNetworkPolicy,
) -> Result<Self> {
let doc =
aria2_protocol::metalink::parser::MetalinkDocument::parse(metalink_bytes, base_uri)
.map_err(|e| {
Aria2Error::Fatal(FatalError::Config(format!("Metalink parse failed: {e}")))
})?;
let mut file = doc.files.get(file_index).cloned().ok_or_else(|| {
Aria2Error::Fatal(FatalError::Config(
"Metalink file index out of range".to_string(),
))
})?;
crate::engine::metalink::to_request_group::MetalinkToRequestGroup::new()
.normalize_file_for_runtime(&mut file, options);
Self::new_with_group_file(group, &file, options, policy)
}
#[cfg(feature = "metalink")]
pub(crate) fn new_with_group_file(
group: Arc<std::sync::RwLock<RequestGroup>>,
file: &aria2_protocol::metalink::parser::MetalinkFile,
options: &DownloadOptions,
policy: &crate::network::OutboundNetworkPolicy,
) -> Result<Self> {
super::validate_http_client_options(options)?;
let dir = options.dir.as_deref().unwrap_or(".");
let output_name = group
.recover()
.output_name()
.unwrap_or_else(|| file.name.clone());
let path = std::path::PathBuf::from(dir).join(output_name);
let sorted_urls: Vec<UrlEntry> = file.get_sorted_urls().into_iter().cloned().collect();
let file_info = FileDownloadInfo {
expected_size: file.size,
hash_entry: file.strongest_hash().cloned(),
pieces: file.pieces.clone(),
torrent_metaurls: file
.meta_urls
.iter()
.filter(|m| m.mediatype == aria2_protocol::metalink::parser::MediaType::Torrent)
.cloned()
.collect(),
sorted_urls,
};
Ok(Self {
group,
output_path: path,
started: false,
completed: false,
completed_bytes: 0,
progress_offset: 0,
file_info: Some(file_info),
grouped_file_infos: Vec::new(),
protocol_runtime: None,
global_limiter: None,
outbound_network_policy: Arc::new(policy.clone()),
#[cfg(feature = "bittorrent")]
public_tracker_catalog: None,
#[cfg(feature = "bittorrent")]
bt_registry: None,
#[cfg(feature = "bittorrent")]
bt_listener: None,
#[cfg(feature = "bittorrent")]
lpd_manager: None,
#[cfg(feature = "bittorrent")]
open_file_budget: None,
})
}
}