use std::sync::Arc;
use tracing::{debug, info, warn};
use crate::error::Aria2Error;
use crate::request::request_group::{
DownloadOptions, FollowMode, GroupId, MetadataInfo, RequestGroup,
};
pub struct CompletedDownloadInfo {
pub content_type: Option<String>,
pub file_path: Option<String>,
pub options: Arc<DownloadOptions>,
pub gid: GroupId,
pub in_memory_download: bool,
pub in_memory_data: Option<Vec<u8>>,
pub base_uri: Option<String>,
}
pub trait PostDownloadHandler: Send + Sync + std::fmt::Debug {
fn can_handle(&self, info: &CompletedDownloadInfo) -> bool;
fn create_child_groups(
&self,
info: &CompletedDownloadInfo,
) -> std::result::Result<Vec<Arc<std::sync::RwLock<RequestGroup>>>, Aria2Error>;
fn create_child_groups_with_allocator(
&self,
info: &CompletedDownloadInfo,
_allocate_gid: &mut dyn FnMut() -> GroupId,
) -> std::result::Result<Vec<Arc<std::sync::RwLock<RequestGroup>>>, Aria2Error> {
self.create_child_groups(info)
}
fn name(&self) -> &'static str;
}
pub fn run_post_download_processing(
info: &CompletedDownloadInfo,
handlers: &[&dyn PostDownloadHandler],
) -> Vec<Arc<std::sync::RwLock<RequestGroup>>> {
let mut next_gid = info.gid.value().saturating_add(1);
let mut allocate_gid = || {
let gid = GroupId::new(next_gid);
next_gid = next_gid.saturating_add(1);
gid
};
run_post_download_processing_with_allocator(info, handlers, &mut allocate_gid)
}
pub fn run_post_download_processing_with_allocator(
info: &CompletedDownloadInfo,
handlers: &[&dyn PostDownloadHandler],
allocate_gid: &mut dyn FnMut() -> GroupId,
) -> Vec<Arc<std::sync::RwLock<RequestGroup>>> {
debug!(
gid = info.gid.value(),
content_type = ?info.content_type,
file_path = ?info.file_path,
"Running post-download processing chain"
);
for handler in handlers {
if handler.can_handle(info) {
info!(
handler = handler.name(),
gid = info.gid.value(),
"Post-download handler matched"
);
match handler.create_child_groups_with_allocator(info, allocate_gid) {
Ok(groups) => {
if groups.is_empty() {
debug!(
handler = handler.name(),
"Handler matched but created no child groups"
);
return Vec::new();
}
let child_gids: Vec<GroupId> =
groups.iter().map(|g| g.recover().gid()).collect();
for child in &groups {
let child_group = child.recover();
child_group.set_following_gid(info.gid);
let metadata_info = info
.base_uri
.as_deref()
.map(|uri| MetadataInfo::new(info.gid, uri))
.unwrap_or_else(MetadataInfo::data_only);
child_group.set_metadata_info(metadata_info);
}
info!(
handler = handler.name(),
parent_gid = info.gid.value(),
children = child_gids.len(),
"Post-download handler created child groups"
);
return groups;
}
Err(e) => {
warn!(
handler = handler.name(),
error = %e,
"Post-download handler failed to create child groups"
);
}
}
}
}
debug!(gid = info.gid.value(), "No post-download handler matched");
Vec::new()
}
pub fn build_handler_chain(options: &DownloadOptions) -> Vec<Box<dyn PostDownloadHandler>> {
#[allow(unused_mut)]
let mut handlers: Vec<Box<dyn PostDownloadHandler>> = Vec::new();
if options
.follow_torrent
.unwrap_or(FollowMode::Follow)
.follows()
{
#[cfg(feature = "bittorrent")]
handlers.push(Box::new(
super::bt_torrent_post_download_handler::BtTorrentPostDownloadHandler::new(),
));
}
if options
.follow_metalink
.unwrap_or(FollowMode::Follow)
.follows()
{
#[cfg(feature = "metalink")]
handlers.push(Box::new(
super::metalink_post_download_handler::MetalinkPostDownloadHandler::new(),
));
}
handlers
}
pub fn extract_download_info(group: &RequestGroup) -> CompletedDownloadInfo {
let gid = group.gid();
let options = group.options_arc();
let initial_source_uri = group.uris().first().cloned();
let (content_type, file_path, base_uri, in_memory_download, in_memory_data) =
if let Some(dctx) = group.download_context.recover().as_ref() {
let fp = dctx.first_file_path().map(|s| s.to_string());
let base_uri = dctx.first_file_entry().and_then(|entry| {
entry
.spent_uris()
.back()
.cloned()
.or_else(|| entry.remaining_uris().front().cloned())
});
let base_uri = base_uri.or(initial_source_uri.as_ref().map(|uri| uri.to_string()));
let in_mem = group.is_in_memory_download();
let data = group.in_memory_data();
(group.content_type(), fp, base_uri, in_mem, data)
} else {
(
group.content_type(),
None,
initial_source_uri.map(|uri| uri.to_string()),
group.is_in_memory_download(),
group.in_memory_data(),
)
};
CompletedDownloadInfo {
content_type,
file_path,
options,
gid,
in_memory_download,
in_memory_data,
base_uri,
}
}
#[cfg(any(feature = "bittorrent", feature = "metalink"))]
pub(crate) fn path_has_extension(candidate: &str, extensions: &[&str]) -> bool {
let path = reqwest::Url::parse(candidate)
.map(|url| url.path().to_owned())
.unwrap_or_else(|_| candidate.to_owned());
let path = path.to_ascii_lowercase();
extensions.iter().any(|extension| path.ends_with(extension))
}
use crate::util::rwlock_ext::RwLockRecover;
#[cfg(test)]
mod tests {
use super::*;
#[cfg(all(feature = "bittorrent", feature = "metalink"))]
#[test]
fn test_build_handler_chain_respects_follow_modes() {
let disabled = DownloadOptions {
follow_torrent: Some(FollowMode::Disabled),
follow_metalink: Some(FollowMode::Disabled),
..Default::default()
};
assert!(build_handler_chain(&disabled).is_empty());
let memory = DownloadOptions {
follow_torrent: Some(FollowMode::Memory),
follow_metalink: Some(FollowMode::Memory),
..Default::default()
};
assert_eq!(build_handler_chain(&memory).len(), 2);
let mixed = DownloadOptions {
follow_torrent: Some(FollowMode::Follow),
follow_metalink: Some(FollowMode::Disabled),
..Default::default()
};
assert_eq!(build_handler_chain(&mixed).len(), 1);
}
struct MockHandler {
name_str: &'static str,
can_handle_result: bool,
groups: Vec<Arc<std::sync::RwLock<RequestGroup>>>,
}
impl std::fmt::Debug for MockHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MockHandler")
.field("name_str", &self.name_str)
.field("can_handle_result", &self.can_handle_result)
.finish()
}
}
impl PostDownloadHandler for MockHandler {
fn can_handle(&self, _info: &CompletedDownloadInfo) -> bool {
self.can_handle_result
}
fn create_child_groups(
&self,
_info: &CompletedDownloadInfo,
) -> std::result::Result<Vec<Arc<std::sync::RwLock<RequestGroup>>>, Aria2Error> {
Ok(self.groups.clone())
}
fn name(&self) -> &'static str {
self.name_str
}
}
fn make_info() -> CompletedDownloadInfo {
CompletedDownloadInfo {
content_type: None,
file_path: None,
options: Arc::new(DownloadOptions::default()),
gid: GroupId::new(1),
in_memory_download: false,
in_memory_data: None,
base_uri: None,
}
}
#[test]
fn test_no_handler_matches() {
let handlers: Vec<&dyn PostDownloadHandler> = vec![];
let info = make_info();
let result = run_post_download_processing(&info, &handlers);
assert!(result.is_empty());
}
#[test]
fn test_handler_matches_and_creates_groups() {
let child = Arc::new(std::sync::RwLock::new(RequestGroup::new(
GroupId::new(100),
vec!["http://example.com/file.bin".to_string()],
DownloadOptions::default(),
)));
let handler = MockHandler {
name_str: "mock",
can_handle_result: true,
groups: vec![child],
};
let info = make_info();
let handlers: Vec<&dyn PostDownloadHandler> = vec![&handler];
let result = run_post_download_processing(&info, &handlers);
assert_eq!(result.len(), 1);
assert_eq!(result[0].recover().following_gid(), Some(GroupId::new(1)));
}
#[test]
fn test_handler_error_continues() {
#[derive(Debug)]
struct FailHandler;
impl PostDownloadHandler for FailHandler {
fn can_handle(&self, _info: &CompletedDownloadInfo) -> bool {
true
}
fn create_child_groups(
&self,
_info: &CompletedDownloadInfo,
) -> std::result::Result<Vec<Arc<std::sync::RwLock<RequestGroup>>>, Aria2Error>
{
Err(Aria2Error::Parse("parse error".to_string()))
}
fn name(&self) -> &'static str {
"fail"
}
}
let fail = FailHandler;
let info = make_info();
let handlers: Vec<&dyn PostDownloadHandler> = vec![&fail];
let result = run_post_download_processing(&info, &handlers);
assert!(result.is_empty());
}
}